GCD (hdu 5726)
GCD
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1497 Accepted Submission(s): 483
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.
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).
5
1 2 4 6 7
4
1 5
2 4
3 4
4 4
1 8
2 4
2 4
6 1
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<queue>
7 #include<map>
8 #include<math.h>
9 using namespace std;
10 typedef long long LL;
11 int RMQ[20][100005];
12 int ans[100005];
13 map<int,LL>my;
14 int gcd(int n,int m)
15 {
16 if(m==0)
17 return n;
18 else if(n%m==0)
19 {
20 return m;
21 }
22 else return gcd(m,n%m);
23 }
24 int main(void)
25 {
26 int i,j,k;
27 int n,m;
28 scanf("%d",&k);
29 int ca=0;
30 while(k--)
31 {
32 my.clear();
33 scanf("%d",&n);
34 for(i=1; i<=n; i++)
35 {
36 scanf("%d",&ans[i]);
37 }
38 for(i=1; i<=n; i++)
39 {
40 RMQ[0][i]=ans[i];
41 }
42 scanf("%d",&m);
43 for(i=1; i<20; i++)
44 {
45 for(j=1; j<=n; j++)
46 {
47 if(j+(1<<i)-1<=n)
48 {
49 RMQ[i][j]=gcd(RMQ[i-1][j],RMQ[i-1][j+(1<<(i-1))]);
50 }
51 }
52 }
53 for(i=1; i<=n; i++)
54 {
55 for(j=i; j<=n;)
56 {
57 int t=log2(j-i+1);
58 int ac=gcd(RMQ[t][i],RMQ[t][j-(1<<t)+1]);
59 int l=j;
60 int r=n;
61 int id=0;
62 while(l<=r)
63 {
64 int mid=(l+r)/2;
65 int c=log2(mid-i+1);
66 int ak=gcd(RMQ[c][i],RMQ[c][mid-(1<<c)+1]);
67 if(ak>=ac)
68 {
69 id=mid;
70 l=mid+1;
71 }
72 else r=mid-1;
73 }
74 my[ac]+=id-j+1;
75 j=id+1;
76 }
77 }
78 printf("Case #%d:\n",++ca);
79 while(m--)
80 {
81 int x,y;
82 scanf("%d %d",&x,&y);
83 if(x>y)
84 {
85 swap(x,y);
86 }
87 int ct=log2(y-x+1);
88 int acc=gcd(RMQ[ct][x],RMQ[ct][y-(1<<ct)+1]);
89 printf("%d %lld\n",acc,my[acc]);
90 }
91 }
92 return 0;
93 }
GCD (hdu 5726)的更多相关文章
- HDU 5726 GCD 区间GCD=k的个数
GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- D - GCD HDU - 1695 -模板-莫比乌斯容斥
D - GCD HDU - 1695 思路: 都 除以 k 后转化为 1-b/k 1-d/k中找互质的对数,但是需要去重一下 (x,y) (y,x) 这种情况. 这种情况出现 x ,y ...
- HDU 5726 GCD (RMQ + 二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 给你n个数,q个询问,每个询问问你有多少对l r的gcd(a[l] , ... , a[r]) ...
- HDU 5726 GCD(DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5726 [题目大意] 给出数列An,对于询问的区间[L,R],求出区间内数的GCD值,并且求出GCD ...
- HDU 5726 GCD(RMQ+二分)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5726 题意:给出一串数字,现在有多次询问,每次询问输出(l,r)范围内所有数的gcd值,并且输出有多 ...
- hdu 5726 GCD 暴力倍增rmq
GCD/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence ...
- HDU 5726 GCD
传送门 GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem ...
- HDU 5726 GCD (2016 Multi-University Training Contest 1)
Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description Give y ...
- hdu 5726 GCD 倍增+ 二分
题目链接 给n个数, 定义一个运算f[l,r] = gcd(al, al+1,....ar). 然后给你m个询问, 每次询问给出l, r. 求出f[l, r]的值以及有多少对l', r' 使得f[l, ...
随机推荐
- PDFium 渲染
PDFium 是 Chromium 的 PDF 渲染引擎,许可协议为 BSD 3-Clause.不同于 Mozilla 基于 HTML5 的 PDF.js,PDFium 是基于 Foxit Softw ...
- SpringBoot整合Shiro 三:整合Mybatis
搭建环境见: SpringBoot整合Shiro 一:搭建环境 shiro配置类见: SpringBoot整合Shiro 二:Shiro配置类 整合Mybatis 添加Maven依赖 mysql.dr ...
- vim文本编辑器的基本使用
vim文本编辑器的基本使用 1. vi和vim的区别和联系 可以说vim是vi的增强版,在使用vim编辑文本时,可以根据字体颜色来判断编写程序的正确性. 2. vim文本编辑器的常用命令 1. 编辑指 ...
- 学习java的第二十五天
一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...
- 日常Java 2021/9/21
将Java数组中的元素前后反转.题目要求:已知一个数组arr = {11,12,13,14,15}用程序实现把该数组中的元素值交换,交换后的数组arr = { 15,14,13,12,11},并输出交 ...
- 【leetcode】834. Sum of Distances in Tree(图算法)
There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. You are ...
- 100个Shell脚本——【脚本2】截取字符串
[脚本2]截取字符串 一.脚本 现有一个字符串如下: http://www.aaa.com/root/123.htm 请根据以下要求截取出字符串中的字符: 1.取出www.aaa.com/root/1 ...
- 【Git项目管理】Git分支 - 远程分支
远程分支 远程引用是对远程仓库的引用(指针),包括分支.标签等等. 你可以通过 git ls-remote (remote) 来显式地获得远程引用的完整列表,或者通过 git remote show ...
- ORACLE 本session产生的redo
select * from v$statname a ,v$mystat bwhere a.STATISTIC# = b.STATISTIC# and a.name = 'redo size';
- [学习总结]5、Android的ViewGroup中事件的传递机制(二)
下面是第一篇的连接 Android的ViewGroup中事件的传递机制(一) 关于onInterceptTouchEvent和onTouchEvent的详细解释. 1 public class Mai ...