标签:
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 323 Accepted Submission(s): 162
1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 5 int a[1000050],b[1000000],k,f[1000050],s[1000050][10]; 6 7 int Sieve(int n) 8 { 9 a[1]=0;k=0;a[0]=0; 10 for (int i = 2; i <= n; i++) 11 a[i] =1; 12 for (int i = 2; i <= sqrt(n); i++) 13 { 14 if (a[i]) 15 for (int j = i; j*i <=n; j++) 16 a[j * i] = 0; 17 } 18 for (int i = 0; i <= n; i++) 19 { 20 if (a[i]==1) 21 { 22 k++; 23 b[k]=i; 24 } 25 } 26 } 27 28 int gcd(int a,int b) 29 { 30 if(a<b) 31 return gcd(b,a); 32 else if(b==0) 33 return a; 34 else 35 return gcd(b,a%b); 36 } 37 38 int main() 39 { 40 int T; 41 int i,j,k; 42 Sieve(1000000); 43 memset(f,0,sizeof(f)); 44 for(i=2;i<=1000000;i++) 45 { 46 int x=i; 47 k=1; 48 while(1) 49 { 50 if(x==1) 51 { 52 break; 53 } 54 if(a[x]==1) 55 { 56 f[i]++; 57 break; 58 } 59 60 if(x%b[k]==0) 61 { 62 f[i]++; 63 while(x%b[k]==0) 64 { 65 x=x/b[k]; 66 } 67 } 68 k++; 69 } 70 //printf("%d ",f[i]); 71 } 72 memset(s,0,sizeof(s)); 73 for(i=1;i<=1000000;i++) 74 { 75 for(j=1;j<=7;j++) 76 s[i][j]=s[i-1][j]; 77 s[i][f[i]]++; 78 } 79 scanf("%d",&T); 80 int l,r; 81 int num[15]; 82 while(T--) 83 { 84 memset(num,0,sizeof(num)); 85 scanf("%d %d",&l,&r); 86 for(i=1;i<=8;i++) 87 num[i]=s[r][i]-s[l-1][i]; 88 int ma=0; 89 for(i=10;i>=1;i--) 90 { 91 if(num[i]>0) 92 { 93 if(num[i]>=2) 94 { 95 if(i>ma) 96 ma=i; 97 } 98 else 99 { 100 for(j=i-1;j>=1;j--) 101 { 102 if(num[j]>0) 103 if(gcd(i,j)>ma) 104 ma=gcd(i,j); 105 } 106 } 107 108 } 109 } 110 printf("%d\n",ma); 111 } 112 return 0; 113 }
标签:
原文地址:http://www.cnblogs.com/cyd308/p/4684204.html