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;
}

[ACM] POJ 2154 Color (Polya计数优化,欧拉函数)的更多相关文章

  1. POJ 2154 【POLYA】【欧拉】

    前记: TM终于决定以后干啥了.这几天睡的有点多.困饿交加之间喝了好多水.可能是灌脑了. 切记两件事: 1.安心当单身狗 2.顺心码代码 题意: 给你N种颜色的珠子,串一串长度问N的项链,要求旋转之后 ...

  2. POJ-2888 Magic Bracelet(Burnside引理+矩阵优化+欧拉函数+逆元)

    Burnside引理经典好题呀! 题解参考 https://blog.csdn.net/maxwei_wzj/article/details/73024349#commentBox 这位大佬的. 这题 ...

  3. POJ 2154 color (polya + 欧拉优化)

    Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). You ...

  4. POJ 2154 Color [Polya 数论]

    和上题一样,只考虑旋转等价,只不过颜色和珠子$1e9$ 一样的式子 $\sum\limits_{i=1}^n m^{gcd(i,n)}$ 然后按$gcd$分类,枚举$n$的约数 如果这个也化不出来我莫 ...

  5. 题解报告:poj 2480 Longge's problem(欧拉函数)

    Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...

  6. poj3696 快速幂的优化+欧拉函数+gcd的优化+互质

    这题满满的黑科技orz 题意:给出L,要求求出最小的全部由8组成的数(eg: 8,88,888,8888,88888,.......),且这个数是L的倍数 sol:全部由8组成的数可以这样表示:((1 ...

  7. POJ 2773 Happy 2006【GCD/欧拉函数】

    根据欧几里德算法,gcd(a,b)=gcd(a+b*t,b) 如果a和b互质,则a+b*t和b也互质,即与a互质的数对a取模具有周期性. 所以只要求出小于n且与n互质的元素即可. #include&l ...

  8. POJ 3696 The Luckiest number (欧拉函数,好题)

    该题没思路,参考了网上各种题解.... 注意到凡是那种11111..... 22222..... 33333.....之类的序列都可用这个式子来表示:k*(10^x-1)/9进而简化:8 * (10^ ...

  9. POJ 3090 Visible Lattice Points 【欧拉函数】

    <题目链接> 题目大意: 给出范围为(0, 0)到(n, n)的整点,你站在(0,0)处,问能够看见几个点. 解题分析:很明显,因为 N (1 ≤ N ≤ 1000) ,所以无论 N 为多 ...

随机推荐

  1. 0x53 区间DP

    石子合并 搞笑 #include<cstdio> #include<iostream> #include<cstring> #include<cstdlib& ...

  2. WebRequestSugar

    用法 WebRequestSugar ws = new WebRequestSugar(); //可选参数 //ws.SetAccept //ws.SetContentType //ws.SetCoo ...

  3. 【联系】—— Beta 分布与二项分布、共轭分布

    1. 伯努利分布与二项分布 伯努利分布:Bern(x|μ)=μx(1−μ)1−x,随机变量 x 取值为 0,1,μ 表示取值为 1 的概率: 二项分布:Bin(m|N,μ)=(Nm)μm(1−μ)N− ...

  4. EOJ 1114 素数环

    题意 一个由自然数 1…n (n≤18) 素数环就是如下图所示,环上任意两个节点上数值之和为素数. 1 / \   4  2 \ /    3 Input 输入只有一个数 n,表示你需要建立一个 1… ...

  5. Blender插件之Panel

    目标 [x] 总结Blender之Panel 总结 Blender之Panel需要从Blender界面组成开始理解. 直观上Blender的界面层次为 Editors ‣ Regions ‣ (Tab ...

  6. java异常处理和自定义异常利用try和catch让程序继续下去(回来自己再写个例子试运行下)

    注意:想在catch的参数里使用自定义的异常,则必须先将这个异常抛出才行.(throws是具有抛出异常的能力,并未抛出,throw new MyException是抛出异常,catch是捕获异常,只有 ...

  7. python ansible api

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @File : test2.py # @Author: Anthony.waa # @Date : 20 ...

  8. javascript实现双击网页自动滚动,单击滚动停止

    当网页中有长篇文章时,浏览起来就比较吃劲了,想想一边忙着拖动滚动条,一边忙着浏览,确实挺累人的.为了客人能够轻松的浏览,我们可以使用script代码实现网页的自动滚屏,当双击网页的时候,网页将会自动向 ...

  9. 带你认识闻名遐迩的ZBrush

    ZBrush®是一款数字雕刻和绘画软件,它以其强大的功能和直观的工作流程彻底改变了3D行业.ZBrush 4R8是目前最新版本,它秉持一贯的简洁界面风格,给如今的数字艺术工作者提供了世界领先的工具.它 ...

  10. HAOI2006 受欢迎的牛 缩点

    不难分析出我们就是要求是否有唯一一个出度为0的强连通分量. Code: #include<cstdio> #include<stack> #include<algorit ...