polya定理+欧拉函数优化
来源:互联网 发布:单身比例 知乎 编辑:程序博客网 时间:2024/06/08 01:11
poj-2154
题意:给出两个整数n和p,代表n个珠子,n种颜色,要求不同的项链数,翻转置换不考虑。结果模p.
题解:
我们知道gcd(i,n)表示了循环节的个数。例如gcd(2,6) = 2, 它的具体过程为:[1,3,5] [2,4,6]
对于任意一个循环置换,他所有循环节的长度为 n / gcd(i,n),在上面的例子中: 循环节长度 = 6 / gcd(2,6) = 3
为了方便说明,用L表示循环节的长度,显然 L | n
如果我们枚举L,求出对于每一个L有多少个i, 使得 L = n / gcd (i,n), 那么我们实际上也得到了循环节个数为 n / L 的置换个数。
将L = n / gcd (i,n)转换一下得到:n / L = gcd(i,n )
设 cnt = n / L = gcd(i, n) 注:cnt表示循环节个数,L表示每一个循环节的长度
因为 cnt | i, 所以可令 i = cnt * t; ( 因为0 <= i < n, 所以0 <= t < n / cnt = L )
又因为 cnt = n / L, 所以 n = cnt * L;
则 gcd ( i, n ) = gcd ( cnt*t, cnt*L ) = cnt; ①
可知当 gcd ( t, L ) = 1 时 ① 式成立。
由于 gcd ( t, L ) = 1 的个数就是 Euler(L)的个数,
所以我们可以得到结论:循环节个数为n/L的置换有Euler(L)个。
#include<cmath>#include<cstring>#include<algorithm>#include<cstdio>using namespace std;int euler(int n,int m){ //返回euler(n) int res=n,a=n; for(int i=2;i*i<=a;i++){ if(a%i==0){ res=res/i*(i-1);//先进行除法是为了防止中间数据的溢出 while(a%i==0) a/=i; } } if(a>1) res=res/a*(a-1); return res%m;}int mod_exp ( int a, int b, int m )//a^b %m{ int ret = 1; a = a % m; while ( b >= 1 ) { if ( b & 1 ) ret = ret * a % m; a = a * a % m; b >>= 1; } return ret;}int Polya ( int n, int m ){ int ret = 0; for ( int l = 1; l*l <= n; l++ ) { if ( n % l ) continue; ret = (ret + euler(l,m) * mod_exp(n, n/l-1, m)) % m; if ( l * l == n ) break; ret = (ret + euler(n/l,m) * mod_exp(n, l-1, m)) % m; } return ret;}int main(){ int n, m, cs; scanf("%d",&cs); while ( cs-- ) { scanf("%d%d",&n,&m); printf("%d\n",Polya(n,m)); } return 0;}
0 0
- polya定理+欧拉函数优化
- POJ 2154 Color Polya定理+欧拉函数优化 -
- poj 2154 Color Polya定理 欧拉函数优化
- POJ 2154 Color Polya定理+欧拉函数优化
- poj2154 polya定理+欧拉函数
- 【POJ2154】Color-Polya定理+欧拉函数
- POJ 2154 Color Polya(Polya定理+欧拉函数)
- hdu 5868 Different Circle Permutation Polya定理 欧拉函数优化
- POJ 2154 Color Polya定理+欧拉函数
- POJ 2154 Color (Polya定理+欧拉函数)
- POJ2154 Color【Polya定理】【欧拉函数】【整数快速幂】
- POJ 2154 Color (Polya定理&欧拉函数)
- Burnside引理、Polya定理和欧拉函数
- POJ 2154 Color(polya定理+欧拉函数)
- POJ 2154 color(欧拉函数,Polya定理)
- Poj 2154 Color (Polya计数 欧拉函数优化)
- poj 2154 Color(polya计数 + 欧拉函数优化)
- POJ2154 Polya+欧拉函数
- 磁盘IO满负荷性能分析
- iOS动画进阶(一)图层树
- Android Intent Action
- 使用jquery实现js跨域
- ffmpeg+OpenCV Linux下安装和配置
- polya定理+欧拉函数优化
- poj-1751 Highways
- LeetCode Insertion Sort List
- ZOJ Problem Set - 2095 Divisor Summation
- Java面向对象
- 【004】虚方法
- 获取当前正在运行的App的包名
- 禁止浏览器缓存方案
- eclipse如何修改dynamic web module version