【POJ 2154】 Color (置换、burnside引理)
ColorDescription
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 30000Sample Output
1
3
11
70
629Source
【分析】
经典的置换和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引理)的更多相关文章
- BZOJ_[HNOI2008]_Cards_(置换+Burnside引理+乘法逆元+费马小定理+快速幂)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1004 共n个卡片,染成r,b,g三种颜色,每种颜色的个数有规定.给出一些置换,可以由置换得到的 ...
- 【BZOJ1004】【HNOI2008】Cards 群论 置换 burnside引理 背包DP
题目描述 有\(n\)张卡牌,要求你给这些卡牌染上RGB三种颜色,\(r\)张红色,\(g\)张绿色,\(b\)张蓝色. 还有\(m\)种洗牌方法,每种洗牌方法是一种置换.保证任意多次洗牌都可用这\( ...
- POJ 2888 Magic Bracelet ——Burnside引理
[题目分析] 同样是Burnside引理.但是有几种颜色是不能放在一起的. 所以DP就好了. 然后T掉 所以矩阵乘法就好了. 然后T掉 所以取模取的少一些,矩阵乘法里的取模尤其要注意,就可以了. A掉 ...
- POJ 2154 Color ——Burnside引理
[题目分析] 数据范围有些大. 然后遍求欧拉函数,遍求和就好了,注意取模. [代码] #include <cstdio> #include <cstring> #include ...
- poj 2154 Color——带优化的置换
题目:http://poj.org/problem?id=2154 置换的第二道题! 需要优化!式子是ans=∑n^gcd(i,n)/n (i∈1~n),可以枚举gcd=g,则有phi( n/g )个 ...
- poj 2154 Color < 组合数学+数论>
链接:http://poj.org/problem?id=2154 题意:给出两个整数 N 和 P,表示 N 个珠子,N种颜色,要求不同的项链数, 结果 %p ~ 思路: 利用polya定理解~定理内 ...
- [ACM] POJ 2154 Color (Polya计数优化,欧拉函数)
Color Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7630 Accepted: 2507 Description ...
- poj 2154 Color(polya计数 + 欧拉函数优化)
http://poj.org/problem?id=2154 大致题意:由n个珠子,n种颜色,组成一个项链.要求不同的项链数目.旋转后一样的属于同一种.结果模p. n个珠子应该有n种旋转置换.每种置换 ...
- 组合数学 - 波利亚定理 --- poj : 2154 Color
Color Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7873 Accepted: 2565 Description ...
- poj 2154 Color
这是道标准的数论优化的polya题.卡时卡的很紧,需要用int才能过.程序中一定要注意控制不爆int!!!我因为爆intWA了好久=_=…… 题目简洁明了,就是求 sigma n^gcd(i,n):但 ...
随机推荐
- JDK工具学习
javap: 可以对照源代码和字节码,从而了解很多编译器内部的工作. 查看class字节码:JDK有自带的工具包,使用javap命令打开.class文件就行 javap -c JAVAPTest
- Android项目分包---总结-------直接使用
注: 本文是从该文摘抄而来的.简单的说,就是阅读了该文,然后,再自己复述,复制形成该文. 1.罗列Android项目的分包规则 微盘使用分包规则 如下: 1).第一层com.sin ...
- 【BZOJ】1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害
[题意]给定无向图,现在可能有一些点已经被删除,只给出信息是c个点未被删除且不能到达结点1,求最少的删除点个数. [算法]最小割 [题解]本题和1的区别是:1求的是最少的不能到达1的结点数,那么就把损 ...
- UIPageControl---iOS-Apple苹果官方文档翻译
本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/Ch ...
- 解决嵌套GridView显示不全的问题
package com.adan.selectcitydome.view; import android.content.Context; import android.util.AttributeS ...
- js中字符串的操作
1.length 获取字符串长度 var str = "hello world"; alert(str); 2.索引 通过下标获取字符串指定位置的字符,但是不能改变该索引对应的值 ...
- 使用yo -v查看yeoman版本号
使用yo -v无法查看yeoman版本,这是旧版本的方法 新版本使用yo --version即可查看
- python自动开发之第二十五天
一.组合搜索 参考: http://www.cnblogs.com/ccorz/p/5985205.html 二.JSONP 1.在同源策略下,在某个服务器下的页面是无法获取到该服务器以外的数据的,但 ...
- C基础 redis缓存访问
引言 先说redis安装, 这里采用的环境是. Linux version --generic (buildd@lgw01-) (gcc version (Ubuntu -14ubuntu2) ) # ...
- webapi-1 给现有MVC 项目添加 WebAPI
1. 增加一个WebApi Controller, VS 会自动添加相关的引用,主要有System.Web.Http,System.Web.Http.WebHost,System.Net.Http 2 ...