Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10322   Accepted: 3360

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

数学问题 统计 polya原理

仍然是项链的套路,没有翻转只有旋转同构。

需要用到欧拉函数优化

1、数据范围大,要先打好素数筛

2、for统计答案的时候要一起算n和n/i,这样只用循环到sqrt(n)

没加这两个T飞了

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#define LL long long
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int pri[mxn],cnt=;
bool vis[mxn];
void init(){
for(int i=;i<mxn;i++){
if(!vis[i])
pri[++cnt]=i;
for(int j=;j<=cnt && (long long)pri[j]*i<mxn;j++){
vis[pri[j]*i]=;
if(i%pri[j]==)break;
}
}
return;
}
int phi(int x){
int res=x;
int m=sqrt(x+0.5);
// for(int i=1;i<=cnt && x>1;i++){
for(int i=;i<=cnt && pri[i]<=m;i++){
if(x%pri[i]==){
res=res/pri[i]*(pri[i]-);
while(x%pri[i]==)
x/=pri[i];
}
}
// printf("x:%d %d\n",x,res);
if(x>)res=res/x*(x-);
return res;
}
int n,p;
int ksm(int c,int k){
int res=;
while(k){
if(k&)(res*=c)%=p;
c=c*c%p;
k>>=;
}
return res;
}
int main(){
int i,j;
int T=read();
init();
while(T--){
n=read();p=read();
int ans=;
for(i=;i*i<n;i++){
if(n%i==){
// printf("i:%d phi:%d\n",n/i,phi(n/i));
ans+=ksm(n%p,i-)*(phi(n/i)%p);
ans%=p;
ans+=ksm(n%p,n/i-)*(phi(i)%p);
ans%=p;
}
}
if(i*i==n)ans=(ans+ksm(n%p,i-)*(phi(i)%p))%p;
printf("%d\n",ans%p);
}
return ;
}

POJ2154 Color的更多相关文章

  1. POJ2154 Color【 polya定理+欧拉函数优化】(三个例题)

    由于这是第一天去实现polya题,所以由易到难,先来个铺垫题(假设读者是看过课件的,不然可能会对有些“显然”的地方会看不懂): 一:POJ1286 Necklace of Beads :有三种颜色,问 ...

  2. 【数论】【Polya定理】【枚举约数】【欧拉函数】【Java】poj2154 Color

    你随便写一下出来,发现polya原理的式子里面好多gcd是相同的,gcd(n,i)=k可以改写成gcd(n/k,i/k)=1,也就是说指数为k的项的个数为phi(n/k),就很好求了,最后除的那个n直 ...

  3. POJ2154 Color(Polya定理)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11654   Accepted: 3756 Description Bead ...

  4. POJ2154 Color 【Polya定理 + 欧拉函数】

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

  5. poj2154 Color ——Polya定理

    题目:http://poj.org/problem?id=2154 今天学了个高端的东西,Polya定理... 此题就是模板,然而还是写了好久好久... 具体看这个博客吧:https://blog.c ...

  6. polya/burnside 学习

    参考链接: http://www.cnblogs.com/hankers/archive/2012/08/03/2622231.html http://blog.csdn.net/raalghul/a ...

  7. 置换群和Burnside引理,Polya定理

    定义简化版: 置换,就是一个1~n的排列,是一个1~n排列对1~n的映射 置换群,所有的置换的集合. 经常会遇到求本质不同的构造,如旋转不同构,翻转交换不同构等. 不动点:一个置换中,置换后和置换前没 ...

  8. HDU2481 Toy

    Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission ...

  9. Sgu294He's Circles

    Description 有一个长度为N的环,上面写着'X'和'E',问本质不同的环有多少种.(N不超过200000). Input The input file contains a single i ...

随机推荐

  1. Error loading MySQLdb module: libmysqlclient.so.18: cannot open shared object file: No such file or directory

    在hue上配置Mysql的时候,出现的错误:  发生服务器错误: Error loading MySQLdb module: libmysqlclient.so.18: cannot open sha ...

  2. 新手入门Sqlalchemy

    此文已由作者尤炳棋授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 入职考拉半年多,一直在从事KLQA平台的开发,KLQA平台后端是用基于python的flask框架搭建的.F ...

  3. OBS源码编译开发

    本文来自网易云社区 作者:梁敏 OBS简介 OBS(Open Broadcaster Software)是免费开源的视频录制和直播软件,支持运行在windows,Mac和linux平台.官方链接 ht ...

  4. mybatis <collection>标签 类型为string时无法获取重复数据错误

    1.场景: fyq_share_house 表 和 fyq_sh_tag 表 两张表是一对多的关系, 一个楼盘对应多个标签,在实体类ShareHouse中使用 /** * 楼盘标签 */ privat ...

  5. 利用selenium自动化登录淘宝

    #encoding=utf-8 from selenium import webdriver from selenium.webdriver.common.action_chains import A ...

  6. C#下16进制和BCD码转换代码

        private static Byte[] ConvertFrom(string strTemp) { try { if (Convert.ToBoolean(strTemp.Length & ...

  7. tomcat8编码设置和gc异常解决

    用startup.bat启动 编码解决: 用编辑器打开catalina.bat文件找到set "JAVA_OPTS=%JAVA_OPTS% %JSSE_OPTS% " 更改为 se ...

  8. remix无法安装的解决方案

    无法安装的原因: 因为remix依赖python 执行python又依赖c++的环境 所以连环导致出错 https://github.com/nodejs/node-gyp 措施一:降级处理 先清理缓 ...

  9. POJ 1463 Strategic game(二分图最大匹配)

    Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot f ...

  10. beta版本冲刺七

    目录 组员情况 组员1:胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:何宇恒 组员11:刘一好 展示组内最新 ...