【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 [题解]这 ...
随机推荐
- gearman的安装与使用
Gearman是一个分发任务的程序框架,它会对作业进行排队自动分配到一系列机器上.gearman跨语言跨平台,很方便的实现异步后台任务.php官方收录:http://php.net/manual/zh ...
- Linux(Centos7)下搭建SVN服务器(新手上路)
以前都是别人直接给地址在svn上,下载或者上传东西,如今要自己建一个版本库用来存放东西.1.安装svnyum install -y subversion 2.查看svn安装位置还有哪些文件rpm -q ...
- Python【每日一问】34
问: 基础题: 定义函数实现以下功能:求出 0-n 所能组成的奇数个数,位数最多 n+1 (0<n<10),比如键盘输入n=7,求出0-7所能组成的奇数个数 提高题: 有如下分数序列: 2 ...
- 【搜索、bfs】Find The Multiple
Problem Given a positive integer n, write a program to find out a nonzero multiple m of n whose de ...
- UVA - 11175 From D to E and Back(思路)
题目: 思路: 如图E:图中a.b.c.d是有向图D中的顶点,如果ac.bc都指向cd,而ac又指向ce,那bc同样应该有一条指向ce的边不然就不能从图D转换来.所以直接枚举顶点就可以了. 代码: # ...
- ubutun 创建左面快捷方式
#http://blog.csdn.net/jizi7618937/article/details/51012552
- python3接口测试某个模块的很多接口有的用post有的用get
没啥好说的,啊哈哈 大神提示可以判断下用post还是get,但是加到哪里合适呢?仔细看认真看 耶耶耶
- shell输出颜色、printf输出颜色
1.echo开启彩色输出: -e 开启echo中的转义: \e或者\033来输出Esc颜色: 恢复默认颜色为:\e[0m; 命令格式: echo -e "\e[字背景颜色:文字颜色m字符串\ ...
- 《阿里巴巴Java开发手册》更新为《Java开发手册》
新版一览:华山版<Java开发手册> <阿里巴巴Java开发手册>始于阿里内部规约,在全球Java开发者共同努力下,已成为业界普遍遵循的开发规范,涵盖编程规约.异常日志.单元测 ...
- 【Codeforces 493D】Vasya and Chess
[链接] 我是链接,点我呀:) [题意] [题解] 会发现两个皇后之间如果只有奇数个位置 也就是n%2==1 那么第二个人总是赢的 因为如果white往下跑的话,black也能往下跑. 第二个人没有输 ...