HDU 5726 GCD
GCD
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Problem Description
Give you a sequence of $N(N≤100,000)$ integers : $a_1,\cdots,a_n(0<a_i≤1000,000,000)$. There are $Q(Q≤100,000)$ queries. For each query $l,r$ you have to calculate $\text{gcd}(a_l,,a_{l+1},\cdots,a_r)$ and count the number of pairs$(l′,r′)(1≤l<r≤N)$such that $\text{gcd}(a_{l′},a_{l′+1},\cdots,a_{r′})$ equal $\text{gcd}(a_l,a_{l+1},...,a_{r})$.
Input
The first line of input contains a number $T$, which stands for the number of test cases you need to solve.
The first line of each case contains a number $N$, denoting the number of integers.
The second line contains $N$ integers, $a_1,\cdots,a_n(0<a_i≤1000,000,000)$.
The third line contains a number $Q$, denoting the number of queries.
For the next $Q$ lines, $i\text{-th}$ line contains two number , stand for the $l_i,r_i$, stand for the $i\text{-th}$ queries.
Output
For each case, you need to output “Case #:t” at the beginning.(with quotes, t means the number of the test case, begin from 1).
For each query, you need to output the two numbers in a line. The first number stands for $\text{gcd}(a_l,a_{l+1}, \cdots,a_r)$ and the second number stands for the number of pairs$(l′,r′)$ such that $\text{gcd}(a_{l′},a_{l′+1},\cdots,a_{r′})$ equal $\text{gcd}(a_l,a_{l+1},\cdots,a_r)$.
Sample Input
1
5
1 2 4 6 7
4
1 5
2 4
3 4
4 4
Sample Output
Case #1:
1 8
2 4
2 4
6 1
Author
HIT
Source
2016 Multi-University Training Contest 1
题意:
支持查询: (1) 区间gcd, (2) gcd值等于k的区间数
Solution:
区间gcd的查询线段树即可解决, 另外还能支持单点修改. 但这题要求支持查询gcd值等于k的区间个数, 线段树就有点乏力了, 因为这个信息大概不太好通过合并区间信息来得到. 我们来考虑区间gcd的性质:
令$\gcd_r(l)\quad (1\le l \le r) $表示, $l$到$r$的$\gcd$. 不难看出:
- $\gcd_r(l)$随着$l$的增大是单调不减的
- $\gcd_r(l)$最多取$\log{a_r}$个值, 因为在区间左端点从$r$移动到$l$的过程中gcd每缩小到一个新值都是因为除以了上个gcd的某个因子, 因而至少缩小为上个gcd的$\frac{1}{2}$, 从而不同的区间$\gcd$值最多有$\log{a_r}$个
因此, 我们可以对每个右端点$r$, 维护函数$\gcd_r(l)$. 实现方法是:
用vector<pair<int,int>> f 存某个函数$\gcd_r(l)$的每一段 (最多有$\log{a_r}$段), f[i].first表示第$i$段的左端点, f[i].second表示第$i$段的函数值.
在维护这$n$个函数的过程中, 用map记录每个$\gcd$出现的次数 (不同$\gcd$值最多有$O(n\log{N})$个, 实际上远达不到这个值.
接下来我们考虑如何利用上面维护好的函数查询某个区间$[l,r]$的$\gcd$.
我们可以在函数$g_r(l)$中二分查询小于等于的$l$的first的最大值对应的second的值, 这便是答案.
言不尽意, 详见代码.
Implementation:
#include <bits/stdc++.h>
using namespace std; const int N(1e5+);
typedef pair<int,int> P; vector<P> f[N];
unordered_map<int,long long> cnt;
int T, n, q, cs; int main(){
for(cin>>T; T--; ){
cin>>n;
for(int i=, gcd, pos; i<=n; i++){
scanf("%d", &gcd), pos=i, f[i].clear();
for(auto x:f[i-]){
if(__gcd(gcd, x.second)!=gcd) f[i].push_back({pos, gcd});
gcd=__gcd(gcd, x.second), pos=x.first;
}
f[i].push_back({pos, gcd});
}
cnt.clear();
for(int i=; i<=n; i++){
int pos=i+;
for(auto x:f[i])
cnt[x.second]+=pos-x.first, pos=x.first;
}
cin>>q;
printf("Case #%d:\n", ++cs);
for(int l, r; q--; ){
scanf("%d%d", &l, &r);
int gcd=lower_bound(f[r].begin(), f[r].end(), P(l, INT_MAX), greater<P>())->second;
printf("%d %lld\n", gcd, cnt[gcd]);
}
}
}
HDU 5726 GCD的更多相关文章
- HDU 5726 GCD 区间GCD=k的个数
GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- HDU 5726 GCD (RMQ + 二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 给你n个数,q个询问,每个询问问你有多少对l r的gcd(a[l] , ... , a[r]) ...
- HDU 5726 GCD(DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5726 [题目大意] 给出数列An,对于询问的区间[L,R],求出区间内数的GCD值,并且求出GCD ...
- HDU 5726 GCD(RMQ+二分)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5726 题意:给出一串数字,现在有多次询问,每次询问输出(l,r)范围内所有数的gcd值,并且输出有多 ...
- hdu 5726 GCD 暴力倍增rmq
GCD/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence ...
- HDU 5726 GCD (2016 Multi-University Training Contest 1)
Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description Give y ...
- hdu 5726 GCD 倍增+ 二分
题目链接 给n个数, 定义一个运算f[l,r] = gcd(al, al+1,....ar). 然后给你m个询问, 每次询问给出l, r. 求出f[l, r]的值以及有多少对l', r' 使得f[l, ...
- HDU 5726 GCD(ST&RMQ)
题目链接 GCD 先ST倍增预处理,f[i][j]表示从i开始(包含第i个数)的连续2^j个数的最大公约数. 这样就可以在O(1)内询问得到a[l]到a[r]之间的所有数的最大公约数的值. 然后对于每 ...
- HDU 5726 GCD (2016多校、二分、ST表处理区间GCD、数学)
题目链接 题意 : 给出一个有 N 个数字的整数数列.给出 Q 个问询.每次问询给出一个区间.用 ( L.R ) 表示.要你统计这个整数数列所有的子区间中有多少个和 GCD( L ~ R ) 相等.输 ...
随机推荐
- 判断 JS 中对象的类型
1.typeof 形如 var x = "xx"; typeof x == 'string' typeof(x) 返回类型有:'undefined' “string” 'numbe ...
- JSON简介以及用法汇总
什么是JSON? JavaScript 对象表示法(JavaScript Object Notation). JSON是一种轻量级的数据交换格式,某个JSON格式的文件内部譬如可以长成这样: { &q ...
- js的bind方法
转载:http://www.jb51.net/article/94451.htm http://www.cnblogs.com/TiestoRay/p/3360378.html https://seg ...
- Log4net使用(三)
第一步 public class logger { private static ILog Info; private static ILog Error; private static ILog W ...
- 站内搜索——Lucene +盘古分词
为了方便的学习站内搜索,下面我来演示一个MVC项目. 1.首先在项目中[添加引入]三个程序集和[Dict]文件夹,并新建一个[分词内容存放目录] Lucene.Net.dll.PanGu.dll.Pa ...
- js简易函数性能测试器
如果你不想用浏览器的js性能测试工具,可以用下面这个简单的函数测试一下(1毫秒一下的就测不出来了) function testFn(fn,param){ var start = new Date(). ...
- mybatis generator使用总结
一.mybatis项目的体系结构 百度mybaits,可以进入mybatis的github:https://github.com/mybatis. mybatis是一个大大的体系,它不是孤立的,它可以 ...
- rhel7修改网卡命名规则
1步:当安装完红帽RHEL7系统安装完成,您的网卡命名是这样的. 第2步:请编辑网卡的配置文件 将”/etc/sysconfig/network-scripts/ifcfg-eno16777736“的 ...
- 【JavaEE企业应用实战学习记录】requestListener
package sanglp.servlet; import javax.servlet.*; import javax.servlet.annotation.WebListener; import ...
- Mysql 慢查询和慢查询日志分析
众所周知,大访问量的情况下,可添加节点或改变架构可有效的缓解数据库压力,不过一切的原点,都是从单台mysql开始的.下面总结一些使用过或者研究过的经验,从配置以及调节索引的方面入手,对mysql进行一 ...