Recovering BST CodeForces - 1025D (区间dp + 暴力枚举)

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 11.

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 nn (2≤n≤7002≤n≤700).

The second line features nn distinct integers aiai (2≤ai≤1092≤ai≤109) — 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 11, print "Yes" (quotes for clarity).

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

Examples

input

6
3 6 9 18 36 108

output

Yes

思路:先暴力gcd求出任意两点间能不能连边。

L[i][j]表示以j为根向左能否到达i,R[i][j]表示以为根,向右能否到达j,L[i][i]明显成立,所以考虑向左一步步移动,同理R向右移动。

在l 和r直接枚举k为根就行。

代码:

#include <bits/stdc++.h>
#define maxn 710
using namespace std;
int n,a[maxn],L[maxn][maxn];
int R[maxn][maxn],g[maxn][maxn];
int main(void)
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]),L[i][i]=R[i][i]=1;
	for (int i=1;i<=n;++i)
		for (int j=1;j<=n;++j)
			g[i][j]=__gcd(a[i],a[j]);
	for (int l=n;l>=1;l--)
		for (int r=l;r<=n;r++)
			for (int k=l;k<=r;k++)
				if (L[l][k] && R[k][r])
				{
					if (l==1 && r==n)
					{
						printf("Yes\n");
						return 0;
					}
					if (g[l-1][k]>1) R[l-1][r]=1;
					if (g[k][r+1]>1) L[l][r+1]=1;
				}
	printf("No\n");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/82120693