bzoj2880
打公式好麻烦 QAQ
为了节省时间去复习,原谅我引用一下别人的博客。。。http://blog.csdn.net/acdreamers/article/details/8542292
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<string> 5 #include<cstring> 6 #define re(i,l,r) for(int i=(l);i<=(r);i++) 7 #define rre(i,r,l) for(int i=(r);i>=(l);i--) 8 using namespace std; 9 typedef long long LL; 10 int prime[10000010],prime_tot,mu[10000010],g[10000010],sum[10000010]; 11 bool bo[10000010]; 12 LL solve(int n,int m) 13 { 14 LL ret=0; 15 if(n>m)swap(n,m); 16 int i=1,xia; 17 while(i<=n) 18 { 19 xia=min(n/(n/i),m/(m/i)); 20 ret+=1LL*(n/i)*(m/i)*(sum[xia]-sum[i-1]); 21 i=xia+1; 22 } 23 return ret; 24 } 25 int main() 26 { 27 mu[1]=1; 28 re(i,2,10000000) 29 { 30 if(!bo[i])prime[++prime_tot]=i,mu[i]=-1,g[i]=1; 31 for(int j=1;j<=prime_tot&&i*prime[j]<=10000000;j++) 32 { 33 bo[i*prime[j]]=1; 34 if(i%prime[j]==0){mu[i*prime[j]]=0,g[i*prime[j]]=mu[i];break;} 35 else mu[i*prime[j]]=-mu[i],g[i*prime[j]]=mu[i]-g[i]; 36 } 37 } 38 re(i,1,10000000)sum[i]=sum[i-1]+g[i]; 39 int t; 40 scanf("%d",&t); 41 while(t--) 42 { 43 int n,m;scanf("%d%d",&n,&m); 44 printf("%lld\n",solve(n,m)); 45 } 46 return 0; 47 }