【CODEFORCES】 D. CGCDSSQ
2 seconds
256 megabytes
standard input
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.
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).
For each query print the result in a separate line.
3
2 6 3
5
1
2
3
4
6
1
2
2
0
1
7
10 20 3 15 1000 60 16
10
1
2
3
4
5
6
10
20
60
1000
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的更多相关文章
- 【Codeforces】Round #491 (Div. 2) 总结
[Codeforces]Round #491 (Div. 2) 总结 这次尴尬了,D题fst,E没有做出来.... 不过还好,rating只掉了30,总体来说比较不稳,下次加油 A:If at fir ...
- 【Codeforces】Round #488 (Div. 2) 总结
[Codeforces]Round #488 (Div. 2) 总结 比较僵硬的一场,还是手速不够,但是作为正式成为竞赛生的第一场比赛还是比较圆满的,起码没有FST,A掉ABCD,总排82,怒涨rat ...
- 【CodeForces】601 D. Acyclic Organic Compounds
[题目]D. Acyclic Organic Compounds [题意]给定一棵带点权树,每个点有一个字符,定义一个结点的字符串数为往下延伸能得到的不重复字符串数,求min(点权+字符串数),n&l ...
- 【Codeforces】849D. Rooter's Song
[算法]模拟 [题意]http://codeforces.com/contest/849/problem/D 给定n个点从x轴或y轴的位置p时间t出发,相遇后按对方路径走,问每个数字撞到墙的位置.(还 ...
- 【CodeForces】983 E. NN country 树上倍增+二维数点
[题目]E. NN country [题意]给定n个点的树和m条链,q次询问一条链(a,b)最少被多少条给定的链覆盖.\(n,m,q \leq 2*10^5\). [算法]树上倍增+二维数点(树状数组 ...
- 【CodeForces】925 C.Big Secret 异或
[题目]C.Big Secret [题意]给定数组b,求重排列b数组使其前缀异或和数组a单调递增.\(n \leq 10^5,1 \leq b_i \leq 2^{60}\). [算法]异或 为了拆位 ...
- 【CodeForces】700 D. Huffman Coding on Segment 哈夫曼树+莫队+分块
[题目]D. Huffman Coding on Segment [题意]给定n个数字,m次询问区间[l,r]的数字的哈夫曼编码总长.1<=n,m,ai<=10^5. [算法]哈夫曼树+莫 ...
- 【CodeForces】906 D. Power Tower 扩展欧拉定理
[题目]D. Power Tower [题意]给定长度为n的正整数序列和模数m,q次询问区间[l,r]累乘幂%m的答案.n,q<=10^5,m,ai<=10^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 [题解]这 ...
随机推荐
- Spartan6系列之SelectIO深入详解及高级应用简介
1. 什么是I/O Tile? 对Spartan-6系列FPGA来说,一个IO Tile包括2个IOB.2个ILOGIC.2个OLOGIC.2个IODELAY. 图 1Spartan-6系列 ...
- HTML <section> 标签
实例 文档中的区段,解释了 PRC: <section> <h1>PRC</h1> <p>The People's Republic of Ch ...
- DiscuzX2.5数据库字典 值得学习
pre_common_admincp_cmenu – 后台菜单收藏表title => ‘菜单名称’url => ‘菜单地址’sort => ’0′ COMMENT ‘菜单类型,备用’ ...
- 新一代 Linux 文件系统 btrfs 简介
https://www.ibm.com/developerworks/cn/linux/l-cn-btrfs/ Btrfs 简介 文件系统似乎是内核中比较稳定的部分,多年来,人们一直使用 ext2/3 ...
- Android 按钮常用点击事件大总结
很多学习Android程序设计的人都会发现每个人对代码的写法都有不同的偏好,比较明显的就是对控件响应事件的写法的不同.因此本文就把这些写法总结一下,比较下各种写法的优劣,希望对大家灵活地选择编码方式可 ...
- 常用HTML5代码片段
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Java垃圾回收简介
Java关键术语 JavaAPI:一系列帮助开发者创建Java应用程序的封装好的库. Java 开发工具包 (JDK):一系列工具帮助开发者创建Java应用程序.JDK包含工具编译.运行.打包.分发和 ...
- uWSGI+nginx+django+virtualenv+supervisor部署项目
一.前言 在部署项目前,你已有一个能够在你本机测试过,能正常启动的Django项目(毕竟本文主要讲解部署Django项目),以及掌握了Linux系统的一些基本命令. 相关链接: Centos7安装py ...
- saturne installation on Ubuntu
test installation Table of Contents 1. installation guide 1 installation guide saturne_installation. ...
- 洛谷 2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
[题解] 就是基环外向树森林找环,然后从环向外统计size就可以了. #include<cstdio> #include<cstring> #include<algori ...