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. jsp学习(五)

    在进行jsp与jdbc连接时,出现这样一个错误,提示如下: java.net.ConnectException: Connection refused: connect 后来发现是由于mysql数据库 ...

  2. Linux cscope命令

    一.简介 Cscope 是一款开源免费的 C/C++浏览工具,自带一个基于文本的用户界面,通过cscope可以很方便地找到某个函数或变量的定义位置.被调用的位置等信息.Cscope对 C /C++支持 ...

  3. HDU 1054 Strategic Game(最小点覆盖+树形dp)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=106048#problem/B 题意:给出一些点相连,找出最小的点数覆盖所有的 ...

  4. Aop 是面向切面编程,

    Aop 是面向切面编程,是在业务代码中可以织入其他公共代码(性能监控等),现在用普通的方法实现AOP http://blog.csdn.net/heyanfeng22/article/details/ ...

  5. Adele的生活

    3个小时的电影,有点冗长,中间抽空吃了个饭,跑出去抽了3根烟,接着洗了个脸才回来接着看完,法国人做事真是拖沓啊有木有. 电影看完后给人一种淡淡的忧伤,第一次了解lesbians的真实世界(如果电影是来 ...

  6. MSF溢出实战教程

    1.  进入终端,开启MSF相关服务 2.  连接数据库 3.  主机扫描 发现如果有MS08_067漏洞,就可以继续渗透 4.  开始溢出 溢出成功的话 sessions -l         查看 ...

  7. Javascript检测用户注册信息

    <html> <head> <title>用户注册</title> <meta http-equiv="content-type&quo ...

  8. jquery autocomplete 简单实用例子

    <link href="../../themes/default/css/jquery.ui.all.css" rel="stylesheet" type ...

  9. c++中try catch的用法

    c++中try catch的用法 标签: c++exception数据库sqlc 2011-10-24 21:49 45622人阅读 评论(3) 收藏 举报  分类: 一点小结(267)  版权声明: ...

  10. apache配置多个虚拟主机

    设置apache 多个虚拟目录记录 #配置第2个虚拟目录<VirtualHost 127.0.0.2:80>ServerName www.xx.comDocumentRoot " ...