HDU 5317(RGCDQ-统计)

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

Output

For each query,output the answer in a single line.

See the sample for more details.

Sample Input

2
2 3
3 5

Sample Output

1
1

Author

ZSTU

Source

2015 Multi-University Training Contest 3

Recommend

wange2014   |   We have carefully selected several similar problems for you:  5405 5404 5403 5402 5401

统计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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-04 06:32:14

HDU 5317(RGCDQ-统计)的相关文章

HDU 5317 RGCDQ (合数分解+预处理)

题目链接:HDU 5317 RGCDQ 题意:定义函数F(x)为x的不同的素因子且小于等于x的个数,询问[l,r]区间中gcd(F(i),F(j))的最大值. 思路:暴力预处理出所有的合数分解结果,发现F(x)最大也只有7,之后就是暴力求出所有1到7出现次数的前缀和.询问的时候就打到O(1)了. AC代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #

hdu 5317 RGCDQ(前缀和)

题目链接:hdu 5317 这题看数据量就知道需要先预处理,然后对每个询问都需要在 O(logn) 以下的复杂度求出,由数学规律可以推出 1 <= F(x) <= 7,所以对每组(L, R),只需要求出它们在 1~7 的范围内的数量,然后暴力求出 gcd 即可.因为符合递增,可以设一个结点 struct { v[8]; } 记录 1~7 的数量,结点间可以相加减,也就可以用前缀和的思想去做(其实我也是看了别人的题解才明白这种思想,一开始用二分不是超时就是 wa 了,不过我竟发现自己手写的二分比

hdu 5317 RGCDQ 筛法+线段树解法

RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 299    Accepted Submission(s): 151 Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and mor

HDU 5317 RGCDQ(素数个数 多校2015啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5317 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 (R

HDU 5317 RGCDQ

RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 323    Accepted Submission(s): 162 Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and mo

hdu 5317 RGCDQ (2015多校第三场第2题)素数打表+前缀和相减求后缀(DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5317 题意:F(x) 表示x的不同质因子的个数结果是求L,R区间中最大的gcd( F(i) , F(j) ),i.j在L,R区间内. 思路:因为2<=L < R<=1000000,所以他们的质因子最多的个数也就7个,也就是说1<=F(x)<=7,因为要求最大的GCD,所以只要知道在L,R区间内每个F(x)的情况就可以知道结果. 代码: 1 #include <stdio.h

hdu 5317 RGCDQ 多校 思维题

点击打开链接题目链接 RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 328    Accepted Submission(s): 164 Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find

HDU 5317 RGCDQ (素因子分解+预处理)

题目链接:传送门 题意: 求区间[l,r]所有数的素因子的种类的最大的最大公约数...额,不知道怎么描述了... 比如说区间内的所有数的素因子种类分别为1,2,3,4,5,6,7那么结果就是gcd(3,6) 分析: 数据的范围是1~1000000,因子数最大为7,我们首先要预处理出所有数的数的因子数,但是因为 数据的范围比较大我们不能直接暴力求结果,因为因子数的可能取值比较小,因此我们可以预处 理出每种取值数的前缀和,然后就可以在O(1)的的时间内求出区间内每种取值的数. 代码如下: #incl

hdu 5317 RGCDQ(预处理)

题意:给定区间[a,b](a,b<10^6),f(x)表示x包含不同素因子的个数,求[a,b]中的maxgcd(f(i),f(j))(a<=i,j<=b): 思路:对于10^6,每个数不超过7个素因子. 预处理出10^6中从1开始的每段区间中f(x)每种情况的数量和. 如果区间中某种f(x)的情况的个数不少于2,则该情况成立,取最大的情况: #include<cstdio> #include<cstring> #include<algorithm> #

2015 HDU 多校联赛 5317 RGCDQ 筛法求解

2015 HDU 多校联赛 5317 RGCDQ 筛法求解 题目  http://acm.hdu.edu.cn/showproblem.php? pid=5317 本题的数据量非常大,測试样例多.数据量大, 所以必须做预处理.也就是用筛法求出全部的F[x],将全部F[x] 打印出来发现.事实上结果不大,最大的数值是7.所以对于每一个区间询问, 直接暴力求取有多少个 1 2 3 4 5 6 7 就可以,从大到小查找.假设出现2个以上 3-7 的数值,那么最大公约数就是该数字. 假设没有出现两个反复