菜鸟的ACM学习之路(codeforces 1025D Recovering BST【区间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

 

Input

2
7 17

Output

No

 

Input

9
4 8 10 12 15 18 33 44 81

Output

Yes

 

Hint

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

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

 

代码如下:

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long LL;
const int maxn = 700 + 10;

int n;
int a[maxn];
int v[maxn][maxn], L[maxn][maxn], R[maxn][maxn], C[maxn][maxn];
int dp[maxn][maxn][2];

int gcd(int a, int b)//辗转相除法(欧几里德算法)求最大公约数
{
	return b ? gcd(b, a%b) : a;
}

int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		L[i][i] = R[i][i] = 1;
		scanf("%d", &a[i]);
	}

	for (int i = 1; i <= n; i++)
		for (int j = i + 1; j <= n; j++)
			v[i][j] = v[j][i] = gcd(a[i], a[j]) != 1;

	int r;
	for (int len = 1; len <= n; len++)
		for (int l = 1; (r = l + len - 1) <= n; l++)
			for (int k = l; k <= r; k++)
				if (L[l][k] && R[k][r])
				{
					C[l][r] = 1;
					if (v[k][l - 1]) R[l - 1][r] = 1;
					if (v[k][r + 1]) L[l][r + 1] = 1;
				}

	printf("%s\n", C[1][n] ? "Yes" : "No");
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值