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

这个题目比较巧妙。

f(i)求的是i的素数因子个数。

首先需要处理因子个数。这个方法很多。

其实对于一个i来说,由于i最大是1000000,然而当2*3*5*7.....*17这时是最大能在这个范围的。其他数的素数因子数目必然小于这个。也就是f(i)最大是7。

这样只需要查询L到R区间内f(i)分别为1, 2, 3, 4, 5, 6, 7的个数,然后暴力枚举gcd的最大值就可以了。

当时人比较二,区间求和直接上了线段树,然后MLE了,然后换成树状数组才AC。。。。

赛后经人提醒才发现,这个完全没有点修改操作,根本不需要使用上述工具。直接用sum数组保存前缀和就可以。然后sum[j] - sum[i-1]就是[i, j]区间的和了。。。

不管怎样第一次使用树状数组,还是附上代码。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#define LL long long using namespace std; //GCD
//求最大公约数
//O(logn)
int gcd(int a, int b)
{
if (b == )
return a;
else
return gcd(b, a%b);
} const int maxN = ; char f[maxN];
bool b[maxN];
int L, R; struct Val
{
int v[]; Val()
{
memset(v, , sizeof(v));
}
Val operator+(Val x)
{
Val ans;
for (int i = ; i <= ; ++i)
ans.v[i] = x.v[i] + v[i];
return ans;
}
Val operator-(Val x)
{
Val ans;
for (int i = ; i <= ; ++i)
ans.v[i] = v[i] - x.v[i];
return ans;
}
}; Val d[maxN]; int lowbit(int x)
{
return x&(-x);
} void add(int pos,Val pls)
{
while(pos <= maxN)//x最大是N
{
d[pos] = d[pos] + pls;
pos += lowbit(pos);
}
} Val sum(int to)
{
Val s;
while(to > )
{
s = s + d[to];
to -= lowbit(to);
}
return s;
} Val query(int from, int to)
{
return sum(to) - sum(from - );
} void init()
{
int m = ;
memset(f,,sizeof(f));
memset(b,,sizeof(b));
for (int i = ; i <= m; i++)
{
if (!b[i])
for(int j = i; j <= m; j = j+i)
{
b[j] = ;
f[j]++;
}
} for (int i = ; i <= m; ++i)
{
Val tmp;
tmp.v[f[i]] = ;
add(i, tmp);
}
} void work()
{
Val p = query(L, R);
int ans = ;
for (int i = ; i <= ; ++i)
{
if (!p.v[i])
continue;
p.v[i]--;
for (int j = i; j <= ; ++j)
{
if (!p.v[j])
continue;
ans = max(ans, gcd(i, j));
}
p.v[i]++;
}
printf("%d\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
init();
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
scanf("%d%d", &L, &R);
work();
}
return ;
}

ACM学习历程—HDU 5317 RGCDQ (数论)的更多相关文章

  1. ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)

    Problem Description On the way to the next secret treasure hiding place, the mathematician discovere ...

  2. ACM学习历程—HDU 3092 Least common multiple(数论 && 动态规划 && 大数)

    Description Partychen like to do mathematical problems. One day, when he was doing on a least common ...

  3. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  4. HDU 5317 RGCDQ (数论素筛)

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

  5. ACM学习历程—HDU5668 Circle(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5668 这题的话,假设每次报x个,那么可以模拟一遍, 假设第i个出局的是a[i],那么从第i-1个出局的人后,重新 ...

  6. ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...

  7. ACM学习历程—HDU5666 Segment(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5666 这题的关键是q为质数,不妨设线段上点(x0, y0),则x0+y0=q. 那么直线方程则为y = y0/x ...

  8. ACM学习历程—HDU5585 Numbers(数论 || 大数)(BestCoder Round #64 (div.2) 1001)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5585 题目大意就是求大数是否能被2,3,5整除. 我直接上了Java大数,不过可以对末尾来判断2和5, ...

  9. ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...

随机推荐

  1. flash插件使用外部数据的方法

    使用xml保存需要改变的数据,如轮播图的图片路径,也可以在xml中指定数据库地址等

  2. 说说我的web前端之路,分享些前端的好书(转)

    WEB前端研发工程师,在国内算是一个朝阳职业,这个领域没有学校的正规教育,大多数人都是靠自己自学成才.本文主要介绍自己从事web开发以来(从大二至今)看过的书籍和自己的成长过程,目的是给想了解Java ...

  3. JavaScript读书笔记(5)-Object Date

    1.Object类型 (1)创建Object实例 第一种方式:new操作符后跟Object构造函数 var person=new Object(); person.name=”Nicholas”; p ...

  4. AWR元数据的迁移或导入到其它数据库

    我们能够将AWR元数据迁移(导入)到其它数据库.低版本号的导入到高版本号,再用高版本号的数据库生成AWR报告,也能使用一些新特性,如 SQL ordered by Physical Reads (Un ...

  5. sublime3 支持 jsx 语法

    添加几个插件即可在js中快速写html babel 可以识别React,并高亮显示ES6 command+shift+p -> install package -> babel 使用 在打 ...

  6. 仿易讯clientloading效果

    以下来实现一个loading效果.详细效果例如以下: 首先对这个效果进行拆分,它由以下部分组成: 1 一个"闪电"样式的图案. 2 "闪电"图案背后是一个圆角矩 ...

  7. 1355: [Baltic2009]Radio Transmission[循环节]

    1355: [Baltic2009]Radio Transmission Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 796  Solved: 538 ...

  8. EasyPlayerPro Windows播放器全屏模式下GDI显示出现黑屏问题解决

    问题来源 2017.12.21 前天有杭州某教育领域客户反馈有部分视频源在全屏模式下显示黑屏: 问题复现 EasyPlayerPro由于没有实现单个窗口完全全屏,故没有暴露该问题,晚上加班,加上单个窗 ...

  9. m*n matrix min rank square matrix

    m*n matrix m*n=1000 f(A)=25 https://www.cs.princeton.edu/courses/archive/spring12/cos598C/svdchapter ...

  10. 前端几个笔试题及答案(bd)

    1.   行内元素.块级元素和空元素(void)举例. 块级元素:<address>.<caption>.<dd>.<div>.<dl>.& ...