Color
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7630   Accepted: 2507

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/tsaid/article/details/7366708

题意:给出两个整数n和p,代表n个珠子,n种颜色,要求不同的项链数,翻转置换不考虑。

结果模p.

题解:

我们知道gcd(i,n)表示了循环节的个数。

比如gcd(2,6) = 2, 它的详细过程为:[1。3。5] [2。4,6]

对于随意一个循环置换,他全部循环节的长度为 n / gcd(i,n),在上面的样例中: 循环节长度 = 6 / gcd(2,6) = 3

为了方便说明。用L表示循环节的长度,显然 L | n

假设我们枚举L,求出对于每个L有多少个i, 使得 L = n / gcd (i,n), 那么我们实际上也得到了循环节个数为 n / L 的置换个数。

将L = n / gcd (i,n)转换一下得到:n / L = gcd(i,n )

设 cnt = n / L = gcd(i, n)  注:cnt表示循环节个数,L表示每个循环节的长度

由于 cnt | i, 所以可令 i = cnt * t; ( 由于0 <= i < n, 所以0 <= t < n / cnt = L )

又由于 cnt = n / L, 所以 n = cnt * L;

则 gcd ( i, n ) = gcd ( cnt*t, cnt*L ) = cnt;  ①

可知当 gcd ( t, L ) = 1 时 ① 式成立。

因为 gcd ( t, L ) = 1 的个数就是 Euler(L)的个数。

所以我们能够得到结论:循环节个数为n/L的置换有Euler(L)个。

代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
bool isprime[50001];
int prime[50001];
int len=0;;
int n,p; void sieve()
{
for(int i=0;i<=50000;i++)
isprime[i]=1;
isprime[0]=isprime[1]=0;
for(int i=2;i<=50000;i++)
{
if(isprime[i])
{
prime[len++]=i;
for(int j=2*i;j<=50000;j+=i)
isprime[j]=0;
}
}
} int euler(int n)
{
int res=n;
for(int i=0;i<len&&prime[i]*prime[i]<=n;i++)
{
if(n%prime[i]==0)
{
res=res/prime[i]*(prime[i]-1);
while(n%prime[i]==0)
n/=prime[i];
}
}
if(n>1)
res=res/n*(n-1);
return res;
} int pow(int p,int n,int mod)
{
int ans=1;
p=p%mod;
while(n)
{
if(n&1)
ans=ans*p%mod;
p=p*p%mod;
n/=2;
}
return ans;
} int main()
{
sieve();
int t;
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+euler(i)%p*pow(n,n/i-1,p))%p;//注意取余的位置。 euler(i)%p不取余就WA
if(i*i==n)//仅仅要一个i就能够了
break;
ans=(ans+euler(n/i)%p*pow(n,i-1,p))%p;
}
printf("%d\n",ans);
}
return 0;
}

最新文章

  1. js继承理解(有引用)
  2. 个人练习--gif图流程
  3. 第十四章:Annotation(注释)
  4. python:浅析python 中__name__ = &#39;__main__&#39; 的作用
  5. ASP.NET Web API 如何通过程序控制返回xml还是json
  6. 1005 Number Sequence(HDU)
  7. JVM调优总结(四)-垃圾回收面临的问题
  8. C++基础知识1
  9. 【Node.js】一、搭建基于Express框架运行环境+更换HTML视图引擎
  10. oracleDB python chines_miscode
  11. IDEA打开maven项目dependencies红线
  12. Kotlin入门(24)如何自定义视图
  13. 基于jest和puppeteer的前端自动化测试实战
  14. Python之岭回归
  15. DevExpress WPF v18.2新版亮点(三)
  16. TZOJ 2560 Geometric Shapes(判断多边形是否相交)
  17. json 删除、添加对象
  18. 2-9 js基础 cookie封装
  19. Qt浅谈内存泄露(总结)
  20. java map 转 json 自编封装

热门文章

  1. To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog]
  2. nyoj--635--Oh, my goddess(dfs)
  3. Python迭代与递归方法实现斐波拉契数列
  4. C - Oleg and shares
  5. Service不完全解析
  6. vuejs开发H5页面总结
  7. ubuntu 安装 OpenCV-CUDA
  8. deeplearning4j – 分布式DL开源项目
  9. [Intermediate Algorithm] - Smallest Common Multiple
  10. 动漫绘画软件优动漫PAINT最近所用文件