传送门

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:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 const int N(1e5+5);
 5 typedef pair<int,int> P;
 6
 7 vector<P> f[N];
 8 unordered_map<int,long long> cnt;
 9 int T, n, q, cs;
10
11 int main(){
12     for(cin>>T; T--; ){
13         cin>>n;
14         for(int i=1, gcd, pos; i<=n; i++){
15             scanf("%d", &gcd), pos=i, f[i].clear();
16             for(auto x:f[i-1]){
17                 if(__gcd(gcd, x.second)!=gcd) f[i].push_back({pos, gcd});
18                 gcd=__gcd(gcd, x.second), pos=x.first;
19             }
20             f[i].push_back({pos, gcd});
21         }
22         cnt.clear();
23         for(int i=1; i<=n; i++){
24             int pos=i+1;
25             for(auto x:f[i])
26                 cnt[x.second]+=pos-x.first, pos=x.first;
27         }
28         cin>>q;
29         printf("Case #%d:\n", ++cs);
30         for(int l, r; q--; ){
31             scanf("%d%d", &l, &r);
32             int gcd=lower_bound(f[r].begin(), f[r].end(), P(l, INT_MAX), greater<P>())->second;
33             printf("%d %lld\n", gcd, cnt[gcd]);
34         }
35     }
36 }

转载于:https://www.cnblogs.com/Patt/p/5742814.html

HDU 5726 GCD相关推荐

  1. HDU 2588 GCD 【Euler + 暴力技巧】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2588 GCD Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  2. 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)

    先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...

  3. HDU 2588 GCD(欧拉函数)

    GCD 思路 题目要求,对于给定的n,mn, mn,m要求有多少数∑i=1ngcd(i,n)>=m\sum _{i = 1} ^{n} gcd(i, n) >= m∑i=1n​gcd(i, ...

  4. HDU - 4497 GCD and LCM 数论gcd

    传送门 文章目录 题意: 思路: 题意: 给三个数的lcmlcmlcm和gcdgcdgcd,求满足条件的三元组组合个数. 思路: 首先lcmmodgcd==0lcm\bmod gcd==0lcmmod ...

  5. HDU - 4497 GCD and LCM

    题意:给出三个数的gcd,lcm,求这三个数的全部的可能 思路 :设x,y,z的gcd为d,那么设x=d*a,y=d*b,z=d*c.a,b.c肯定是互质的.那么lcm=d*a*b*c,所以我们能够得 ...

  6. hdu 5584 gcd/lcm/数学公式

    input T 1<=T<=1000 x y output 有多少个起点可以走n(n>=0)步走到(x,y),只能从(x,y)走到(x,y+lcm(x,y))/(x+lcm(x,y) ...

  7. HDU 2588 GCD amp;amp; GCD问题总结

    GCD(一) 题目: The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written ( ...

  8. HDU 2588 GCD

    题目梗概 T组数据,每组数据输入n,m,问有多少个x满足1<=X<=N 和 gcd (X,N)>=M 思考 我们假设S= gcd(X,N) 那么一定存在a,b使得X=Sa N=Sb ...

  9. hdu 4497 GCD and LCM

    题目意思: 给你三个数的最大公约数G,最小公倍数L,求这样的三位数有多少个. 解: 1   首先如果 L%G!=0  那很明显无解  输出0 2  如果L%G==0  然后 令L=L/G  ,G=1 ...

  10. 2018 Arab Collegiate Programming Contest (ACPC 2018) G. Greatest Chicken Dish (线段树+GCD)

    题目链接:https://codeforces.com/gym/101991/problem/G 题意:给出 n 个数,q 次询问区间[ li,ri ]之间有多少个 GCD = di 的连续子区间. ...

最新文章

  1. 如何快速学好Shell脚本?
  2. 207-Course Schedule
  3. Window.document对象
  4. NCC Meetup 2018 Shanghai 活动小结
  5. Python3 爬虫实战 — 安居客武汉二手房【requests、Beautiful Soup、CSV】
  6. lvs-nat负载均衡实验
  7. java报错信息怎么看_AE-E3D插件无效或提示OPENGL E3D Debug等错误报错信息怎么办?...
  8. Android重写View并且自定义属性(二)
  9. Gvim的基本命令:CTR-C =进入命令行
  10. std在汇编语言是什么指令_汇编语言程序指令整理
  11. 砍掉中国90%的科研人员,对科技发展的影响微乎其微,某教授酒后真言!
  12. Android开源项目及库汇总
  13. 美国大学课堂的契约精神(纽约时报中文网 )
  14. itunes备份是整个手机备份吗_如何使用iTunes备份手机资料
  15. css和html实现梦幻西游页面特效
  16. 安卓投屏助手(B1358)之辅助调试
  17. 2011-07-13 wince上面plg插件生成
  18. 基于微信小程序的学院通知与文件分享系统app设计与实现-计算机毕业设计源码+LW文档
  19. android读取带公章的pdf文件,APP中如何显示带电子签名的PDF文件
  20. python量化金融下单接口特点

热门文章

  1. 广度(宽度)优先搜索思路总结
  2. STM8L051F3基础功能:内部时钟;TIM2定时器;串口及printf;
  3. MySQL查询当天、本周,本月,上一个月的数据
  4. 【转】Java 杂谈(三)
  5. dubbo学习(八)dubbo项目搭建--消费者(服务消费者)
  6. WPFのclipToBounds与maskToBounds的区别
  7. 如何给main传参数
  8. (一)UI设计的一些常识
  9. 自制的JavaScript NodeTree导航栏,纯练手,附源码
  10. (转)华为面试题算什么,这个背会了外企随便进