hdu 5726(二分)
GCD Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
Total Submission(s): Accepted Submission(s): Problem Description
Give you a sequence of N(N≤,) integers : a1,...,an(<ai≤,,). There are Q(Q≤,) queries. For each query l,r you have to calculate gcd(al,,al+,...,ar) and count the number of pairs(l′,r′)(≤l<r≤N)such that gcd(al′,al′+,...,ar′) equal gcd(al,al+,...,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(<ai≤,,). 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 ). For each query, you need to output the two numbers in a line. The first number stands for gcd(al,al+,...,ar) and the second number stands for the number of pairs(l′,r′) such that gcd(al′,al′+,...,ar′) equal gcd(al,al+,...,ar). Sample Input Sample Output
Case #:
/*
难点在于计数,关键要发现gcd的递减性
对于1到n的(l,r)
对于固定的l
gcd(l,r)>=gcd(l,r+1)
对于固定的r
gcd(l,r)<=gcd(l+1,r)
因此可以不用逐一地进行计数
方法为:
枚举每一个左区间,对满足gcd(l,ri)的右区间进行二分查找
跳跃着进行计数
*/
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
#define scan1(x) scanf("%d",&x)
#define scan2(x,y) scanf("%d%d",&x,&y)
#define scan3(x,y,z) scanf("%d%d%d",&x,&y,&z)
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int Max=1e6+;
int dp[Max][];
map<int,LL> vis;
int A[Max];
int gcd(int x,int y)
{
if(x<y) swap(x,y);
return (y==?x:gcd(y,x%y));
}
void RMQ_init(int n)
{
for(int i=; i<=n; i++) dp[i][]=A[i];
for(int j=; (<<j)<=n; j++)
{
for(int i=; i+(<<j)-<=n; i++)
{
dp[i][j]=gcd(dp[i][j-],dp[i+(<<(j-))][j-]);
}
}
}
int RMQ(int l,int r)
{
int k=;
while((<<(k+))<=r-l+) k++;
return gcd(dp[l][k],dp[r-(<<k)+][k]);
}
void Init()
{
vis.clear();
}
int L[Max],R[Max];
int main()
{
int T,ca=;
for(scan1(T); T; T--)
{
int n,m,num;
scan1(n);
for(int i=; i<=n; i++) scan1(A[i]);
RMQ_init(n);
Init();
scan1(m);
for(int i=; i<=m; i++)
{
scan2(L[i],R[i]);
num=RMQ(L[i],R[i]);
vis.insert(make_pair(num,));
}
int l,r,mid,d1,d2,ans,nex;
for(int i=; i<=n; i++)
{
nex=i;
while(nex<=n)
{
d1=RMQ(i,nex);
l=nex;r=n;
while(l<=r)
{
mid=(l+r)>>;
d2=RMQ(i,mid);
if(d2>=d1) l=mid+,ans=mid;
else r=mid-;
}
if(vis.find(d1)!=vis.end())
vis[d1]+=(ans-nex)+;
nex=r+;
}
}
printf("Case #%d:\n",ca++);
for(int i=; i<=m; i++)
{
ans=RMQ(L[i],R[i]);
printf("%d %lld\n",ans,vis[ans]);
}
}
return ;
}
hdu 5726(二分)的更多相关文章
- HDU 5726 GCD 区间GCD=k的个数
GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- hdu 4024 二分
转自:http://www.cnblogs.com/kuangbin/archive/2012/08/23/2653003.html 一种是直接根据公式计算的,另外一种是二分算出来的.两种方法速度 ...
- 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 (2016多校、二分、ST表处理区间GCD、数学)
题目链接 题意 : 给出一个有 N 个数字的整数数列.给出 Q 个问询.每次问询给出一个区间.用 ( L.R ) 表示.要你统计这个整数数列所有的子区间中有多少个和 GCD( L ~ R ) 相等.输 ...
- rmq +二分暴力 hdu 5726
参考博客 题意:n 个数字的数列,有m个询问:求出 L 到 R 的 gcd(最大公约数 ),然后问这整个序列中有多少个区间的 gcd 和这个一样. 分析:L 到 R的gcd直接用RM ...
- hdu 5726 GCD 暴力倍增rmq
GCD/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence ...
- hdu 1669(二分+多重匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1669 思路:由于要求minimize the size of the largest group,由此 ...
随机推荐
- 服务器间打通ssh无密钥
1 打通无密钥 配置HDFS,首先就得把机器之间的无密钥配置上.我们这里为了方便,把机器之间的双向无密钥都配置上. (1)产生RSA密钥信息 ssh-keygen -t rsa 一路回车,直到产生一个 ...
- webview中的页面兼容iphone6和6+
其实写这篇文章的本不该是我,而应该是开发ios的小伙伴,但作为一个前端,我想我还是有必要做一下记录的! 首先我想说下在iphone6或者6+中webview内嵌套的页面宽度已经不在是320px,而是3 ...
- zedboard如何从PL端控制DDR读写(四)
PS-PL之间的AXI 接口分为三种:• 通用 AXI(General Purpose AXI) — 一条 32 位数据总线,适合 PL 和 PS 之间的中低速通信.接口是透传的不带缓冲.总共有四个通 ...
- MVC导出Excel,提供下载Excel
类1: using System.Collections.Generic;using System.Data;using System.Web.Mvc;using System.IO;using Sy ...
- div高度自适应(父元素未知,所有高度跟随子元素最大的高度)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- gsons
java 处理 json格式字符串,目前只使用过Google的Gson库. pom: <dependency> <groupId>com.google.code.gson< ...
- 解决阿里云数据库RDS报错The table '/home/mysql/data3015/tmp/#sql_13975_23' is full
查询任何一条语句都显示 The table '/home/mysql/data3015/tmp/#sql_13975_23' is full 查看了下数据库利用磁盘空间没有满, 阿里云的处理方式: 1 ...
- Centos6.x服务器配置jdk+tomcat+mysql环境
1,jdk配置 由于jdk官网的链接不直接支持wget,可以使用下面的方法下载jdk,其中jdk版本为jdk1.8.0_91: wget --no-check-certificate --no-coo ...
- Android DDMS检测内存泄露
Android DDMS检测内存泄露 DDMS是Android开发包中自带工具,可以测试app性能,用于发现内存问题. 1.环境搭建 参考之前发的Android测试环境搭建相关文章,这里不再复述: 2 ...
- RedHat下安装OPENCV
1.解压 unzip opencv-2.4.9.zip 2.进入目录,cmake CMakeLists.txt 生成build文件 3.使用命令 make 编译 4.使用命令 make instal ...