POJ 2154 Color (Polya + 欧拉函数)
来源:互联网 发布:js 字符串转换json数组 编辑:程序博客网 时间:2024/06/07 17:13
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.
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
题意
有 N
种颜色的珠子要组成长度为 N
的项链,考虑旋转相同的情况算一种,求总共有多少种情况 mod P 。
思路
从题意来看是一道 Polya
计数问题,于是我们想到公式
因为只需要考虑旋转置换,于是
再看
于是分析
显然
然后公式便被我们转化为了:
素数表的辅助可以更快的求解欧拉函数的值,然后我们在枚举
AC 代码
#include<cstdio>#include<cstdlib>#include<cstring>#include<stdlib.h>#include<iostream>#include<queue>#include<vector>#include<map>#include<cmath>#include<algorithm>using namespace std;typedef __int64 LL;const int maxn = 40000;int N,P;int pr[maxn];bool prime[maxn];void getprime() //筛法素数表{ int i,j,k=0; memset(prime,true,sizeof(prime)); for(i=2; i<maxn; i++) { if(prime[i]) { pr[k++]=i; for(j=i+i; j<maxn; j+=i) prime[j]=false; } }}int phi(int n) //求欧拉函数{ int rea=n; for(int i=0; pr[i]*pr[i]<=n; i++) { if(n%pr[i]==0) { rea=rea-rea/pr[i]; while(n%pr[i]==0) n/=pr[i]; } } if(n>1) rea=rea-rea/n; return rea;}int mult(int a,int n) //快速幂取模{ if(n<0)return 0; int ans=1; while(n) { if(n&1)ans=(ans*a)%P; a=(a*a)%P; n>>=1; } return ans;}int main(){ ios::sync_with_stdio(false); int T; getprime(); cin>>T; while(T--) { cin>>N>>P; int ans=0; for(int i=1; i*i<=N; i++) { if(N%i==0) { int no=N/i; int oula=phi(no)%P; int foula=0; if(i*i!=N) foula=phi(i)%P; ans=(ans+mult(N%P,i-1)*oula%P+mult(N%P,no-1)*foula%P)%P; } } cout<<ans<<endl; } return 0;}
- POJ 2154 Color Polya(Polya定理+欧拉函数)
- poj 2154 Color(欧拉函数+Polya)
- POJ 2154 Color (polya 欧拉函数)
- poj 2154 Color(polya + 欧拉函数)
- POJ 2154 Color (Polya定理+欧拉函数)
- poj 2154 Color(polya计数 + 欧拉函数优化)
- POJ 2154 Color (Polya + 欧拉函数)
- POJ 2154 Color(polya定理+欧拉函数)
- POJ 2154 color(欧拉函数,Polya定理)
- POJ 2154 Color Polya定理+欧拉函数
- Poj 2154 Color (Polya计数 欧拉函数优化)
- poj 2154 Color (polya,欧拉函数|容斥原理)
- POJ 2154 Color(Polya原理+欧拉函数)
- POJ 2154 Color (Polya定理&欧拉函数)
- POJ 2154 Color Polya计数法+欧拉函数
- POJ 2154 Color Polya定理+欧拉函数优化 -
- poj 2154 Color Polya定理 欧拉函数优化
- POJ 2154 Color Polya定理+欧拉函数优化
- activeMQ消息类型
- 素数环(搜索)
- 实现一次将多个文件上传到hdfs
- Google VR开发-Cardboard VR SDK生命周期设计
- switch语句(JS)
- POJ 2154 Color (Polya + 欧拉函数)
- ansible
- Spring-framework 源码Intellij IDEA 导入工程
- 视频质量分析系统之音视频测试工具
- ASP.NET Core之跨平台的实时性能监控(2.健康检查)
- cocos2d-x向网页请求数据(C++)
- Laravel数据库操作的三种方式
- httpURLConnection-网络请求的两种方式-get请求和post请求
- 装饰模式