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
  1. 3
  2. 2 6 3
  3. 5
  4. 1
  5. 2
  6. 3
  7. 4
  8. 6
Output
  1. 1
  2. 2
  3. 2
  4. 0
  5. 1
Input
  1. 7
  2. 10 20 3 15 1000 60 16
  3. 10
  4. 1
  5. 2
  6. 3
  7. 4
  8. 5
  9. 6
  10. 10
  11. 20
  12. 60
  13. 1000
Output
  1. 14
  2. 0
  3. 2
  4. 2
  5. 2
  6. 0
  7. 2
  8. 2
  9. 1
  10. 1

题意给你一个序列和q个询问,输出有多少个区间[l,r]满足区间gcd为询问的值。

思路:处理出每一个区间的gcd(要注意合并区间),假设有询问则直接加上去。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. std::map<int, int> id;
  4. vector<pair<int,int> >gcdList;
  5. const int MAXN=100000+5;
  6. int query[MAXN*3],A[MAXN];
  7. long long ans[MAXN*3];
  8. int _gcd(int a,int b){return b?_gcd(b,a%b):a;}
  9. int ID(int x){
  10. if(!id[x])id[x]=id.size();
  11. return id[x];
  12. }
  13. int main(int argc, char const *argv[])
  14. {
  15. int n;
  16. ios_base::sync_with_stdio(false);
  17. gcdList.clear();id.clear();
  18. cin>>n;
  19. for(int i=1;i<=n;i++)cin>>A[i];
  20. int q;cin>>q;
  21. for(int i=1;i<=q;i++){
  22. cin>>query[i];
  23. query[i]=ID(query[i]);
  24. }
  25. for(int i=1;i<=n;i++){
  26. for(int j=0;j<gcdList.size();j++)
  27. gcdList[j].first=_gcd(gcdList[j].first,A[i]);
  28. gcdList.push_back(make_pair(A[i],i));
  29. int cnt=1;
  30. //sort(gcdList.begin(), gcdList.end());
  31. for(int j=1;j<gcdList.size();j++){
  32. if(gcdList[j].first!=gcdList[cnt-1].first)
  33. gcdList[cnt++]=gcdList[j];
  34. }
  35. gcdList.resize(cnt);
  36. for(int j=0;j<gcdList.size();j++){
  37. if(id[gcdList[j].first]){
  38. int r=i+1;
  39. if(j+1<gcdList.size())r=gcdList[j+1].second;
  40. ans[id[gcdList[j].first]]+=r-gcdList[j].second;
  41. }
  42. }
  43. }
  44. for(int i=1;i<=q;i++)cout<<ans[query[i]]<<endl;
  45. return 0;
  46. }

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. IOS之UITabBarController

    在学习IOS开发过程中,针对于UITabBarController的使用也不少出现,UITabBarController和UINavigationController类似,UITabBarContro ...

  2. Android -- 在ScrollView中嵌套ListView

    在做一个工程,这个工程的布局可以相当的复杂,最外面是ScrollView,在ScrollView里面有两个Listview,这下好了,布局出来了,放在机子上跑,卡得想死有木有,信息乱跑乱出现,表示非常 ...

  3. 【RPC】Thrift ICE 等 RPC 框架相关资料

    RPC框架-Thrift-ICE Apache Thrift - Documentation Apache Thrift - Index of tutorial/ Apache Thrift - Ab ...

  4. Cognos开发自定义排序规则的报表和自定义排名报表

    场景:有一个简单的销售数据分析,可以按照日期,按照商品类型来分析订单笔数和订单金额. 目的:用户可以自定义查看按照不同指标排序的数据,用户可以查看按照不同指标排名的前N名数据 一:功能及效果展示 效果 ...

  5. Mapnik读取PostGIS数据渲染图片

    __author__ = 'Administrator' # encoding: utf-8 import sys import datetime import mapnik m = mapnik.M ...

  6. Discuz常见大问题-如何在自定义页面使用首页四格

    根据要求把majianjun文件夹放到指定目录 在DIY模式下点击保存后面的小按钮,然后导入XML文件 默认是采集所有版块的数据,你可以保存之后再次DIY,然后设置数据来源和设置标题等信息. 需要注意 ...

  7. Android 之布局(二)

    3.TableLayout(表格布局) 像表格一样布局,通常情况下,TableLayout有多个TableRow组成,每个TableRow就是一行. <?xml version="1. ...

  8. 互斥锁属性PTHREAD_MUTEX_RECURSIVE

    四.互斥锁属性 线程和线程的同步对象(互斥量,读写锁,条件变量)都具有属性.在修改属性前都需要对该结构进行初始化.使用后要把该结构回收.我们用pthread_ mutexattr_init函数对pth ...

  9. wepy - 与原生有什么不同(watcher监听器.)

    <style> </style> <template> <view>监听值:{{num}}</view> </template> ...

  10. Scalatra文件下载时中文乱码

    可以采用Servlet平台的解决方法进行解决: Ok(file.get, Map( "Content-Type" -> (file.contentType.getOrElse ...