#P5949. Integer Result via Parenthesis Insertion
Integer Result via Parenthesis Insertion
Integer Result via Parenthesis Insertion
You are given a division expression of the form \(X_1/X_2/X_3/\dots/X_n\), where each \(X_i\) is a positive integer and \(X_i \le 10^9\). The default evaluation (without parentheses) is performed from left to right. For example, the expression 1/2/1/2
is evaluated as \(((1/2)/1)/2 = 1/4\). However, by inserting parentheses you may change the order of calculations. For example, \((1/2)/(1/2)=1\).
Your task is to determine whether it is possible to add parentheses to the expression \(E\) so that the final result becomes an integer.
Explanation:
- For an expression with two numbers, i.e. \(X_1/X_2\), the only result is \(X_1/X_2\) and it is an integer if and only if \(X_1\) is divisible by \(X_2\).
- For expressions with \(n \ge 3\) terms, note that the second number \(X_2\) always remains in the denominator no matter how parentheses are added. For every subsequent number \(X_i\) (for \(i\ge3\)), you can decide whether to multiply it into the numerator or keep it in the denominator.
In other words, for \(n\ge3\) one can obtain results of the form:
[ \frac{X_1 \times \prod_{i\in S}X_i}{X_2 \times \prod_{i\notin S,\ i\ge3}X_i} ]
where \(S\) is some subset of \(\{3,4,\dots,n\}\). Output Yes
if there exists at least one such choice so that the expression evaluates to an integer, and No
otherwise.
inputFormat
The first line contains a single integer \(n\) (\(n \ge 2\)), the number of terms in the expression. The second line contains \(n\) space-separated positive integers: \(X_1, X_2, \dots, X_n\).
outputFormat
Output a single line containing Yes
if it is possible to insert parentheses in the given expression such that the result becomes an integer, and No
otherwise.
sample
4
1 2 1 2
Yes