设d(x)为x的约数个数,给定N、M,求
标签:desc problem oid com str prepare class ons discuss
输入文件包含多组测试数据。
T行,每行一个整数,表示你所求的答案。
1<=N, M<=50000
#include<cstdio> #include<iostream> using namespace std; typedef long long ll; const int F=1005; const int N=5e4+5; int n,m,T,tot,prime[N/3],mu[N],c[N];bool check[N]; ll ANS[F][F],sum[N],f[N]; inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } void prepare(int n){ mu[1]=1;f[1]=1;c[1]=1; for(int i=2;i<=n;i++){ if(!check[i]){ prime[++tot]=i; f[i]=2; c[i]=1; mu[i]=-1; } for(int j=1;j<=tot&&i*prime[j]<=n;j++){ check[i*prime[j]]=1; if(!(i%prime[j])){ f[i*prime[j]]=f[i]/(c[i]+1)*(c[i]+2); c[i*prime[j]]=c[i]+1; mu[i*prime[j]]=0; break; } else{ f[i*prime[j]]=f[i]*f[prime[j]]; c[i*prime[j]]=1; mu[i*prime[j]]=-mu[i]; } } } for(int i=1;i<=n;i++) sum[i]=sum[i-1]+mu[i]; for(int i=1;i<=n;i++) f[i]+=f[i-1]; } void solve(){ if(n<=1000&&m<=1000) if(ANS[n][m]){ printf("%lld\n",ANS[n][m]); return ; } ll ans(0); for(int i=1,pos;i<=n;i=pos+1){ pos=min(n/(n/i),m/(m/i)); ans+=(sum[pos]-sum[i-1])*f[n/i]*f[m/i]; } if(n<=1000&&m<=1000) ANS[n][m]=ANS[m][n]=ans; printf("%lld\n",ans); } int main(){ prepare(50000); T=read(); while(T--){ n=read();m=read(); if(n>m) swap(n,m); solve(); } return 0; }
标签:desc problem oid com str prepare class ons discuss
原文地址:http://www.cnblogs.com/shenben/p/6606539.html