传送门:HDU 5317
RGCDQ
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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
题意:定义F(i)为i的质因子的个数(去重),给定一个L,R。求gcd(f(i),f(j))的最大值,要求2<=L<=i<j<=R<=100w.测试组数小于100w
思路:测试组数太多了,必须要O(1)得出每一组测试。先预处理出1到100w的每个数的f(i)。由于i最大只有100w,所以f(i)的最大值只有7。用sum[i][j]记录从0~j,f[j]==i的数的个数。查询的时候,直接枚举f(i)和f(j)的值即可。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 1000005
int prime[10000];
int f[maxn];
void getprime()
{
bool isprime[10005];
memset(isprime,1,sizeof(isprime));
for(int i=2; i*i<=10000; i++)
if(isprime[i])
for(int j=i*i; j<=10000; j+=i) isprime[j]=0;
prime[0]=0;
for(int i=2; i<=10000; i++) if(isprime[i]) prime[++prime[0]]=i;
}
inline int get_num_prime(int x)
{
int cnt=0;
for(int i=1; prime[i]*prime[i]<=x; i++)
{
if(x%prime[i]==0)
{
cnt++;
while(x%prime[i]==0) x/=prime[i];
}
}
if(x>1) cnt++;
return cnt;
}
using namespace std;
int sum[8][maxn];
int gcd(int a,int b)
{
if(b==0) return a;
return gcd(b,a%b);
}
int main()
{
getprime();
for(int i=2; i<maxn; i++)
{
sum[get_num_prime(i)][i]++;
}
for(int j=0; j<8; j++)
for(int i=0; i<maxn; i++)
sum[j][i]+=sum[j][i-1];
int T;
scanf("%d",&T);
while(T--)
{
int L,R;
scanf("%d %d",&L,&R);
int have[8];
memset(have,0,sizeof(have));
for(int i=1; i<8; i++) have[i]=sum[i][R]-sum[i][L-1];
int ans=0;
for(int i=1; i<8; i++)
if(have[i]>=2) ans=i;
else if(have[i])
for(int j=i+1; j<8; j++)
if(have[j])
ans=max(ans,gcd(j,i));
printf("%d\n",ans);
}
return 0;
}