codeforces 475D. CGCDSSQ
D. CGCDSSQ
time limit per test 2 seconds
memory limit per test 256 megabytes
Given a sequence of integers a1, ..., an and q queries x1, ..., xq on it. For each query xi you have to count the number of pairs (l, r)such that 1 ≤ l ≤ r ≤ n and gcd(al, al + 1, ..., ar) = xi.
is a greatest common divisor of v1, v2, ..., vn, that is equal to a largest positive integer that divides all vi.
Input
The first line of the input contains integer n, (1 ≤ n ≤ 105), denoting the length of the sequence. The next line contains n space separated integers a1, ..., an, (1 ≤ ai ≤ 109).
The third line of the input contains integer q, (1 ≤ q ≤ 3 × 105), denoting the number of queries. Then follows q lines, each contain an integer xi, (1 ≤ xi ≤ 109).
Output
For each query print the result in a separate line.
Examples
input
3
2 6 3
5
1
2
3
4
6
output
1
2
2
0
1
input
7
10 20 3 15 1000 60 16
10
1
2
3
4
5
6
10
20
60
1000
output
14
0
2
2
2
0
2
2
1
1
题目大意:
一个长度为n的a数列,q次询问。每次询问一个数值x,求解有多少个[l,r](1<=l<=r<=n)满足Gcd(a[l],a[l+1],……,a[r])为x。
其中n<=1e5,q<=3e5,任意x,a[i]满足1<=x,a[i]<=1e9;
题解:
显然,对于每个询问我们都不得不枚举每个左端点,然后查询满足条件的右端点区间,然而在线超时,所以我们可以把询问用map记录,离线处理。又数组为静态,我们只需要静态维护一下区间最大公约数。同时把统计得到的每个询问结果累加即可。
#include<cstdio>
#include<map>
typedef long long ll;
const int N=(int)1e6+;
inline void read(int &x){
x=;char ch=getchar();
while(ch<''||ch>'') ch=getchar();
while(!(ch<''||ch>'')) x=x*+ch-,ch=getchar();
}
int n,m;
int gcd(int x,int y){return y==?x:gcd(y,x%y);}
std::map<int ,ll > query;
int a[N],lg[N],bin[];
int f[][N];
inline int ques(int l,int r){
if(r==n+) return ;
int t=lg[r-l+];
return gcd(f[t][l],f[t][r-bin[t]+]);
}
inline void init(){
lg[]=-;for(int i=;i<=n;i++)lg[i]=lg[i>>]+;
bin[]=;for(int i=;i<=;i++) bin[i]=bin[i-]<<;
for(int i=;i<=n;i++) f[][i]=a[i];
for(int i=;i<=lg[n];i++)
for(int j=;j+bin[i]<=n+;j++){
f[i][j]=gcd(f[i-][j],f[i-][j+bin[i-]]);
}
}
inline int find(int x,int l,int op){
int r=n+;
while(l<r-){
int mid=l+r>>;
if(ques(op,mid)!=x) r=mid;
else l=mid;
}
return l;
}
inline void solve(int x){
int t=a[x],now=x;
int last;
while(now!=n+){
last=now;
now=find(t,now,x);
if(query[t])query[t]+=now-last+;
now++;t=ques(x,now);
}
}
int x[N];
int main(){
read(n);
for(int i=;i<=n;i++) read(a[i]);
read(m);
init();
for(int i=;i<=m;i++){
read(x[i]);
query[x[i]]=;
}
for(int i=;i<=n;i++)solve(i);
for(int i=;i<=m;i++)
printf("%I64d\n",query[x[i]]-);
//while(1);
}
codeforces 475D. CGCDSSQ的更多相关文章
- Codeforces 475D CGCDSSQ(分治)
题意:给你一个序列a[i],对于每个询问xi,求出有多少个(l,r)对使得gcd(al,al+1...ar)=xi. 表面上是询问,其实只要处理出每个可能的gcd有多少个就好了,当左端点固定的时候,随 ...
- Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数
题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include < ...
- Codeforces 475D CGCDSSQ 区间gcd值
题目链接 题意 给定一个长度为 \(n\) 的数列 \(a_1,...,a_n\) 与 \(q\) 个询问 \(x_1,...,x_q\),对于每个 \(x_i\) 回答有多少对 \((l,r)\) ...
- [CF 475D] CGCDSSQ (RMQ)
题目链接:http://codeforces.com/contest/475/problem/D 是昨天晚上的CF题目,题意是给定你n个数,问你所有子区间内的最小公约数是x的个数是多少 问的康神,了解 ...
- Codeforces 475D 题解(二分查找+ST表)
题面: 传送门:http://codeforces.com/problemset/problem/475/D Given a sequence of integers a1, -, an and q ...
- codeforces 475D
题意:给定n(n<=100000)个1e9以内的数的数组a,然后最多有3*1e5的询问,对于每个询问,给定一个x,问有多少个(l<=r&&gcd(a[l],a[l+1].. ...
- 【CODEFORCES】 D. CGCDSSQ
D. CGCDSSQ time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- Codeforces 475 D.CGCDSSQ
题目说了a的范围小于10^9次方,可实际却有超过的数据...真是醉了 算出以f[i]结尾的所有可能GCD值,并统计: f[i]可以由f[i-1]得出. /* 递推算出所有GCD值,map统计 */ # ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
随机推荐
- 计时器60s
计时器是经常用到的功能,下面以react nativ的例子简介来写一个倒计时60s的小demo. 代码如下: import React, { Component } from 'react'; imp ...
- 【CC2530入门教程-01】IAR集成开发环境的建立与项目开发流程
[引言] 本系列教程就有关CC2530单片机应用入门基础的实训案例进行分析,主要包括以下6部分的内容:1.CC2530单片机开发入门.2.通用I/O端口的输入和输出.3.外部中断初步应用.4.定时/计 ...
- EJB系列 - 会话Bean基础知识
本人博客文章网址:https://www.peretang.com/basic-knowledge-of-session-bean/ 什么是会话 有限的时间周期内,客户端和服务器之间的连接 为什么使用 ...
- 常见浏览器User-Agent大全
http://blog.csdn.net/tianjinjianzhan/article/details/51702232
- XCOM2中敌对生物设计分析(ADVENT篇)
最近,在制作游戏Demo--DroneAssmble的过程中,对于敌对生物的设计,参考了幽浮系列的相关设定,因此着手对幽浮2中的主要敌人进行分析. 我们知道, XCOM2中的敌对生物主要由" ...
- 简单轻量级的一套JS 类库(RapidDevelopmentFramework.JS)
1.最近好久没有更新自己的博客了,一直在考虑自己应该写一些什么.4.2日从苏州回到南京的路上感觉自己的内心些崩溃和失落,我就不多说了? 猛然之间我认为自己需要找一下内心的平衡.决定开发属于自己一套快速 ...
- Linux中的apache的服务命令
1. 启动apachesudo service httpd start 2. 停止服务apachesudo service httpd stop 3. 重新启动apachesudo service h ...
- 【Android Developers Training】 27. 序言:和其它应用交互
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 22. 与其他fragment通信
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- easyui框架--基础篇(一)-->数据表格datagrid(php与mysql交互)
前 言 php easyui框架--本篇学习主要是 easyui中的datagrid(数据表格)框架. 本篇学习主要通过讲解一段代码加GIF图片学习datagrid(数据表格)中的一些常用属 ...