hud 5726 GCD st表+二分
来源:互联网 发布:淘宝客seo 编辑:程序博客网 时间:2024/06/05 05:37
题目:
GCD
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3531 Accepted Submission(s): 1265
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 | We have carefully selected several similar problems for you: 6022 6021 6020 6019 6018
给定n≤10^5个数,|Ai|≤10^9,以及Q ≤ 10^5次询问
定义gcd(l, r)=gcd(al, al+1, ⋯, ar)
对于每次询问输出gcd(l, r),以及整个区间内有多少个区间的gcd(l’, r’)为这个值。
分析:
gcd(l, r)不难求 由于左端点固定时区间gcd也具有单调性(单调不增,而且每次减小至少衰减为原来的1/2),可以用st表预处理一下,然后以O(1)的时间复杂度进行查询
关键是第二个问题(对于我而言略难..二分学得不扎实...过程想了好一会儿才想清楚) 整个区间内有多少个区间的gcd(l’, r’)为这个值
思路还是预处理 对于从1到n的每一个点,分别以该点为左端点,
可以用map<int,long long>记录预处理结果 first表示每种不同的gcd值,second表示具有该gcd值的区间个数
代码:
Orz http://blog.csdn.net/became_a_wolf/article/details/52529697
代码清晰易懂(尽管讲解有些小的瑕疵) 几乎照搬了....
#include<bits/stdc++.h>using namespace std;int n,m,a[100010];int f[100010][17];map<int,long long> mp;int gcd(int a,int b){ return b?gcd(b,a%b):a; }void rmq(){for(int j=1;j<=n;j++) f[j][0]=a[j];for(int i=1;i<18;i++){for(int j=1;j<=n;j++){if(j+(1<<i)-1 <= n) f[j][i]=gcd(f[j][i-1],f[j+(1<<i-1)][i-1]);}}}int look(int l,int r){int k=(int)log2((double)(r-l+1));return gcd(f[l][k],f[r-(1<<k)+1][k]);}void setTable(){mp.clear();//多组样例 记得每次清空mp for(int i=1;i<=n;++i){//枚举每一个左端点 记录所有以该点为左端点可能出现的区间gcd值 int findnum=f[i][0],j=i;//设置初始(最大)查找值为该点本身值 while(j<=n){int l=j,r=n;while(l<r){//二分寻找最远的右端点使得gcd[l,r]=findnumint mid=(l+r+1)>>1;if(look(i,mid)==findnum) l=mid;else r=mid-1;}mp[findnum]+=l-j+1;j=l+1;findnum=look(i,j);//记录下一个gcd值 }}} int main(){//2480MS15036Kint t,l,r;int cas=1;scanf("%d",&t);while(t--){printf("Case #%d:\n",cas++);scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d",&a[i]);rmq();setTable();scanf("%d",&m);for(int i=0;i<m;i++){scanf("%d%d",&l,&r);int g=look(l,r);printf("%d %I64d\n",g,mp[g]);}}return 0;}
0 0
- hud 5726 GCD st表+二分
- hdu 5726 GCD (二分+ST表)★
- HDOJ 5726 GCD 二分+ST表
- hdu5726 GCD st表 + 二分
- HDU 5726 GCD [ST表+暴力二分]【数据结构|杂类】
- HDU 5726 GCD(ST+二分)
- HDU 5726 GCD 【GCD】【ST表+二分】【线段树+暴力枚举】
- GCD (ST表,二分求区间查询)
- 维护区间gcd(线段树 || ST表+二分)
- HDU 5726 GCD (ST 表 +查询)
- hdu 5726 GCD 【RMQ+st】
- hdu5726 GCD ST表+离线
- HDU 5875 二分+st表
- hdu5726 GCD+ST表 by:lethalboy
- codeforces 475D CGCDSSQ ST表+二分
- codeforces689D Friends and Subsequences 二分+ST表
- codeforces 689D ST表+二分 模板
- HDU5289:Assignment(二分 + ST表)
- 常见面试题整理--数据库篇(每位开发者必备)
- 随机梯度下降
- windows 配置apache+php+mysql环境
- Android问题:ScrollView默认位置不是最顶部最全解决方案
- QT入门学习随笔
- hud 5726 GCD st表+二分
- 三维游戏图形学学习笔记(二)延迟渲染
- 商品展示
- WindowManager.LayoutParams详解
- K最近邻算法
- PX4开发指南中文版维护说明
- 浅谈指针
- YAHOO!军规
- 深度学习相关最新图书推荐