由于这是第一天去实现polya题,所以由易到难,先来个铺垫题(假设读者是看过课件的,不然可能会对有些“显然”的地方会看不懂):

一:POJ1286 Necklace of Beads :有三种颜色,问可以翻转,可以旋转的染色方案数,n<24。

1,n比较小,恶意的揣测出题人很有可能出超级多组数据,所以先打表。

2,考虑旋转:

for(i=;i<n;i++)  sum+=pow(n,gcd(n,i));  

3,考虑翻转:

if(n&)  sum+=n*pow(,n/+) ;
else {
sum+=n/*pow(,n/) ;
sum+=n/*pow(,n/+) ;
}

4,除以总置换数(2*n=n+n/2+n/2):

sum/=(*n);

5,终极代码:

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
#define ll long long
ll ans[];
ll gcd(ll a,ll b) { if(b==) return a; return gcd(b,a%b); }
ll pow(ll a,ll x)
{
ll res=;
while(x){
if(x&) res*=a;
x>>=;
a*=a;
} return res;
}
int main()
{
ll n,i,sum;
for(n=;n<=;n++){
sum=;
for(i=;i<n;i++) sum+=pow(,gcd(n,i));
if(n&) sum+=n*pow(,n/+) ;
else {
sum+=n/*pow(,n/) ;
sum+=n/*pow(,n/+) ;
}
ans[n]=sum/n/;
}
while(~scanf("%lld",&n)){
if(n==-) return ;
printf("%lld\n",ans[n]);
}
return ;
}

二:HDU3923:Invoker :把3改成m即可。

1,注意有乘法的话担心超int还是用long long保险。

2,注意除置换群2*n的时候要逆元。。。比赛的时候就是没有加逆元,导致我牛客第四场排名15。。。不然连续5场前10挺好的。。。

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
const int Mod=;
#define ll long long
int gcd(int a,int b) { if(b==) return a; return gcd(b,a%b); }
int qpow(int a,int x)
{
int res=;a%=Mod;
while(x){
if(x&) res=(ll)res*a%Mod;
x>>=;
a=(ll)a*a%Mod;
} return res;
}
int main()
{
int T,n,m,i,Case=,ans;
scanf("%d",&T); while(T--){
scanf("%d%d",&m,&n);
ans=;
for(i=;i<n;i++) ans=(ans+qpow(m,gcd(n,i)))%Mod;
if(n&) ans=(ans+n*qpow(m,n/+))%Mod;
else {
ans=(ans+n/*qpow(m,n/))%Mod;
ans=(ans+n/*qpow(m,n/+))%Mod;
}
ans=(ll)ans%Mod*qpow(*n,Mod-)%Mod;
printf("Case #%d: %d\n",++Case,ans);
}
return ;
}

----------------------------------------------------分界线----------------------------------------------------

下面的需要加优化了。

三:POJ2154 Color: 求可以旋转,不可以翻转的置换,n个珠子,n种颜色,答案mod K,n<1e9。

那么本题只考虑旋转,则:

for(i=;i<n;i++)  ans+=pow(n,gcd(n,i));
ans/=n;

之前莫比乌斯那里,我们常用技巧是合并。  这里考虑gcd相同的合并。(做多了还是有点灵感滴,yeah)。

得到:

解决这个公式需要:快速幂+欧拉函数+一些素数的常识。

1,快速幂不说了,注意先模运算。

2,欧拉函数,枚举L,即n的素因子,然后根据phi=L*(1-1/p1)*(1-1/p2)...得到欧拉函数。

3,素数常识,在这里指的是一个数n的素因子最多有一个大于根号n,所以一直除,把根号n前的素数都除完了,留下的一定是大于根号n的一个素数。

//可以线筛一部分欧拉函数出来,不够的再枚举素数;枚举的话不超过根号个,所以复杂度不会太高。
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int ans, Mod;
int pri[maxn+],cnt,vis[maxn+],phi[maxn+];
int qpow(int a,int x)
{
int res=;a%=Mod;//这里a一定要除一下,不然会超int,猪啊。
while(x){
if(x&) res=res*a%Mod;
x>>=; a=a*a%Mod;
} return res%Mod;
}
void prime()
{
phi[]=;
for(int i=;i<=maxn;i++){
if(!vis[i]) pri[++cnt]=i,phi[i]=i-;
for(int j=;j<=cnt&&pri[j]*i<=maxn;j++) {
vis[pri[j]*i]=;
if(i%pri[j]==) {
phi[i*pri[j]]=phi[i]*pri[j];
break;
}
else phi[i*pri[j]]=phi[i]*(pri[j]-);
}
}
}
int Phi(int x)
{
if(x<=maxn) return phi[x]%Mod;
int res=x;
for(int i=;pri[i]*pri[i]<=x;i++)
if(x%pri[i]==){
res=(res-res/pri[i]);
while(x%pri[i]==) x/=pri[i];
}
if(x!=) res=(res-res/x);
return res%Mod;
}
int main()
{
int n,T; prime();
scanf("%d",&T);
while(T--){
ans=; scanf("%d%d",&n,&Mod);
for(int i=;i*i<=n;i++){
if(n%i!=) continue;
ans=(ans+qpow(n,i-)*Phi(n/i))%Mod;
if(i*i!=n) ans=(ans+qpow(n,n/i-)*Phi(i))%Mod;
}
printf("%d\n",ans);
} return ;
}

--------------------【优化】-----------

--------------------【here】-------------

这些基本的都可以用欧拉函数来优化,比如上面的第二个例子。

上面的代码是500+ms。

优化后0ms。

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
const int Mod=1e9+;
#define ll long long
ll ans;
ll pri[maxn+],cnt,vis[maxn+],phi[maxn+];
ll qpow(ll a,ll x)
{
ll res=;a%=Mod;//这里a一定要除一下,不然会超int,猪啊。
while(x){
if(x&) res=res*a%Mod;
x>>=; a=a*a%Mod;
} return res%Mod;
}
void prime()
{
phi[]=;
for(int i=;i<=maxn;i++){
if(!vis[i]) pri[++cnt]=i,phi[i]=i-;
for(int j=;j<=cnt&&pri[j]*i<=maxn;j++) {
vis[pri[j]*i]=;
if(i%pri[j]==) {
phi[i*pri[j]]=phi[i]*pri[j];
break;
}
else phi[i*pri[j]]=phi[i]*(pri[j]-);
}
}
}
int main()
{
ll n,m,T,Case=; prime();
scanf("%lld",&T);
while(T--){
ans=; scanf("%lld%lld",&m,&n);
for(ll i=;i*i<=n;i++){
if(n%i!=) continue;
ans=(ans+qpow(m,i)*phi[n/i])%Mod;
if(i*i!=n) ans=(ans+qpow(m,n/i)*phi[i])%Mod;
}
if(n&) ans=(ans+n*qpow(m,n/+))%Mod;
else {
ans=(ans+n/*qpow(m,n/))%Mod;
ans=(ans+n/*qpow(m,n/+))%Mod;
}
ans=ans%Mod*qpow(*n,Mod-)%Mod;
printf("Case #%lld: %lld\n",++Case,ans);
} return ;
}

其他类似的题目还有,HDOJ:2084、2647、1812、3411、2865、2481。POJ:1286、2409、2154、2888。

POJ2154 Color【 polya定理+欧拉函数优化】(三个例题)的更多相关文章

  1. 【poj2154】Color Polya定理+欧拉函数

    题目描述 $T$ 组询问,用 $n$ 种颜色去染 $n$ 个点的环,旋转后相同视为同构.求不同构的环的个数模 $p$ 的结果. $T\le 3500,n\le 10^9,p\le 30000$ . 题 ...

  2. poj2154Color polya定理+欧拉函数优化

    没想到贱贱的数据居然是错的..搞得我调了一中午+晚上一小时(哦不d飞LJH掉RP毕竟他是BUFF)结果重判就对了五次.. 回归正题,这题傻子都看得出是polya定理(如果你不是傻子就看这里),还没有翻 ...

  3. poj2409 & 2154 polya计数+欧拉函数优化

    这两个题都是项链珠子的染色问题 也是polya定理的最基本和最经典的应用之一 题目大意: 用m种颜色染n个珠子构成的项链,问最终形成的等价类有多少种 项链是一个环.通过旋转或者镜像对称都可以得到置换 ...

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

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

  5. POJ2154 Color 【Polya定理 + 欧拉函数】

    题目 Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). ...

  6. poj2154(polya定理+欧拉函数)

    题目链接:http://poj.org/problem?id=2154 题意:n 种颜色的珠子构成一个长为 n 的环,每种颜色珠子个数无限,也不一定要用上所有颜色,旋转可以得到状态只算一种,问有多少种 ...

  7. poj 2154 Color【polya定理+欧拉函数】

    根据polya定理,答案应该是 \[ \frac{1}{n}\sum_{i=1}^{n}n^{gcd(i,n)} \] 但是这个显然不能直接求,因为n是1e9级别的,所以推一波式子: \[ \frac ...

  8. 【POJ2154】Color Pólya定理+欧拉函数

    [POJ2154]Color 题意:求用$n$种颜色染$n$个珠子的项链的方案数.在旋转后相同的方案算作一种.答案对$P$取模. 询问次数$\le 3500$,$n\le 10^9,P\le 3000 ...

  9. Luogu4980 【模板】Polya定理(Polya定理+欧拉函数)

    对于置换0→i,1→i+1……,其中包含0的循环的元素个数显然是n/gcd(i,n),由对称性,循环节个数即为gcd(i,n). 那么要求的即为Σngcd(i,n)/n(i=0~n-1,也即1~n). ...

随机推荐

  1. iOS -- 字符串(NSString *)转uint8_t的两种方法

    // 第一种 NSString *connID = ((Collector *)weakSelf.globalMutableArray[i]).orignalConnID; ] intValue]; ...

  2. python(5)- 简单练习:python三级菜单优化

    python三级菜单优化,菜鸟版链接:http://www.cnblogs.com/xuyaping/p/6648170.html menu = { '北京':{ '海淀':{ '五道口':{ 'so ...

  3. docker--caffe

    Running an official image You can run one of the automatic builds. E.g. for the CPU version: docker ...

  4. Scrapy爬虫入门系列1 安装

    安装python2.7 参见CentOS升级python 2.6到2.7 安装pip 参见CentOS安装python setuptools and pip‎ 依赖 https://docs.scra ...

  5. Mvc之Ajax上传图片

    MyAjaxForm下载地址,点击此处下载 视图部分: @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Lay ...

  6. 【BZOJ1064】[Noi2008]假面舞会 DFS树

    [BZOJ1064][Noi2008]假面舞会 Description 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择 ...

  7. 九度OJ 1141:Financial Management (财务管理) (平均数)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:939 解决:489 题目描述: Larry graduated this year and finally has a job. He's ...

  8. mongodb学习之:安全和认证

    mongodb默认是不认证的,默认没有账号,只要能连接上服务就可以对数据库进行各种操作,mongodb认为安全最好的方法就是在一个可信的环境中运行它,保证之后可信的机器才能访问它.因此需要在登录的时候 ...

  9. spawn类参数command详解

    我们主要来看spawn类它的构造方法参数主要有command,从字面上就是指spawn类的子程序用来执行的子程序,也就是系统所能够执行的相应的命令,对于command这个参数,我们是以字符串的方式给出 ...

  10. Windows10搭建FTP服务器

    配置FTP服务器步骤: 第一步: 打开控制面板--->选择程序--->启动或关闭Windows功能--->勾选FTP服务器等.如下图: 第二步: 右键此电脑--->点击管理-- ...