SP419 TRANSP - Transposing is Fun
:::info[Hint]{open}
注意到矩阵的边长是
显然
| 自 |
|---|
视转置为置换
发掘一下
令
反演一下:
预处理因数、
:::warning[Corner Case]{open}
注意特判
:::success[TRANSP - Transposing is Fun - SP419.cpp]
#include<bits/stdc++.h>
using namespace std;
const int maxv=1e6+10,v=1e6;
const int maxn=14000000;
const int mod=1000003;
int yw[maxv],phi[maxv],prime[maxv>>2],pri;
bool vis[maxv];
int head[maxv],pos[maxn],nxt[maxn],idx;
int qkpow(int a,int p){
int ans=1;
while(p){
if(p&1) ans=1ll*ans*a%mod;
a=1ll*a*a%mod,p>>=1;
}
return ans;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
yw[0]=1;
for(int i=1;i<=v;i++){
for(int j=i;j<=v;j+=i){
pos[++idx]=i,nxt[idx]=head[j],head[j]=idx;
}
}
for(int i=1;i<=v;i++){
yw[i]=(yw[i-1]<<1);
if(yw[i]>=mod) yw[i]-=mod;
}
phi[1]=1;
for(int i=2;i<=v;i++){
if(!vis[i]) prime[++pri]=i,phi[i]=i-1;
for(int j=1;j<=pri;j++){
if(i*prime[j]>v) break;
vis[i*prime[j]]=1;
if(i%prime[j]==0){
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
phi[i*prime[j]]=phi[i]*phi[prime[j]];
}
}
int t;cin>>t;
int a,b,n,tmp,ans;
while(t--){
cin>>a>>b;
if(!a and !b){cout<<"0\n";continue;}
tmp=__gcd(a,b);
n=(a+b)/tmp;
ans=0;
for(int i=head[n];i;i=nxt[i]){
ans+=1ll*yw[pos[i]*tmp]*phi[n/pos[i]]%mod;
if(ans>=mod) ans-=mod;
}
ans=yw[a+b]-1ll*qkpow(n,mod-2)*ans%mod;
if(ans<0) ans+=mod;
cout<<ans<<"\n";
}
return 0;
}
:::