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, ...
随机推荐
- java中接口可以继承接口
今天阅读别人的代码才发现,接口是可以继承接口的 一个类只能extends一个父类,但可以implements多个接口. 一个接口则可以同时extends多个接口,却不能implements任何接口. ...
- 14 - springboot的@Configuration、@Bean、@Import()、@ImportResource()、@Conditional说明
1.@Configuration.@Bean.@Import().@ImportResource().@Conditional 分析源码的时候总会见到标题中的这几个注解,因此:弄一篇博客来说明一下吧, ...
- 学习java 7.10
学习内容: List 集合:有序集合,用户可以精确控制列表中每个元素的插入位置 List 集合特点:有序:存储和取出的元素顺序一致 可重复:存储的元素可以重复 增强for循环:简化数组和 Collec ...
- Erda 系列 Meetup「成都站」携手SOFAStack 和你聊聊云原生基础设施建设那点事儿
技术控快上车啦秋天的第一场活动一起来收获技术干货吧! 主题: 云原生基础设施建设的现在及未来时间: 2021 年 9 月 11 日 (周六) 13:30-17:00活动地点: 四川省成都市蚂蚁 C 空 ...
- 23. 关于Ubuntu中Could not get lock /var/lib/dpkg/lock解决方案
原文:https://blog.csdn.net/u011596455/article/details/60322568 版权声明:本文为博主原创文章,转载请附上博文链接! 在Ubuntu中,有时候运 ...
- Hive(五)【DQL数据查询】
目录 一. 基本查询 1.1 算数运算符 1.2 常用聚合函数 1.3 limit 1.4 where 1.5 比较运算符(between|in|is null) 1.6 LIKE和RLIKE 1.7 ...
- Vue相关,vue父子组件生命周期执行顺序。
一.实例代码 父组件: <template> <div id="parent"> <child></child> </div& ...
- Oracle中的job(定时任务)
oracle中的job(定时任务)由dbms_job包下的函数实现.关于job的理论知识可参考https://blog.csdn.net/apextrace/article/details/77675 ...
- 【C/C++】PAT A1025 Ranking/算法笔记
题目意思大概是输入一堆人的学号,成绩,给出学号,总排名,考场号,考场内排名. 这是我第一次写的: #include <iostream> #include <algorithm> ...
- 第三届“传智杯”全国大学生IT技能大赛(初赛A组)题解
留念 C - 志愿者 排序..按照题目规则说的排就可以.wa了两发我太菜了qwq #include<bits/stdc++.h> using namespace std; const in ...