/* Problem ID:hdu 4259 Meaning: Analyzing:置换群+lcm(循环节长度) */ #include <iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<vector> using namespace std; typedef struct even{int y1,y2,x;}even; #define FOR(i,s,t) for(int i=(s); i<(t); i++) #define LL long long #define BUG puts("here!!!") #define STOP system("pause") #define file_r(x) freopen(x, "r", stdin) #define file_w(x) freopen(x, "w", stdout) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define maxn 808 int A[maxn][maxn],V[maxn]; LL gcd(LL a,LL b) {return a?gcd(b%a,a):b;} int main(){ int n,k; while(~scanf("%d%d",&n,&k)&&(n||k)){ int cnt=1,i; for(i=0;;i++){ for(int j=0;j<k;j++) A[i][j]=cnt++; if(cnt>n) break; } int M=1; for(int j=0;j<k;j++){ for(int m=i;m>=0;m--){ if(A[m][j]<=n&&A[m][j]) V[M++]=A[m][j]; } } int vis[maxn]; LL res=1,len; memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++){ if(!vis[i]){ len=0; int t=i; while(1){ if(vis[t]) break; len++; vis[t]=1; t=V[t]; } res=res/gcd(len,res)*len; } } printf("%I64d\n",res); } return 0; }
再贴下标程,很赞0.0
#include <iostream> #include <vector> #include <cstring> using namespace std; long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; } int A[1010]; bool vis[1010]; int main() { int N, K; for(cin >> N >> K; N || K; cin >> N >> K) { /* Compute the permutation induced by the shuffle. */ int M = 0; for(int i = 0; i < K && i < N; i++) { for(int j = (N - i - 1) / K * K + i; j >= 0; j -= K) { A[M++] = j; cout<<M-1<<" --> "<<A[M-1]<<endl; } } /* Compute the lcm of the cycle lengths of the permutation. */ long long res = 1; memset(vis, 0, sizeof(vis)); for(int i = 0; i < N; i++) { int x = i; int ln = 0; while(!vis[x]) { vis[x] = true; x = A[x]; ln++; } /* Careful to divide then multiply here. */ if(ln) res = res / gcd(res, ln) * ln; } cout << res << endl; } }