传送门

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的更多相关文章

  1. HDU 5726 GCD 区间GCD=k的个数

    GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  2. HDU 5726 GCD (RMQ + 二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 给你n个数,q个询问,每个询问问你有多少对l r的gcd(a[l] , ... , a[r]) ...

  3. HDU 5726 GCD(DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5726 [题目大意] 给出数列An,对于询问的区间[L,R],求出区间内数的GCD值,并且求出GCD ...

  4. HDU 5726 GCD(RMQ+二分)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5726 题意:给出一串数字,现在有多次询问,每次询问输出(l,r)范围内所有数的gcd值,并且输出有多 ...

  5. hdu 5726 GCD 暴力倍增rmq

    GCD/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence ...

  6. HDU 5726 GCD (2016 Multi-University Training Contest 1)

      Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Description Give y ...

  7. hdu 5726 GCD 倍增+ 二分

    题目链接 给n个数, 定义一个运算f[l,r] = gcd(al, al+1,....ar). 然后给你m个询问, 每次询问给出l, r. 求出f[l, r]的值以及有多少对l', r' 使得f[l, ...

  8. HDU 5726 GCD(ST&RMQ)

    题目链接 GCD 先ST倍增预处理,f[i][j]表示从i开始(包含第i个数)的连续2^j个数的最大公约数. 这样就可以在O(1)内询问得到a[l]到a[r]之间的所有数的最大公约数的值. 然后对于每 ...

  9. HDU 5726 GCD (2016多校、二分、ST表处理区间GCD、数学)

    题目链接 题意 : 给出一个有 N 个数字的整数数列.给出 Q 个问询.每次问询给出一个区间.用 ( L.R ) 表示.要你统计这个整数数列所有的子区间中有多少个和 GCD( L ~ R ) 相等.输 ...

随机推荐

  1. 20145230GDB调试汇编堆栈过程分析

    20145230GDB调试汇编堆栈过程分析 分析过程 出现的问题:一开始无法编译,是因为我们的Linux中没有安装一个库. 进入之前先设置断点,之后disassemble可以获取汇编代码,用i r指令 ...

  2. Theano2.1.4-基础知识之图结构

    来自:http://deeplearning.net/software/theano/tutorial/symbolic_graphs.html Graph Structures Theano是将符号 ...

  3. 制作stick侧边栏导航效果

    其实这种效果网上好多的插件,但是我想自己实现看看,其实把思路理清实现起来就非常简单了,让我们看看: 分析: 从图中我们可以看出:右边的top=100px,那么它应该在第一个框的top+height=1 ...

  4. 让 Generator 自启动

    文章同步自个人博客:http://www.52cik.com/2016/07/11/generator-co.html 此前只是简单使用而没有真正的去研究 Generator,这次要好好折腾下这货. ...

  5. Code Review 五问五答

    Code Review 是什么? Code Review即代码审查,程序猿相互审核对方的代码. Code Review能获得什么好处? 提高代码可维护性 你写的代码不再只有编译器看了,你得写出审核人能 ...

  6. WPF学习之路由事件

    原文:http://www.cnblogs.com/lxy131/archive/2010/08/10/1796754.html WPF中新添加了一种事件---路由事件 路由事件与一般事件的区别在于: ...

  7. Tomcat本地服务器搭建

    首先,下载jdk-8u111-windows-x64.exe,然后配置环境,以安装目录D:\jdk1.8.0_111为例: 新建一个变量: 然后打开path新建两个变量: 最后去控制台敲javac或者 ...

  8. thinkphp 配置多数据库

    1配置文件中配置另一数据库连接信息 例如: 'TestModelConfig' => array( //'配置项'=>'配置值' 'DB_TYPE' => 'mysql', // 数 ...

  9. 【JavaEE企业应用实战学习记录】sessionListener

    package sanglp.servlet; import javax.servlet.ServletContext; import javax.servlet.annotation.WebList ...

  10. Android下常见动画

    摘要:Android中常见的的动画有三种:属性动画.补间动画.帧动画. 注.因为前两种内容较多,后补 一.属性动画 二.补间动画 三.帧动画:本质是将一些连贯的图片加载形成连贯的动画效果 1.在Dra ...