Color

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

【分析】  

  经典的置换和burnside引理应用。只有旋转。

  考虑旋转i个单位,那么循环节有gcd(n,i)个

  可以化出求 sigma(n^gcd(i,n))/n

  根据莫比乌斯反演得 sigma(n^d*phi[n/d])/n   (d|n) 【表示我莫比乌斯反演白学了ORZ

  即sigma(n^(d-1)*phi[n/d]) (d|n)

  就算一算就好了。【不用欧拉筛,筛不到n的,先求出n的质因子,那么d的质因子也在里面,然后根据定义分解质因数求phi

  最后要除以n,不能求逆元哦,每次少乘一个n就好了。

  然而我还WA着!!!!【AC了再放代码吧

  好了AC了:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Maxn 35000 int n,p;
int pri[Maxn],phi[Maxn],pl;
int d[Maxn],dl;
bool vis[Maxn]; void init()
{
pl=;
memset(vis,,sizeof(vis));
for(int i=;i<=Maxn-;i++)
{
if(vis[i]==)
{
pri[++pl]=i;
phi[i]=i-;
}
for(int j=;j<=pl;j++)
{
if(i*pri[j]>Maxn-) break;
vis[i*pri[j]]=;
if(i%pri[j]!=) phi[i*pri[j]]=phi[i]*(pri[j]-);
else phi[i*pri[j]]=phi[i]*pri[j];
if(i%pri[j]==) break;
}
}
phi[]=;
// for(int i=2;i<=10;i++) printf("%d ",phi[i]);
// printf("\n");
} int qpow(int a,int b,int p)
{
a%=p;
int ans=;
while(b)
{
if(b&) ans=(ans*a)%p;
a=(a*a)%p;
b>>=;
}
return ans;
} void get_d(int n)
{
dl=;
for(int i=;i<=pl;i++) if(n%pri[i]==)
{
while(n%pri[i]==) n/=pri[i];
d[++dl]=pri[i];
}
if(n!=) d[++dl]=n;
} int gphi(int x)
{
int ans=x;
for(int i=;i<=dl;i++) if(x%d[i]==)
{
ans=ans/d[i]*(d[i]-);
}
return ans;
} int main()
{
int T;
scanf("%d",&T);
init();
while(T--)
{
scanf("%d%d",&n,&p);
get_d(n);
int ans=;
for(int i=;i*i<=n;i++) if(n%i==)
{
ans=(ans+(qpow(n,i-,p)*(gphi(n/i)%p)))%p;
if(i*i!=n) ans=(ans+(qpow(n,n/i-,p)*(phi[i]%p)))%p;
}
printf("%d\n",ans);
}
return ;
}

2017-01-13 11:48:09

【POJ 2154】 Color (置换、burnside引理)的更多相关文章

  1. BZOJ_[HNOI2008]_Cards_(置换+Burnside引理+乘法逆元+费马小定理+快速幂)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1004 共n个卡片,染成r,b,g三种颜色,每种颜色的个数有规定.给出一些置换,可以由置换得到的 ...

  2. 【BZOJ1004】【HNOI2008】Cards 群论 置换 burnside引理 背包DP

    题目描述 有\(n\)张卡牌,要求你给这些卡牌染上RGB三种颜色,\(r\)张红色,\(g\)张绿色,\(b\)张蓝色. 还有\(m\)种洗牌方法,每种洗牌方法是一种置换.保证任意多次洗牌都可用这\( ...

  3. POJ 2888 Magic Bracelet ——Burnside引理

    [题目分析] 同样是Burnside引理.但是有几种颜色是不能放在一起的. 所以DP就好了. 然后T掉 所以矩阵乘法就好了. 然后T掉 所以取模取的少一些,矩阵乘法里的取模尤其要注意,就可以了. A掉 ...

  4. POJ 2154 Color ——Burnside引理

    [题目分析] 数据范围有些大. 然后遍求欧拉函数,遍求和就好了,注意取模. [代码] #include <cstdio> #include <cstring> #include ...

  5. poj 2154 Color——带优化的置换

    题目:http://poj.org/problem?id=2154 置换的第二道题! 需要优化!式子是ans=∑n^gcd(i,n)/n (i∈1~n),可以枚举gcd=g,则有phi( n/g )个 ...

  6. poj 2154 Color < 组合数学+数论>

    链接:http://poj.org/problem?id=2154 题意:给出两个整数 N 和 P,表示 N 个珠子,N种颜色,要求不同的项链数, 结果 %p ~ 思路: 利用polya定理解~定理内 ...

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

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

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

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

  9. 组合数学 - 波利亚定理 --- poj : 2154 Color

    Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7873   Accepted: 2565 Description ...

  10. poj 2154 Color

    这是道标准的数论优化的polya题.卡时卡的很紧,需要用int才能过.程序中一定要注意控制不爆int!!!我因为爆intWA了好久=_=…… 题目简洁明了,就是求 sigma n^gcd(i,n):但 ...

随机推荐

  1. 【CodeForces】896 B. Ithea Plays With Chtholly

    [题目]B. Ithea Plays With Chtholly [题意]交互题,有n格,每次给一个[1,c]的数字,回答填入的位置后再次给数字,要求在m轮内使n格填满且数列不递减.n,m>=2 ...

  2. 小程序_RSA加密功能

    这是开发的第三个小程序,基于一个物流系统,简化功能开发下单流程.登录的时候,系统是使用RSA进行加解密的. 流程:第一个接口获取到后端传过来的密匙共钥(publicKey),通过公钥使用RSA加密密码 ...

  3. Angular2.0 基础: 环境搭建

    最近在学习Angular2的使用,其实看过Angular2 文档的都知道,相比于之前的Angular1,Angular2 的改动还是挺大的. 而对于‘angular2 的本地开发环境的搭建的中,我们首 ...

  4. 2.0 docker安装

    问题列表: Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again 解:处 ...

  5. AGC025简要题解

    AGC025简要题解 B RGB Coloring 一道简单题,枚举即可. C Interval Game 考虑可以进行的操作只有两种,即左拉和右拉,连续进行两次相同的操作是没有用的. 左拉时肯定会选 ...

  6. 嵌入式 uboot引导kernel,kernel引导fs【转】

    转自:http://www.cnblogs.com/lidabo/p/5383934.html#3639633 1.uboot引导kernel: u-boot中有个bootm命令,它可以引导内存中的应 ...

  7. Oracle 合并 merger into

    merge into copy_emp1 c  using employees e  on (c.employee_id=e.employee_id)when matched then  update ...

  8. VPS性能综合测试(7):服务器压力测试,VPS系统负载测试

    1.可能有的VPS主机使用性能测评工具得出的结果很优秀,但是最终运用到实际生产时却发现VPS主机根本无法承受理论上应该达到的流量压力,这时我们就不得不要怀疑VPS商是不是对VPS主机的参数进行了“篡改 ...

  9. RadioButton 带下划线切换的案例

    xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id=& ...

  10. [会装]Spark standalone 模式的安装

    1. 简介 以standalone模式安装spark集群bin运行demo. 2.环境和介质准备 2.1 下载spark介质,根据现有hadoop的版本选择下载,我目前的环境中的hadoop版本是2. ...