大意: 给定$n$个数, 任意两个$gcd>1$的数间可以连边, 求是否能构造一棵BST.

数据范围比较大, 刚开始写的$O(n^3\omega(1e9))$竟然T了..优化到$O(n^3)$才过.

思路就是先排个序, 记$L[i][j]$表示区间$[i,j]$是否能组成以$i-1$为根的$BST$, $R[i][j]$为区间$[i,j]$能否组成以$j+1$为根的BST. 然后暴力转移即可.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#define pb push_back
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std; const int N = 750;
int n, a[N], g[N][N];
vector<int> fac[N];
int L[N][N], R[N][N], c[N][N]; vector<int> calc(int x) {
vector<int> v;
for (int i=2; i*i<=x; ++i) if (x%i==0) {
v.pb(i);
while (x%i==0) x/=i;
}
if (x>1) v.pb(x);
return v;
} int main() {
scanf("%d", &n);
REP(i,1,n) scanf("%d", a+i);
sort(a+1,a+1+n);
REP(i,1,n) {
fac[i] = calc(a[i]);
a[i] = 1;
for (int j:fac[i]) a[i] *= j;
}
REP(i,1,n) REP(j,i+1,n) {
for (int t:fac[i]) {
if (a[j]%t==0) c[i][j]=c[j][i]=1;
}
}
REP(d,1,n) for (int l=1,r=l+d-1;r<=n;++l,++r) {
if (d==1) {
L[l][r] = c[l-1][l];
R[l][r] = c[l+1][l];
continue;
}
if (L[l+1][r]) {
if (d==n) return puts("Yes"),0;
if (l!=1&&!L[l][r]) L[l][r]=c[l-1][l];
if (r!=n&&!R[l][r]) R[l][r]=c[r+1][l];
}
if (R[l][r-1]) {
if (d==n) return puts("Yes"),0;
if (l!=1&&!L[l][r]) L[l][r]=c[l-1][r];
if (r!=n&&!R[l][r]) R[l][r]=c[r+1][r];
}
REP(k,l+1,r-1) {
if (R[l][k-1]&&L[k+1][r]) {
if (d==n) return puts("Yes"),0;
if (l!=1&&!L[l][r]) L[l][r]=c[l-1][k];
if (r!=n&&!R[l][r]) R[l][r]=c[r+1][k];
}
}
}
puts("No");
}

Recovering BST CodeForces - 1025D (区间dp, gcd)的更多相关文章

  1. CodeForces 512B(区间dp)

    D - Fox And Jumping Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

  2. Timetable CodeForces - 946D (区间dp)

    大意: n天, 每天m小时, 给定课程表, 每天的上课时间为第一个1到最后一个1, 一共可以逃k次课, 求最少上课时间. 每天显然是独立的, 对每天区间dp出逃$x$次课的最大减少时间, 再对$n$天 ...

  3. codeforces 1140D(区间dp/思维题)

    D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  4. Codeforces 1114D(区间DP)

    题面 传送门 分析 法1(区间DP): 首先,我们可以把连续的相等区间缩成一个数,用unique来实现,不影响结果 {1,2,2,3,3,3,5,3,4}->{1,2,3,5,3,4} 先从一个 ...

  5. CodeForces - 1107E 区间DP

    和紫书上的Blocks UVA - 10559几乎是同一道题,只不过是得分计算不同 不过看了半天紫书上的题才会的,当时理解不够深刻啊 不过这是一道很好区间DP题 细节看代码 #include<c ...

  6. D. Recovering BST Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)

    http://codeforces.com/contest/1025/problem/D 树 dp 优化 f[x][y][0]=f[x][z][1] & f[z+1][y][0] ( gcd( ...

  7. CodeForces 149D 区间DP Coloring Brackets

    染色有三个条件: 对于每个点来说要么不染色,要么染红色,要么染蓝色 对于每对配对的括号来说,有且只有一个一边的括号被染色 相邻的括号不能染成相同的颜色 首先可以根据给出的括号序列计算出括号的配对情况, ...

  8. Zuma CodeForces - 607B (区间DP)

    大意: 给定字符串, 每次删除一个回文子串, 求最少多少次删完. #include <iostream> #include <cstdio> #define REP(i,a,n ...

  9. Codeforces 940 区间DP单调队列优化

    A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...

随机推荐

  1. 在不切换分支的情况下,如何在all branches中快速查看指定分支,相对其他分支的状态

    假设需要查看的分支为temp 1.git show temp 通过这个可以拿到commit id 2.查看TortoiseGit的日志,左下角勾选所有分支 3.在日志界面搜索commit id,然后右 ...

  2. angular 中如何使用自定义组件

    1.创建header组件 ng g component components/header header.component.ts import { Component, OnInit } from ...

  3. Rabbitmq Plugin configuration unchanged. 问题完全解决方案

    当执行:rabbitmq-plugins.bat enable rabbitmq_management 命令时候出现 错误如下: Plugin configuration unchanged. App ...

  4. 算法习题---4-3黑白棋(UVa220)

    一:题目 系统提示当前旗手W/B(白/黑)下子,例如W下子,那么W下的位置必须是夹住黑色棋子的位置才可以. 夹住方式:横向.竖向.斜向 注意落子后将夹住的黑棋吞噬变为白棋 (一)题目详解 .棋盘以数组 ...

  5. 最简单的freemarker用法实例

          1.下载freemarker-2.3.19.jar到web项目的lib下. 2.新建freemarker引擎协助类 package com.bxsurvey.sys.process.uti ...

  6. LeetCode_125. Valid Palindrome

    125. Valid Palindrome Easy Given a string, determine if it is a palindrome, considering only alphanu ...

  7. hdfs中删除文件、文件夹、抓取内容

    删除文件   bin/hdfs dfs -rm output2/* 删除文件夹   bin/hdfs dfs -rm -r output2 抓取内容     bin/hdfs dfs -cat /us ...

  8. react中如何处理日期格式整理

    1.第一种模式——对应组件:DatePicker: 需要引入 import moment from "moment"; values.cfjdrq = moment(values. ...

  9. React中如何实现模态框每次打开都是初始界面

    问题描述如下 解决方案:每次点击打开模态框的时候为,当前模态框设置一个独立的key值,代码如下: /* * 上传文件的模块框控制 * */ showFileModal = () => { thi ...

  10. Python3之匿名函数

    当我们在传入函数时,有些时候,不需要显式定义函数.直接传入匿名函数更方便,例如 >>> list(map(lambda x:x*x,[1,2,3,4,5,6,7,8,9])) [1, ...