Link:http://acm.hdu.edu.cn/showproblem.php?pid=5317
RGCDQ
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1070 Accepted Submission(s): 484
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
Source
AC code:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<map>
#include<vector>
#define LL long long
#define MAXN 1000010
using namespace std;
int n,k,i,j,mm;
int f[1000010];
LL ans=0;
int gcd(int a,int b)
{
if(a<b)
swap(a,b);
if(b==0)
return a;
return gcd(b,a%b);
}
int cnt[8];
int s[1000001][8];
void init()
{
memset(f,0,sizeof(f));
memset(s,0,sizeof(s));
for(int i=2;i<=1000000;i++) // 确保i为素数,i必须从2开始,不能从1开始!!!
{
if(f[i])//筛除合数 ,确保i为素数
continue;
f[i]=1;
for(j=2;j*i<=1000000;j++)//j*i为以i为质因子的数
{
f[j*i]++;
}
}
// s[2][1]=1;
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=1000000;i++)
{
cnt[f[i]]++;
for(j=1;j<=7;j++)
{
s[i][j]=cnt[j];
// s[i][j]=s[i-1][j];
}
//s[i][f[i]]++;
}
}
int main()
{
init();
//freopen("D:\in.txt","r",stdin);
int T,st,ed,ans,i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&st,&ed);
int c[8];
memset(c,0,sizeof(c));
ans=0;
int num=0;
int a[22];//存储区间各个质因子的个数的值
for(i=1;i<=7;i++)
{
c[i]=s[ed][i]-s[st][i];
if(i==f[st])
c[i]++;
if(c[i]>=2)
{
a[++num]=i;
a[++num]=i;
}
else if(c[i]==1)
{
a[++num]=i;
}
}
for(i=1;i<=num-1;i++)
{
for(j=i+1;j<=num;j++)
{
ans=max(ans,gcd(a[i],a[j]));
}
}
printf("%d\n",ans);
}
return 0;
}