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

BestCoder Round #51 (div.2)

/* ***********************************************
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

时间: 2024-10-01 07:01:08

HDOJ 5392 Infoplane in Tina Town LCM的相关文章

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

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 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]置换的最小循环长度,最小公倍数取模,输入挂

题意:给一个置换,求最小循环长度对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 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以内小规模

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,