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. EF更新时出错,An error occurred while updating the entries. See the inner exception for details

           在使用EF进行更新数据时出错,报出的异常是 "An error occurred while updating the entries. See the inner excep ...

  2. 怎么使用pipenv管理你的python项目

    原文链接:https://robots.thoughtbot.com/how-to-manage-your-python-projects-with-pipenv 翻译者:Jiong 在thought ...

  3. C++ XML文件解析

    使用tinyxml2库,git地址https://github.com/leethomason/tinyxml2 只需要使用tinyxml2.h tinyxml2.cpp即可,同时需要using na ...

  4. AMF3 在Unity中使用AMF3和Java服务器通信

    现在在做的项目是一个网页游戏的移植到移动端. 所以服务器直接使用原来的代码.原来的游戏是as3实现,使用flash amf3数据通信. Unity 使用C#作为脚本语言,所以就需要.net的amf3解 ...

  5. Mybatis学习系列(五)关联查询

    前面几节的示例基本都是一些单表查询,实际项目中,经常用到关联表的查询,比如一对一,一对多等情况.在Java实体对象中,一对一和一对多可是使用包装对象解决,属性使用List或者Set来实现,在mybat ...

  6. 入口文件-npm run dev

    如果你是用vue.js官网提供的脚手架工具并沿用默认配置的话,你执行npm run dev的时候会出来页面,是因为你根目录下的package.json文件里script配置了"dev&quo ...

  7. Struts1之logic标签

    logic是Struts1中的逻辑标签 <%@ taglib prefix="logic" uri="http://struts.apache.org/tags-l ...

  8. 纯真IP数据库(qqwry.dat)转换成最新的IP数据库格式(ipwry.dat)

    纯真IP数据库(qqwry.dat)转换成最新的IP数据库格式(ipwry.dat) 转载自:http://blog.cafeboy.org/2011/02/25/qqwry-to-ipwry/ ht ...

  9. [洛谷P3803] 【模板】多项式乘法(FFT, NTT)

    题目大意:$FFT$,给你两个多项式,请输出乘起来后的多项式. 题解:$FFT$,由于给的$n$不是很大,也可以用$NTT$做 卡点:无 C++ Code:  FFT: #include <cs ...

  10. [Leetcode] word break ii拆分词语

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...