ACM学习历程—HDU 5317 RGCDQ (数论)
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
See the sample for more details.
这个题目比较巧妙。
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 (数论)的更多相关文章
- ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)
Problem Description On the way to the next secret treasure hiding place, the mathematician discovere ...
- ACM学习历程—HDU 3092 Least common multiple(数论 && 动态规划 && 大数)
Description Partychen like to do mathematical problems. One day, when he was doing on a least common ...
- ACM学习历程—HDU 5512 Pagodas(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...
- HDU 5317 RGCDQ (数论素筛)
RGCDQ Time Limit: 3000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit Status ...
- ACM学习历程—HDU5668 Circle(数论)
http://acm.hdu.edu.cn/showproblem.php?pid=5668 这题的话,假设每次报x个,那么可以模拟一遍, 假设第i个出局的是a[i],那么从第i-1个出局的人后,重新 ...
- ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...
- ACM学习历程—HDU5666 Segment(数论)
http://acm.hdu.edu.cn/showproblem.php?pid=5666 这题的关键是q为质数,不妨设线段上点(x0, y0),则x0+y0=q. 那么直线方程则为y = y0/x ...
- ACM学习历程—HDU5585 Numbers(数论 || 大数)(BestCoder Round #64 (div.2) 1001)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5585 题目大意就是求大数是否能被2,3,5整除. 我直接上了Java大数,不过可以对末尾来判断2和5, ...
- ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...
随机推荐
- JavaScript -- 没事看看
@.delete 原文:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/delete
- CentOS系统环境下安装MongoDB
(1)进入MongoDB下载中心:http://www.mongodb.org/downloads We recommend using these binary distributions (官方推 ...
- Xcode6中如何对scrollview进行自动布局(autolayout)
本文转载至 http://www.cocoachina.com/ios/20141011/9871.html XCodeAutolayoutscrollView Xcode6中极大的增强了IB ...
- java设计模式之迭代器模式
一.迭代器模式简介 迭代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 迭代器模式让我们能游走于聚合内的每一个元素,而又不暴露内部的表示.把游走的任务放在迭代器上,而不是 ...
- t60替换alt,super,ctrl
发现T60的左边在ctrl 与 alt 有个win 键,所以就进行了映射 网上有一个把alt->ctrl, super-> alt, ctrl->super的script, 见 ht ...
- input file 选择Excel
说明:开发环境 vs2012 asp.net mvc4 c# ,使用file 选择Excel文件 传到后台 使用Aspose.Cells获取Excel sheet页的名称 1.HTML代码 <% ...
- 九度OJ 1003:A+B
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:15078 解决:6299 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开. 现在请计 ...
- vs2013 solution文件解析
1 定义一个project Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "render", &quo ...
- 一个小公司的前端笔试HTML CSS JS
网上有这套题的答案,版本也很多,我做了很多参考.本文就当个小笔记,可能有错误,还望指正~ 第1章 Html篇 1. 你做的网页在哪些浏览器测试过?这些浏览器的内核分别是什么? 浏览器类型 内核 Fi ...
- Ubuntu12.04如何修改窗口背景色为眼睛保护色来保护眼睛,强力推荐!!
最近突然发现盯着屏幕看的久了,眼睛会非常的痛苦,因此想改变一下系统的窗口背景颜色.其实看代码主要是在Eclipse里面察看,因此一开始我就想改变Eclipse的文本编辑框的背景颜色,效果如下图所示. ...