首页 写随笔

cdcq(本博客废弃!现用博客:https://www.cnblogs.com/cdcq/)

本博客废弃!现用博客:https://www.cnblogs.com/cdcq/

导航

【BZOJ2820】ygy的gcd

不知道为什么不想写总结,只是(因为是用别人的权限号交的所以)屯一个代码

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 #define ll long long
 8 const ll inf=(ll)2e9;
 9 ll rd(){ll z=0,mk=1;  char ch=getchar();
10     while(ch<'0'||ch>'9'){if(ch=='-')mk=-1;  ch=getchar();}
11     while(ch>='0'&&ch<='9'){z=(z<<3)+(z<<1)+ch-'0';  ch=getchar();}
12     return z*mk;
13 }
14 void wt(ll x){if(x<0)  putchar('-'),x=-x;
15     int wtp=0;  char wtc[52];
16     while(x)  wtc[++wtp]=(x%10)+'0',x/=10;
17     while(wtp)  putchar(wtc[wtp--]);
18 }
19 int n,m;
20 int miu[11000000],prm[11000000],prp=0;  bool prg[11000000];
21 ll f[11000000],s[11000000];
22 void gtmiu(){
23     memset(prg,0,sizeof(prg));
24     miu[1]=1;
25     for(int i=2;i<=10000000;++i){
26         if(!prg[i])  prm[++prp]=i,miu[i]=-1;
27         for(int j=1;j<=prp && prm[j]*i<=10000000;++j){
28             prg[i*prm[j]]=true;
29             if(!(i%prm[j])){  miu[i*prm[j]]=0;  break;}
30             miu[i*prm[j]]=-miu[i];
31         }
32     }
33     for(int i=1;i<=prp;++i)for(int j=1;j*prm[i]<=10000000;++j)
34         f[j*prm[i]]+=miu[j];
35     for(int i=1;i<=10000000;++i)  s[i]=s[i-1]+f[i];
36 }
37 int main(){
38     //freopen("ddd.in","r",stdin);
39     gtmiu();
40 int T;  cin>>T;  while(T--){
41     n=rd(),m=rd();
42     ll ans=0;
43     if(n>m)  swap(n,m);
44     for(int i=1,j;i<=n;i=j+1){
45         j=min(n/(n/i),m/(m/i));
46         ans+=(s[j]-s[i-1])*(n/i)*(m/i);
47     }
48     wt(ans),putchar('\n');
49     continue;
50 }
51     return 0;
52 }
View Code

 

posted on 2017-04-30 14:51  cdcq_old  阅读(199)  评论(0编辑  收藏  举报