[CQOI2018]解锁屏幕

时间:2023-03-09 16:02:56
[CQOI2018]解锁屏幕

嘟嘟嘟



这题感觉真的很简单……

\(O(n ^ 2 logn)\)的做法特别好理解,但得开O2。



看数据范围,肯定是状压dp。但刚开始我没想通状压啥,因为点与点之间还有顺序问题。但后来发现这个顺序是子问题,转移的时候只用记录最后一个点。

所以dp[i][j]表示选的点集为\(i\),最后一个点为\(j\)的时的答案。转移的时候再枚举一个不在\(i\)中的\(k\),如果\(j\)和\(k\)之间的点都被选了,就可以转移。

如果每次暴力判断能否转移,就达到了\(O(n ^ 3 logn)\)。所以\(O(n ^ 3)\)预处理一下,a[i][j]表示\(i\)和\(j\)连线上有哪些点。

这样转移成立的条件就是i | a[j][k] == i了。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 25;
const ll mod = 1e8 + 7;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
} int n;
struct Point
{
int x, y;
In bool operator < (const Point& oth)const
{
return x < oth.x;
}
In Point operator + (const Point& oth)const
{
return (Point){x + oth.x, y + oth.y};
}
In Point operator - (const Point& oth)const
{
return (Point){x - oth.x, y - oth.y};
}
In int operator * (const Point& oth)const
{
return x * oth.y - y * oth.x;
}
}p[maxn]; In bool line(Point A, Point B, Point C)
{
if(C.y < min(A.y, B.y) || C.y > max(A.y, B.y)) return 0;
return C.x >= A.x && C.x <= B.x && (B - A) * (C - A) == 0;
}
int a[maxn][maxn];
In void init()
{
sort(p + 1, p + n + 1);
for(int i = 1; i < n; ++i)
for(int j = i + 1; j <= n; ++j)
{
for(int k = 1; k <= n; ++k)
if(k != i && k != j && line(p[i], p[j], p[k])) a[i][j] |= (1 << (k - 1));
a[j][i] = a[i][j];
}
} ll dp[(1 << 20) + 5][maxn]; int main()
{
n = read();
for(int i = 1; i <= n; ++i) p[i].x = read(), p[i].y = read();
init();
for(int i = 1; i <= n; ++i) dp[1 << (i - 1)][i] = 1;
for(int i = 1; i < (1 << n); ++i)
for(int j = 1; j <= n; ++j)
if(i & (1 << (j - 1)))
for(int k = 1; k <= n; ++k)
if((!(i & (1 << (k - 1)))) && (i | a[j][k]) == i)
{
dp[i | (1 << (k - 1))][k] += dp[i][j];
dp[i | (1 << (k - 1))][k] %= mod;
}
ll ans = 0;
for(int i = 15; i < (1 << n); ++i)
{
int cnt = 0;
for(int j = 1; j <= n; ++j) if(i & (1 << (j - 1))) ++cnt;
if(cnt < 4) continue;
for(int j = 1; j <= n; ++j) ans = (ans + dp[i][j]) % mod;
}
write(ans), enter;
return 0;
}