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的更多相关文章

  1. Codeforces 475D CGCDSSQ(分治)

    题意:给你一个序列a[i],对于每个询问xi,求出有多少个(l,r)对使得gcd(al,al+1...ar)=xi. 表面上是询问,其实只要处理出每个可能的gcd有多少个就好了,当左端点固定的时候,随 ...

  2. Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数

    题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include < ...

  3. Codeforces 475D CGCDSSQ 区间gcd值

    题目链接 题意 给定一个长度为 \(n\) 的数列 \(a_1,...,a_n\) 与 \(q\) 个询问 \(x_1,...,x_q\),对于每个 \(x_i\) 回答有多少对 \((l,r)\) ...

  4. [CF 475D] CGCDSSQ (RMQ)

    题目链接:http://codeforces.com/contest/475/problem/D 是昨天晚上的CF题目,题意是给定你n个数,问你所有子区间内的最小公约数是x的个数是多少 问的康神,了解 ...

  5. Codeforces 475D 题解(二分查找+ST表)

    题面: 传送门:http://codeforces.com/problemset/problem/475/D Given a sequence of integers a1, -, an and q ...

  6. codeforces 475D

    题意:给定n(n<=100000)个1e9以内的数的数组a,然后最多有3*1e5的询问,对于每个询问,给定一个x,问有多少个(l<=r&&gcd(a[l],a[l+1].. ...

  7. 【CODEFORCES】 D. CGCDSSQ

    D. CGCDSSQ time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  8. Codeforces 475 D.CGCDSSQ

    题目说了a的范围小于10^9次方,可实际却有超过的数据...真是醉了 算出以f[i]结尾的所有可能GCD值,并统计: f[i]可以由f[i-1]得出. /* 递推算出所有GCD值,map统计 */ # ...

  9. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

随机推荐

  1. 微信小程序 - 自定义创建

    自定义创建与默认创建完全相同, 只是不要勾选quick start即可 淡定(不要看到报错就紧张, 一定要淡定) 看看它说了什么, no such file or directory(没有文件或目录) ...

  2. java递归算法实现 数字人民币大写转换

    最近穷死了 ,没钱吃饭ing 写点钱给自己吧!public class Test{ public static String getChar(long a){ int b = (int)a; Map ...

  3. Oracle Job定时任务的使用详解

    oracle中的job能为你做的就是在你规定的时间格式里执行存储过程,定时执行一个任务 .下面是一个小案例,定时每15分钟向一张表插入一条数据 一 1.创建一张测试表 -- Create table ...

  4. Chapter 5. MPEG-4 Visual

    本章着重介绍有关MPEG-4 Visual标准的细节. Tool 编码工具集合的子集(比如支持交织等). Object 视频元素(比如一个矩形视频帧,或者一个任意形状的区域,静止的图像). Profi ...

  5. css加载会造成阻塞吗?

    终于考试完了,今天突然想起来前阵子找实习的时候,今日头条面试官问我,js执行会阻塞DOM树的解析和渲染,那么css加载会阻塞DOM树的解析和渲染吗?所以,接下来我就来对css加载对DOM树的解析和渲染 ...

  6. dynamic-load-apk 插件与宿主方法互调

    新建项目 DlPluginHost,下载dynamic-load-apk源码 1.将dynamic-load-apk 文件夹中的lib做为module导入到DlPlginHost 2.导入到Plugi ...

  7. Watson Conversation Service Implementation Methodology

    Watson Conversation Service Implementation Methodology In order to implement the WCS successfully. Y ...

  8. Mac之OS系统下搭建JavaEE环境 <五> 之Mysql数据库的安装及配置

    这里将推荐两款 集成的Mysql环境 十分轻便好用,MAMP 和 XAMPP MAMP XAMPP 1.MAMP下载 官网: https://www.mamp.info/en/ 下载安装即可使用 MA ...

  9. 英文版windows7中文软件显示乱码的解决办法

    一.打开控制面板,修改语言的归属地为China 修改完成之后重启,一般能解决大部分问题,如果依然有部分显示乱码,就需要去修改注册表

  10. 使用C#创建SQLite桌面应用程序

    本文属于原创,转载请注明出处,谢谢! 一.开发环境 操作系统:Windows 10 X64 开发环境:VS2015 编程语言:C# .NET版本:.NET Framework 4.0 目标平台:X86 ...