Skip to content

Commit 003b383

Browse files
committed
test: codeforces contest/475/D
py
1 parent 4476ea9 commit 003b383

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

codeforces/contest/475/D/main.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
D. CGCDSSQ
3+
time limit per test2 seconds
4+
memory limit per test256 megabytes
5+
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.
6+
7+
is a greatest common divisor of v1,v2,...,vn, that is equal to a largest positive integer that divides all vi.
8+
9+
Input
10+
The first line of the input contains integer n, (1≤n≤10^5), denoting the length of the sequence. The next line contains n space separated integers a1,...,an, (1≤ai≤10^9).
11+
12+
The third line of the input contains integer q, (1≤q≤3×10^5), denoting the number of queries. Then follows q lines, each contain an integer xi, (1≤xi≤10^9).
13+
14+
Output
15+
For each query print the result in a separate line.
16+
"""
17+
18+
from math import gcd
19+
from collections import defaultdict
20+
21+
n = int(input())
22+
nums = list(map(int, input().split()))
23+
24+
g = []
25+
cnts = defaultdict(int)
26+
for i, x in enumerate(nums):
27+
g.append([x, i])
28+
29+
j = 0
30+
left = 0
31+
for p in g:
32+
p[0] = gcd(p[0], x)
33+
if g[j][0] != p[0]:
34+
j += 1
35+
g[j] = p
36+
else:
37+
g[j][1] = p[1]
38+
cnts[p[0]] += p[1] - left + 1
39+
left = p[1] + 1
40+
del g[j + 1:]
41+
42+
q = int(input())
43+
for _ in range(q):
44+
x = int(input())
45+
print(cnts[x])

0 commit comments

Comments
 (0)