RGCDQ
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1593 Accepted Submission(s): 674
Problem Description
Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and more interesting things about GCD. Today He comes up with Range Greatest Common Divisor Query (RGCDQ). What’s RGCDQ? Please let me explain it to you gradually. For a positive integer x, F(x) indicates the number of kind of prime factor of x. For example F(2)=1. F(10)=2, because 10=2*5. F(12)=2, because 12=2*2*3, there are two kinds of prime factor. For each query, we will get an interval [L, R], Hdu wants to know
maxGCD(F(i),F(j))
(L≤i<j≤R)
Input
There are multiple queries. In the first line of the input file there is an integer T indicates the number of queries.
In the next T lines, each line contains L, R which is mentioned above.
All input items are integers.
1<= T <= 1000000
2<=L < R<=1000000
In the next T lines, each line contains L, R which is mentioned above.
All input items are integers.
1<= T <= 1000000
2<=L < R<=1000000
Output
For each query,output the answer in a single line.
See the sample for more details.
See the sample for more details.
Sample Input
2 2 3 3 5
Sample Output
1 1
Author
ZSTU
Source
查询区间[l,r]最大的GCD=(F[i],F[j]),F[i]=不同质因子个数
注意到不同质因子个数在100w范围内最多就8个,即2*3*5*7*11*13*17*19>100w
如此暴力就好了。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define eps 1e-8
#define LL long long
#define db double
#define maxn 1000001
int f[maxn+100];
int sum[maxn][10];
int d[10][10];
bool flag[maxn+100];
int a[10];
int gcd(int x,int y)
{
if(y==0) return x;
else
return gcd(y,x%y);
}
void init()
{
memset(flag,true,sizeof flag);
memset(sum,0,sizeof sum);
flag[1] = false;
for(int i =2;i<=maxn;i++)
if(flag[i])
{
//cout<<i<<endl;
int j = i;
while(j<=maxn){flag[j] = false;f[j]++;j+=i;}
}
// for(int i = 1;i<=10;i++)
// cout<<i<<":"<<f[i]<<endl;
for(int i =1;i<=maxn;i++)
{
for(int j = 1;j<=8;j++)
sum[i][j]=sum[i-1][j];
sum[i][f[i]]++;
//cout<<i<<":"<<endl;
// for(int j=1;j<=8;j++)
// cout<<sum[i][j]<<" ";
// cout<<endl;
}
for(int i =1;i<=8;i++)
{
for(int j = 1;j<=8;j++)
{
d[i][j] = gcd(i,j);
// cout<<d[i][j]<<" ";
}
// cout<<endl;
}
}
int main(){
init();
int T;
scanf("%d",&T);
while(T--)
{
int x,y;
scanf("%d%d",&x,&y);
//cout<<x<<y<<endl;
for(int i = 1;i<=8;i++) a[i] = sum[y][i] - sum[x-1][i];
int ans = 1;
for(int i = 1;i<=8;i++)
if(a[i]>1) ans = max(ans,i);
else if(a[i] == 1)
for(int j = 1;j<i;j++)
if(a[j] == 1)
ans = max(ans,d[i][j]);
printf("%d\n",ans);
}
return 0;
}