POJ 3128 Leonardo's Notebook (置换)

Leonardo‘s Notebook

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2324   Accepted: 988

Description

— I just bought Leonardo‘s secret notebook! Rare object collector Stan Ucker was really agitated but his friend, special investigator Sarah Kepticwas unimpressed. 
— How do you know it is genuine? 
— Oh, it must be, at that price. And it is written in the da Vinci code. Sarah browsed a few of the pages. It was obvious to her that the code was a substitution cipher, where each letter of the alphabet had been substituted by another letter. 
— Leonardo would have written the plain-text and left it to his assistant to encrypt, she said. And he must have supplied the substitution alphabet to be used. If we are lucky, we can find it on the back cover! She turned up the last page and, lo and behold, there was a single line of all 26 letters of the alphabet: 
QWERTYUIOPASDFGHJKLZXCVBNM 
— This may be Leonardo‘s instructions meaning that each A in the plain-text was to be replaced by Q, each B withW, etcetera. Let us see... To their disappointment, they soon saw that this could not be the substitution that was used in the book. Suddenly, Stan brightened. 
— Maybe Leonardo really wrote the substitution alphabet on the last page, and by mistake his assistant coded that line as he had coded the rest of the book. So the line we have here is the result of applying some permutation TWICE to the ordinary alphabet! Sarah took out her laptop computer and coded fiercely for a few minutes. Then she turned to Stan with a sympathetic expression. 
— No, that couldn‘t be it. I am afraid that you have been duped again, my friend. In all probability, the book is a fake.

Write a program that takes a permutation of the English alphabet as input and decides if it may be the result of performing some permutation twice.

Input

The input begins with a positive number on a line of its own telling the number of test cases (at most 500). Then for each test case there is one line containing a permutation of the 26 capital letters of the English alphabet.

Output

For each test case, output one line containing Yes if the given permutation can result from applying some permutation twice on the original alphabet string ABC...XYZ, otherwise output No.

Sample Input

2
QWERTYUIOPASDFGHJKLZXCVBNM
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Sample Output

No
Yes

Source

题意:

  给你一串大写的英文字母, 问你能不能通过2次置换ABCD···XYZ使得变成你要的字母串。

题解:

  如果存在一个置换 (a1, a2, a3)。那么 (a1, a2, a3)(a1, a2, a3) = (a1, a3, a2)。

  如果存在一个置换(b1, b2, b3, b4)。那么 (b1, b2, b3, b4)(b1, b2, b3, b4) = (b1, b3)(b2, b4)

  证明:

(a1, a2, a3)表示的是 a1 -> a2 , a2 -> a3 , a3 -> a1。

(需要学习置换的乘法)

当任意两个长度为n(奇数)的置换,都可以找到一个置换A , 满足A^2 = B。

当任意两个不相交的长度为n(奇数偶数都可以)循环置换 B, C ,都能找到一个长度为2n的循环置换A, 满足 A^2 = B C。

所以只要长度为偶数的置换有偶数个就可以输出Yes。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 #include <cmath>
 7 #include <vector>
 8 #include <queue>
 9 #include <stack>
10 #include <set>
11 using namespace std;
12 typedef long long LL;
13 #define ms(a, b) memset(a, b, sizeof(a))
14 #define pb push_back
15 #define mp make_pair
16 const int INF = 0x7fffffff;
17 const int inf = 0x3f3f3f3f;
18 const int mod = 1e9+7;
19 const int maxn = 100000+10;
20 void init(){
21
22 }
23 void solve() {
24     char B[30];
25     int vis[30], cnt[30], T;
26     scanf("%d", &T);
27     while(T--){
28         scanf("%s", B);
29         ms(vis, 0);
30         ms(cnt, 0);
31         for(int i = 0;i<26;i++){
32             if(!vis[i]){
33                 int j = i, n = 0;
34                 //cnt为长度
35                 do{
36                     vis[j] = 1;
37                     j = B[j] - ‘A‘;
38                     n++;
39                 }while(j!=i);
40                 cnt[n]++;
41             }
42         }
43         int ok = 1;
44         for(int i = 2;i<=26;i+=2)
45             if(cnt[i]%2==1)
46                 ok = 0;
47         if(ok)  printf("Yes\n");
48         else    printf("No\n");
49     }
50 }
51 int main() {
52 #ifdef LOCAL
53     freopen("input.txt", "r", stdin);
54 //        freopen("output.txt", "w", stdout);
55 #endif
56     ios::sync_with_stdio(0);
57     cin.tie(0);
58     init();
59     solve();
60     return 0;
61 }

POJ 3128 Leonardo's Notebook (置换)

时间: 2024-12-21 01:57:30

POJ 3128 Leonardo's Notebook (置换)的相关文章

poj 3128 Leonardo&#39;s Notebook(置换的幂)

http://poj.org/problem?id=3128 大致题意:输入一串含26个大写字母的字符串,可以把它看做一个置换,判断这个置换是否是某个置换的平方. 思路:详解可参考置换群快速幂运算 研究与探讨. 可以先正着考虑一个置换的平方出现什么情况.对于置换中的循环,若其长度为偶数,平方以后一定分成了两个长度相等的循环,若长度是奇数,平方以后仍是一个循环,长度不变.因此,考虑当前置换,若某个循环的长度为偶数,那么它一定是原始置换平方得来的,而且等长度的循环一定有偶数个.对于长度为奇数的循环,

