这道题,我没看懂,先放代码,下次再看。
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 108Output
Yes
Input
2 7 17Output
No
Input
9 4 8 10 12 15 18 33 44 81Output
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");
}