hdoj 5392 Infoplane in Tina Town

题目链接: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-08-29 01:50:25

hdoj 5392 Infoplane in Tina Town的相关文章

HDOJ 5392 Infoplane in Tina Town LCM

找循环节,分解质因数,求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 Tin

HDU 5392 Infoplane in Tina Town

Infoplane in Tina Town Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 805    Accepted Submission(s): 168 Problem Description There is a big stone with smooth surface in Tina Town. When peop

hdu 5392 Infoplane in Tina Town(数学)

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. als

hdu 5392 Infoplane in Tina Town (质因子分解求gcd)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5392 题意:至今没弄懂题意.按admin的意思猜的:求出每个循环的长度,然后求出这些长度的最小公倍数.结果%3221225473. 分析:首先求出每个循环的长度len,由于结果很大,用gcd求最小公倍数的时候不能直接模3221225473(模下gcd是不正确的......),可以将所有的长度len分解质因子,记录每种质因子的最大数量,,最后快速幂求结果. 代码: #include <iostream>

hdu5392 Infoplane in Tina Town(LCM)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Infoplane in Tina Town Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 518    Accepted Submission(s): 74 Problem Description There i

hdu 5392 Zball in Tina Town(素数)

题意:求(x-1)!modx(x<10^9),哥德巴赫猜想,打表得当x为素数时,结果为x-1,合数时为0:x=4时为2特判: 思路:判断素数的时候脑子抽了,想成素数大数了,然后一直TLE; 当要判断的数的平方根为10^8级别时再考虑大数情况... #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int t,n,m; int main(){ int i,j,k,f

hdoj 5391 Zball in Tina Town

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5391 相关数论结论: 威尔逊定理——当且仅当p为素数时:( p -1 )! ≡ p-1 ( mod p ) 这道题一开始看到的时候以为是暴力的题目,快速阶乘取模,后来其他题做不出来回头看这道题, 发现可以找规律,当p是合数的时候,P的因子全都可以在P前面的数字的因子里找到, 当p是素数的时候,(p-1)!%p = p-1,就尝试写了一下,2A: 因为4是个例外,怕还有其他例外情况,在100以内小规模

[hdu5392 Infoplane in Tina Town]置换的最小循环长度,最小公倍数取模,输入挂

题意:给一个置换,求最小循环长度对p取模的结果 思路:一个置换可以写成若干循环的乘积,最小循环长度为每个循环长度的最小公倍数.求最小公倍数对p取模的结果可以对每个数因式分解,将最小公倍数表示成质数幂的乘积形式,然后用快速幂取模,而不能一边求LCM一边取模. 由于这题数据量太大,需要用到输入挂,原理是把文件里面的东西用fread一次性读到内存. 输入挂模板: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 namespace IO { const s

hdu 5391 Zball in Tina Town

点击此处即可传送 hdu 5391 唉,我以为带7的基本上都是素数,所以一直拿1007算,结果....唉,一把辛酸泪啊,算了,不说了,上正事: Problem Description Tina Town is a friendly place. People there care about each other. Tina has a ball called zball. Zball is magic. It grows larger every day. On the first day,