GCD(st表+二分)
GCD
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3432 Accepted Submission(s): 1227
Problem Description
Input
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 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
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std; #define LL long long
#define MAXN 100005 int n;
int num[MAXN];
int mn[MAXN];
int dp[MAXN][];
map<int,LL> res; int gcd(int a,int b)
{ return b==?a:gcd(b,a%b);} void Init()
{
mn[]=-;
for (int i=;i<=n;i++)
{
mn[i]=((i&(i-))==)?mn[i-]+:mn[i-];
dp[i][]=num[i];
}
for (int j=;j<=mn[n];j++)
for (int i=;i+(<<j)-<=n;i++)
{
dp[i][j]=gcd(dp[i][j-],dp[i+(<<(j-))][j-]);
}
} int st_calc(int l,int r)
{
int k = mn[r-l+];
return gcd(dp[l][k],dp[r-(<<k)+][k]);
} void erfen()
{
res.clear();
for (int i=;i<=n;i++) //以i为左起点的区间的所有公约数
{
int cur = i , gc = num[i]; //用二分求出
while (cur <= n)
{
int l=cur,r=n;
while(l<r)
{
int mid = (l+r+)>>;
if (st_calc(i,mid)==gc) l=mid;
else r = mid -;
}
if (res.count(gc)) res[gc]+=l-cur+;
else res[gc]=l-cur+;
cur = l + ;
if (cur<=n)
gc = gcd(gc,num[cur]);
}
}
} int main()
{
int t;
int cas=;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
for (int i=;i<=n;i++)
scanf("%d",&num[i]);
Init(); erfen(); int q;
scanf("%d",&q);
printf("Case #%d:\n",++cas);
while (q--)
{
int x,y;
scanf("%d%d",&x,&y);
int kk = st_calc(x,y);
printf("%d %lld\n",kk,res[kk]);
}
}
return ;
}
GCD(st表+二分)的更多相关文章
- 2016多校联合训练1 D题GCD (ST表+二分)
暑假颓废了好久啊...重新开始写博客 题目大意:给定10w个数,10w个询问.每次询问一个区间[l,r],求出gcd(a[l],a[l+1],...,a[r])以及有多少个区间[l',r']满足gcd ...
- 「ZJOI2018」胖(ST表+二分)
「ZJOI2018」胖(ST表+二分) 不开 \(O_2\) 又没卡过去是种怎么体验... 这可能是 \(ZJOI2018\) 最简单的一题了...我都能 \(A\)... 首先我们发现这个奇怪的图每 ...
- 【BZOJ-4310】跳蚤 后缀数组 + ST表 + 二分
4310: 跳蚤 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 180 Solved: 83[Submit][Status][Discuss] De ...
- BZOJ4556 [Tjoi2016&Heoi2016]字符串 SA ST表 二分答案 主席树
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ4556.html 题目传送门 - BZOJ4556 题意 给定一个长度为 $n$ 的字符串 $s$ . ...
- hdu5289 ST表+二分
用裸的St表+暴力枚举查询时稳TLE的,可以枚举每个区间的起点+二分满足条件的区间右端,这样复杂度是O(nlogn) #include<iostream> #include<cstr ...
- luoguP5108 仰望半月的夜空 [官方?]题解 后缀数组 / 后缀树 / 后缀自动机 + 线段树 / st表 + 二分
仰望半月的夜空 题解 可以的话,支持一下原作吧... 这道题数据很弱..... 因此各种乱搞估计都是能过的.... 算法一 暴力长度然后判断判断,复杂度\(O(n^3)\) 期望得分15分 算法二 通 ...
- 洛谷P4501/loj#2529 [ZJOI2018]胖(ST表+二分)
题面 传送门(loj) 传送门(洛谷) 题解 我们对于每一个与宫殿相连的点,分别计算它会作为多少个点的最短路的起点 若该点为\(u\),对于某个点\(p\)来说,如果\(d=|p-u|\),且在\([ ...
- ZSTU 4241 圣杯战争(ST表+二分)
题目链接 ZSTU 4241 问题转化为有很多区间,现在每次给定一个区间求这个区间和之前所有区间中的某一个的交集的最大长度. 强制在线. 首先我们把所有的区间预处理出来. 然后去重(那些被包含的小区 ...
- Codeforces 713D Animals and Puzzle(二维ST表+二分答案)
题目链接 Animals and Puzzle 题意 给出一个1e3 * 1e3的01矩阵,给出t个询问,每个询问形如x1,y1,x2,y2 你需要回答在以$(x1, y1)$为左上角,$(x1, ...
随机推荐
- 转:全面分析 Spring 的编程式事务管理及声明式事务管理
转:from: https://www.ibm.com/developerworks/cn/education/opensource/os-cn-spring-trans/
- java 中文转Unicode 以及 Unicode转中文
package com.sun; public class Snippet { public static void main(String[] args) { String cn ...
- 状态机工作流,顺序工作流和Flowchart
什么是工作流,工作流可以说是对业务处理过程的建模,当我们设计工作流的时候,我们首先要分析业务处理过程中要经历的步骤.然后,我们就可以利用WF创建工作流模型来模拟业务的处理过程. WF工作流包含两种类型 ...
- MFC对话框贴图基础上控件Stasic变成透明的
对应WM_CTLCOLOR函数 加入下面代码: HBRUSH CMFCApplication2Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) ...
- Gradle学习小结
build.gradle(依赖配置) // 普通java工程 apply plugin: 'java' // Idea工程 apply plugin: 'idea' // war工程,需要有webap ...
- WebService学习小结
基于web的服务,服务器整理资源供多个客户端应用访问,是一种多个跨平台跨语言的应用间通信整合的方案 使用场景:天气预报.股票.地图,火车票 schema约束复习 <!-- book.xsd,定义 ...
- BIOS截图中文
- mysql 主从切换
4)提升slave为master Stop slave: Reset master; Reset slave all; 在5.6.3版本之后 Reset slave; 在5.6.3版本之前 查看sla ...
- day14—JQuery编程基础
JQuery 1.什么是jQuery jQuery是一个优秀的JavaScript框架.一个轻量级的JavaScript类库.jQuery的核心理念是Write less.Do more. 使用jQu ...
- 防火墙firewall的设置和查看
systemctl start firewalld.service # 开启防火墙firewallsystemctl stop firewalld.service # 停止防火墙firewall sy ...