【HDOJ 5726】GCD(RMQ+二分)
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
Sample Output
题意:
有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+二分)的更多相关文章
- HDU 5726 GCD (RMQ + 二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 给你n个数,q个询问,每个询问问你有多少对l r的gcd(a[l] , ... , a[r]) ...
- 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)个区间查询[ ...
- GCD (RMQ + 二分)
RMQ存的是区间GCD,然后遍历 i: 1->n, 然后不断地对[i, R]区间进行二分求以i为起点的相同gcd的区间范围,慢慢缩减区间. #include<bits/stdc++.h&g ...
- 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 (2016多校、二分、ST表处理区间GCD、数学)
题目链接 题意 : 给出一个有 N 个数字的整数数列.给出 Q 个问询.每次问询给出一个区间.用 ( L.R ) 表示.要你统计这个整数数列所有的子区间中有多少个和 GCD( L ~ R ) 相等.输 ...
- HDU 5726 GCD 区间GCD=k的个数
GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- *HDU3486 RMQ+二分
Interviewe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 ...
- hdu 3486 Interviewe (RMQ+二分)
Interviewe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分
原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...
随机推荐
- DBGridEh常用技巧
一.增加多表头显示方式 DBGridEh1.UseMultiTitle:=True; //打开多标题显示方式 DBGridEh1.Columns[].Title.Caption:='员工编号'; // ...
- maven学习(四)maven的生命周期
官网:http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html maven有三套相互独立的生命周期, ...
- sqlserver2008 insert语句性能
在sqlserver2008中“新建查询”,执行批量添加语句的执行时间: declare @i int ) begin INSERT INTO [xxx].[dbo].[北京万奇亚讯科技_QueryL ...
- C#mail发送
这里,简单封装一个函数来发送邮件,代码如下: /// <summary> /// 邮件发送辅助类 /// </summary> public class MailHelper ...
- Congestion Avoidance in TCP
Congestion Avoidance in TCP Consequence of lack of congestion control When a popular resource is sha ...
- htm5 手机自适应问题 文本框被激活(获取焦点)时,页面会放大至原来尺寸。
加上这句话就ok啦 <meta name="viewport" content="width=device-width, initial-scale=1.0, mi ...
- selenium层级定位及鼠标键盘操作
#code:utf-8 from selenium import webdriver from selenium.webdriver.common.action_chains import Actio ...
- February 10 2017 Week 6 Friday
Example is always more efficacious than precept. 身教胜于言教. Always match your words with your actions. ...
- python UI自动化实战记录八:添加配置
添加配置文件写入测试地址等,当环境切换时只需修改配置文件即可. 1 在项目目录下添加文件 config.ini 写入: [Domain] domain = http://test.domain.cn ...
- 使用ant进行邮件发送,ant发送已存在的html文件
Jenkins上使用发送邮件功能一直有问题,放弃Jenkins配置,使用ant的进行发送邮件,参考文档可以: https://www.jianshu.com/p/04cfce59890a 我这里是要发 ...