Codeforces457-D. CGCDSSQ

原題鏈接

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

Input

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

Output

For each query print the result in a separate line.

Examples
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

枚舉以第i個數結尾區間的最大公約數,和第i+1個數求最大公約數,的出的結果就是以第i+1個數爲結尾的所有區間最大公約數

#include <bits/stdc++.h>
#define maxn 100005
#define MOD 1000000007
using namespace std;
typedef long long ll;

map<int, int> m[2];
map<int, ll> ans;
int num[maxn];
int gcd(int a, int b){
	return b ? gcd(b, a % b) : a;
}
int main(){
//	freopen("in.txt", "r", stdin);
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; i++)
	 scanf("%d", num+i);
	map<int, int> ::iterator iter;
	int k = 0;
	for(int i = 0; i < n; i++){
		for(iter = m[k].begin(); iter != m[k].end(); iter++){
			m[k^1][gcd(iter->first, num[i])] += iter -> second;
		}
		m[k^1][num[i]] += 1;
		m[k].clear();
		k ^= 1;
		for(iter = m[k].begin(); iter != m[k].end(); iter++){
			ans[iter->first] += iter -> second;
		}
	}
	int q, a;
	scanf("%d", &q);
	while(q--){
		scanf("%d", &a);
		printf("%I64d\n", ans[a]);
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章