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.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
随机推荐
- sharepoint rest api 创建文档库 文件夹
function createFolder() { var requestHeaders = { "Accept": "application/json;odata=ve ...
- AutoResetEvent类的使用
线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号.如果 AutoResetEvent 处于非终止状态,则该线程阻塞,并等待当前控制资源的线程通过调用 Set 发出资源可用的 ...
- HTML基本文件, CSS基础
HTML 一.HTML基本文件 [meta标签] 1.charset属性:单独使用.设置文档字符集编码格式. >>>写法:<meta charset ...
- phpcms和php格式化时间戳
用PHPCMS V9 建站时,经常会用到时间标签,它是通用标签调用-日期时间格式化,适用全站. 一.日期时间格式化显示: a\标准型:{date('Y-m-d H:i:s', $rs['inputti ...
- WebService小记
这个问题找了好多地方都没有结果,自己暂且总结一下吧,也不算是解决问题的根本途径,但是也不失为一种办法.当时用了wsimport wsdl2java xfire 都没有解决,大牛能解决的话,欢迎留言. ...
- Java内存分配及垃圾回收机制(未完待待续)
Java内存区域 1.内存区域 jvm运行时数据区域 程序计数器 Java虚拟机栈 本地方法栈 方法区 Java堆 大图 2.概念解释 程序计数器 线程私有的一块很小的内存空间,它是当前线程所执行 ...
- shell中source与sh区别
shell中使用source conf.sh,是直接运行conf.sh的命令,不创建子shell,类似与html中include,而sh是则创建子shell, 子shell里面 的变量父shell无法 ...
- [leetcode-541-Reverse String II]
Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...
- 13.如何生成订单号,用uuid
String orderNum = UUID.randomUUID().toString().replaceAll("-", "");
- JS之正则表达式
一.正则表达的目标: 1.使用表单事件和脚本函数实现表单验证 2.使用String对象和文本框控件常用属性和方法实现客户端验证 二.什么需要表单验证: 1.表单元素是否为空 2.用户名和密码 3.E- ...