题解:CF2172E Number Maze

· · 题解

luogu link
codeforces link

Solution

我们只需要分讨 n 的值,然后求第 j 和第 k 个排列,最后求 x,y 就可以了。

求下一个排列可以用函数 next_permutation,用 j-1,k-1 次就可以求第 j,k 个排列。

求 x,y 可以枚举,如果两个排列里同一位的数相同,那么 x 加一,否则 y 加一。原因:都是排列,不相同肯定是错位了。

Code

:::success[code]

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef char ch;
typedef string str;
typedef double db;
typedef __int128 i128;
const ll inf=9e18,maxn=5;
const i128 Inf=1e35;
ll n,a[maxn],b[maxn],m,j,k,x,y,t;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>t;
    while(t--)
    {
        cin>>n>>j>>k;
        x=y=0;
        if(n==12) m=2,a[1]=b[1]=1,a[2]=b[2]=2;
        else if(n==123) m=3,a[1]=b[1]=1,a[2]=b[2]=2,a[3]=b[3]=3;
        else m=4,a[1]=b[1]=1,a[2]=b[2]=2,a[3]=b[3]=3,a[4]=b[4]=4;
        j--,k--;
        while(j--) next_permutation(a+1,a+m+1);
        while(k--) next_permutation(b+1,b+m+1);
        for(int i=1;i<=m;i++)
        {
            if(a[i]==b[i]) x++;
            else y++;
        }
        cout<<x<<"A"<<y<<"B\n";
    }
}

:::