探索flutter框架开发的app在移动应用市场的潜力与挑战
645
2022-11-09
D. 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
63 6 9 18 36 108
output
Yes
input
27 17
output
No
input
94 8 10 12 15 18 33 44 81
output
Yes
题目大概:
给出n个数,按升序给出,组成一颗二叉树,每两个相连节点之间必须有公约数。问是否能够组成这棵树。
思路:
预处理一下互质的数。
枚举根,dfs左右区间,dp+记忆化搜索看是否能找出一组解。
代码:
#include
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~