POJ 2154 Color(组合数学-波利亚计数,数论-欧拉函数,整数快速幂)
来源:互联网 发布:证书查询系统源码php 编辑:程序博客网 时间:2024/06/08 19:12
Color
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 7693 Accepted: 2522
Description
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.
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
51 300002 300003 300004 300005 30000
Sample Output
131170629
Source
POJ Monthly,Lou Tiancheng
题目大意:
T组测试数据,每组一个n表示1个项链有n个颜色可以涂在n个钻石上,通过旋转相同的算一种方案,问你方案数是多少?
解题思路:
解题代码:很裸的波利亚计数,转化为的公式就是 ans=sum{ n^( gcd(1,n)-1 ) ,n^( gcd(2,n)-1 ),n^( gcd(3,n)-1 ) .....n^( gcd(n,n)-1 ) },因为这个n比较大10^9,所以暴力超时。
因此枚举 gcd(k,n)=l 的有多少个,也就是 k=l*x ,n=l*y,也就是gcd(x,y)=1,也就是找到 1~y与y互质的数有多少个,答案:欧拉函数
#include <iostream>#include <cstdio>using namespace std;typedef long long ll;int n,p;inline ll getPhi(int x){ ll ans=x; for(int i=2;i*i<=x;i++){ if(x%i==0){ ans=ans/i*(i-1); while(x%i==0) x/=i; } } if(x>1) ans=ans/x*(x-1); return ans;}inline ll pow_mod(ll a,ll b){ ll sum=1; while(b>0){ if(b&1) sum=(sum*a)%p; a=(a*a)%p; b/=2; } return sum%p;}int main(){ int t; scanf("%d",&t); while(t-- >0){ scanf("%d%d",&n,&p); ll ans=0; for(int i=1;i*i<=n;i++){ if(n%i==0){ if(i*i==n) ans=(ans+(getPhi(i)*pow_mod(n,i-1))%p)%p; else{ ans=(ans+(getPhi(n/i)*pow_mod(n,i-1))%p)%p; ans=(ans+(getPhi(i)*pow_mod(n,n/i-1))%p)%p; } } } printf("%lld\n",ans%p); } return 0;}
1 0
- POJ 2154 Color(组合数学-波利亚计数,数论-欧拉函数,整数快速幂)
- poj 2154 Color(欧拉函数,快速幂,波利亚计数)
- POJ - 2154 Color(波利亚计数)(欧拉函数)
- poj 2154 Color(polya计数 + 欧拉函数优化)
- HDU 1695 GCD (数论-整数和素数,欧拉函数,组合数学-容斥原理)
- Poj 2154 Color (Polya计数 欧拉函数优化)
- poj 2154 Color 欧拉函数优化的ploya计数
- POJ 2154 Color Polya计数法+欧拉函数
- POJ2154 Color【Polya定理】【欧拉函数】【整数快速幂】
- poj 2154 Color 波利亚定理 欧拉函数 快速幂 线性筛
- [ACM] POJ 2154 Color (Polya计数优化,欧拉函数)
- HDU 3240 Counting Binary Trees(组合数学-卡特兰数,数论-整数快速幂,数论-求逆元)
- poj 2154 Color polya计数+欧拉优化
- poj 2154 Color(欧拉函数+Polya)
- POJ 2154 Color (ploya欧拉函数)
- POJ 2154 Color (polya 欧拉函数)
- poj 2154 Color(polya + 欧拉函数)
- POJ 2154 Color (Polya定理+欧拉函数)
- Spring3.2重学------各个Jar包的作用。
- hdu1598--结题报告
- 【算法小总结】拓扑排序+例题解析
- 推荐一个优秀的国产源代码托管服务
- Eclipse设置:背景与字体大小和xml文件中字体大小调整
- POJ 2154 Color(组合数学-波利亚计数,数论-欧拉函数,整数快速幂)
- STM32F10x.s 启动文件
- poj2528(线段树+离散化)
- Eclipse 快捷键使用
- 哈夫曼编码树的经典题目
- iOS —— 内存泄露问题
- boost::random产生随机数的一种方法
- 信用卡3D验证相关资料
- 添加ftp用户及对应目录