關于置換群的題目下面列舉幾個:
POJ 2369 Permutations
題目還是比較簡單的,就是一次求出每個循環節的長度,取它們的公倍數即可
//
// main.cpp
// poj 2369 Permutations
//
// Created by ccccsober on 7/2/16.
// Copyright ? 2016 cccsober. All rights reserved.
//
#include <iostream>
#include <algorithm>
#include <cassert>
#include <string>
#include <sstream>
#include <math.h>
#include <set>
#include <bitset>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#include <deque>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cctype>
#include <complex>
using namespace std;
const int MAX1= (1e3) +2 ;
//const int MAX2= ;
//const int Mod= ;
const double plus=0.49999999;
const int INF=0x3f3f3f3f;
typedef long long LL;
typedef unsigned long long ULL;
#define M_PI 3.141592653589
int num[MAX1];
bool vis[MAX1];
int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
int lcm(int a,int b){
return a/gcd(a,b)*b;
}
int main(int argc, const char * argv[]) {
int n;
while(scanf("%d",&n)!=EOF){
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
int ans=1;
for(int i=1;i<=n;i++){
int t=i;
int cnt=0;
while(!vis[t]){
vis[t]=1;
t=num[t];
cnt++;
}
if(cnt)
ans=lcm(ans,cnt);
}
cout<<ans<<endl;
}
return 0;
}
POJ 1026 Cipher
題目總體還是比較簡單,算出每個置換群的階,依次算出每個 k%loopsize[i] 就能得到結果了,下面相對詳細的講解下:
題目中:
{1 2 3 4 5 6 7 8 9 10}
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ?{1,4,7},{2,5},{3},{6,8},{9,10}
{4 5 3 7 2 8 1 6 10 9} ?loopsize[10]={3,2,1,3,2,2,3,2,2,2}
求loopsize的時候可以選擇遞歸,這個很好的。
一開始我一直糾結算法復雜度,后來想想,一開始球loopsize的時候其實就是每個元素遍歷一遍,這個不可能減少,而且也不算太高 ,在O(n)總體的復雜度也差不多。
給出AC參考代碼:就是個遞歸著loopsize,之后處理下,代碼實際不到60行...
//
// main.cpp
// poj 1026 Cipher
//
// Created by ccccsober on 7/3/16.
// Copyright ? 2016 cccsober. All rights reserved.
//
#include <iostream>
#include <algorithm>
#include <cassert>
#include <string>
#include <sstream>
#include <math.h>
#include <set>
#include <bitset>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#include <deque>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cctype>
#include <complex>
using namespace std;
const int MAX1= 202 ;
//const int MAX2= ;
//const int Mod= ;
const double plus=0.49999999;
const int INF=0x3f3f3f3f;
typedef long long LL;
typedef unsigned long long ULL;
int num[MAX1];
int loopsize[MAX1];
bool vis[MAX1];
char result[MAX1];
char str[MAX1];
#define M_PI 3.141592653589
void recursion(int i,int& n){
if(!vis[i]){
vis[i]=1;
n++;
recursion(num[i],n);
}
loopsize[i]=n;
}
void _init(){
memset(vis,0,sizeof(vis));
}
void _init2(){
memset(result,0,sizeof(result));
memset(str,0,sizeof(str));
}
int main(int argc, const char * argv[]) {
freopen("/Users/sperc4/Desktop/input.txt", "r", stdin);
int n;
while(scanf("%d",&n)!=EOF){
_init();
if(!n) break;
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
int k;
for(int i=1;i<=n;i++){
int t=0;
if(!vis[i])
recursion(i,t);
}
while(scanf("%d",&k),k){
_init2();
getchar();
gets(str+1);
for(int i=1;i<=n;i++){
int t=k%loopsize[i];
int pos=i;
while(t--){
pos=num[pos];
}
result[pos]=str[i];
}
for(int i=1;i<=n;i++){
if(result[i])
printf("%c",result[i]);
else
printf(" ");
}
printf("\n");
}
printf("\n");
}
// freopen("/Users/sperc4/Desktop/output.txt","w",stdout);
fclose(stdin);
// fclose(stdout);
return 0;
}
POJ 1721 CARDS
題目跟我預想的并不一樣...
我開始覺得進行n次的置換那么的到應該等價下面分析:
n=1 有
那么如果是n次置換:
我覺得既然n是1~1000,那么這里應該會用到 快速冪取模,之后的到的數學式子等價于=
自己在筆算的時候可以得到這樣的結果:
但說實話,直接怎樣我實在是不知道如何進行了...
下面給出一個思路是別人的:
poj 1721
怎么說,我一開始很疑惑,為什么經過多番置換一定可以回到原來的數組...
比如下面這個例子:
1 2 3 4 5 6 7 8 9
6 3 5 9 7 2 1 8 4 后來會發現是下面的情況:
1 2 3 4 5 6 7 8 9 1 2 3 4 5 7 8 9 1 2 3 4 5 6 7 8 9
6 3 5 9 7 2 1 8 4 ? 2 5 7 4 1 6 8 9 ? 5 1 6 4 2 7 3 8 9
我測試過后面總是在后面那兩個中不斷循環,這個置換那個循環節就是2嗎?我要怎么從第一個得出他的循環節就是2?
但仔細研究了下題目敘述的到結果是:
這句話保證最后一定可以回到原始的狀態...
**
Alice and Bob play a game. Alice first writes down all the numbers from 1 to N in some random order: a1, a2, ..., aN. Then she arranges the cards so that the position ai holds the card numbered ai+1, for every 1 <= i <= N-1, while the position aN holds the card numbered a1. **
...
最后還是給出一個參考的代碼:
我考慮很久要不要把多余內容刪掉,后來想想,留出自己的思考過程何嘗不是好的足跡
//
// main.cpp
// poj 1721 CARDS
//
// Created by ccccsober on 7/6/16.
// Copyright ? 2016 cccsober. All rights reserved.
//
#include <iostream>
#include <algorithm>
#include <cassert>
#include <string>
#include <sstream>
#include <math.h>
#include <set>
#include <bitset>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#include <deque>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cctype>
#include <complex>
using namespace std;
const int MAX1= (1e3) +3;
//const int MAX2= ;
//const int Mod= ;
const double plus=0.49999999;
const int INF=0x3f3f3f3f;
typedef long long LL;
typedef unsigned long long ULL;
#define M_PI 3.141592653589
int temp[MAX1];
int ans[MAX1];
int data[MAX1];
bool vis[MAX1];
//int gcd(int a,int b){
// return b==0?a:gcd(b,a%b);
//}
//int lcm(int a,int b){
// return a/gcd(a,b)*b;
//}
bool check_same(int*a ,int*b,int n){
for(int i=1;i<=n;i++)
if(a[i]!=b[i]) return false;
return true;
}
int main(int argc, const char * argv[]) {
freopen("/Users/sperc4/Desktop/input.txt", "r", stdin);
int n,s;
while(scanf("%d%d",&n,&s)!=EOF){
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++){
scanf("%d",&ans[i]);
data[i]=ans[i];
}
int loopsize=1;
for(int i=1;i<=n;i++)
temp[i]=data[data[i]];
// memcmp(data,temp,sizeof(temp));
for(int i=1;i<=n;i++)
data[i]=temp[i];
while(!check_same(temp,ans,n)){
loopsize++;
for(int i=1;i<=n;i++)
temp[i]=data[data[i]];
for(int i=1;i<=n;i++)
data[i]=temp[i];
}
// int t=0;
// for(int i=1;i<=n;i++){
// int k=i;
// t=0;
// while(!vis[k]){
// t++;
// vis[k]=1;
// k=ans[ans[k]];
// }
// if(t)
// loopsize=lcm(t,loopsize);
// }
//
int plus=loopsize-s%loopsize;
// cout<<"loopsize "<<loopsize<<endl;
for(int i=0;i<plus;i++){
for(int j=1;j<=n;j++){
temp[j]=ans[ans[j]];
}
memcpy(ans,temp,sizeof(temp));
}
for(int i=1;i<=n;i++)
cout<<ans[i]<<endl;
// cout<<")))))))"<<endl;
}
// fclose(stdin);
// fclose(stdout);
return 0;
}
POJ 3821 Leonardo's Notebook
這個題目也不錯,非常好的利用了循環群分裂的一些性質。
比較好的論文:置換群快速冪運算+研究與探討
里面只看前半部分就綽綽有余了。
因為gcd(2*k,2)=2說明凡是置換p*p得到的結果,其實都是由p分裂過來的。長度為奇數的置換,不用在意,因為gcd(2*k+1,2)=1,長度為2*k 的循環,這個就是關注點了。任何一個 2*k 必然是 4*k分裂出現。并且又一個和它對應。那么,長度為偶數的一定是成對出現