HDU-5392 Infoplane in Tina Town(分解质因数法求最小公倍数)
2015-08-16 17:12
429 查看
Infoplane in Tina Town
Time Limit: 14000/7000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)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
题意:给出一个置换,求它的循环长度。数据范围 :3*10^6
思路:求出每一个循环的长度,然后再求他们的最小公倍数(题解说最小公倍数要:分解质因数,然后用快速幂和乘法)
我按照这样的方法做还是用时5s+(写的又太渣了),幸亏hdu将时间从3500ms放宽到7000ms。
不知道那些1400ms的大神怎么做的。。。
#include <cstdio> #include <cstring> using namespace std; const int MAXN=269; const unsigned long long mod=3221225473; int n,a[3000001],b,cnt[3000001],tmp,tim[3000001],times,num,prime[MAXN]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597,1601,1607,1609,1613,1619,1621,1627,1637,1657,1663,1667,1669,1693,1697,1699,1709,1721,1723}; bool vis[3000001]; void Decom(int t) {//分解质因数 for(int i=0;i<MAXN;++i) { tmp=0; while(t%prime[i]==0) { ++tmp; t/=prime[i]; } if(tim[prime[i]]<tmp)//统计每个质因数最多出现的次数 tim[prime[i]]=tmp; if(t==1) return ; } ++tim[t]; } unsigned long long quickpow(unsigned long long m,unsigned long long n) {//模mod快速幂 unsigned long long b=1; while(n>0) { if(n&1) b=(b*m)%mod; n=n>>1; m=(m*m)%mod; } return b; } int main() { int i,T; unsigned long long ans;//怕溢出,所以基本都改成unsigned long long了,但是貌似没有这样的数据,long long也能过 scanf("%d",&T); while(T--) { scanf("%d",&n); for(i=1;i<=n;++i) { scanf("%d",a+i); vis[i]=false; tim[i]=cnt[i]=0; } cnt[0]=tmp=num=0; for(i=1;tmp!=n;++i) {//统计每一个循环的长度 if(!vis[i]) { vis[i]=true; ++tmp; ++cnt[num]; b=a[i]; while(b!=i) { vis[b]=true; ++tmp; ++cnt[num]; b=a[b]; } if(cnt[num]!=1) ++num; else cnt[num]=0;//如果循环长度为1,则不计入 } } for(i=0;i<num;++i) Decom(cnt[i]); ans=1; for(i=2;i<=n;++i) if(tim[i]!=0) ans=(ans*quickpow(i,tim[i]))%mod; printf("%I64u\n",ans); } return 0; }
相关文章推荐
- linux学习之路(9)
- 以“遥远的银河”为例浅谈扫描和信息维护的技巧
- Hadoop单机模式安装入门(Ubuntu系统)
- shell脚本:正则表达式-初
- 架构师应该亲力亲为
- scala 学习笔记(06) OOP(下)多重继承 及 AOP
- Linux解压乱码
- Linux解压乱码
- linux下I/O缓冲
- nginx手记-命令
- 内联(行级)元素不能设置margin-top
- linux目录与路径
- Linux学习笔记-----如何用本地主机(win7)的浏览器访问虚拟机中的Apache服务器中的网页:??
- linux c/c++ 检测程序是否在运行
- 安装archlinux+cinnamon笔记
- (tomcat访问不了的两种解决方法)Bad Request(Invalid Hostname)
- Linux学习笔记-----RPM包管理----源码包安装
- hdu 2188 悼念512汶川大地震遇难同胞——选拔志愿者(Bash Game)
- 网站
- linux学习之路(8)