Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11758   Accepted: 3783

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
  n种颜色的珠子组成一个长度为n的圆环,旋转能够到达的属于重复状态,问一共有多少种不同的方案。
  由polya定理可知 ans=SUM{ ngcd(k,n) |  0<=k<n} / n ,n<1e9  如果直接算的话肯定会T。由于所有的gcd(k,n)都是n的因子,可以考虑对
相同的因子分成一组,这样找到每一组的个数就好办了, ans=1/n * SUM{ s(d)*nd |  d|n ,s(d)为使得gcd(k,n)=d成立的k的个数 },
gcd(k,n)==d   -->  gcd(k/d,n/d)==1  --> phi(n/d)  。 所以得出s(d)=phi(n/d) ,枚举一下n的因子统计答案就好了。
  在计算phi的时候先把sqrt(1e9)之内的素数筛出来再计算会更快,否则会超时。
  

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
#define LL long long
#define PI acos(-1.0)
#define N 33333
int mod;
bool isp[N+];
vector<int>prime;
void init(){
int m=sqrt(N+0.5);
for(int i=;i<=N;i++){
if(!isp[i]){
prime.push_back(i);
for(int j=i*i;j<=N;j+=i)isp[j]=;
}
}
}
int qpow(int a,int b,int m){
a%=m;
int r=;
while(b){
if(b&) r=r*a%m;
b>>=;
a=a*a%m;
}
return r%m;
}
int euler(int n){
int m=sqrt(n+0.5);
int ans=n;
for(int i=;prime[i]<=m;++i){
if(n%prime[i]==){
ans=ans/prime[i]*(prime[i]-);
while(n%prime[i]==) n/=prime[i];
}
}
if(n>) ans=ans/n*(n-);
return ans%mod;
}
int main()
{
int t,n,i,j,k,d;
init();
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&mod);
int ans=;
for(i=;i*i<n;++i){
if(n%i==){
(ans+=(euler(n/i)*qpow(n,i-,mod)%mod))%=mod; (ans+=(euler(i)*qpow(n,n/i-,mod)%mod))%=mod;
}
}
if(i*i==n) (ans+=(euler(i)*qpow(n,i-,mod)%mod))%=mod;
cout<<ans<<endl;
}
return ;
}

poj-2154-polya+euler函数的更多相关文章

  1. POJ burnside&&polya整理练习

    POJ 2409 Let it Bead 这题就是polya公式的直接套用,唯一麻烦的是置换群的种类数,由于可以翻转,所以除了要加上pow(c,gcd(s,i))这些平面旋转的置换群,还要加上翻转的. ...

  2. POJ 2407.Relatives-欧拉函数O(sqrt(n))

    欧拉函数: 对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目. 对于一个正整数N的素数幂分解N=P1^q1*P2^q2*...*Pn^qn. Euler函数表达通式:euler(x)=x(1 ...

  3. 2021.08.10 Euler函数总结

    2021.08.10 Euler函数总结 知识: 记 φ(n) 表示在 [1,n] 中与 n互质的数的个数. 1.p为质数,则 \[φ(p^l)=p^l-p=p^{l-1}(p-1) \] 注:每p个 ...

  4. poj 2154 Color(polya计数 + 欧拉函数优化)

    http://poj.org/problem?id=2154 大致题意:由n个珠子,n种颜色,组成一个项链.要求不同的项链数目.旋转后一样的属于同一种.结果模p. n个珠子应该有n种旋转置换.每种置换 ...

  5. [ACM] POJ 2154 Color (Polya计数优化,欧拉函数)

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

  6. poj 2154 Color【polya定理+欧拉函数】

    根据polya定理,答案应该是 \[ \frac{1}{n}\sum_{i=1}^{n}n^{gcd(i,n)} \] 但是这个显然不能直接求,因为n是1e9级别的,所以推一波式子: \[ \frac ...

  7. poj2409 & 2154 polya计数+欧拉函数优化

    这两个题都是项链珠子的染色问题 也是polya定理的最基本和最经典的应用之一 题目大意: 用m种颜色染n个珠子构成的项链,问最终形成的等价类有多少种 项链是一个环.通过旋转或者镜像对称都可以得到置换 ...

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

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

  9. POJ 2154 【POLYA】【欧拉】

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

  10. POJ 2154 Color [Polya 数论]

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

随机推荐

  1. NOIP2018退役祭

    退役感受 在写下这个标题的时候,我的心情是复杂的,无非就是感觉像对一位将要赶往战场的士兵说:"你的战争已经输掉了." 退役了,没有什么好说的.无论再怎么抱怨这题出的真烂也无法改变了 ...

  2. gitlab git

    git网站是进不去的需要加权限才能进去!!!!!!!! 登录进去后 ssh-keygen -t rsa -C "gitlab用户名一般是邮箱" 一路设置好 Use the code ...

  3. UESTC 1697 简单GCD问题(一) 筛法

    简单GCD问题(一) Time Limit: 1500/500MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) 秦队长给你两 ...

  4. 下载安装 Android sdk

    下载地址: https://www.androiddevtools.cn/ 选择sdk 选择版本 将解压出的整个文件夹复制或者移动到 your sdk 路径/platforms文件夹,然后打开SDK ...

  5. Mongodb 创建管理员帐号与普通帐号

    数据库操作权限 readAnyDatabase 任何数据库的只读权限 userAdminAnyDatabase 任何数据库的读写权限 userAdminAnyDatabase 任何数据库用户的管理权限 ...

  6. CentOS6.5下安装配置MySQL数据库

    一.MySQL简介 说到数据库,我们大多想到的是关系型数据库,比如MySQL.Oracle.SQLServer等等,这些数据库软件在Windows上安装都非常的方便,在Linux上如果要安装数据库,咱 ...

  7. Rancher中的服务升级实验

    个容器副本,使用nginx:1.13.0镜像.假设使用一段时期以后,nginx的版本升级到1.13.1了,如何将该服务的镜像版本升级到新的版本?实验步骤及截图如下: 步骤截图: 个容器,选择镜像ngi ...

  8. MySQL学习(十二)

    视图 view 在查询中,我们经常把查询结果当成临时表来看, view是什么?view可以看成一张虚拟表,是表通过某种运算得到的一个投影. 表的变化会影响到视图 既然视图只是表的某种查询的投影,所以主 ...

  9. Asp.net core 学习笔记 ( Azure key-vault )

    参考 : https://docs.microsoft.com/en-us/aspnet/core/security/key-vault-configuration?view=aspnetcore-2 ...

  10. JavaScript学习第一天(一)

    JavaScript介绍 JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本 ...