POJ 2154 color 波利亚群置换
来源:互联网 发布:金建软件 编辑:程序博客网 时间:2024/05/16 17:09
Color
Time Limit: 2000MS Memory Limit: 65536KTotal 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.
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
51 300002 300003 300004 300005 30000
Sample Output
131170629
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;}
- POJ 2154 color 波利亚群置换
- 【POJ 2154】Color(置换群)
- POJ-2154 Color
- poj 2154 Color
- POJ 2154 Color
- poj 2154 Color
- POJ 2154 Color
- POJ 2154 Color
- POJ 2154 Color
- poj 2154 color
- POJ 2154 Color 【polya+dfs】
- POJ 2154 Color Ploya定理
- poj 2154 Color ----polya计数
- POJ 2154 Color(polya)
- poj 2154 Color( polya 计数)
- POJ 2154 Color(polya+欧几里得)
- Color POJ
- Color POJ
- hadoop命令笔记
- C++中金融库Quantlib的配置和使用
- Java应用程序项目的打包与发行
- 时间和日期操作
- Ubuntu下安装adb
- POJ 2154 color 波利亚群置换
- 开机报 XKDHA is missing
- 【学习】富客户端应用程序(RCA)分层设计的要点浅析
- 提高支持N协议无线网卡速率
- 一个多月来的心血,好不容易出来的系统,多谢老大的美术设计。。。
- eclipse重构详解
- 一个实例理解Java Reference
- JQuery特效,上下移动的选项卡
- C++对象内存布局测试总结