记录编号 | 220423 | 评测结果 | AAAAAA | ||
---|---|---|---|---|---|
题目名称 | [SPOJ 422] 转置更好玩 | 最终得分 | 100 | ||
用户昵称 | 是否通过 | 通过 | |||
代码语言 | C++ | 运行时间 | 7.497 s | ||
提交时间 | 2016-01-18 15:59:07 | 内存使用 | 0.27 MiB | ||
#include<cstdio> #include<cstring> #include<iostream> using namespace std; const int SIZEN=60,SIZEM=1010; typedef long long LL; const LL MOD=1000003; int cnt=0,P[SIZEM]={0}; void prime() { bool co[SIZEM]={0}; for(int i=2;i<SIZEM;i++) { if(!co[i]) { P[cnt++]=i; for(int j=i*i;j<SIZEM;j+=i) co[j]=1; } } } int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); } LL quickpow(LL x,int y) { LL re=1; while(y>0) { if(y&1) re*=x;x*=x;y>>=1; re%=MOD;x%=MOD; } return re; } int facts[SIZEN],factr[SIZEN],facti[SIZEN];//值,底数,次数 int fn=0; void getfact(int a)//对a进行质因数分解 { fn=0; for(int i=0;i<cnt;i++) { if(a%P[i]==0) { fn++; facts[fn]=1; factr[fn]=P[i]; facti[fn]=0; while(a%P[i]==0) { a/=P[i]; facti[fn]++; facts[fn]*=P[i]; } } } if(a>1) { fn++; facts[fn]=a; factr[fn]=a; facti[fn]=1; } } void dfs(LL &ans,int g,int phi,int dep) { if(dep==fn+1) { //cout<<phi<<" "<<g<<endl; ans+=(LL)phi*quickpow(2,g); ans%=MOD; return; } int i=0,p=facts[dep]; while(i<=facti[dep]) { //facts/p是factr的i次方 //cout<<"i:"<<i<<endl; dfs(ans,g*facts[dep]/p,phi*(p-p/factr[dep]),dep+1); i++;p/=factr[dep]; } } void exgcd(int a,int b,LL &x,LL &y) { if(b==0) {x=1,y=0;} else { exgcd(b,a%b,y,x); y-=a/b*x; } } int calc(int a,int b) { LL ans=0,K=0; if(!a||!b) return 0; LL g=gcd(b,a+b); getfact((a+b)/g); dfs(K,g,1,1); LL x=0,y=0; x=quickpow((a+b)/g,MOD-2); x%=MOD; ans=quickpow(2,a+b)-(K*x)%MOD; ans=(ans+MOD)%MOD; return ans; } void read() { int a,b; scanf("%d%d",&a,&b); printf("%d\n",calc(a,b)); } int main() { freopen("transp2.in","r",stdin); freopen("transp2.out","w",stdout); int T; prime(); scanf("%d",&T); while(T>0) { T--; read(); } return 0; }