RGCDQ
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1963 Accepted Submission(s): 830
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
Recommend
统计f[i]出现次数即可
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (1000000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int P[MAXN],siz=0,b[MAXN]={0};
int gcd(int a,int b){if (b) return gcd(b,a%b);return a;}
void make_prime(int n)
{
Fork(i,2,n)
{
if (!b[i])
{
P[++siz]=i;
}
For(j,siz)
{
if (P[j]*i>n) break;
b[P[j]*i]=b[i];
if (gcd(P[j],i)==1) b[P[j]*i]++;
if (i%P[j]==0) break;
}
}
}
int t[8],cnt[MAXN][8]={0};
const int n=1000000,mm=7;
int main()
{
// freopen("B.in","r",stdin);
make_prime(n);
For(i,n)
{
For(j,7)
cnt[i][j]=cnt[i-1][j]+(bool)(b[i]+1==j);
}
// For(i,30) cout<<i<<' '<<b[i]+1<<endl;
int T;cin>>T;
while(T--)
{
int l,r;
scanf("%d%d",&l,&r);
For(i,7) t[i]=cnt[r][i]-cnt[l-1][i];
int ans=0;
For(i,7)
For(j,7)
{
if (t[i]&&t[j]&&((i!=j)||(t[i]>1))) ans=max(ans,gcd(i,j));
}
printf("%d\n",ans);
}
return 0;
}
本文详细介绍了RGCDQ算法的基本概念、输入输出规范及具体实现步骤,通过实例展示了如何利用该算法解决区间最大公约数查询问题。重点强调了算法的时间复杂度和空间复杂度分析,以及如何通过优化数据结构提升效率。
114

被折叠的 条评论
为什么被折叠?



