UPC:2526 Color the necklace
来源:互联网 发布:结婚礼物 知乎 编辑:程序博客网 时间:2024/06/06 12:36
http://acm.upc.edu.cn/problem.php?id=2526
这个题是http://blog.csdn.net/kkkwjx/article/details/21525325的加强版,也是Polya定理非常经典的题目。
由于最后要除以2和4,在模运算里面直接除是不行的,要乘上2或4模MOD的乘法逆元。
用到了Polya定理,求欧拉函数,求逆元,快速幂模
#include<iostream>#include <cstdio>#include <cstring>#include <cmath>#define ll long long#define MOD 20090531using namespace std;void gcd(ll a,ll b,ll &d,ll &x,ll &y){ if(!b) { d=a; x=1; y=0; } else { gcd(b,a%b,d,y,x); y-=x*(a/b); }}ll inv(ll a,ll n){ ll d,x,y; gcd(a,n,d,x,y); return d==1?(x+n)%n:-1;}ll pow_mod(ll a,ll b){ ll res=1; while(b) { if(b&1)res=res*a%MOD; b=b>>1; a=a*a%MOD; } return res;}int euler_phi(int n){ int m=sqrt(n+0.5); int ans=n; for(int i=2; i<=m; ++i) if(n%i==0) { ans=ans/i*(i-1); while(n%i==0) n=n/i; } if(n>1) ans=ans/n*(n-1); return ans;}ll mod(ll val){ return (val%MOD+MOD)%MOD;}int main(){ int T; scanf("%d",&T); ll n2=inv(2,MOD),n4=inv(4,MOD); while(T--) { int n; scanf("%d",&n); int s=sqrt(n+0.5); ll ans=0; for(int i=1; i<=s; ++i) if(n%i==0) { if(n/i!=i) { ans+=mod(euler_phi(n/i)*pow_mod(n,i-1)); ans+=mod(euler_phi(i)*pow_mod(n,n/i-1)); ans=mod(ans); } else ans+=mod(euler_phi(i)*pow_mod(n,n/i-1)); } ans=mod(ans*n2); ll res=0; if(n%2) { res=mod(pow_mod(n,(n+1)/2)); res=mod(res*n2); } else { res=mod(mod(pow_mod(n,n/2))+mod(pow_mod(n,n/2+1))); res=mod(res*n4); } printf("%lld\n",mod(ans+res)); } return 0;}
0 0
- UPC:2526 Color the necklace
- The Necklace
- 2217: Rescue The Princess(UPC)
- nyoj-Color the necklace(Ploya定理 + 欧拉函数 + 扩展欧几里得(求逆元))
- nyoj-Color the necklace(Ploya定理 + 欧拉函数 + 扩展欧几里得(求逆元))
- The number of steps - UPC 2225 dp
- uva 10054 - The Necklace
- 10054 - The Necklace(***)
- UVaOJ 10054 - The Necklace
- uva 10054 - The Necklace
- UVa 10054 - The Necklace
- 10054 - The Necklace
- 10054 - The Necklace
- uva10054-The Necklace(项链)
- The Necklace UVA10054
- UVA 10054 The Necklace
- Uva-10054-The Necklace
- UVaOJ10054---The Necklace
- Fedora9安装VMwaretools鼠标定位不准的解决方法
- gvim----中文显示乱码
- 清除UITableView底部多余的分割线
- 基于C++和JavaScript的全平台全栈式游戏开发解决方案的思考
- 得到数组的最后一个数的趣味实现
- UPC:2526 Color the necklace
- java项目组月度考核表(KPI)
- mysql查询字段为null的方法
- Apache Tajo™ - An open source big data warehouse system in Hadoop
- java编码终极探秘
- 【PyQt实例6】渐变效果
- iOS 沙盒 理解 文件系统
- AS3.0 Bitmap类实现图片3D旋转效果
- 用cmd来运行java