传送门

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. 让 innerHTML 进来的 script 代码跑起来

    今天来简单聊聊如何让 innerHTML 进来的 scrip 代码跑起来的问题. 前台请求一个接口,接口返回一些 HTML 标签拼接成的字符串,以供前端直接 innerHTML 生成 DOM 元素,这 ...

  2. 一个看似很简单的SQL却难倒了很多人

    一个选课表,有学生id,课程id,老师id,要求选出同时选了语文和数学的学生 USE [tempschool] GO /****** 对象: Table [dbo].[SelectC] 脚本日期: 0 ...

  3. Linux下 RabbitMQ的安装与配置-3

    一  Erlang安装 1.RabbitMQ是基于Erlang的,所以首先必须配置Erlang环境. 从Erlang的官网http://www.erlang.org/download.html 下载最 ...

  4. Pearson(皮尔逊)相关系数及MATLAB实现

    转自:http://blog.csdn.net/wsywl/article/details/5727327 由于使用的统计相关系数比较频繁,所以这里就利用几篇文章简单介绍一下这些系数. 相关系数:考察 ...

  5. Spring3+Mybatis3+Mysql+ivy+liquibase

    Spring3+Mybatis3+Mysql+ivy+liquibase 集成 近一周时间所学技术:整合Spring+MyBatis+MySql+ivy+liquibase Mybatis:是一个基于 ...

  6. android开发------初识Activity

    之前我们简单说过,Activity实际上是一个窗体,用来存放我们的程序外观. 我们先来创建一个空的Activity,不加载任何layout.要做的是,定义自己的类,继承android的Activity ...

  7. 网站移植到linux上后常犯的错误

    常犯的错误 1:gcc库没装或者没装全 表现:没有可用的C编译器 同类错误:提示g++ not found, 解决:出现以上错误,则是因为gcc编译器没装,或者是没装全. 挂载光盘,到Pakeges里 ...

  8. SpringMVC学习--校验

    简介 项目中,通常使用较多是前端的校验,比如页面中js校验.对于安全要求较高点建议在服务端进行校验. 服务端校验: 控制层conroller:校验页面请求的参数的合法性.在服务端控制层conrolle ...

  9. extjs的一些简单动画1

    Ext.Element 类也定义了部分动画函数.我们先来看看Ext.Fx 类中的重要方法. 1.slideIn ( [String anchor], [Object options] ): 功能:滑入 ...

  10. 编写实现连接oracle数据库并返回Connection对象的Java工具类

    只需要实现一个功能,所以只写一个方法,为了方便调用,设为静态方法 package com.jv; import java.sql.Connection; import java.sql.DriverM ...