HDU 5726 GCD (rmq+二分 or 线段树 维护区间gcd)
来源:互联网 发布:淘宝店铺全屏轮播海报 编辑:程序博客网 时间:2024/05/29 13:20
GCD
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4325 Accepted Submission(s): 1543
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 numberN , denoting the number of integers.
The second line containsN integers, a1,...,an(0<ai≤1000,000,000) .
The third line contains a numberQ , denoting the number of queries.
For the nextQ lines, i-th line contains two number , stand for the li,ri , stand for the i-th queries.
The first line of each case contains a number
The second line contains
The third line contains a number
For the next
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 forgcd(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) .
For each query, you need to output the two numbers in a line. The first number stands for
Sample Input
151 2 4 6 741 52 43 44 4
Sample Output
Case #1:1 82 42 46 1
Author
HIT
Source
2016 Multi-University Training Contest 1
Recommend
wange2014
思路:首先我们考虑求出一个区间的gcd,这里可以用线段树求出,主要的就是查询符合条件的子区间的个数,这里需要预处理所有子区间了,小于N的子区间的gcd种类数最多为logN个,所以说我们用map存储gcd的个数和种类,以a[i]为右端点进行扫描,右端点不断向后移动,以a[i]为右端点的gcd=gcd(以a[i-1]为右端点的gcd,a[i]),我们通过记录以前一个元素为右端点的gcd,然后扫描就可以了,因为最多会有logn个,这样就可以求出所有子区间的gcd种类和个数,最后查询的时候O(1)的复杂度,总的复杂度O(nlognlogn)。
思路2:一个连续区间的GCD,用倍增法预处理一下,就能做到 O(1)查询
对于相同区间计数,就把询问先离线一下
枚举区间左端点,区间GCD是随右端点递减的,并且是阶梯式的
并且由于GCD递减的很快,这样一个阶梯只有几层,可以当作log的
所以对于每一个GCD,二分右端点,就能求出答案
时间复杂度
总结:
对于 gcd 取模等等,你要知道 随着右端点的右移是单调不减的,所以你枚举左端点, 二分找到跟gcd相同的最大区间, 然后把区间外的第一个加进来, 当做新的gcd, 继续二分, 这样根据gcd最多有log个, 所以会跳的很快。。另外快速求一段连续序列的gcd可以str表做到查询的时候 只有gcd复杂度。
二分+st代码:
#include <iostream>#include <cstring>#include <cstdio>#include <map>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 1e5 + 7;int f[maxn][20], a[maxn], n, m, lg2[maxn];map<int, ll> mp;void RMQ() //区间gcd也可以rmq求解{ for(int i = 1; i <= n; i++) f[i][0] = a[i]; for(int i = 2; i <= n; i++) lg2[i] = lg2[i/2]+1; for(int j = 1; j < 18; j++) { for(int i = 1; i <= n; i++) { if(i+(1<<j)-1 <= n) { f[i][j] = __gcd(f[i][j-1], f[i+(1<<j-1)][j-1]); } } }}int Find(int l, int r){ int k = lg2[r-l+1]; return __gcd(f[l][k], f[r-(1<<k)+1][k]); //区间l,r的gcd}void init(){ mp.clear(); for(int i = 1; i <= n; i++) //枚举起点,找i为起点的区间,从而预处理出所有区间的gcd, { int gcd = f[i][0], s = i; //s是二分区找跟当前区间gcd相同的区间 while(s <= n) { int l = s, r = n, ans; while(l <= r) { int mid = (l+r)>>1; if(Find(i, mid) == gcd) ans = mid, l = mid + 1; //gcd是单调不增的 else r = mid - 1; } mp[gcd] += ans-s+1; //跟i,s gcd相同的区间个数 s = ans+1; //更新区间终点,以及二分的起点, 因为i-ans都跟i-s一样的,可以直接跳过 gcd = Find(i, s); //重新求新的区间的gcd } }}int main(){ int _, l, r, ca = 1; scanf("%d", &_); while(_--) { printf("Case #%d:\n", ca++); scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); RMQ(); init(); scanf("%d", &m); while(m--) { scanf("%d%d", &l, &r); int gcd = Find(l, r); printf("%d %lld\n", gcd, mp[gcd]); } } return 0;}
线段树代码copy的,正好跟我代码风格好像啊
#include<stdio.h> #include<math.h> #include<string.h> #include<stack> #include<set> #include<map> #include<queue> #include<vector> #include<iostream> #include<algorithm> #define MAXN 1010000 #define LL long long #define ll __int64 #define INF 0x7fffffff #define mem(x) memset(x,0,sizeof(x)) #define PI acos(-1) #define eps 1e-8 using namespace std; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;} double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;} //head struct s { int num,l,r; }tree[MAXN*4]; //mp1存储以a[i-1]为右端点的gcd种类及个数,mp2记录以a[i]为右端点的gcd种类及个数,为临时中间变量,cnt,记录gcd种类及个数 map<ll,ll>mp1,mp2,cnt; map<ll,ll>::iterator it; ll a[MAXN]; void build(int i,int L,int R) { tree[i].l=L;tree[i].r=R; if(L==R) { tree[i].num=a[L]; } else { int mid=(L+R)/2; build(i*2,L,mid);build(i*2+1,mid+1,R); tree[i].num=gcd(tree[i*2].num,tree[i*2+1].num); } } ll query(int i,int L,int R) { if(tree[i].l==L&&tree[i].r==R) { return tree[i].num; } if(R<=tree[i*2].r) return query(i*2,L,R); else if(L>=tree[i*2+1].l) return query(i*2+1,L,R); else { int mid=(tree[i].l+tree[i].r)/2; return gcd(query(i*2,L,mid),query(i*2+1,mid+1,R)); } } int main() { int t;scanf("%d",&t); int cas=0; while(t--) { int n;scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%I64d",&a[i]); build(1,1,n);mp1.clear();mp2.clear();cnt.clear(); for(int i=1;i<=n;i++) { cnt[a[i]]++; mp2[a[i]]++; for(it=mp1.begin();it!=mp1.end();it++) { ll k=gcd(a[i],it->first); cnt[k]+=it->second; mp2[k]+=it->second; } mp1.clear(); for(it=mp2.begin();it!=mp2.end();it++) { mp1[it->first]=it->second; } mp2.clear(); } int m;scanf("%d",&m); printf("Case #%d:\n",++cas); while(m--) { int L,R;scanf("%d%d",&L,&R); ll ans=query(1,L,R); printf("%I64d %I64d\n",ans,cnt[ans]); } } return 0; }
阅读全文
0 0
- HDU 5726 GCD (rmq+二分 or 线段树 维护区间gcd)
- HDU 5726 GCD (线段树维护区间gcd)
- 维护区间gcd(线段树 || ST表+二分)
- HDU 5726 GCD(RMQ+二分)(线段树也可)
- hdu 5726 GCD (区间gcd-RMQ)
- hdu 5726 GCD (区间RMQ )
- HDU 5726 GCD(RMQ+二分)
- HDU 5726 GCD(rmq+二分)
- HDU 5726 GCD [RMQ+二分]
- HDU 5726 GCD(RMQ + 二分)
- Hdu-5726 GCD (二分 + RMQ)
- HDU 5726 GCD (RMQ + 二分)
- hdu 5726 GCD rmq 二分
- hdu 5726 区间gcd RMQ+二分 || 暴力枚举
- HDU 5726 求gcd=k的区间的个数 (二分+RMQ)
- hdu 5726 GCD (线段树 + 区间预处理)
- HDU 5726 GCD 区间gcd查询 MAP RMQ 优化
- hdu 5726 gcd rmq 求相同gcd的区间数量
- JavaWeb之Cookie
- sublime text3 安装、配置sftp插件
- Python3 安装 pip3 出现的问题及解决方案
- Linux 下挂载新硬盘方法
- Cloud Foundry 组件的指标(一)—Cloud Controller
- HDU 5726 GCD (rmq+二分 or 线段树 维护区间gcd)
- Java设计模式编程中的状态模式和策略模式
- 大热的机器学习,身为程序员的你该如何开启?
- Ambari——大数据平台的搭建利器(Ambari 的架构和工作原理)
- 第3周项目1(1)- 顺序表的基本运算
- CAShapeLayer的属性 path, 配合上 UIBezierPath(贝塞尔曲线)使用
- OPENGL常用坐标系介绍
- yum源的超级简单配置
- 渐变测试