D. CGCDSSQ
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Sample test(s)
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

题解:题目大意是说给你一个序列。然后有q个询问,每一个询问描写叙述成一个正整数c,问你在这个序列中,区间上的最大公约数等于c的区间共同拥有多少个。

由于我们仅仅关心个数而并不关心区间,所以能够递推来写。如果我们知道了第以i个数为结尾的全部区间上的最大公约数,我们就能够统计他的个数。而我们在讨论i+1的时候,我们是能够推出以第i+1个数结尾的全部最大公约数的(和第i个数的结果依次取GCD即可),然后统计个数(事实上就是直接加进去)。

最后记得每次把第i个数的结果加到ANS里面即可了。

代码并不长。时间也不长。果然是由于map太快了么= =||

#include <iostream>
#include <cstring>
#include <map>
#include <cstdio> using namespace std; int a[100005],n,q,c; int gcd(int a,int b)
{
if (!b) return a;
else return gcd(b,a%b);
} map<int,long long> ans;
map<int,int> last;
map<int,int> now;
map<int,int>::iterator itr; int main()
{
scanf("%d",&n);
for (int i=0;i<n;i++) scanf("%d",&a[i]);
ans.clear();
for (int i=0;i<n;i++)
{
swap(last,now);
now.clear();
for (itr=last.begin();itr!=last.end();itr++)
now[gcd(itr->first,a[i])]+=itr->second;
now[a[i]]++;
for (itr=now.begin();itr!=now.end();itr++)
ans[itr->first]+=itr->second;
}
scanf("%d",&q);
for (int i=0;i<q;i++)
{
scanf("%d",&c);
printf("%I64d\n",ans[c]);
}
return 0;
}

【CODEFORCES】 D. CGCDSSQ的更多相关文章

  1. 【Codeforces】Round #491 (Div. 2) 总结

    [Codeforces]Round #491 (Div. 2) 总结 这次尴尬了,D题fst,E没有做出来.... 不过还好,rating只掉了30,总体来说比较不稳,下次加油 A:If at fir ...

  2. 【Codeforces】Round #488 (Div. 2) 总结

    [Codeforces]Round #488 (Div. 2) 总结 比较僵硬的一场,还是手速不够,但是作为正式成为竞赛生的第一场比赛还是比较圆满的,起码没有FST,A掉ABCD,总排82,怒涨rat ...

  3. 【CodeForces】601 D. Acyclic Organic Compounds

    [题目]D. Acyclic Organic Compounds [题意]给定一棵带点权树,每个点有一个字符,定义一个结点的字符串数为往下延伸能得到的不重复字符串数,求min(点权+字符串数),n&l ...

  4. 【Codeforces】849D. Rooter's Song

    [算法]模拟 [题意]http://codeforces.com/contest/849/problem/D 给定n个点从x轴或y轴的位置p时间t出发,相遇后按对方路径走,问每个数字撞到墙的位置.(还 ...

  5. 【CodeForces】983 E. NN country 树上倍增+二维数点

    [题目]E. NN country [题意]给定n个点的树和m条链,q次询问一条链(a,b)最少被多少条给定的链覆盖.\(n,m,q \leq 2*10^5\). [算法]树上倍增+二维数点(树状数组 ...

  6. 【CodeForces】925 C.Big Secret 异或

    [题目]C.Big Secret [题意]给定数组b,求重排列b数组使其前缀异或和数组a单调递增.\(n \leq 10^5,1 \leq b_i \leq 2^{60}\). [算法]异或 为了拆位 ...

  7. 【CodeForces】700 D. Huffman Coding on Segment 哈夫曼树+莫队+分块

    [题目]D. Huffman Coding on Segment [题意]给定n个数字,m次询问区间[l,r]的数字的哈夫曼编码总长.1<=n,m,ai<=10^5. [算法]哈夫曼树+莫 ...

  8. 【CodeForces】906 D. Power Tower 扩展欧拉定理

    [题目]D. Power Tower [题意]给定长度为n的正整数序列和模数m,q次询问区间[l,r]累乘幂%m的答案.n,q<=10^5,m,ai<=10^9. [算法]扩展欧拉定理 [ ...

  9. 【CodeForces】741 D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)

    [题意]给定n个点的树,每条边有一个小写字母a~v,求每棵子树内的最长回文路径,回文路径定义为路径上所有字母存在一种排列为回文串.n<=5*10^5. [算法]dsu on tree [题解]这 ...

随机推荐

  1. dubbo-monitor安装及配置过程

    安装 1. 使用git下载(git clone https://github.com/alibaba/dubbo.git)或者从http://dubbo.io/下载源码 2. cd到dubbo的根目录 ...

  2. JavaScript 中实现 sleep

    来自推特上 Windows 故障分析的笑话 图片来源:me.me 推上看到的笑话,Windows 故障分析的实现. 然后想起来 JavaScript 中如何实现这个 sleep() 函数让代码暂停指定 ...

  3. 关闭的连接: next

    1.最近做了一个项目,扫描读取了第三方数据库的数据,结果本来在公司测试没有问题的程序在客户那边一直报如下错误: java.sql.SQLException: 关闭的连接: next 代码如下: //第 ...

  4. keepalived(nginx的高可用)安装文档

    1. 安装环境 su - root yum -y install kernel-devel* yum -y install openssl-* yum -y install popt-devel yu ...

  5. linux命令 info

    info命令是Linux下info格式的帮助指令. 就内容来说,info页面比man page编写得要更好.更容易理解,也更友好,但man page使用起来确实要更容易得多.一个man page只有一 ...

  6. AD7606

    在只给芯片的RANGE和PAR_SER引脚上电(不给芯片加电)的时候,芯片严重发热. 改回给芯片加电,发热消失,芯片正常工作,芯片没有损坏. 版权声明:本文为博主原创文章,未经博主允许不得转载.

  7. 解决Windows Server 2012 R2 Datacenter云服务器无法运行opencv python程序的问题

    写了个基于opencv的python程序,pyinstaller 32位机打包后在win7/win10 32/64正常运行,在Windows Server 2012 R2 Datacenter云服务器 ...

  8. type="timestamp"与type="date"区别

    type="timestamp"-----数据库中保存的时间为年月日时分秒 与type="date"---------数据库中保存的时间为年月日

  9. Android BottomSheet:便捷易用的底部滑出面板(1)

    Android BottomSheet:便捷易用的底部滑出面板(1) Android BottomSheet是github上的一个第三方开源项目,其主页:https://github.com/Flip ...

  10. Python学习笔记 (2.1)标准数据类型之Number(数字)

    Python3中,数字分为四种——int,float,bool,complex int(整型) 和数学上的整数表示没啥区别,没有大小限制(多棒啊,不用写整数高精了),可正可负.还可表示16进制,以 0 ...