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. (转) K-Means聚类的Python实践

    本文转自: http://python.jobbole.com/87343/ K-Means聚类的Python实践 2017/02/11 · 实践项目 · K-means, 机器学习 分享到:1 原文 ...

  2. Katana的WebAPI集成Swagger 解决方案

    这位大哥写的博客很清楚了,我就不重复了. http://www.cnblogs.com/caodaiming/p/4156476.html 错误解决 http://blog.csdn.net/gold ...

  3. PTA 堆栈操作合法性(20 分)

    7-1 堆栈操作合法性(20 分) 假设以S和X分别表示入栈和出栈操作.如果根据一个仅由S和X构成的序列,对一个空堆栈进行操作,相应操作均可行(如没有出现删除时栈空)且最后状态也是栈空,则称该序列是合 ...

  4. hihoCoder 1145 幻想乡的日常(树状数组 + 离线处理)

    http://hihocoder.com/problemset/problem/1145?sid=1244164 题意: 幻想乡一共有n处居所,编号从1到n.这些居所被n-1条边连起来,形成了一个树形 ...

  5. 【Ruby】【变量】

    知识点[Ruby 中$开头的全局变量.内部变量.隐藏变量介绍] Ruby 中充满了一系列的隐藏变量,我们可以从这些预定义的全局变量中获取一些有意思的信息. 全局进程变量 $$ 表示当前运行的 ruby ...

  6. React Native实现微信分享

    (一)前言 现阶段大家在使用React Native开发项目的时候,基本都会使用到微信好友或者微信朋友圈分享功能吧,那么今天我就带大家实现以下RN微信好友以及朋友圈的分享功能. 刚创建的React N ...

  7. JS中的document.title可以获取当前网页的标题

    <!DOCTYPE html> <html> <head> <title>jb51.net</title> </head> &l ...

  8. SSL证书申请,如何快速通过SSL文件验证。

    申请SSL证书会让我们进行验证域名,一般方式如下: 1.FTP验证 2.文件验证 3.DNS验证 这三种方式各有各的优缺点,本文解决如何在IIS的环境下通过sslforfree网站的文件验证. 域名: ...

  9. node项目配置成nginx启动

    node项目配置成nginx启动 1.新建ant.conf upstream antNodeJs{ server 127.0.0.1:8000; keepalive 64; } server { li ...

  10. prometheus的agent 二次开发代码参考

    import com.codahale.metrics.MetricRegistry;import io.prometheus.client.CollectorRegistry;import io.p ...