GCD

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

Submit Status

Description

Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). There are Q(Q≤100,000) queries. For each query l,r you have to calculate gcd(al,,al+1,...,ar) and count the number of pairs(l′,r′)(1≤l<r≤N)such that gcd(al′,al′+1,...,ar′) equal gcd(al,al+1,...,ar).

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, a1,...,an(0<ai≤1000,000,000).

The third line contains a number Q, denoting the number of queries.

For the next Q lines, i-th line contains two number , stand for the li,ri, stand for the i-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 gcd(al,al+1,...,ar) and the second number stands for the number of pairs(l′,r′) such that gcd(al′,al′+1,...,ar′) equal gcd(al,al+1,...,ar).

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
 1 //2016.8.9
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<map>
 5 #include<algorithm>
 6 #include<cmath>
 7
 8 using namespace std;
 9
10 typedef long long ll;
11 const int N = 100005;
12 int dp[N][20];//d[i][j]表示从第i个数字开始向后2^j个数字这段区间内的gcd,具有递减性
13 map<int, ll> mp;
14
15 void init_rmq(int n)//初始化dp,求出每段区间的gcd
16 {
17     for(int j = 1; j < (int)log2(n)+1; j++)
18       for(int i = 1; i <= n; i++)
19       {
20           if(i+(1<<j)-1 <= n)
21             dp[i][j] = __gcd(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
22       }
23 }
24
25 int rmq(int l, int r)//查询
26 {
27     int k = (int)log2(r-l+1);
28     return __gcd(dp[l][k], dp[r-(1<<k)+1][k]);
29 }
30
31 int main()
32 {
33     int n, q, l, r, T, kase = 0;
34     cin>>T;
35     while(T--)
36     {
37         printf("Case #%d:\n", ++kase);
38         cin>>n;
39         mp.clear();
40         for(int i = 1; i <= n; i++)
41           scanf("%d", &dp[i][0]);
42         init_rmq(n);
43
44 //利用二分求具有相同gcd区间的数目
45 //-----------------------------------------------------------------------------------
46         for(int i = 1; i <= n; i++)
47         {
48             int a = i, b = n, mid, tmp, vs;
49             while(1)
50             {
51                 tmp = a;
52                 vs = rmq(i, a);
53                 while(a <= b)
54                 {
55                     mid = (a+b)>>1;
56                     if(rmq(i, mid)<vs) b = mid-1;
57                     else a = mid+1;
58                 }
59                 mp[vs]+=1ll*(b-tmp+1);
60                 b = n;
61                 if(a>b)break;
62             }
63         }
64 //------------------------------------------------------------------------------------
65
66         cin>>q;
67         while(q--)
68         {
69             scanf("%d%d", &l, &r);
70             int ans = rmq(l, r);
71             cout<<ans<<" "<<mp[ans]<<endl;
72         }
73     }
74
75     return 0;
76 }

转载于:https://www.cnblogs.com/Penn000/p/5754843.html

HDU5726(RMQ二分)相关推荐

  1. 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分

    原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...

  2. 【HDU - 5875】Function(线段树,区间第一个小于某个数的数 或 RMQ二分)

    题干: The shorter, the simpler. With this problem, you should be convinced of this truth.        You a ...

  3. HDU5726 GCD(rmq+二分)

    这道题是2016第一场多校的1004,趁机优化了一发 题意:10W长度的数组,10W个询问,让你回答任意两点间的gcd和全局相同gcd的数量 思路:用由于gcd一段固定以后具有单调性的,用rmq nl ...

  4. 2016 Multi-University Training Contest 1 GCD【RMQ+二分】

    因为那时候没怎么补所以就分到了未搞分组里!!!然后因为标题如此之屌吧= =点击量很高,然后写的是无思路,23333,估计看题人真的是觉得博主就是个撒缺.废话不多说了,补题... update2016/ ...

  5. poj 2452(RMQ+二分)

    解题思路:这题实际上就是求某区间上的最值问题,可以先枚举区间的起始位置,然后二分去搜索比起始位置大的数且位置最远(这里可以用RMQ算法区间内的最小值),找到之后再利用RMQ算法找这段区间内的最大的,如 ...

  6. YbtOJ-相似子串【SA,RMQ,二分】

    正题 题目大意 给出一个长度为nnn的字符串,两个串相似当且仅当可以通过每种字符置换使得它们相同. qqq次询问这个字符串所有子串中和这个串中sl,rs_{l,r}sl,r​子串有多少个相似的. 1≤ ...

  7. jzoj4817-square【区间RMQ,二分答案】

    正题 题目链接:https://jzoj.net/senior/#contest/show/2956/2 题目大意 n∗mn*mn∗m的010101矩阵,每次询问(x1,y1,x2,y2)(x1,y1 ...

  8. Codeforces - tag::data structures 大合集 [占坑 25 / 0x3f3f3f3f]

    371D 小盘子不断嵌套与大盘子,最后与地面相连,往里面灌水,溢出部分会往下面流,求每次操作时当前的盘子的容量 其实这道题是期末考前就做好了的.. 链式结构考虑并查集,然后没了(求大佬解释第一个T的点 ...

  9. 应该是最全的算法学习路线了吧法学习路线了吧

    写在前面: 因为博主的技术有限,算法也是很难得一门科学,我只能给出尽量全的分类,然后尽可能的讲解. 但是有些算法超出了本人的能力范畴,说实在的就是我太菜了.所以大家就当本文是目录型的文章即可. 本人是 ...

最新文章

  1. 如何在windows xp下使用ntfs权限控制
  2. FZU-Problem 2191 完美的数字
  3. 大数据之Linux早课9.21
  4. Android 学习指南(2017版)
  5. 使用Introspector(Java内省机制)实现Map转换为JavaBean
  6. 创建字符串枚举的最好方法
  7. HTML+CSS+JS实现 ❤️canvas 3D云动画效果❤️
  8. android xml黑体字_如何在 Android 上使用思源黑体作为系统字体?
  9. 容器编排技术 -- 本地运行Kubrenetes v1.0
  10. opensource项目_最佳Opensource.com:政府
  11. PixelFormat 枚举
  12. 2019-09-30
  13. 【开源微信】Java实现基于Redis公众号模板消息队列
  14. Python学习第二章:变量和简单类型
  15. 写给小白的网站优化初步全过程
  16. 【操作系统 · 磁盘】磁盘调度
  17. python基础坑点
  18. python制作好看的界面_python漂亮界面
  19. 工业自动化控制-组态王2
  20. Python视频制作 MoviePy框架afx音频效果示例

热门文章

  1. 几何畸变的类型_第七讲 几何纠正(摄影测量与遥感).pdf
  2. Keras深度学习实战(3)——神经网络性能优化技术详解
  3. python中字符串模块_Python字符串模块
  4. mockito无效_Mockito模拟无效方法
  5. Python将字符串转换为列表
  6. 2019交通顶级期刊_2019年顶级11面试准备书
  7. 每个人都应该知道的Android Studio快捷方式
  8. Java StringBuilder
  9. linux压缩命令gzip_Linux gzip命令示例
  10. Akka相关术语 译《fifteen》