Recovering BST(cf505 D)

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

Copy

6
3 6 9 18 36 108

output

Copy

Yes

input

Copy

2
7 17

output

Copy

No

input

Copy

9
4 8 10 12 15 18 33 44 81

output

Copy

Yes

题意:给一个单调递增的序列,问能不能构成一颗满足条件的二叉搜索树,需要满足的条件为,子节点与父亲节点的值不互质。

思路:这种重构树的问题,一般都会满足最优子结构,这个题也满足, 所以我们可以考虑区间dp,区间dp需要枚举断点,而对于树,很显而易见,枚举的断点其实就是树根,所以我们需要先考虑子树的问题,当我们枚举的断点满足条件时,只有当两个子树都能构成满足条件的树时k才能满足条件,并且当k满足条件时,对于当前的区间[l, r],我们可以向外扩张两个点,如果gcd(a[l-1], a[k])!=1,那么以k为根的树可以作为以l-1为父亲节点的一个子树,同理如果gcd(a[r+1], a[k])!=1, 那么也可以作为一个子树,我们可以开两个数组标记一下。

AC代码:

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<string.h>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = 700 + 10;
int c[maxn][maxn], L[maxn][maxn], R[maxn][maxn], v[maxn][maxn], a[maxn], n;
void init()
{
    mem(c, 0);
    mem(L, 0);
    mem(R, 0);
    mem(v, 0);
    mem(a, 0);
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin >> n)
    {
        init();
        for(int i = 1; i <= n; i++)
        {
            cin >> a[i];
            L[i][i] = R[i][i] = 1;
        }
        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;
            }
        }
        for(int len = 1; len <= n; len++)
        {
            for(int l = 1; l <= n-len+1; l++)
            {
                int r = l+len-1;
                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;
                        }
                    }
                }
            }
        }
        string ans = c[1][n] ? "Yes" : "No";
        cout << ans << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/MALONG11124/article/details/82012761