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. SSL、TLS协议格式、HTTPS通信过程、RDP SSL通信过程

    相关学习资料 http://www.360doc.com/content/10/0602/08/1466362_30787868.shtml http://www.gxu.edu.cn/college ...

  2. Recon-Erlang线上系统诊断工具

    Erlang系统素以稳定可靠闻名,但是它也是c实现的,也是要管理比如内存,锁等等复杂的事情,也会出现Crash,而且crash的时候大部分原因是因为内存问题.为此erlang运行期提供了强大的自省机制 ...

  3. Myeclipse连接mysql数据库

    第一步:下载mysql驱动 驱动地址:http://www.mysql.com/products/connector/ .选择JDBC Driver for MySQL (Connector/J). ...

  4. easyUI框架之学习2--添加左侧导航栏

    <head> function addTab(title, url) { if ($('#tableContainer').tabs('exists', title)) { $('#tab ...

  5. MyEclipse------黑科技

    自动计算器(+,-,*,/) <form method="post" oninput="o.value = parseInt(a.value) + parseInt ...

  6. TFS 配置 报表权限问题

    通过[项目门户网站]访问,却被提示ReportService权限不足时, 提示:处理报表时出错. (rsProcessingAborted)  对数据集 “dsiteration” 执行查询失败 同样 ...

  7. 锋利的jQuery-4--动画方法总结简表

  8. CentOS 7 firewalld使用简介

    1.firewalld简介 firewalld是centos7的一大特性,最大的好处有两个:支持动态更新,不用重启服务:第二个就是加入了防火墙的“zone”概念   firewalld有图形界面和工具 ...

  9. 安装 RPM 包或者安装源码包

    安装 RPM 包或者安装源码包 在windows下安装一个软件很轻松,只要双击.exe的文件,安装提示连续“下一步”即可,然而linux系统下安装一个软件似乎并不那么轻松了,因为我们不是在图形界面下. ...

  10. 字符串模拟赛T1

    // source code from laekov for c0x17 #define PRID "bxjl" #include <cstdio> #include ...