RGCDQ

题意:F(x)表示x的质因子的种数。给区间[L,R],求max(GCD(F(i),F(j)) (L≤i<j≤R)。(2<=L < R<=1000000)

题解:可以用素数筛求质因子种数(这不用多说,看下代码init()中内容就能理解)。然而R的范围太大,会TLE。因此只能用空间换时间了。

可以用一个二维数组num[i][j] 保存x<=i&&F(x)=j的x的个数。(预处理,有点dp的思想)

2*3*5*7*11*13*17 > 10 ^ 6,即在1~1e6的范围内最多有7个素数相乘。so F(x)最大为7,即j<=7。

给出L,R,则num[R][j]-num[L][j]为在[L,R]区间F(x)==j的x的个数b[j]。根据b[j]的值,很容易人工求出答案。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=1e6+5; int f[N],num[N][10],b[10];
void init() //筛选法求f(x)
{
memset(f,0,sizeof(f));
for(int i=2;i<N;i++)
{
if(f[i]==0)
{
f[i]=1;
for(int j=i+i;j<N;j+=i)
{
f[j]++;
}
}
}
for(int i=2;i<N;i++)
{
for(int j=1;j<8;j++)
{
num[i][j]=num[i-1][j];
}
num[i][f[i]]++;
}
} int main()
{
int t,l,r;
init();
cin>>t;
while(t--)
{
scanf("%d%d",&l,&r);
int ans=0;
for(int i=1;i<8;i++)
{
b[i]=num[r][i]-num[l-1][i];
if(num[r][i]-num[l-1][i]>1)
ans=i;
}
if(ans!=0)
printf("%d\n",ans);
else if((b[2]>0&&b[4]>0)||(b[2]>0&&b[6]>0)||(b[6]>0&&b[4]>0))
printf("2\n");
else if(b[3]>0&&b[6]>0)
printf("3\n");
else
printf("1\n");
}
return 0;
}

RGCDQ

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2539    Accepted Submission(s): 1012

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

hdu5317 RGCDQ (质因子种数+预处理)的更多相关文章

  1. hdu5317 RGCDQ 统计

    // hdu5317 RGCDQ // // 题目大意: // // 给定一个闭区间[l,r],定义f(x)是x的不同的质因子的个数 // 比方: 12 = 2 * 2 * 3,是两种.所以f(x) ...

  2. 解题报告 之 HDU5317 RGCDQ

    解题报告 之 HDU5317 RGCDQ Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to ...

  3. HDU-5317 RGCDQ ,暴力打表!

    RGCDQ 暴力水题,很可惜比赛时没有做出来,理清思路是很简单的. 题意:定义f(i)表示i的素因子个数,给你一段区间[l,r],求max_gcd(f(i),f(j)).具体细节参考题目. 思路:数据 ...

  4. Educational Codeforces Round 89 (Rated for Div. 2)D. Two Divisors 线性筛质因子

    题目链接:D:Two Divisors 题意: 给你n个数,对于每一个数vi,你需要找出来它的两个因子d1,d2.这两个因子要保证gcd(d1+d2,vi)==1.输出的时候输出两行,第一行输出每一个 ...

  5. Openjudge 1.13-21:最大质因子序列(每日两水)

    总时间限制:  1000ms 内存限制:  65536kB 描述 任意输入两个正整数m, n (1 < m < n <= 5000),依次输出m到n之间每个数的最大质因子(包括m和n ...

  6. 快速求n的质因子(数论)

    快速求n的质因子 如何尽快地求出n的质因子呢?我们这里又涉及两个好的算法了! 第一个:用于每次只能求出一个数的质因子,适用于题目中给的n的个数不是很多,但是n又特别大的 #include<std ...

  7. UVA 10780 Again Prime? No Time. 分解质因子

    The problem statement is very easy. Given a number n you have to determine the largest power of m,no ...

  8. 一个数n的最大质因子

    #include<cstdio> #include<cmath> using namespace std; #define Max(x, y) (x > y ? x : ...

  9. BZOJ 3181([Coci2012]BROJ-最小质因子为p的第k小素数)

    3181: [Coci2012]BROJ Time Limit: 10 Sec   Memory Limit: 64 MB Submit: 26   Solved: 7 [ Submit][ Stat ...

随机推荐

  1. cmd执行SQL语句

    首先新建txt文本,复制粘帖以下内容,保存为cmd执行SQL.bat.注意是bat文件 osql -S 服务器名称 -d 数据库 -U 登录名 -P 密码 -i 盘符:\文件夹\文件.sql

  2. mvc实现上传图片(上传和预览)webuploader

    笔者看到mvc最近比较流行,而很多使用一些比较旧的的方法上传图片,再次安利一下百度的webuploader控件吧 webuploader第一步要先下载一些插件这点可以在webuploader官网上下载 ...

  3. Sql Server 备份还原失败错误ERROR:3145(备份集中的数据库备份与现有的数据库不同)及解决办法

    SQL Server备份文件bak,备份后还原出现错误3145,备份集中的数据库备份与现有的 'xxx' 数据库不同. 解决办法如下: 1,新建一个与现有数据库重名的数据库. 如果您不知道数据库名称, ...

  4. (1-2)配置的升级 - ASP.NET从MVC5升级到MVC6

    ASP.NET从MVC5升级到MVC6 总目录 MVC5和MVC6的区别 在MVC5的时候,web.config文件无疑是整个配置的核心,从web配置,到应用程序的参数,都可以写在web.config ...

  5. Angular2正式版发布,Wijmo抢先支持

    Angular2正式版发布 9月15日,Angular 2 的最终版正式发布了.作为 Angular 1 的全平台继任者 -- Angular 2 的最终版,意味着什么? 意味着稳定性已经得到了大范围 ...

  6. [示例] Firemonkey 不规则按钮实做

    利用 Firemonkey 控件的组合及可塑性,可以做出千变万化的效果及功能,下面展示一个不规则按钮的实做: 效果图: 实做方法: 开一个新工程 Multi-Device Application 放一 ...

  7. maven引入的jar带了时间戳

    正常情况下,我们引入的第三方jar一般都是类似spider-1.0.5-SNAPSHOT.jar的名称,但是有些环境下,我们引入的三方jar会带上了时间戳,如下所示: spider-1.0.5-201 ...

  8. (原)3.2 Zookeeper应用 - 数据的发布与订阅

    本文为原创文章,转载请注明出处,谢谢 数据的发布与订阅 1.应用 服务端监听数据改变,客户端创建/更新节点数据,客户端提供数据,服务端处理 2.原理 客户端监控节点数据改变事件(例如配置信息,下图的c ...

  9. Web AppBuilder Widget使用共享类库的方式

    Web AppBuilder是Esri公司推出的快速WebGIS应用搭建工具,具有以下特性: 不需要编程,快速创建应用 WYSIWYG 交互式应用 支持2D和3D应用 基于ArcGIS API for ...

  10. reason: '[<__NSDictionary0 0x7fda88f00c90> setValue:forUndefinedKey:]: this class is not key value c

    reason: '[<__NSDictionary0 0x7fda88f00c90> setValue:forUndefinedKey:]: this class is not key v ...