poj_2154
2015-08-26 14:32
295 查看
polya+euler+素数优化=1200ms+
[code]/* ID: CaoLei PROG: poj_2154.cpp LANG: C++ */ #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <set> #include <queue> #include <map> #include <cmath> #include <vector> using namespace std; #define N 1000010 #define pi acos(-1.0) #define inf 100000000 typedef int ll; typedef unsigned long long ull; int prime[35001]; bool is[35001]; void p_table(){ int p=0; for(int i=2;i<=35000;i++){ if(!is[i]){ prime[++p]=i; for(int j=2*i;j<=35000;j+=i){ is[j]=true; } } } } int euler_phi(int n){ int m=(int)sqrt(n+0.5); int ans=n; for(int i=1;prime[i]<=m;i++){ if(n%prime[i]==0){ ans=ans/prime[i]*(prime[i]-1); while(n%prime[i]==0) n/=prime[i]; } } if(n>1) ans=ans/n*(n-1); return ans; } ll pow(ll a,ll m,ll p){ ll ans=1,tmp=a; while(m){ if(m&1){ ans*=tmp; ans%=p; } tmp*=tmp; tmp%=p; m>>=1; } return ans; } ll n,p; ll polya(){ ll ans=0,c=n; for(int i=1;i*i<=n;i++){ if(n%i==0){ ans=(ans+euler_phi(n/i)%p*pow(n%p,i-1,p)%p)%p; if(i != n/i) //枚举另一边 ans = (ans + euler_phi(i)%p * pow(n%p, n/i-1, p)) % p; } } return ans; } int main(){ freopen("in.txt","r",stdin); p_table(); int t; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&p); printf("%d\n",polya()); } return 0; }
相关文章推荐
- 简单的makefile
- Android启动过程深入解析
- tabhost设置动画
- Oracle 常用函数的用法
- java类初始化顺序
- yii2 去掉index.php的方法
- js 备忘
- iOS 截屏方法
- 设计模式-观察者模式
- 【Leetcode】Rotate Image
- 关于linux socket 编程 端口复用的理解
- 有关虚函数的覆盖 和 子类中函数的隐藏
- [leetcode-208]Implement Trie (Prefix Tree)(java)
- Tomcat配置HTTPS
- 关于jsp页面是放在webroot目录下和web-inf下优缺点
- Lucene4.9学习笔记——Lucene建立索引
- Oracle 时间加减操作
- redis 分布式锁
- 自学QT值QVector和qFill的用法
- Entity framework的一些用法