POJ 2154 color 波利亚群置换

本文介绍了如何使用波利亚原理解决计算不同颜色圆珠手链种类数量的问题,包括输入测试案例和输出答案,以及代码实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Color
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4071 Accepted: 1371

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
解法就是用波利亚原理
不知道为什么,我用__int64就会超时,但是用int就会WA
我考虑到可能是二分幂的时候会超过int,于是换了很多个写法的二分幂模板。最后只有一个可以过
我的代码:
#include<stdio.h> #include<string.h> typedef int ll; ll prime[35000],m; bool flag[35000]; ll eular(ll n) { ll ret=1,i; for(i=2;i*i<=n;i++) { if(n%i==0) { ret=ret*(i-1); n=n/i; while(n%i==0) { n=n/i; ret=ret*i; } } if(n==1) break; } if(n>1) ret=ret*(n-1); return ret%m; } ll exmod(ll p,ll n) { ll sq=1; while(n>0) { if(n%2==1) sq=sq*p%m; p=(p%m)*(p%m)%m; n/=2; } return sq%m; } int main() { ll n,t,ans,i; scanf("%d",&t); while(t--) { ans=0; scanf("%d%d",&n,&m); for(i=1;i*i<n;i++) { if(n%i==0) { ans=(ans+eular(n/i)*exmod(n,i-1)%m)%m; ans=(ans+eular(i)*exmod(n,n/i-1)%m)%m; } } if(i*i==n) ans=(ans+eular(i)*exmod(n,i-1)%m)%m; printf("%d\n",ans%m); } return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值