using
System;
using
System.Collections.Generic;
class
GFG{
static
int
N = 1000000;
static
List<
int
> prime =
new
List<
int
>();
static
void
SieveOfEratosthenes()
{
bool
[]check =
new
bool
[N + 1];
for
(
int
i = 0; i <= N; i += 1){
check[i] =
true
;
}
for
(
int
p = 2; p * p <= N; p++) {
if
(check[p] ==
true
) {
prime.Add(p);
for
(
int
i = p * p; i <= N; i += p)
check[i] =
false
;
}
}
}
class
Node {
public
int
key;
public
Node left, right;
};
static
Node newNode(
int
key)
{
Node temp =
new
Node();
temp.key = key;
temp.left = temp.right =
null
;
return
(temp);
}
static
bool
areChilrenPrimeFactors(Node parent,
Node a,
Node b)
{
if
(prime.Contains(a.key)&& prime.Contains(b.key)
&& (parent.key % a.key == 0
&& parent.key % b.key == 0))
return
true
;
else
return
false
;
}
static
int
getCount(Node node)
{
if
(node ==
null
)
return
0;
List<Node> q =
new
List<Node>();
int
count = 0;
q.Add(node);
while
(q.Count!=0) {
Node temp = q[0];
q.RemoveAt(0);
if
(temp.left!=
null
&& temp.right !=
null
) {
if
(areChilrenPrimeFactors(
temp, temp.left,
temp.right))
count++;
}
if
(temp.left !=
null
)
q.Add(temp.left);
if
(temp.right !=
null
)
q.Add(temp.right);
}
return
count;
}
static
int
findSize(Node node)
{
if
(node ==
null
)
return
0;
return
1
+ findSize(node.left)
+ findSize(node.right);
}
public
static
void
Main(String[] args)
{
Node root = newNode(10);
root.left = newNode(2);
root.right = newNode(5);
root.right.left = newNode(18);
root.right.right = newNode(12);
root.right.left.left = newNode(2);
root.right.left.right = newNode(3);
root.right.right.left = newNode(3);
root.right.right.right = newNode(2);
root.right.right.right.left = newNode(7);
SieveOfEratosthenes();
Console.Write(getCount(root) +
"\n"
);
}
}