PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000153. Đếm đoạn con theo GCD (CGCDSSQ)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm đoạn con theo GCD (CGCDSSQ)

CGCDSSQ

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a sequence of positive integers A1,…,ANA_1,\ldots,A_N. For any 1≤L≤R≤N1\le L\le R\le N, define the interval GCD as gcd⁡(AL,…,AR)\gcd(A_L,\ldots,A_R).

Each of QQ queries gives a positive integer XX. Count the pairs (L,R)(L,R) for which the GCD of that contiguous subarray is exactly XX. Different index pairs are counted separately, and the array never changes.

Input

The first line contains NN; the second contains NN values AiA_i. The next line contains QQ, followed by QQ lines, each containing XX.

Output

For each query, output the number of contiguous subarrays whose GCD equals XX.

Subtasks

  • Subtask 1 (20 points): 1≤N,Q≤501 \le N,Q \le 50; 1≤Ai,Xj≤1001 \le A_i,X_j \le 100.
  • Subtask 2 (30 points): 1≤N,Q≤50001 \le N,Q \le 5000; 1≤Ai,Xj≤1061 \le A_i,X_j \le 10^6.
  • Subtask 3 (50 points): 1≤N≤1051 \le N \le 10^5; 1≤Q≤3⋅1051 \le Q \le 3\cdot10^5; 1≤Ai,Xj≤1091 \le A_i,X_j \le 10^9.

Examples

Input

3
2 6 3
5
1
2
3
4
6

Output

1
2
2
0
1

Explanation

The six subarrays have GCDs: [2]→2[2]\to2, [2,6]→2[2,6]\to2, [2,6,3]→1[2,6,3]\to1, [6]→6[6]\to6, [6,3]→3[6,3]\to3, and [3]→3[3]\to3. Thus the counts for X=1,2,3,4,6X=1,2,3,4,6 are 1,2,2,0,11,2,2,0,1.