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. mac下配置环境变量-mongo

    一 1打开终端查看echo $PATH所有环境变量会显示2输入sudo vi ~/.bash_profile回车后输入密码,然后到达vim查看状态3输入i改为编辑态,在后面追加路径4按esc然后shi ...

  2. FCKeditor 添加行距、字体功能 (转载)

    一.首先为FCKeditor添加外部插件在fckeditor/editor/plugins文件夹下建立新文件夹lineHeight,并在其中创建fckplugin.js文件,在其文件中办輸入代码:FC ...

  3. Django—Model

    Django 模型是与数据库相关的,与数据库相关的代码一般写在 models.py 中,Django 支持 Sqlite3.MySQL.PostgreSQL 等数据库,只需要在 settings.py ...

  4. 【免费公测】阿里云SSD云盘,不仅仅是IO提速10倍

    今天很高兴为大家介绍最新的ECS存储服务:SSD云盘. SSD云盘基于全SSD存储介质.利用阿里云飞天分布式存储技术,提供数据可靠性99.999%的高性能存储:该产品具备以下特点: l  高性能:单个 ...

  5. windows 下进程与线程的遍历

    原文:http://www.cnblogs.com/Apersia/p/6579376.html 在Windows下进程与线程的遍历有好几种方法. 进程与线程的遍历可以使用<TlHelp.h&g ...

  6. 4.Linux文件系统层次体系标准

    这是不完整的linux文件系统层次体系标准,不是所有Linux发行版都根据这个标准,但大多数都是: 目录 评论 / 根目录,万物起源. /bin 包含系统启动和运行所必须的二进制程序. /boot 包 ...

  7. google离线小恐龙-备份

    开启方法: 地址栏输入: chrome://dino 空格开始

  8. Linux->Ubuntu下配置telnet环境

    1.首先查看telnet运行状态 netstat -a | grep telnet 输出为空,表示没有开启该服务 2.安装openbsd-inetd apt-get install openbsd-i ...

  9. Qt::FocusPolicy的使用

    http://blog.csdn.net/imxiangzi/article/details/50742813

  10. zt C++标准库set类型

    C++标准库set类型 分类: C++编程语言 2012-11-06 10:53 909人阅读 评论(0) 收藏 举报 目录(?)[-] 在set中添加元素 从set中获取元素 set容器只是单纯的键 ...