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 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.

Input

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).

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个询问,输出有多少个区间[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题的更多相关文章

  1. 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 ...

  2. Bayan 2015 Contest Warm Up D. CGCDSSQ (math,pair,map,暴力)

    哎,只能转题解了,,, 8165031                 2014-10-10 15:53:42     njczy2010     D - CGCDSSQ             GN ...

  3. 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 ...

  4. CodeForces - 633D Fibonacci-ish 大数标记map+pair的使用

    Fibonacci-ish Yash has recently learnt about the Fibonacci sequence and is very excited about it. He ...

  5. 2018 ICPC 徐州网络预赛 Features Track (STL map pair)

    [传送门]https://nanti.jisuanke.com/t/31458 [题目大意]有N个帧,每帧有K个动作特征,每个特征用一个向量表示(x,y).两个特征相同当且仅当他们在不同的帧中出现且向 ...

  6. COCI2014/2015 Contest#1 D MAFIJA【基环树最大独立点集】

    T1725 天黑请闭眼 Online Judge:COCI2014/2015 Contest#1 D MAFIJA(原题) Label:基环树,断环+树形Dp,贪心+拓扑 题目描述 最近天黑请闭眼在 ...

  7. BZOJ 4236 "JOIOJI"(前缀和+map+pair)

    传送门: [1]:BZOJ [2]:洛谷 •题解 定义数组 a,b,c 分别表示 'J' , 'O' , 'I' 的前缀和: 要想使区间 (L,R] 满足条件当且仅当 a[R]-a[L] = b[R] ...

  8. CF988 C. Equal Sums【map+pair/hash/任选两个序列,两个序列都除去他们中的一个数,使的总和相同】

    [链接]:CF988C [题意]:在n个序列中任选两个序列,两个序列都除去他们中的一个数,使的总和相同 [分析]:map<int,pair<int,int> > mp,从0~m ...

  9. Codeforces Round #486 (Div. 3) C "Equal Sums" (map+pair<>)

    传送门 •题意 给k个数列,从中k个数列中找出任意2个数列 i ,j 使得数列i删除第x个数,和数列j删除第y个数的和相等 若存在,输出 i ,x 和 j,y •思路 每个数列之间的联系为数列的和之间 ...

随机推荐

  1. layer和3D仿射变换

    1.视图的显示基于图层,通过控制图层同样能控制显示效果,获取当前的视图的layer,并为其增加圆角边框. //设置layer边框的宽度为2 view.layer.borderWidth=; //如果需 ...

  2. leetcode 二分查找 Search in Rotated Sorted ArrayII

    Search in Rotated Sorted Array II Total Accepted: 18500 Total Submissions: 59945My Submissions Follo ...

  3. ElasticSearch+Kinaba 在Windows下的安装

    转自:https://blog.csdn.net/qq_28795681/article/details/79723455 1.下载安装java 2.下载ElasticSearch和Kinaba,并解 ...

  4. asp.net网站项目调用page,或者ashx页面不能用反射

    public class TestHandler : System.Web.IHttpHandler { public bool IsReusable { get { return false; } ...

  5. 自定义cas客户端核心过滤器AuthenticationFilter

    关于cas客户端的基本配置这里就不多说了,不清楚的可以参考上一篇博文:配置简单cas客户端.这里是关于cas客户端实现动态配置认证需要开发说明. 往往业务系统中有些模块或功能是可以不需要登录就可以访问 ...

  6. VS2010中生成遇到的 web.config 问题

    1. 错误:无法在此路径使用此配置节.当站点管理员使用继承的配置文件中的  <location allowOverride="false">  锁定对此节的访问时会出现 ...

  7. 【转】TCP/IP详解学习笔记(一)

      TCP/IP详解学习笔记   这位仁兄写得太好了. http://blog.csdn.net/goodboy1881/category/204448.aspx TCP/IP详解学习笔记(13)-T ...

  8. ThinkPHP的A方法,R方法,M方法,D方法区别

    在Thinkphp中,实例化对象有这么几种方法,如果是类,有A和R方法,区别是A方法只是对象的实例化,而R方法是可以同时实例化对象里面的方法的,这里需要去指定,如下面的实例代码: <?php n ...

  9. 编译时:virtual memory exhausted: Cannot allocate memory(转)

    一.问题 当安装虚拟机时系统时没有设置swap大小或设置内存太小,编译程序会出现virtual memory exhausted: Cannot allocate memory的问题,可以用swap扩 ...

  10. Oracle 12c CDB PDB

    先说基本用法: 先按11G之前进行 conn / as sysdba; create user test identifed by test; ORA-65096: 公用用户名或角色名无效. 查官方文 ...