HDOJ 5392 Infoplane in Tina Town LCM

本文介绍了一种通过宏回放解决游戏挑战的方法。玩家需要计算重复宏操作使序列复原所需的次数,并输出结果模3×2^30+1。文章提供了一个C++实现示例。


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

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值