hdu 5726 GCD 暴力倍增rmq
GCD/center>
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5726
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
Hint
题意
给你n个数,Q次询问,问你(l,r)区间的gcd是多少,然后再问你整个序列中,有多少子串的gcd和询问的GCD是相同的。
题解:
线段树TLE了,应该是我们写丑了……
然后改成了倍增RMQ才过的。
考虑gcd这个东西,枚举起点后,他最多log(1e9)种可能,所以我们直接枚举起点,然后暴力二分到每个gcd的区间,然后直接算这个gcd的贡献。
那么我们的询问就都可以O(1)回答了。
代码
#include <bits/stdc++.h>
#define rep(a,b,c) for(int (a)=(b);(a)<=(c);++(a))
#define drep(a,b,c) for(int (a)=(b);(a)>=(c);--(a))
#define pb push_back
#define mp make_pair
#define sf scanf
#define pf printf
#define two(x) (1<<(x))
#define clr(x,y) memset((x),(y),sizeof((x)))
#define dbg(x) cout << #x << "=" << x << endl;
const int mod = 1e9 + 7;
int mul(int x,int y){return 1LL*x*y%mod;}
int qpow(int x , int y){int res=1;while(y){if(y&1) res=mul(res,x) ; y>>=1 ; x=mul(x,x);} return res;}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 15;
int N,a[maxn],b[maxn][18],mm[maxn];
map < int , long long > ha;
void initrmp(int n)
{
mm[0]=-1;
for(int i=1;i<=n;i++){
mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
}
}
int query(int l,int r){
int k = mm[r-l+1];
return __gcd(b[l][k],b[r-(1<<k)+1][k]);
}
int main(int argc,char *argv[]){
int T=read(),cas=0;
while(T--){
ha.clear();
N=read();
initrmp(N);
rep(i,1,N){
a[i]=read();
b[i][0]=a[i];
}
rep(j,1,17) for(int i = 1 ; i + ( 1 << j ) - 1 <= N ; ++ i) b[i][j]=__gcd( b[i][j-1] , b[i + two(j-1)][j-1] );
rep(i,1,N){
int cur = i , gc = a[i];
while( cur <= N ){
int l = cur , r = N;
while( l < r ){
int mid = l + r + 1 >> 1;
if(query(i,mid)==gc) l = mid ;
else r = mid - 1;
}
if(ha.count(gc)) ha[gc] +=(l-cur+1);
else ha[gc]=(l-cur+1);
cur = l + 1 , gc = __gcd( gc , a[l + 1] );
}
}
int Q=read();
pf("Case #%d:\n",++cas);
while(Q--){
int l = read(),r=read(),gc=query(l,r);
pf("%d",gc);
if(ha.count(gc)) pf(" %I64d\n",ha[gc]);
else pf(" 0\n");
}
}
return 0;
}
hdu 5726 GCD 暴力倍增rmq的更多相关文章
- HDU 5726 GCD(ST&RMQ)
题目链接 GCD 先ST倍增预处理,f[i][j]表示从i开始(包含第i个数)的连续2^j个数的最大公约数. 这样就可以在O(1)内询问得到a[l]到a[r]之间的所有数的最大公约数的值. 然后对于每 ...
- HDU 5726 GCD 区间GCD=k的个数
GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- 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(RMQ+二分)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5726 题意:给出一串数字,现在有多次询问,每次询问输出(l,r)范围内所有数的gcd值,并且输出有多 ...
- hdu 5726 GCD 倍增+ 二分
题目链接 给n个数, 定义一个运算f[l,r] = gcd(al, al+1,....ar). 然后给你m个询问, 每次询问给出l, r. 求出f[l, r]的值以及有多少对l', r' 使得f[l, ...
- HDU 5726 GCD(DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5726 [题目大意] 给出数列An,对于询问的区间[L,R],求出区间内数的GCD值,并且求出GCD ...
- 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 (2016多校、二分、ST表处理区间GCD、数学)
题目链接 题意 : 给出一个有 N 个数字的整数数列.给出 Q 个问询.每次问询给出一个区间.用 ( L.R ) 表示.要你统计这个整数数列所有的子区间中有多少个和 GCD( L ~ R ) 相等.输 ...
- HDU 5726 GCD
传送门 GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem ...
随机推荐
- [转载]WIN7已经记住访问另一台电脑的账号和密码 凭证
http://jingyan.baidu.com/article/a3aad71aaa32eeb1fb0096c8.html
- Angular 下的 directive (part 1)
directive 指令 Directive components 指令部分 使用指令自动引导一个AngularJS应用.ngApp指令指定应用程序的根元素,通常是放在页面的根元素如: < ...
- swift3.0之后的Error处理
在之前的版本中,Swift中Error与OC中NSError没有关系.但是现在两者可以互相强转. 我们看看两者的区别:Error是一个实现Error协议的枚举或者结构体,对外能够获取的具体信息只有ra ...
- 01:MFC应用程序编程
一 MFC的发展 VC 1.0->VC 5.0->VC 6.0->VC2008 SP1)->VS2010 二 MFC基础 1 MFC 微软基础类库 采用类的方式,将Win32 ...
- 第10月第20天 afnetwork like MKNetworkEngine http post
1. + (AFHTTPRequestOperation *)requestSellerWithCompletion:(requestFinishedCompletionBlock)successBl ...
- 在线建立或重做mysql主从复制架构方法(传统模式和GTID模式)【转】
mysql主从复制架构,是mysql数据库主要特色之一,绝大多数公司都有用到. 而GTID模式是基于事务的复制模式的意思,发展到现在也是越来越多人用. 以前很多文章,介绍搭建mysql主从复制架构,是 ...
- django和flask关于oralce数据库配置
Django中关于Oracle数据库配置 # 使用SERVICE_NAME DATABASES = { 'default': { 'ENGINE': 'django.db.backends.oracl ...
- SOA并不能解决高并发事务
传统SOA架构其实无法面对高并发事务. 这种方式不适合热点资源,也就是高并发场合. 虽然乐观锁短,但是容易产生脏数据. SOA是以服务这个方式对外提供功能,我们很显然喜欢在Service中加上JTA等 ...
- AndroidStudio3.0以上版本的坑
原文:https://blog.csdn.net/ytfunnysite/article/details/78864556 1.Error:Failed to resolve: com.android ...
- 数学之美——HMM模型(一)介绍
一直想写点关于数学方面的blog,这对于数据挖掘分析,NLP处理等都有着比较重要的作用,之前在CSDN上想写点HMM方面的文章,一直没写成,最近几天终于抽点时间完成了HMM的文章,加以整理,遂有这个系 ...