设为首页 加入收藏

TOP

map+pair Bayan 2015 Contest Warm Up D题
2015-07-20 17:30:26 来源: 作者: 【 】 浏览:4
Tags:map pair Bayan 2015 Contest Warm


D. CGCDSSQ 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 
  
   
using namespace std;
std::map
   
     id; vector
    
      >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
     
      



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇poj 1703 Find them, Catch them.. 下一篇MIT 操作系统实验 MIT JOS lab2

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Linux_百度百科 (2025-12-26 12:51:52)
·Shell 流程控制 | 菜 (2025-12-26 12:51:49)
·TCP/UDP协议_百度百科 (2025-12-26 12:20:11)
·什么是TCP和UDP协议 (2025-12-26 12:20:09)
·TCP和UDP详解 (非常 (2025-12-26 12:20:06)