poj 2154 color
来源:互联网 发布:淘宝双十一超级红包 编辑:程序博客网 时间:2024/05/17 06:49
Color
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10383 Accepted: 3378
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
【分析】
低头!http://blog.csdn.net/acdreamers/article/details/8656247
快速幂写错了!快速幂写错了!快速幂写错了!
白学了!退役了!
【代码】
//poj 2154 color #include<cmath>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define ll long long#define M(a) memset(a,0,sizeof a)#define fo(i,j,k) for(int i=j;i<=k;i++)using namespace std;const int mxn=36005;bool vis[mxn];int pri[mxn];int T,n,p,tot;inline void init(){ fo(i,2,36000) { if(!vis[i]) pri[++tot]=i; for(int j=1;j<=tot && i*pri[j]<=36000;j++) { vis[i*pri[j]]=1; if(i%pri[j]==0) break; } }}inline int phi(int x){ int cnt=x; for(int i=1;pri[i]*pri[i]<=x;i++) { if(x%pri[i]==0) { cnt-=cnt/pri[i]; while(x%pri[i]==0) x/=pri[i]; } } if(x>1) cnt-=cnt/x; return cnt%p;}inline int ksm(int x,int k){ x=x%p; if(k==0) return 1; if(k==1) return x; int tmp=ksm(x,k>>1); if(k&1) return tmp*tmp%p*x%p; return tmp*tmp%p;}int main(){ init(); scanf("%d",&T); while(T--) { int ans=0; scanf("%d%d",&n,&p); for(int i=1;i*i<=n;i++) if(n%i==0) { ans=(ans+phi(n/i)*ksm(n,i-1))%p; if(i*i==n) continue; ans=(ans+phi(i)*ksm(n,n/i-1))%p; } printf("%d\n",ans); } 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 【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
- Color POJ
- JS中数据类型间转换
- HTML5复习 (9)
- 第七单元
- 7
- 特征点检测及跟踪
- poj 2154 color
- Construct the Rectangle
- cndn博客搬家了
- nodemcu搭建web服务器出现的问题
- test2
- 【NGUI】标签页
- Spring mvc,jQuery和JSON数据交互
- 17-05-02 Spring概述 学习笔记
- VS打开老版本的解决方案报错情况