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. FTPHelper类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...

  2. 深入Asyncio(四)Coroutines

    Coroutines asyncio在3.4版本添加到Python中,但通过async def和await关键字创建coroutines的语法是3.5才加入的,在这之前,人们把generators当作 ...

  3. Docker入门系列3:使用

    入门 首先强烈建议玩一遍官方的入门教程,Interactive commandline tutorial,下面是答案: 查看版本:docker version 搜索Image:docker searc ...

  4. Memcache安装与使用

    一.资源下载 安装memcached 之前必需要先安装 libevent 分别在libevent和memcached的官网下载安装包libevent-1.4.14b-stable.tar.gz和mem ...

  5. 再看GS接包过程

    再看GS接包过程 bool GameServer::ProcessLoop(packet& rPkt) { if(false == m_spDataLayer->Recv(rPkt)) ...

  6. python之Matplotlib 和Numpy

    1.matplotlib http://www.cnblogs.com/TensorSense/p/6802280.html https://wenku.baidu.com/view/e1c15c9d ...

  7. python编程基础:《http://www.cnblogs.com/wiki-royzhang/category/466416.html》

    windows自动化 http://www.cnblogs.com/wiki-royzhang/category/466416.html

  8. 关于EF输出sql的执行日志

    sqlserver中可以使用sql profiler:但是mysql当中无法查看:只能借助于组件: ADO.NET Entity Framework CodeFirst 如何输出日志(EF4.3) 用 ...

  9. mvn deploy

    mvn:deploy在整合或者发布环境下执行,将最终版本的包拷贝到远程的repository,使得其他的开发者或者工程可以共享. 以将ojdbc14传到nexus中的thirdparty为例 一 配置 ...

  10. 爬虫-【selenium——webElement常用方法】

    a)clear——清除元素的内容 driver.find_element_by_id("**").clesr() b)send_keys——在元素上模拟按键输入 driver.fi ...