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. 写一个类似淘宝的ios app需要用到哪些技术?

    写一个类似淘宝的ios app需要用到哪些技术? 让我想起了有人私信我,说不缺钱,做个类似知乎的东西,包括加运营,需要多少钱. 扯淡结束,正好最近看了一点这方面的东西,也许对题主来说有点帮助. 手机淘 ...

  2. Hyper和Vmware冲突,Device/Credential Guard 不兼容

    切换到VM的时候,采用关闭策略 1.PS管理员关闭命令 bcdedit /set hypervisorlaunchtype off 2.系统设置,启用或关闭Windows功能那里,关闭Hyper-V ...

  3. table合并单元格 colspan(跨列)和rowspan(跨行)

    colspan和rowspan这两个属性用于创建特殊的表格. colspan是“column span(跨列)”的缩写.colspan属性用在td标签中,用来指定单元格横向跨越的列数: 在浏览器中将显 ...

  4. MySql数据库中乱码问题解决方案

    show variables like 'character%';    //查看当前各系统位置的字符编码格式 问题一: Incorrect string value: '\xB4\xF3\xB4\x ...

  5. SVO在ROS下的配置与运行

    最近在做实验的时候,需要配置SVO,下面讲讲其中的过程以及遇到的问题: 首先说明配置环境:Ubuntu 14.04 + ROS indigo,ROS的安装我参考了ROS的官网上给出的教程:http:/ ...

  6. Ssh safe

    新建用户,设置密码 useradd eason passwd eason 不允许root直接登陆 修改配置文件 vi /etc/ssh/sshd_config 禁止root登录 查找“#PermitR ...

  7. 在APP开发中,如何优雅的设计APP页面

    1.明确页面设计在整个产品设计中的位置 互联网产品设计的流程大致是:产品定位——需求分析——信息架构设计——流程设计——页面框架设计——设计说明——输出设计文档.可以看到页面设计是处于整个流程的后期, ...

  8. dotnetnuke7.3.3 下弹出对话框(dnnConfirm())的使用

    今天用dnn做一个列表里边有一个删除操作,就想做个对话框确定是否删除? 正常理解马上想到js的confirm("")函数,但是发现Dnn把这个函数给重写啦,弹出的对话框竟然是英文的 ...

  9. wp7图片上传服务器

    做一个wp7手机上传图片到服务器的功能,具体丝路是在手机端做一个照相或者选择图片的功能,点击上传,在服务器端做一个一般处理程序,接受上传的文件,存入文件夹,下面是主要代码: 手机端代码: /// &l ...

  10. Javascript阻止表单提交

    Javascript阻止表单提交 Html 1.<form name="loginForm" action="login.aspx" method=&qu ...