import
java.util.*;
import
java.lang.*;
class
GFG{
static
class
Node
{
int
key;
Node left, right;
}
static
Node newNode(
int
key)
{
Node temp =
new
Node();
temp.key = key;
temp.left = temp.right =
null
;
return
(temp);
}
static
int
find_x(
int
n)
{
if
(n ==
1
)
return
1
;
double
num, den, p;
num = Math.log10(n);
int
x =
0
, no =
0
;
for
(
int
i =
2
; i <= n; i++)
{
den = Math.log10(i);
p = num / den;
no = (
int
)(Math.pow(i, (
int
)p));
if
(Math.abs(no - n) < 1e-
6
)
{
x = i;
break
;
}
}
return
x;
}
static
boolean
is_key(
int
n,
int
x)
{
double
p;
p = Math.log10(n) / Math.log10(x);
int
no = (
int
)(Math.pow(x, (
int
)p));
if
(n == no)
return
true
;
return
false
;
}
static
int
evenPaths(Node node,
int
count,
int
x)
{
if
(node ==
null
|| !is_key(node.key, x))
{
return
count;
}
if
(node.left ==
null
&&
node.right ==
null
)
{
count++;
}
count = evenPaths(node.left,
count, x);
return
evenPaths(node.right,
count, x);
}
static
int
countExpPaths(Node node,
int
x)
{
return
evenPaths(node,
0
, x);
}
public
static
void
main(String[] args)
{
Node root = newNode(
27
);
root.left = newNode(
9
);
root.right = newNode(
81
);
root.left.left = newNode(
3
);
root.left.right = newNode(
10
);
root.right.left = newNode(
70
);
root.right.right = newNode(
243
);
root.right.right.left = newNode(
81
);
root.right.right.right = newNode(
909
);
int
x = find_x(root.key);
System.out.println(countExpPaths(root, x));
}
}