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.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
随机推荐
- POJ 1207 3N+1 Problem
更简单的水题,穷举法即可. 需要注意的点: 1.i 和 j的大小关系不确定,即有可能 i>j 2.即使i>j,最后输出的结果也要严格按照输出,亦即如果输入10,1,则对应输出也应为 10 ...
- Cordova各个插件使用介绍系列(一)—$cordovaSms发送短信
详情链接地址:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/cordova-1-cordovasms/ 这是调用手机发送短信的插件 ...
- phpcms v9模版调用代码
首页调用栏目{pc:content action="category" siteid="$siteid" num="15" order=&q ...
- Vulkan Tutorial 21 Staging buffer
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Introduction 顶点缓冲区现在已经可以正常工作,但相比于显卡内部读取数据, ...
- 更改zendstudio花括号匹配显示的方法
- thinkphp带查询条件的分页
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- jquery取出所有包含class='engineer_val'的值
$(".engineer_val").each(function(){ //jquery取出所有包含class='engineer_val'的值 $(); });
- 既然函数也是对象,那么为什么this不指向普通函数?
function a(){ var b=1; function exp(){ alert(this.b); } exp(); } var b=2; a(); 既然函数是对象,且exp是在a中运行的,那 ...
- Spring Boot 集成swagger实例
原文:https://github.com/x113773/testall/issues/5 1. 首先添加maven依赖``` <dependency> <groupId>i ...
- 在MacOS中,Unity使用VSCode开发,4.7版本无法正常使用C#
我在MacOS中安装了两个版本的Unity,一个是4.7版本,一个是5.6版本,在5.6版本中使用VSCode打开项目时,可以正常代码提示和查看,但是打开4.7版本的项目时,无法正常提示和查看. 经过 ...