POJ2154 Polya+欧拉函数
来源:互联网 发布:软件开发学费多少 编辑:程序博客网 时间:2024/05/18 03:01
首先声明一下,这题不是我自己做的。
可以说基本是拿了别人的东西,之所以写这篇文章一是为了理清自己的思路,二是将自己遇到的问题和大家分享。
虽说是第一个Polya的题目,还是感觉自己真的好水!!
参考:
http://blog.csdn.net/acdreamers/article/details/8656247
http://blog.csdn.net/wsniyufang/article/details/6671122
思路:
1、根据Polya原理:
其中:
L为染色方案计数,
Q为置换群,
q为置换群中的置换,
m为可染的颜色数,指数(rang ta(q),符号不会打,只能将就)为置换q的变换下的循环数。
2、推论:在n个格子中,顺时针旋转 i 个格子,循环个数为gcd(n,i),每个循环长度为n/gcd(n,i)。
也就是说,根据这条推论可以得到:L= 1/n * sum( m^gcd(n,i) );
但是,直接这样求得复杂度为O(n),而n<=1e9,是不可行的!!
3、优化(不是自己做的):
剩下的就是代码:
//2154_Color #include<cstdio>#include<cmath>#define N 1000000010<span style="color:#ff0000;">//必须为int ,否则超时!</span> typedef int LL;LL ans;int phi[36000]; LL pow(LL x,LL y,LL m){//x^y%mLL r1=1;<span style="color:#ff0000;">//必须取模,否则WA ! </span>x%=m;while(y){if(y&1)r1=r1*x%m;x=(x*x)%m;y=y>>1;}return r1;}void getPhi(){int n=sqrt(N);for(int i=2;i<=n;i++){if(!phi[i]){for(int j=i*i;j<=n;j+=i){phi[j]=1;}}}for(int i=2,j=0;i<=n;i++){if(!phi[i]){phi[j++]=i;}}}int Euler(int x){int r=x;for(int i=0;phi[i]*phi[i]<=x;i++){if(x%phi[i]==0) {r=r-r/phi[i];while(x%phi[i]==0) x/=phi[i];}}//!!if(x>1) r=r-r/x;return r;}int main(){int t;LL p,n;scanf("%d",&t);getPhi();while(t--){ans=0;scanf("%d%d",&n,&p);//solve()int i;for(i=1;i*i<n;i++){if(n%i==0){<span style="color:#ff0000;">//!!注意:如果i是n的约数,则n/i也是n的约数,也要做同样的处理! </span>ans=( ans+ (Euler(n/i)%p) * pow(n, i-1,p) ) %p ;ans=( ans+ (Euler(i) %p) * pow(n,n/i-1,p) ) %p ;}}if(i*i==n)ans=( ans+ (Euler(i)%p) * pow(n,i-1,p) ) %p ;printf("%d\n",ans);}return 0;}
0 0
- POJ2154 Polya+欧拉函数
- poj2154 polya定理+欧拉函数
- 【POJ2154】Color-Polya定理+欧拉函数
- POJ2154 Color【Polya定理】【欧拉函数】【整数快速幂】
- POJ2154——Color(Polya定理+筛素数+欧拉函数)
- poj2154质因数分解+快速幂+欧拉函数解决
- POJ2154(Pólya定理与欧拉函数优化)
- POJ2154(Pólya定理与欧拉函数优化)
- poj2154 polya theory
- POJ 2154 Color Polya(Polya定理+欧拉函数)
- poj 2154 Color(欧拉函数+Polya)
- polya定理+欧拉函数优化
- POJ 2154 Color (polya 欧拉函数)
- poj 2154 Color(polya + 欧拉函数)
- POJ2154 以欧拉函数优化的BurnSide定理(有部分详细证明)
- POJ 2154 Color Polya定理+欧拉函数
- POJ 2154 Color (Polya定理+欧拉函数)
- Poj 2154 Color (Polya计数 欧拉函数优化)
- scrollTo与scrollBy
- android自定义View实现图片上传进度显示(仿手机QQ上传效果)
- solr入门之solr安全控制的研究和实践(一)
- c++第四次作业
- Oracle--sqlplus常用命令,持续更新...
- POJ2154 Polya+欧拉函数
- Word无法打开该文件,因为文件格式与文件扩展名不匹配的解决办法
- LeeCode-Sort Colors
- 对正则表达式的一些汇总
- DOS BAT中用echo显示一些保留字符
- Android-OptionMenu选项菜单
- LeeCode-Remove Element
- ØMQ -入门教程(序)
- log4j.properties 从jar中抽身