map+pair Bayan 2015 Contest Warm Up D题
2 seconds
256 megabytes
standard input
standard output
Given a sequence of integers a1, ..., an andq queries
x1, ..., xq on it. For each queryxi 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 ofv1, v2, ..., vn,
that is equal to a largest positive integer that divides allvi.
The first line of the input contains integer n, (1 ≤ n ≤ 105), denoting the length of the sequence. The next line containsn
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 followsq
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个询问,输出有多少个区间[l,r]满足区间gcd为询问的值。
思路:处理出每一个区间的gcd(要注意合并区间),假设有询问则直接加上去。
#include <bits/stdc++.h>
using namespace std;
std::map<int, int> id;
vector<pair<int,int> >gcdList;
const int MAXN=100000+5;
int query[MAXN*3],A[MAXN];
long long ans[MAXN*3];
int _gcd(int a,int b){return b?_gcd(b,a%b):a;}
int ID(int x){
if(!id[x])id[x]=id.size();
return id[x];
}
int main(int argc, char const *argv[])
{
int n;
ios_base::sync_with_stdio(false);
gcdList.clear();id.clear();
cin>>n;
for(int i=1;i<=n;i++)cin>>A[i];
int q;cin>>q;
for(int i=1;i<=q;i++){
cin>>query[i];
query[i]=ID(query[i]);
}
for(int i=1;i<=n;i++){
for(int j=0;j<gcdList.size();j++)
gcdList[j].first=_gcd(gcdList[j].first,A[i]);
gcdList.push_back(make_pair(A[i],i));
int cnt=1;
//sort(gcdList.begin(), gcdList.end());
for(int j=1;j<gcdList.size();j++){
if(gcdList[j].first!=gcdList[cnt-1].first)
gcdList[cnt++]=gcdList[j];
}
gcdList.resize(cnt);
for(int j=0;j<gcdList.size();j++){
if(id[gcdList[j].first]){
int r=i+1;
if(j+1<gcdList.size())r=gcdList[j+1].second;
ans[id[gcdList[j].first]]+=r-gcdList[j].second;
}
}
}
for(int i=1;i<=q;i++)cout<<ans[query[i]]<<endl;
return 0;
}
map+pair Bayan 2015 Contest Warm Up D题的更多相关文章
- Bayan 2015 Contest Warm Up D题(GCD)
D. CGCDSSQ time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- Bayan 2015 Contest Warm Up D. CGCDSSQ (math,pair,map,暴力)
哎,只能转题解了,,, 8165031 2014-10-10 15:53:42 njczy2010 D - CGCDSSQ GN ...
- Codeforces Round #378 (Div. 2) D. Kostya the Sculptor map+pair
D. Kostya the Sculptor time limit per test 3 seconds memory limit per test 256 megabytes input stand ...
- CodeForces - 633D Fibonacci-ish 大数标记map+pair的使用
Fibonacci-ish Yash has recently learnt about the Fibonacci sequence and is very excited about it. He ...
- 2018 ICPC 徐州网络预赛 Features Track (STL map pair)
[传送门]https://nanti.jisuanke.com/t/31458 [题目大意]有N个帧,每帧有K个动作特征,每个特征用一个向量表示(x,y).两个特征相同当且仅当他们在不同的帧中出现且向 ...
- COCI2014/2015 Contest#1 D MAFIJA【基环树最大独立点集】
T1725 天黑请闭眼 Online Judge:COCI2014/2015 Contest#1 D MAFIJA(原题) Label:基环树,断环+树形Dp,贪心+拓扑 题目描述 最近天黑请闭眼在 ...
- BZOJ 4236 "JOIOJI"(前缀和+map+pair)
传送门: [1]:BZOJ [2]:洛谷 •题解 定义数组 a,b,c 分别表示 'J' , 'O' , 'I' 的前缀和: 要想使区间 (L,R] 满足条件当且仅当 a[R]-a[L] = b[R] ...
- CF988 C. Equal Sums【map+pair/hash/任选两个序列,两个序列都除去他们中的一个数,使的总和相同】
[链接]:CF988C [题意]:在n个序列中任选两个序列,两个序列都除去他们中的一个数,使的总和相同 [分析]:map<int,pair<int,int> > mp,从0~m ...
- Codeforces Round #486 (Div. 3) C "Equal Sums" (map+pair<>)
传送门 •题意 给k个数列,从中k个数列中找出任意2个数列 i ,j 使得数列i删除第x个数,和数列j删除第y个数的和相等 若存在,输出 i ,x 和 j,y •思路 每个数列之间的联系为数列的和之间 ...
随机推荐
- scala 学习笔记五 foreach, map, reduce
例子 val v = Vector(,,,) ) println(s) //输出:Vector(2, 4, 6, 8) val v2 = Vector(,,,) var v3 = v2.reduce( ...
- word图片自动编号与引用(转)
http://blog.csdn.net/hunauchenym/article/details/5915616 用Word时,可能会遇到文中使用了大量的图片的情况,这时,若采用手动为图片编号的方法, ...
- Cognos中新建SQLserver数据源的步骤
1:配置-数据源连接-新建数据源-指定数据源名称 2:选择数据库类型,暂时不配置jdbc 3:指定服务器,数据库名称,登陆用户名和密码 4:测试 5:测试OK(OLE DB类型的) 6:返回上一步 , ...
- X86-64寄存器和栈帧
简介 通用寄存器可用于传送和暂存数据,也可参与算术逻辑运算,并保存运算结果.除此之外,它们还各自具有一些特殊功能.通用寄存器的长度取决于机器字长,汇编语言程序员必须熟悉每个寄存器的一般用途和特殊用途, ...
- (字符串)最长公共字串(Longest-Common-SubString,LCS)
题目: 给定两个字符串X,Y,求二者最长的公共子串,例如X=[aaaba],Y=[abaa].二者的最长公共子串为[aba],长度为3. 子序列是不要求连续的,字串必须是连续的. 思路与代码: 1.简 ...
- Nginx学习笔记(三)------配置文件nginx.conf说明
#user nobody; #开启进程数 <=CPU数 worker_processes ; #错误日志保存位置 #error_log logs/error.log; #error_log l ...
- DevExpress学习02——DevExpress 14.1的汉化
汉化资源: 汉化补丁:dxKB_A421_DXperience_v14.1_(2014-06-09):http://www.t00y.com/file/86576990 汉化工具:DXperience ...
- STL - 容器 - List
List内部结构完全不同于array, vector, deque. 它提供了两个pointer,指向第一个和最后一个元素. 不支持随机访问元素,因此要访问第n个元素必须爬过n - 1个元素. 在任何 ...
- No configuration found for the specified action解决办法(转)
使用Struts2,配置一切正常,使用常用tag也正常,但是在使用<s:form>标记时,发现控制台总是输出警告信息, 警告信息内容如下: 警告: No configuration fou ...
- IntelliJ IDEA 注册码失效
破解补丁无需使用注册码,下载地址:http://idea.lanyus.com/jar/JetbrainsCrack-2.6.2.jar idea14 keygen下载地址:http://idea.l ...