Time Limit: 3000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit Status

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 \max GCD(F(i),F(j)) (L\leq i<j\leq 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
 

Source

2015 Multi-University Training Contest 3
题意:如题,x是一个正整数,f(x)表示x的素因子种类数, F(2)=1. F(10)=2,因为10=2*5. F(12)=2, 因为12=2*2*3。现在给定两个数l和r,问在l和r这个区间内任取两个数i,j中gcd(f(i),f(j))的最大值。给定t组数据,每组给定l和r,输出结果。
题解:先用素筛法打表筛选出每个数的素因子种类数,我们发现2*3*5*7*11*13*17=510510,注意虽然这个数小于10的6次方,但是已经足够证明7已经是最大值了,因为这7个素数是素数中最小的7个。f(i)只考虑i的素因子种类个数,不考虑这个素因子是否和j的素因子是同一个。举个例子,比如i=2*3*5*7*11*13*17=510510,种类数为7,j=2*3*5*7*11*13*19=570570,种类数也为7,所以如果lr区间中包含ij那么输出7。事实上,510510和570570是最小的两个包含7种素因子的数。说到这里就可以写了,涉及到一点递推的知识,用sum[maxn][8]存储,sum[i][j]表示对于数i来说,2到i中所有数的素因子种类数为j的数的个数。i从7遍历到1,如果sum[r][i]-sum[l-1][i]>=2,说明该区间内存在至少两个数的素因子种类数为i,break输出即可,因为我们要的是最大值。注意初始的时候要把ans定义为1,因为可能所有数的素因子种类数都不相等比如6,7这组数据,f(6)=2,f(7)=1,gcd(2,1)=1。输出1而不是0,虽然输出0也是能AC的但是不符合题意。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=1e6+;
int num[maxn];
int sum[maxn][];
void getnum()
{
memset(num,,sizeof(num));
memset(sum,,sizeof(sum));
for(int i=;i<maxn;i++)
{
if(!num[i])
{
for(int j=i;j<maxn;j+=i)
num[j]++;
}
}
for(int i=;i<maxn;i++)
for(int x=;x<=;x++)
{
sum[i][x]=sum[i-][x];
if(num[i]==x)
sum[i][x]++;
}
}
int main()
{
getnum();
int t;
scanf("%d",&t);
while(t--)
{
int l,r,ans=;
scanf("%d%d",&l,&r);
for(int i=;i>=;i--)
{
if(sum[r][i]-sum[l-][i]>=)
{
ans=i;
break;
}
}
printf("%d\n",ans);
}
return ;
}

HDU 5317 RGCDQ (数论素筛)的更多相关文章

  1. hdu 5317 RGCDQ(前缀和)

    题目链接:hdu 5317 这题看数据量就知道需要先预处理,然后对每个询问都需要在 O(logn) 以下的复杂度求出,由数学规律可以推出 1 <= F(x) <= 7,所以对每组(L, R ...

  2. ACM学习历程—HDU 5317 RGCDQ (数论)

    Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more an ...

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

    题目链接:pid=5317" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=5317 Prob ...

  4. 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 ...

  5. 2015 Multi-University Training Contest 3 hdu 5317 RGCDQ

    RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  6. HDU 5317 RGCDQ

    题意:f(i)表示i的质因子个数,给l和r,问在这一区间内f(i)之间任意两个数最大的最大公倍数是多少. 解法:先用筛法筛素数,在这个过程中计算f(i),因为f(i)不会超过7,所以用一个二维数组统计 ...

  7. HDU 5317 RGCDQ (质数筛法,序列)

    题意:从1~1000,000的每个自然数质因子分解,不同因子的个数作为其f 值,比如12=2*2*3,则f(12)=2.将100万个数转成他们的f值后变成新的序列seq.接下来T个例子,每个例子一个询 ...

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

    2015 HDU 多校联赛 5317 RGCDQ 筛法求解 题目  http://acm.hdu.edu.cn/showproblem.php? pid=5317 本题的数据量非常大,測试样例多.数据 ...

  9. hdu 5317 合数分解+预处理

    RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

随机推荐

  1. BZOJ-3670 动物园 KMP+奇怪的东西

    YveH爷再刷KMP,DCrusher看他刷KMP,跟着两个人一块刷KMP... 3670: [Noi2014]动物园 Time Limit: 10 Sec Memory Limit: 512 MB ...

  2. BZOJ1208 宠物收养所

    Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...

  3. 2-SAT开坑

    Reference:http://blog.csdn.net/jarjingx/article/details/8521690 其中伍昱的ppt不错. 2SAT最裸的模型: 一国有n个党派,每个党派在 ...

  4. kindeditor粘贴word文档内容时去除格式的方法?如何设置为默认无文本格式呢?

    打开文件夹找到kindeditor-min.js文件,搜索pasteType函数,默认值是2.设置为1即可. 设置粘贴类型,0:禁止粘贴, 1:纯文本粘贴, 2:HTML粘贴.

  5. jquery------添加jQuery对象方法

    my.js $(document).ready(function(){ (function($){ $.fn.swapClass=function(class1,class2){ if(this.ha ...

  6. hdu 2049 不容易系列之(4)——考新郎

    在本博AC代码中,求CNM用的是Anm/amm没用阶乘的形式,两者皆可 #include <stdio.h> int main(void) { long long a,b,larr[21] ...

  7. tcp MSL

    MSL(最大分段生存期)指明TCP报文在Internet上最长生存时间,每个具体的TCP实现都必须选择一个确定的MSL值.RFC 1122建议是2分钟. TIME_WAIT 状态最大保持时间是2 * ...

  8. php实现文件安全下载

    public function downloads($name){ $name_tmp = explode("_",$name); $type = $name_tmp[0]; $f ...

  9. myBatis 实现用户表增操作(复杂型)

    增加 @Test public void addTest(){ String resource = "mybatis-config.xml"; SqlSession sqlSess ...

  10. Jquery中的 height(), innerHeight() outerHeight()区别

    jQuery中的 height innerHeight outerHeight区别 标准浏览器下: height:高度 innerHeight:高度+补白 outerHeight:高度+补白+边框,参 ...