标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2462 Accepted Submission(s): 1032
1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<stdlib.h> 5 #include<string.h> 6 #include<queue> 7 int oula[100005]; 8 bool prime[100005]; 9 int ans[100005]; 10 long long bns[100005]; 11 int id[100005]; 12 using namespace std; 13 typedef long long LL; 14 queue<int>que; 15 int main(void) 16 { 17 int i,j,k; 18 for(i=0; i<100005; i++) 19 oula[i]=i; 20 oula[1]=0; 21 memset(prime,0,sizeof(prime)); 22 for(i=2; i<1000; i++) 23 { 24 if(!prime[i]) 25 { 26 for(j=i; i*j<=100000; j++) 27 { 28 prime[i*j]=true; 29 } 30 } 31 } 32 int cnt=0; 33 for(i=2; i<=100000; i++) 34 if(!prime[i]) 35 ans[cnt++]=i; 36 for(i=0; i<cnt; i++) 37 { 38 for(j=1; j*ans[i]<=100000; j++) 39 { 40 oula[j*ans[i]]/=ans[i]; 41 oula[j*ans[i]]*=(ans[i]-1); 42 } 43 } 44 scanf("%d",&k); 45 int n,m; 46 while(k--) 47 { 48 scanf("%d %d",&n,&m); 49 memset(id,0,sizeof(id)); 50 int xx=max(n,m); 51 int cc=min(n,m); 52 if(cc==1) 53 printf("%d\n",xx); 54 else 55 { 56 long long sum=0; 57 for(i=2; i<=cc; i++) 58 { 59 sum+=oula[i]; 60 } 61 sum*=2; 62 for(i=cc+1; i<=xx; i++) 63 { 64 int vv=i; 65 int t=0; 66 int flag=0; 67 while(vv>1) 68 { 69 if(vv%ans[t]) 70 { 71 t++; 72 flag=0; 73 } 74 else if(vv%ans[t]==0&&flag==0) 75 { 76 que.push(ans[t]); 77 flag=1; 78 vv/=ans[t]; 79 } 80 else if(vv%ans[t]==0&&flag) 81 { 82 vv/=ans[t]; 83 } 84 if(vv==1) 85 break; 86 } 87 int ak=0; 88 while(!que.empty()) 89 { 90 id[ak++]=que.front(); 91 que.pop(); 92 } 93 LL ff=0; 94 int ii; 95 for(ii=1; ii<=(1<<ak)-1; ii++) 96 { 97 int tt=0; 98 LL pp=1; 99 for(j=0; j<ak; j++) 100 { 101 if(ii&(1<<j)) 102 { 103 tt++; 104 pp*=id[j]; 105 } 106 } 107 if(tt%2==0) 108 { 109 ff-=cc/pp; 110 } 111 else ff+=cc/pp; 112 } 113 sum+=cc-ff; 114 115 } 116 sum+=1; 117 printf("%lld\n",sum); 118 } 119 } 120 return 0; 121 }
标签:
原文地址:http://www.cnblogs.com/zzuli2sjy/p/5458965.html