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>
#include <cstdio>
#include <cstring>
#include <cmath>
typedef long 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;
}
时间: 2024-10-11 01:27:59

hdu 5392 Infoplane in Tina Town (质因子分解求gcd)的相关文章

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

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

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

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

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

hdu5392--Infoplane in Tina Town(置换群+质因子分解求最小公倍数)

题目链接:点击打开链接 题目大意:给出一种操作a[1],a[2],,,,a[n],代表每交换一次,1位置的数到a[1]位置,2位置的数到a[2]位置,,, 问最终交换多少次可以恢复初始的情况. 题目给出一个置换,要求置换的次数,也就是所有轮换个数的最小公倍数.首先求出所有轮换的个数,然后求最小公倍数的时候不能用gcd,因为Mod的取余太大,所以用质因子分解,统计每个质因子出现的最多次数,计算最终的值. #include <cstdio> #include <cstring> #in

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,

hdu 5391 Zball in Tina Town(打表找规律)

问题描述 Tina Town 是一个善良友好的地方,这里的每一个人都互相关心. Tina有一个球,它的名字叫zball.zball很神奇,它会每天变大.在第一天的时候,它会变大11倍.在第二天的时候,它会变大22倍.在第nn天的时候,它会变大nn倍. zball原来的体积是11.Tina想知道,zball在第n-1n−1天时的体积对nn取模是多大呢? Tina是一个蠢蠢的女孩子,当然不会算啦,所以她请你帮她算出这个答案呢. 输入描述 第一行一个正整数TT,表示数据组数 接下来TT行,每行一个正整