import
java.io.*;
class
Node {
int
gcd;
int
min;
int
cnt;
}
class
GFG {
static
final
int
N =
100005
;
static
Node[] tree =
new
Node[
5
* N];
static
void
buildtree(
int
low,
int
high,
int
pos,
int
[] a)
{
if
(low == high) {
tree[pos] =
new
Node();
tree[pos].min = tree[pos].gcd = a[low];
tree[pos].cnt =
1
;
return
;
}
int
mid = (low + high) >>
1
;
buildtree(low, mid,
2
* pos +
1
, a);
buildtree(mid +
1
, high,
2
* pos +
2
, a);
tree[pos] =
new
Node();
tree[pos].gcd = gcd(tree[
2
* pos +
1
].gcd,
tree[
2
* pos +
2
].gcd);
if
(tree[
2
* pos +
1
].min < tree[
2
* pos +
2
].min) {
tree[pos].min = tree[
2
* pos +
1
].min;
tree[pos].cnt = tree[
2
* pos +
1
].cnt;
}
else
if
(tree[
2
* pos +
1
].min
> tree[
2
* pos +
2
].min) {
tree[pos].min = tree[
2
* pos +
2
].min;
tree[pos].cnt = tree[
2
* pos +
2
].cnt;
}
else
{
tree[pos].min = tree[
2
* pos +
1
].min;
tree[pos].cnt = tree[
2
* pos +
1
].cnt
+ tree[
2
* pos +
2
].cnt;
}
}
static
int
gcd(
int
a,
int
b)
{
return
b ==
0
? a : gcd(b, a % b);
}
static
Node query(
int
s,
int
e,
int
low,
int
high,
int
pos, Node[] tree)
{
Node dummy =
new
Node();
if
(e < low || s > high) {
dummy.gcd = dummy.min = dummy.cnt =
0
;
return
dummy;
}
if
(s >= low && e <= high) {
Node result =
new
Node();
result.gcd = tree[pos].gcd;
result.min = tree[pos].min;
if
(result.gcd != result.min)
result.cnt =
0
;
else
result.cnt = tree[pos].cnt;
return
result;
}
int
mid = (s + e) >>
1
;
Node ans1
= query(s, mid, low, high,
2
* pos +
1
, tree);
Node ans2 = query(mid +
1
, e, low, high,
2
* pos +
2
, tree);
Node ans =
new
Node();
if
(ans1.gcd !=
0
&& ans2.gcd !=
0
) {
ans.gcd = gcd(ans1.gcd, ans2.gcd);
ans.min = Math.min(ans1.min, ans2.min);
if
(ans.gcd != ans.min)
ans.cnt =
0
;
else
{
if
(ans1.min == ans2.min)
ans.cnt = ans2.cnt + ans1.cnt;
else
if
(ans1.min < ans2.min)
ans.cnt = ans1.cnt;
else
ans.cnt = ans2.cnt;
}
return
ans;
}
else
if
(ans1.gcd !=
0
)
return
ans1;
else
if
(ans2.gcd !=
0
)
return
ans2;
return
dummy;
}
static
int
answerQuery(
int
[] a,
int
n,
int
l,
int
r,
Node[] tree)
{
return
query(
0
, n -
1
, l -
1
, r -
1
,
0
, tree).cnt;
}
public
static
void
main(String[] args)
{
int
a[] = {
3
,
4
,
2
,
2
,
4
,
6
};
int
n = a.length;
buildtree(
0
, n -
1
,
0
, a);
int
l =
1
, r =
4
;
System.out.println(answerQuery(a, n, l, r, tree));
l =
2
;
r =
6
;
System.out.println(answerQuery(a, n, l, r, tree));
}
}