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. 第三篇 Postman之 Tests(后置处理器,断言)

    第二篇里讲了手动设置全局变量及局部变量的方法,但是这有一个缺点,就是每次测试之前,都需要获取相关变量值,手动再填写更新到对应的全局变量或者局部变量里,这对于想进行自动化执行的人或者懒人就不太友好了,本 ...

  2. LINQ学习笔记——(1)添加扩展方法

    目的:  对已存在类型的行为进行扩展 注意事项:    扩展方法是一种特殊的静态方法    扩展方法必须在静态类中定义    扩展方法的优先级低于同名的类方法    扩展方法只在特定的命名空间内有效 ...

  3. LeetCode - 66. Plus One(0ms)

    Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The ...

  4. POJ 1015 Jury Compromise (动态规划)

    dp[i][j]代表选了i个人,D(J)-P(J)的值为j的状态下,D(J)+P(J)的最大和. #include <cstdio> #include <cstring> #i ...

  5. 数据结构7——BFS

    一.重拾关键 宽度优先搜索,也有称为广度优先搜索,简称BFS.类似于树的按层次遍历的过程. 初始状态:图G所有顶点均未被访问过,任选一点v. 遍历过程:假设从图中某顶点v出发,在访问了v之后依次访问v ...

  6. Week7 Teamework from Z.XML-任务分配

    任务分配 Z.XML任务初步分配新鲜出炉,请关注! 初步估计,我们的项目需要191小时.但是根据敏捷开发的方法,我们将在开发过程中根据情况迅速调整任务分配,以适应当时问题.

  7. Internet History,Technology and Security

    Internet History,Technology and Security(简单记录) First Week High Stakes Research in Computing,and Comm ...

  8. 一个简单的NetCore项目:2 - 登录

    1-UI,登陆界面布局 PS:使用的是metronic 框架,没有用过的可以自行百度. 1.1 metronic 放在wwwroot文件夹下面 1.2  metronic 中的 open sans 使 ...

  9. java面向对象课程设计-数学表达式计算器

    项目简介 设计一个计算器,其能够: 1)由用户输入一个简单的四则运算表达式,求出其计算结果后显示. 2)特殊数学函数,如:绝对值.取整.三角函数.倒数.平方根.平方.立方等. 3)对一定范围内的数字将 ...

  10. php开发中处理emoji表情和颜文字的兼容问题

    背景:随着手机的普及,现在移动开发很火爆,已经远远超过了pc端.在移动设备经常会发生用户发送的内容中包含emoji表情,在显示时就是乱码.一般是因为Mysql表设计时,都是用UTF8字符集的.把带有e ...