找循环节,分解质因数,求LCM
Infoplane in Tina Town
Time Limit: 14000/7000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1627 Accepted Submission(s): 380
Problem Description
There is a big stone with smooth surface in Tina Town. When people go towards it, the stone surface will be lighted and show its usage. This stone was a legacy and also the center of Tina Town’s calculation and control system. also, it can display events in
Tina Town and contents that pedestrians are interested in, and it can be used as public computer. It makes people’s life more convenient (especially for who forget to take a device).
Tina and Town were playing a game on this stone. First, a permutation of numbers from 1 to n were
displayed on the stone. Town exchanged some numbers randomly and Town recorded this process by macros. Town asked Tine,”Do you know how many times it need to turn these numbers into the original permutation by executing this macro? Tina didn’t know the answer
so she asked you to find out the answer for her.
Since the answer may be very large, you only need to output the answer modulo 3?230+1=3221225473 (a
prime).
Input
The first line is an integer T representing
the number of test cases. T≤5
For each test case, the first line is an integer n representing
the length of permutation. n≤3?106
The second line contains n integers
representing a permutation A1...An.
It is guaranteed that numbers are different each other and all Ai satisfies
( 1≤Ai≤n).
Output
For each test case, print a number ans representing
the answer.
Sample Input
2 3 1 3 2 6 2 3 4 5 6 1
Sample Output
2 6
Source
/* *********************************************** Author :CKboss Created Time :2015年08月17日 星期一 15时04分29秒 File Name :HDOJ5392.cpp ************************************************ */ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib> #include <vector> #include <queue> #include <set> #include <map> using namespace std; typedef long long int LL; const int maxn=3003000; int n; int a[maxn]; bool vis[maxn]; vector<LL> loop; void nextInt(int &x) { bool flag=false; char ch; x=0; while(ch=getchar()) { if(ch>='0'&&ch<='9') { flag=true; x=x*10+ch-'0'; } else { if(flag==true) break; } } } int prime[maxn/10],pn; int wei[220000]; bool used[maxn]; void init() { memset(used,true,sizeof(used)); for(int i=2;i<=maxn;i++) { if(used[i]) { prime[pn++]=i; for(int j=i*2;j<=maxn;j+=i) used[j]=false; } } } const LL mod=3221225473LL; LL quickPow(LL x,int m) { LL e=1LL; while(m) { if(m&1) e=(e*x)%mod; x=(x*x)%mod; m/=2; } return e%mod; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); init(); int T_T; scanf("%d",&T_T); while(T_T--) { loop.clear(); memset(wei,0,sizeof(wei)); memset(vis,false,sizeof(vis)); nextInt(n); bool gogo=true; for(int i=1;i<=n;i++) { nextInt(a[i]); if(gogo&&a[i]!=i) gogo=false; } if(gogo) { puts("0"); continue; } for(int i=1;i<=n;i++) { if(vis[i]) continue; int cur=0,u=i; while(true) { if(vis[u]) break; vis[u]=true; cur++; u=a[u]; } loop.push_back(cur); } for(int i=0,sz=loop.size();i<sz;i++) { int x=loop[i]; int nt=0; while(x!=1) { if(x%prime[nt]==0) { int temp=0; while(x%prime[nt]==0) { temp++; x/=prime[nt]; } wei[nt]=max(wei[nt],temp); } nt++; } } LL ans=1; for(int i=0;i<220000;i++) { if(wei[i]) { ans=(ans*quickPow(prime[i],wei[i]))%mod; } } cout<<ans<<endl; } return 0; }
版权声明:来自: 码代码的猿猿的AC之路 http://blog.csdn.net/ck_boss