【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):但 ...
随机推荐
- Bzoj3224 / Tyvj 1728 普通替罪羊树
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 12015 Solved: 5136 Description 您需要写一种数据结构(可参考题目标题), ...
- 【LibreOJ】#541. 「LibreOJ NOIP Round #1」七曜圣贤
[题意]一开始车上有编号为0~a的红茶,过程中出现的红茶编号仅有[0,b),有三种操作: 1.买进编号未在车上出现过的红茶. 2.丢掉车上指定编号的红茶. 3.将最早丢出去的红茶捡回来. 每次操作后求 ...
- 【codevs】3196 黄金宝藏
[算法]区间DP+博弈论 [题解]其实它都不是博弈题…… 很自然的可以设f[i][j]表示i~j先手可取得的最大价值. 容易得到转移式:f[i][j]=max(a[i]+sum[i+1~j]-f[i+ ...
- tornado 学习之GET POST方法 -- (转)
import torndb import tornado.web import tornado.ioloop from tornado.options import define,options,pa ...
- perl_nc.pl
#!/usr/bin/perl use strict; use IO::Socket; use IO::Select; use Getopt::Std; my %option;getopts('lp: ...
- Deep Learning基础--26种神经网络激活函数可视化
在神经网络中,激活函数决定来自给定输入集的节点的输出,其中非线性激活函数允许网络复制复杂的非线性行为.正如绝大多数神经网络借助某种形式的梯度下降进行优化,激活函数需要是可微分(或者至少是几乎完全可微分 ...
- 【Educational Codeforces Round 22】
又打了一场EDU,感觉这场比23难多了啊…… 艹还是我太弱了. A. 随便贪心一下. #include<bits/stdc++.h> using namespace std; ,ans=- ...
- python中eval函数使用
把字符串转换为字典: s = "{'a':1}" eval(s)
- MongoDB的win安装教程
写在前面的 Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐.Mongo DB很好的实现了面向对象的思想(OO思想),在Mo ...
- LeetCode解题报告—— Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...