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 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 

题意:

有N个数,求第L个数到第R个数的最大公约数,并且求出任意区间内最大公约数为ans的数量。

题解:

对于区间[L,R],如果L固定不变,R不断右移时,gcd的值在不断下降,而且每次下降的幅度都不小于一半。我们只要枚举左端点L,然后二分L到N的区间,找到最大公约数为gcd的最大区间,记录这个gcd的数量并更新到map中即可。

#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
const int MAX=;
int dp[MAX][];
int mm[MAX];
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} void initrmq(int n,int b[])
{
mm[]=-;
for(int i=;i<=n;i++)
{
mm[i]=((i&(i-))==)?mm[i-]+:mm[i-];
dp[i][]=b[i];
}
for(int j=;j<=mm[n];j++)
for(int i=;i+(<<j)-<=n;i++)
dp[i][j]=gcd(dp[i][j-],dp[i+(<<(j-))][j-]);
}
ll rmq(int x,int y)
{
int k=mm[y-x+];
return gcd(dp[x][k],dp[y-(<<k)+][k]);
} ll find(int l,int r)
{
int k=(int)log2((double)(r-l+));
return gcd(dp[l][k], dp[r-(<<k)+][k]);
} map<int,long long>ma;
int main()
{
ios::sync_with_stdio(false);
int T,n,ca=,i,j;
cin>>T;
while(T--)
{
ma.clear();
int b[MAX];
cin>>n;
for(i=;i<=n;i++)
cin>>b[i]; initrmq(n,b);
for(i=;i<=n;i++)
{
int I=i;
ll now=b[i];
while(I!=n+)
{
int preI=I,N=n;
while(I!=N)
{
int mid=(I+N)/+;
if(find(I,mid)==now)
I=mid;
else N=mid-;
}
ma[now]+=N-preI+1ll;
I++;
if(I!=n+)
now=gcd(now,b[I]);
}
}
cout<<"Case #"<<ca++<<":"<<endl;
int q;
cin>>q;
for(i=;i<=q;i++)
{
int l,r;
cin>>l>>r;
ll ans=rmq(l,r);
cout<<ans<<" "<<ma[ans]<<endl;
}
}
return ;
}

【HDOJ 5726】GCD(RMQ+二分)的更多相关文章

  1. HDU 5726 GCD (RMQ + 二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 给你n个数,q个询问,每个询问问你有多少对l r的gcd(a[l] , ... , a[r]) ...

  2. 2016 Multi-University Training Contest 1 GCD RMQ+二分(预处理)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 题意:有N(N <= 100,000),之后有Q(Q <= 100,000)个区间查询[ ...

  3. GCD (RMQ + 二分)

    RMQ存的是区间GCD,然后遍历 i: 1->n, 然后不断地对[i, R]区间进行二分求以i为起点的相同gcd的区间范围,慢慢缩减区间. #include<bits/stdc++.h&g ...

  4. hdu 5726 GCD 倍增+ 二分

    题目链接 给n个数, 定义一个运算f[l,r] = gcd(al, al+1,....ar). 然后给你m个询问, 每次询问给出l, r. 求出f[l, r]的值以及有多少对l', r' 使得f[l, ...

  5. HDU 5726 GCD (2016多校、二分、ST表处理区间GCD、数学)

    题目链接 题意 : 给出一个有 N 个数字的整数数列.给出 Q 个问询.每次问询给出一个区间.用 ( L.R ) 表示.要你统计这个整数数列所有的子区间中有多少个和 GCD( L ~ R ) 相等.输 ...

  6. HDU 5726 GCD 区间GCD=k的个数

    GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  7. *HDU3486 RMQ+二分

    Interviewe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  8. hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 ...

  9. hdu 3486 Interviewe (RMQ+二分)

    Interviewe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  10. 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分

    原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...

随机推荐

  1. 一周一个小demo — vue.js实现备忘录功能

    这个vue实现备忘录的功能demo是K在github上找到的,K觉得这是一个用来对vue.js入门的一个非常简单的demo,所以拿在这里共享一下. (尊重他人劳动成果,从小事做起~  demo原git ...

  2. 29_Future模式2_JDK内置实现

    [Future使用场景] Future表示一个可能未完成的一部任务的结果,针对这个结果可以添加CallBack,以便在任务执行成功或失败后作出相应的操作. Future模式非常适合在处理耗时很长的业务 ...

  3. Android Timer和TimerTask

    以下内容根据 The JavaTM Tutorial 和相关API doc翻译整理,以供日后参考: 1.概览 Timer是一种定时器工具,用来在一个后台线程计划执行指定任务.它可以计划执行一个任务一次 ...

  4. PHP中empty、isset和is_null的使用区别

    关于PHP中empty().isset() 和 is_null() 这三个函数的区别,之前记得专门总结过,上次又被问到,网上已经很多,就用几个例子来说明: 测试用例选取: <?php $a;$b ...

  5. 使用qt帮助 查看样式表stylesheet的帮助文档

    QCreactor帮助文档中搜索的关键字 Qt Style Sheets Examples        有所有控件的样式例子 Qt Style Sheets Reference      控件的所有 ...

  6. restful知识点之一CBV

    urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^FBVTEST/', views.FBV_Test.as_view()), ] url ...

  7. SQL点点滴滴_查询类型和索引-转载

    当您考虑是否要对列创建索引时, 请估计在查询中使用列的方式, 下表介绍了索引对其有用的查询类型. 表中的示例基于 AdventureWorks2008R2 示例数据库, 在 SQL Server Ma ...

  8. JAVA中的那些名词解释!

    1.JDO: (Java Data Object )是Java对象持久化的新的规范,也是一个用于存取某种数据仓库中的对象的标准化API.作用:用于存取某种数据仓库中的对象 2.JPA: JPA是Jav ...

  9. mysql中replicate_wild_do_table和replicate_do_db区别

    使用replicate_do_db和replicate_ignore_db时有一个隐患,跨库更新时会出错. 如在Master(主)服务器上设置 replicate_do_db=test(my.conf ...

  10. asmlinkage的作用

    本文转载自:http://blog.chinaunix.net/uid-24945116-id-83893.html 学习啦! asmlinkage是个宏,使用它是为了保持参数在stack中.因为从汇 ...