POJ 3128 Leonardo&#39;s Notebook [置换群]

传送门 题意:26个大写字母的置换$B$,是否存在置换$A$满足$A^2=B$ $A^2$,就是在循环中一下子走两步 容易发现,长度$n$为奇数的循环走两步还是$n$次回到原点 $n$为偶数的话是$\frac{n}{2}$次,也就是说分裂成了两个循环 综上$B$中长度为偶数的循环有奇数个就是不存在啦 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #inclu

poj 3128 Leonardo&#39;s Notebook (置换群的整幂运算)

题意:给你一个置换P,问是否存在一个置换M,使M^2=P 思路:资料参考 <置换群快速幂运算研究与探讨> https://wenku.baidu.com/view/0bff6b1c6bd97f192279e9fb.html 结论一: 一个长度为 l 的循环 T,l 是 k 的倍数,则 T^k 是 k 个循环的乘积,每个循环分别是循环 T 中下标 i mod k=0,1,2- 的元素按顺序的连接. 结论二:一个长度为 l 的循环 T,gcd(l,k)=1,则 T^k 是一个循环,与循环 T 不一

uva 12103 - Leonardo&#39;s Notebook(置换)

题目链接:uva 12103 - Leonardo's Notebook 题目大意:给出26个字母的置换,问是否存在一个置换A,使得A2=B 解题思路:将给定置换分解成若干个不相干的循环,当循环的长度n为奇数时,可以由两个循环长度为n的循环的乘积得来,也可以由两个循环长度为2n的拆分而来:对于长度n为偶数的,只能由两个循环长度为2n的拆分而来,所以判断是否存在有循环长度为偶数的个数是奇数个即可. #include <cstdio> #include <cstring> #inclu

【POJ 3128】Leonardo&#39;s Notebook

这道题的问题就是说能否对一个给定的置换进行开方运算 关于这个问题讲的最为详细的是05年集训队论文 潘震皓:<置换群快速幂运算研究与探讨> 对于一个长度为l的轮换,若gcd(l,k)==1,则可以开k方 若gcd(l,k)!=1则对于单个循环是不能开k方的 而若有m个长度为l的轮换,只需要保证gcd(m*l,k)==m就可以 因为开k方是k次方的逆运算,只要保证目标轮换的k次方会分裂成m个数就好了 而若能保证gcd(m*l,k)==m,则m|k,且gcd(l,k/m)==1,即m为k的因子 则最

LA 3641 (置换 循环的分解) Leonardo&#39;s Notebook

给出一个26个大写字母的置换B,是否存在A2 = B 每个置换可以看做若干个循环的乘积.我们可以把这些循环看成中UVa 10294的项链, 循环中的数就相当于项链中的珠子. A2就相当于将项链旋转了两个珠子间的距离,珠子0.2.4...构成一个循环,一共有gcd(n, 2)个循环,每个循环的长度为n / gcd(n, 2) 所以当一个循环的长度为奇数的时候,平方以后还是原来的长度: 当一个循环的长度为偶数的时候,平方以后就会分解为两个长度都等于原来循环长度一半的循环. 先将置换B分解循环,对于其

Leonardo&#39;s Notebook UVALive - 3641(置换)

题意: 给出26个大写字母的置换B,问是否存在一个置换A,使得A2 = B 解析: 两个长度为n的相同循环相乘,1.当n为奇数时结果也是一个长度为n的循环:2. 当n为偶数时分裂为两个长度为n/2 (这个n/2可能是奇数 也可能是偶数)的循环 那么倒推 意思也就是说 对于长度为奇数的循环B(奇数个相同长度的倒推1  偶数个相同长度的倒推2)  总可以找出来一个循环A  使得A2 = B 而对于长度为偶数的循环B   只有偶数个相同长度的才能从2倒推 不然 就不能倒推 即找不到一个A使得A2 =

UVALA 3641 Leonardo&#39;s Notebook

Polya定理应用: 题意:给出一个置换B,问有没有一个置换A,使得A^2=B. 思路:对于置换的循环节,比如(a1,a2,a3)和(b1,b2,b3,b4),那么对于奇数长度的循环节,发现进行两次乘法以后还是奇数长度,偶数长度的循环节分解为两个长度相同的循环节.那么就可以对B中的循环节进行判断,奇数长度的不同管,这个总能找到满足要求的,偶数循环节的数量也要是偶数,这样才能两两配对. 3641 Leonardo's Notebook - I just bought Leonardo's secr

UVALive - 3641 Leonardo&#39;s Notebook(polya计数)

题意:给出26个大写字母的置换B,问是否存在一个置换A,使A*A=B? 两个长度为N的相同循环相乘,当N为奇数时结果也是一个长度为N的循环,当N为偶数时分裂为两个长度为N/2的循环.相反,对于一个任意长度为N的奇数循环B,都能找到一个长度为N的循环A使得A*A=B,对于任意两个长度为N(N不一定为偶数)的不相交循环B和C,都能找到一个长度为2N的循环A使得A*A=B*C. 于是只要判断置换B里循环长度相同的且都为偶数(2,4,6, 8.....)的循环个数是不是都为偶数(偶数就能两两配对),只要