[lnsyoj2359] 密码

题意

给出两个数 \(a, b\),给出 \(T\) 个询问,每个询问求给出的排列能否通过某种置换变换而成,且给出的两个值可以通过加或减 \(a,b\) 的操作得到。满足输出 Yes

sol

由于 \(a, b\) 进行互相加减的操作不会改变两值的最大公约数,因此计算最大公约数并判断即可。
前半段问题可以转换为求置换 \(A\) 使得 \(A^2 = A'\)。对 \(A^2\) 进行循环分解,可以得到:任意一个奇数环长度都不变,而任意一个偶数环都会变为两个长度相同的环。因此,判断 \(A'\) 中是否任意长度的偶数环都有偶数个即可。

代码

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
typedef long long LL;

const int N = 35;

int g[N];
LL a, b;
int n, m;
bool st[N];

int main(){
    scanf("%lld%lld", &a, &b);
    scanf("%d%d", &n, &m);
    while (n -- ){
        LL x, y;
        for (int i = 1; i <= m; i ++ ) scanf("%d", &g[i]);
        scanf("%lld%lld", &x, &y);
        if (__gcd(x, y) != __gcd(a, b)){
            puts("No");
            continue;
        }
        memset(st, false, sizeof st);
        int evencnt = 0;

        for (int i = 1; i <= m; i ++ ) 
            if (!st[i]){
                int cnt = 0, p = i;
                while (!st[p]){
                    st[p] = true;
                    cnt ++ ;
                    p = g[p];
                }
                if (cnt % 2 == 0) evencnt ++ ;
            }
        
        puts(evencnt % 2 ? "No" : "Yes");
    }

    return 0;
}
posted @ 2024-10-02 16:28  是一只小蒟蒻呀  阅读(1)  评论(0编辑  收藏  举报