GCD

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2742    Accepted Submission(s): 980

Problem 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. /*
  2. HDU 5726 GCD 区间GCD=k的个数
  3.  
  4. problem:
  5. 给你一列数字,然后是m个询问,每次询问区间[l,r]的gcd以及整个序列中有多少个区间的gcd与之相等
  6.  
  7. solve:
  8. 第一个可以通过线段树或者类rmq的方法来解决.但是求区间的个数就不知怎么弄了- -
  9. 最开始想的是每次询问求出值之后用 二分+枚举右端点 的思路来查找有多少个区间的
  10. 但是发现整个是递减的,感觉很难确定区间的大小,卒.
  11.  
  12. 看别人题解才发现可以预处理,就一个左端点l而言,[l+1,n]中的到l的区间gcd是递减的.
  13. 例:
  14. GCD[l,j] = 4,GCD[l,j+1] = 4,GCD[l,j+2] = 2
  15. 感觉题解相当于枚举以l为左点的所有区间GCD值,然后二分到当前GCD值的最右点,计算出区间的个数
  16.  
  17. 因此会涉及很多次区间GCD查询,用线段树的话超时,用RMQ实现O(1)的查询AC
  18. 至于时间复杂度, 并不会算QAQ
  19.  
  20. hhh-2016-08-15 21:35:11
  21. */
  22. #include <algorithm>
  23. #include <iostream>
  24. #include <cstdlib>
  25. #include <cstdio>
  26. #include <cstring>
  27. #include <map>
  28. #define lson i<<1
  29. #define rson i<<1|1
  30. #define ll long long
  31. #define key_val ch[ch[root][1]][0]
  32. using namespace std;
  33. const int maxn = 100010;
  34. const int inf = 0x3f3f3f3f;
  35. int a[maxn];
  36. int m[maxn];
  37. int dp[maxn][20];
  38.  
  39. ll gcd(ll a,ll b)
  40. {
  41. if(b==0) return a;
  42. else return gcd(b,a%b);
  43. }
  44. void iniRMQ(int n,int c[])
  45. {
  46. m[0] = -1;
  47. for(int i = 1; i <= n; i++)
  48. {
  49. m[i] = ((i&(i-1)) == 0)? m[i-1]+1:m[i-1];
  50. dp[i][0] = c[i];
  51. }
  52. for(int j = 1; j <= m[n]; j++)
  53. {
  54. for(int i = 1; i+(1<<j)-1 <= n; i++)
  55. dp[i][j] = gcd(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
  56. }
  57. }
  58.  
  59. int RMQ(int x,int y)
  60. {
  61. int k = m[y-x+1];
  62. return gcd(dp[x][k],dp[y-(1<<k)+1][k]);
  63. }
  64.  
  65. map<int,ll>mp;
  66.  
  67. void ini(int n)
  68. {
  69. mp.clear();
  70. for(int i = 1;i <= n;i++)
  71. {
  72. int now = a[i],j = i;
  73. while(j <= n)
  74. {
  75. int l = j,r = n;
  76. while(l < r)
  77. {
  78. int mid = (l+r+1) >> 1;
  79. if(RMQ(i,mid) == now)
  80. l = mid;
  81. else
  82. r = mid-1;
  83. }
  84. mp[now] += (ll)(l-j+1);
  85. j = l+1;
  86. now = RMQ(i,j);
  87. }
  88. }
  89. }
  90.  
  91. int main()
  92. {
  93. int T,n,m;
  94. int cas = 1;
  95. // freopen("in.txt","r",stdin);
  96. scanf("%d",&T);
  97. while(T--)
  98. {
  99. printf("Case #%d:\n",cas++);
  100. scanf("%d",&n);
  101. for(int i = 1; i <= n; i++)
  102. {
  103. scanf("%d",&a[i]);
  104. }
  105. iniRMQ(n,a);
  106. ini(n);
  107. scanf("%d",&m);
  108. int a,b;
  109. for(int i =1; i <= m; i++)
  110. {
  111. scanf("%d%d",&a,&b);
  112. int ans1 = RMQ(a,b);
  113. printf("%d %I64d\n",ans1,mp[ans1]);
  114. }
  115. }
  116. return 0;
  117. }

  

HDU 5726 GCD 区间GCD=k的个数的更多相关文章

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

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

  2. hdu 5726 GCD GCD+线段树+区间预处理+map

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

  3. HDU 5726 GCD (RMQ + 二分)

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

  4. HDU 5726 GCD(RMQ+二分)

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

  5. HDU 5726 GCD

    传送门 GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem ...

  6. HDU 5726 GCD(DP)

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

  7. hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)

    CA Loves GCD  Accepts: 64  Submissions: 535  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: 2 ...

  8. HDU-1695 GCD(求一个区间内与一个数互质的个数)

    题意: 给你一个T,是样例的个数,接下来是五个数l1,r1,l2,r2,k  前四个数代表两个区间(l1,r1),(l2,r2)这个题l1=1,l2=1; 取x1属于(1,r1),x2属于(1,r2) ...

  9. GCD (hdu 5726)

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

随机推荐

  1. python自动发邮件

    from email.header import Header from email.mime.text import MIMEText from email.utils import parsead ...

  2. css变化代码

    <!DOCTYPE html><html>    <head>        <meta charset="UTF-8">      ...

  3. Java看书学习笔记

    1.POM:maven ,项目管理工具存放Jar包的文件2.mybatis-generator-core-1.3.2 生成文件 生成语句: java -jar mybatis-generator-co ...

  4. Angular 学习笔记 ( PWA + App Shell )

    PWA (Progressive Web Apps) 是未来网页设计的方向. 渐进式网站. Angular v5 开始支持 pwa 网站 (所谓支持意思是说有一些 build in 的方法和规范去实现 ...

  5. Spring Security 入门(1-3-2)Spring Security - http元素 - intercept-url配置

    http元素下可以配置登录页面,也可以配置 url 拦截. 1.直接配置拦截url和对应的访问权限 <security:http use-expressions="false" ...

  6. Python基础题

    1. 执行Python脚本的两种方式: Chmod +x 脚本 ./脚本(路径的方式) Python 脚本 2. 简述位.字节的关系 一个字节=8位 3. 简述ASCII.unicode.utf-8/ ...

  7. Linux命令-权限

    Linux命令权限   1.新建用户natasha,uid为1000, gid为555, 备注信息为"master" 2.修改natasha用户的家目录为/Natasha 3.查看 ...

  8. pandas.read_csv参数详解

    读取CSV(逗号分割)文件到DataFrame 也支持文件的部分导入和选择迭代 更多帮助参见:http://pandas.pydata.org/pandas-docs/stable/io.html 参 ...

  9. python的错误处理

    一.python的错误处理 在程序运行的过程中,如果发生了错误,可以事先约定返回一个错误代码,这样,就可以知道是否有错以及出错的原因. 在操作系统提供的调用中,返回错误码非常常见.比如打开文件的函数o ...

  10. 浅析Java的Frok/Join框架

    一丶Fork/Join框架产生背景: 随着并发需求的不断提高和硬件的不断发展,程序并行执行仿佛就提上日程上来了,伟大的毛主席就说过:"人多力量大",所以如果一件事可以分配给多个人同 ...