Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees!

Recently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has been thoroughly assembling the tree for a long time, comes home and sees his creation demolished, he'll get extremely upset. 

To not let that happen, Dima has to recover the binary search tree. Luckily, he noticed that any two vertices connected by a direct edge had their greatest common divisor value exceed $1$.

Help Dima construct such a binary search tree or determine that it's impossible. The definition and properties of a binary search tree can be found here.


-----Input-----

The first line contains the number of vertices $n$ ($2 \le n \le 700$).

The second line features $n$ distinct integers $a_i$ ($2 \le a_i \le 10^9$) — the values of vertices in ascending order.


-----Output-----

If it is possible to reassemble the binary search tree, such that the greatest common divisor of any two vertices connected by the edge is greater than $1$, print "Yes" (quotes for clarity).

Otherwise, print "No" (quotes for clarity).


-----Examples-----
Input
6
3 6 9 18 36 108

Output
Yes

Input
2
7 17

Output
No

Input
9
4 8 10 12 15 18 33 44 81

Output
Yes



-----Note-----

The picture below illustrates one of the possible trees for the first example. [Image] 

The picture below illustrates one of the possible trees for the third example. [Image]

Source: codeforces | Difficulty: HARD
Tags: Complete search

Number theory

Tree algorithms

Dynamic programming

Mathematics


Solutions:
from math import gcd
import random, time, sys
input = sys.stdin.buffer.readline

def main():
	n = int(input())
	a = list(map(int, input().split()))
	start = time.time()
	GCD = [1 << i for i in range(n + 1)]
	for i in range(n):
		for j in range(i):
			if gcd(a[i], a[j]) > 1:
				GCD[i] += 1 << j
				GCD[j] += 1 << i
	check1 = [1 << j for j in range(n + 1)]
	check2 = [1 << j for j in range(n + 1)]
	for i in range(n):
		check1[0] |= int(bool(check1[0] & check2[i] & GCD[i + 1])) * (1 << i + 1)
		for j in range(1, n - i):
			check1[j] |= int(bool(check1[j] & check2[i + j] & GCD[j + i + 1])) * (1 << i + j + 1)
			check2[j + i] |= int(bool(check1[j] & check2[i + j] & GCD[j - 1])) * (1 << j - 1)
	ans = bool(check1[0] & check2[n - 1])
	print('Yes' if ans else 'No')
main()


from sys import stdin
from math import gcd
n = int(stdin.readline())
a = [int(x) for x in stdin.readline().split()]
c = []
ld = []
rd = []

def check(l, r, e):
	if r == l:
		return c[l][e] > 0
	if e < l and ld[l][r - l] != 0:
		return ld[l][r - l] == 1
	elif e > r and rd[l][r - l] != 0:
		return rd[l][r - l] == 1
	for i in range(l, r + 1):
		if c[i][e] > 0:
			if i == l or check(l, i - 1, i):
				if i == r or check(i + 1, r, i):
					if e < l:
						ld[l][r - l] = 1
					else:
						rd[l][r - l] = 1
					return True
	if e < l:
		ld[l][r - l] = -1
	else:
		rd[l][r - l] = -1
	return False
for i in range(n):
	c.append([0] * n)
	ld.append([0] * n)
	rd.append([0] * n)
for i in range(n):
	for j in range(i + 1, n):
		if gcd(a[i], a[j]) > 1:
			c[i][j] = c[j][i] = 1
ans = False
for i in range(n):
	if i == 0 or check(0, i - 1, i):
		if i == n - 1 or check(i + 1, n - 1, i):
			ans = True
			break
if ans:
	print('Yes')
else:
	print('No')