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. 求解复数组 中模较大的N个数

    //求解复数组 中模较大的N个数 void fianN_Complex(Complex outVec[], int& len, std::vector<int>& inde ...

  2. Java下HttpUnit和Jsoup的Http抓取

    简单记录下:搜集信息-分析问题-解决问题 关于html文档的操作现成库有: HttpUnit 很老了,不更了 http://www.httpunit.org/  20 May 2008 HttpUni ...

  3. 一个用消息队列 的人,不知道为啥用 MQ,这就有点尴尬

    消息队列 为什么写这篇文章? 博主有两位朋友分别是小A和小B: 小A,工作于传统软件行业(某社保局的软件外包公司),每天工作内容就是和产品聊聊需求,改改业务逻辑.再不然就是和运营聊聊天,写几个SQL, ...

  4. A charge WIFI point base on airbase-ng+dhcp+lamp+wiwiz

    Make wifi as a hot point Make a script echo $0 $1 case $1 in "start") sleep 1 ifconfig wla ...

  5. python 基础2.5 循环中continue与breake用法

    示例1: #循环退出,break continue.break 跳出最外层循环:continue跳出内层循环 #当 i=5时,通过continue 跳出当前if循环,不在执行if循环中后边的语句.i= ...

  6. 【BZOJ4999】This Problem Is Too Simple! 离线+树状数组+LCA

    [BZOJ4999]This Problem Is Too Simple! Description 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: 1. C i x(0<=x<2 ...

  7. EasyNVR无插件直播服务如何配合EasyBMS使用以及实现流媒体管理功能概述

    本文转自:https://blog.csdn.net/black_3717/article/details/79769195 功能概要: 1.摄像机的无插件直播: 2.摄像机的低延时直播: 3.摄像机 ...

  8. 九度OJ 1060:完数VS盈数 (数字特性)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5590 解决:2093 题目描述: 一个数如果恰好等于它的各因子(该数本身除外)子和,如:6=3+2+1.则称其为"完数" ...

  9. 九度OJ 1043:Day of Week(星期几) (日期计算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5349 解决:1923 题目描述: We now use the Gregorian style of dating in Russia. ...

  10. Optimizer in SQL - Catalyst Optimizer in Spark SQL

    SELECT sum(v) FROM (    SELECT score.id, 100+80+score.math_score+ score.english_score AS v    FROM p ...