码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 5392 Infoplane in Tina Town LCM

时间:2015-08-17 17:21:50      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:


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

For each test case, the first line is an integer n representing the length of permutation. n3?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 ( 1Ain).
 

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
 


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

HDOJ 5392 Infoplane in Tina Town LCM

标签:

原文地址:http://blog.csdn.net/ck_boss/article/details/47726993

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!