POJ 2154 Color(polya定理+欧拉函数)

Color
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10821 Accepted: 3499

Description

Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of the necklace can be produced. You should know that the necklace might not use up all the N colors, and the repetitions that are produced by rotation around the center of the circular necklace are all neglected. 

You only need to output the answer module a given number P. 

Input

The first line of the input is an integer X (X <= 3500) representing the number of test cases. The following X lines each contains two numbers N and P (1 <= N <= 1000000000, 1 <= P <= 30000), representing a test case.

Output

For each test case, output one line containing the answer.

Sample Input

5
1 30000
2 30000
3 30000
4 30000
5 30000

Sample Output

1
3
11
70
629

Source

POJ Monthly,Lou Tiancheng

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top


题意:给你n个珠子组成一个项链,让你用n种颜色给这n个珠子染色,

两个项链一样当前仅当旋转已经角度后能重合,问你组成项链的方案数。

题解:polya定理计数的模板题,然而这里n有10亿这么大,所以不能暴力找gcd(i,n)。
我们设l=n/gcd(n,i)为循环节的长度,则gcd(n,i)=n/l。
我们设cnt=gcd(n,i),表示循环节的个数。因为cnt一定是i的因数,故设i=cnt*x。
又因为n=cnt*l,故gcd(n,i)=gcd(cnt*l,cnt*x)=cnt,该等式成立当且仅当gcd(l,x)=1
此时问题转化为求gcd(l,x)=1,时x的个数,也就是求l的欧拉函数(不懂的自行百度欧拉函数)
总复杂度由O(nlog(n) )变为O(sqrt(n)log(n))。。。。。
#include<set>      
#include<map>         
#include<stack>                
#include<queue>                
#include<vector>        
#include<string>     
#include<time.h>    
#include<math.h>                
#include<stdio.h>                
#include<iostream>                
#include<string.h>                
#include<stdlib.h>        
#include<algorithm>       
#include<functional>        
using namespace std;                
#define ll long long          
#define inf 1000000000         
#define mod 1000000007                 
#define maxn  50005    
#define lowbit(x) (x&-x)                
#define eps 1e-9    
int a[maxn]={1,1},b[maxn],cnt;
void init()
{
	ll i,j;
	for(i=2;i<maxn;i++)
	{
		if(a[i])
			continue;
		b[++cnt]=i;
		for(j=i*i;j>0 && j<maxn;j+=i)
			a[j]=1;
	}
}
ll ol(ll x)  
{  
    ll i,res=x;  
    for(i=1;i<=cnt && b[i]*b[i]<=x;i++)  
    {  
        if(x%b[i]==0)  
        {  
            res=res-res/b[i];  
            while(x%b[i]==0)  
                x/=b[i];  
        }  
    }  
    if(x>1)  
        res=res-res/x;  
    return res;  
} 
int q(int x,int y,int p)    
{    
    int res=1; 
	x=x%p;
    while(y)    
    {    
        if(y%2)    
            res=res*x%p;    
        x=x*x%p;    
        y/=2;    
    }    
    return res;    
}    
int main(void)    
{    
	init();
    int n,i,j,p,t;
	scanf("%d",&t);
    while(t--)    
    {    
		int ans=0;
		scanf("%d%d",&n,&p);
		for(i=1;i*i<=n;i++)
		{
			if(n%i==0)
			{
				ans=(ans+ol(i)%p*q(n,n/i-1,p))%p;
				if(i*i==n)
					break;
				ans=(ans+ol(n/i)%p*q(n,i-1,p))%p;
			}
		}
        printf("%d\n",ans);    
    }    
    return 0;    
}  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值