题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5392
1 #include<stdio.h> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 #include<set> 6 using namespace std; 7 const int MAXN = 3*1e6+10; 8 const unsigned int MOD = 3221225473; 9 int n, T; 10 int a[MAXN]; 11 int vis[MAXN]; 12 int num[MAXN]; 13 int tmp; 14 int t; 15 int ct; 16 long long ans; 17 set<int>s; 18 set<int>::iterator it; 19 int cnt[MAXN]; 20 int main(){ 21 scanf("%d",&T); 22 while(T--){ 23 t = 1; 24 scanf("%d",&n); 25 for( int i = 1; i <= n; ++i){ 26 scanf("%d",&a[i]); 27 } 28 memset(vis,0,sizeof(vis)); 29 for( int i = 1; i <= n; ++i){ 30 if( vis[i] != 0 ) 31 continue; 32 tmp = a[a[i]]; 33 num[t] = 1; 34 while( a[i] != tmp ){ 35 vis[tmp] = 1; 36 tmp = a[tmp]; 37 num[t]++; 38 } 39 vis[tmp] = 1; 40 t++; 41 } 42 memset(cnt,0,sizeof(cnt)); 43 ans = 1; 44 for(int i = 1; i < t; ++i){ 45 for( int d = 2; d <= num[i]; ++d){ 46 ct = 0; 47 while( num[i] % d == 0 ){ 48 num[i] = num[i] / d; 49 ct++; 50 } 51 s.insert(d); 52 cnt[d] = max(cnt[d],ct); 53 } 54 } 55 for(it = s.begin();it!=s.end();++it){ 56 ans = ans*pow((*it),cnt[*it]); 57 ans %= MOD; 58 } 59 printf("%I64d\n",ans); 60 } 61 }
时间: 2024-11-09 01:57:35