Title: http://acm.hdu.edu.cn/showproblem.php?pid=5392
Test instructions: So far I have not understood test instructions. Guess by admin: Find out the length of each cycle and find the least common multiple of these lengths. Results%3221225473.
Analysis: First find out the length of each cycle Len, because the result is very large, with GCD to find least common multiple time can not direct mode 3221225473 (Die gcd is not correct ...), can be all the length of Len decomposition of the mass factor, recording the maximum number of each mass factor, Finally, a quick power is obtained.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath>typedef long LL; Using namespace std;const LL mod = 3221225473;const int sz = 3e6+7; const int MAXN = 4000;int prime[maxn],nprime,a[sz],f[sz];bool isprime[maxn],visit[sz];void doprime () {Nprime=0;int i,j; for (i=1;i<maxn;i+=2) isprime[i]=true;isprime[1]=false;isprime[2]=true;for (i=2;i<maxn;i++) {if (Isprime[i]) { Prime[nprime++]=i;for (j=2*i;j<maxn;j+=i) Isprime[j]=false;}} void divide (int x) {int ret=0,cnt;for (int i=0;i<nprime;i++) {if (x%prime[i]==0) {x/=prime[i];cnt=1;while (X%prime[i] ==0) {x/=prime[i];cnt++;} F[prime[i]]=max (f[prime[i]],cnt); if (x==1) break;}} if (x>1) F[x]=max (f[x],1);} int Getlen (int x) {int Ret=1,cur=a[x];visit[x]=true;while (x!=cur) {visit[cur]=true;ret++;cur=a[cur];} return ret;} ll Mypow (int a,int n) {LL ret=1;while (n) {if (n&1) ret=ret*a%mod;n>>=1;a=a*a%mod;} return ret;} void Solve () {LL ans=1;for (int i=0;i<sz;i++) if (F[i]) Ans=ans*mypow (I,f[i])%mod;printf ("%lld\n", ans);} int main () {doprime (), int ncase,n,i,j;scanf ("%d", &ncase), while (ncase--) {scanf ('%d ', &n); for (i=1;i<=n;i+ +) scanf ("%d", &a[i]), memset (F,0,sizeof (f)), memset (visit,false,sizeof (visit)), for (i=1;i<=n;i++) if (!visit[ I]) divide (Getlen (i)); Solve ();} return 0;}
Hdu 5392 Infoplane in Tina town (mass factor decomposition for GCD)