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. java 开发环境安装

    一.在mac上安装jdk 1. 下载Mac版本的JDK并安装      http://www.oracle.com/technetwork/java/javase/downloads/index.ht ...

  2. 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bccced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中

    // test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  3. beego的MVC架构介绍

    beego 的 MVC 架构介绍 beego 是一个典型的 MVC 框架,它的整个执行逻辑如下图所示: 通过文字来描述如下: 在监听的端口接收数据,默认监听在 8080 端口. 用户请求到达 8080 ...

  4. git 工具 - 子模块(submodule)

    From: https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E5%AD%90%E6%A8%A1%E5%9D%97 子模块 有种情况我们经常 ...

  5. 模式识别之svm()---支持向量机svm 简介1995

    转自:http://www.blogjava.net/zhenandaci/archive/2009/02/13/254519.html 作者:Jasper 出自:http://www.blogjav ...

  6. IdentityServer4 + SignalR Core +RabbitMQ 构建web即时通讯(三)

    IdentityServer4 + SignalR Core +RabbitMQ 构建web即时通讯(三) 后台服务用户与认证 新建一个空的.net core web项目Demo.Chat,端口配置为 ...

  7. maven scope runtime

    https://blog.csdn.net/ningbohezhijunbl/article/details/25818069 There are 6 scopes available: compil ...

  8. Linux搭建FTP服务器实战

    首先准备一台Linux系统机器(虚拟机也可), 检测出是否安装了vsftpd软件: rpm -qa |grep vsftpd 如果没有输出结果,就是没有安装. 使用命令安装,安装过程中会有提示,直接输 ...

  9. SecureCRT连接VMWare中Linux

    SecureCRT连接vmvare虚拟机ubuntu的前提条件1.   使用主机可以ping通虚拟机,或虚拟机可以ping通主机2.   虚拟机已经开启ssh服务,可以使用sudo apt-get i ...

  10. LeetCode:分发饼干【455】

    LeetCode:分发饼干[455] 题目描述 假设你是一位很棒的家长,想要给你的孩子们一些小饼干.但是,每个孩子最多只能给一块饼干.对每个孩子 i ,都有一个胃口值 gi ,这是能让孩子们满足胃口的 ...