bzoj2560 串珠子 状压DP
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=2560
题解
大概是这类关于无向图的联通性计数的套路了。
一开始我想的是这样的,考虑容斥,那么就是令 \(dp[i][S]\) 表示我钦定了 \(i\) 个连通块必须断开其余随意的方案数,然后 DP 完以后容斥加起来就可以了。
但是这样是 \(O(3^nn)\) 的,好像没有前途。
然后想到这个关于无向连通图计数的套路:用对于每一个状态,用总的方案数减去不连通的方案数。但是为了不连通的方案数不重不漏,我们可以在包含某一个钦定的点的连通块处统计。
令 \(dp[S]\) 表示 \(S\) 集合中的点联通的方案数,\(f[S]\) 为 \(S\) 集合中的点的无向图的个数。\(f\) 显然很好求。
dp 转移就是枚举包含钦定的点的集合,这个集合必须联通。不妨设这个钦定的点为 \(p\)。
\]
然后就没有了,时间复杂度为 \(O(3^nn + 2^nn^2)\)。
#include<bits/stdc++.h>
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back
template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
}
#define lowbit(x) ((x) & -(x))
const int N = 16 + 7;
const int M = (1 << 16) + 7;
const int P = 1e9 + 7;
int n, S;
int a[N][N];
int dp[M], f[M];
inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
inline int fpow(int x, int y) {
int ans = 1;
for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
return ans;
}
inline void ycl() {
S = (1 << n) - 1;
for (int s = 0; s <= S; ++s) {
int &ans = f[s] = 1;
for (int i = 1; i <= n; ++i) if ((s >> (i - 1)) & 1)
for (int j = i + 1; j <= n; ++j) if ((s >> (j - 1)) & 1)
ans = (ll)ans * (a[i][j] + 1) % P;
}
}
inline void DP() {
for (int s = 1; s <= S; ++s) {
dp[s] = f[s];
int tp = lowbit(s);
for (int sta = s; sta; sta = (sta - 1) & s)
if ((sta & tp) && s != sta) sadd(dp[s], P - (ll)f[s ^ sta] * dp[sta] % P);
}
}
inline void work() {
ycl();
DP();
printf("%d\n", dp[S]);
}
inline void init() {
read(n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) read(a[i][j]);
}
int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}
bzoj2560 串珠子 状压DP的更多相关文章
- bzoj2560串珠子 状压dp+容斥(?)
2560: 串珠子 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 515 Solved: 348[Submit][Status][Discuss] ...
- 【bzoj2560】串珠子 状压dp+容斥原理
题目描述 有 $n$ 个点,点 $i$ 和点 $j$ 之间可以连 $0\sim c_{i,j}$ 条无向边.求连成一张无向连通图的方案数模 $10^9+7$ .两个方案不同,当且仅当:存在点对 $(i ...
- BZOJ 2560: 串珠子 (状压DP+枚举子集补集+容斥)
(Noip提高组及以下),有意者请联系Lydsy2012@163.com,仅限教师及家长用户. 2560: 串珠子 Time Limit: 10 Sec Memory Limit: 128 MB Su ...
- 【BZOJ2560】串珠子 状压DP+容斥
[BZOJ2560]串珠子 Description 铭铭有n个十分漂亮的珠子和若干根颜色不同的绳子.现在铭铭想用绳子把所有的珠子连接成一个整体. 现在已知所有珠子互不相同,用整数1到n编号.对于第i个 ...
- BZOJ1195 [HNOI2006]最短母串 【状压dp】
题目 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. 输入格式 第一行是一个正整数n(n<=12),表示给定的字符串的 ...
- 【UVA1633】禁止的回文串(状压DP)
题意: 输入正整数n和k(1<=n<=400,1<=k<=10),求长度为n的01串中有多少个不含长度至少为k的回文连续子串.例如,n=k=3时只有4个串满足条件:001,01 ...
- bzoj2560串珠子(子集dp)
铭铭有n个十分漂亮的珠子和若干根颜色不同的绳子.现在铭铭想用绳子把所有的珠子连接成一个整体. 现在已知所有珠子互不相同,用整数1到n编号.对于第i个珠子和第j个珠子,可以选择不用绳子连接,或者在ci, ...
- bzoj 1195: [HNOI2006]最短母串【状压dp】
我有病吧--明明直接枚举是否匹配就可以非要写hash,然后果然冲突了(--我个非酋居然还敢用hash 设f[s][i]为已选串状态为s并且最后一个串是i,还有预处理出g[i][j]表示最长有长为g[i ...
- BZOJ1195 HNOI2006最短母串(状压dp)
按照子串出现的先后考虑.令f[i][j]为已经出现的字符串集合为i,最后一个出现的字符串为j时的最短串长,预处理一下任意两个串的最长重叠长度,转移显然.有点麻烦的是字典序,强行增加代码难度. 另一个比 ...
随机推荐
- BUUCTF |[0CTF 2016]piapiapia
步骤: nickname[]=wherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewhere ...
- ::before和::after的详细介绍
原文传送门: https://www.cnblogs.com/staro... 一.介绍 css3为了区分伪类和伪元素,伪元素采用双冒号写法. 常见伪类--:hover,:link,:active,: ...
- iOS打印各种类型数据
整型占位符说明 : %d : 十进制整数, 正数无符号, 负数有 “-” 符号; %o : 八进制无符号整数, 没有 0 前缀; %x : 十六进制无符号整数, 没有 0x 前缀; %u : 十进制无 ...
- 夜雨惊风 Linux下卸载openjdk,安装jdk
1.查看java版本 1 2 3 4 [root@CFDB2 ~]# java -version openjdk version "1.8.0_171" OpenJDK Runti ...
- nessus 安装
下载安装包: https://www.tenable.com/downloads/nessus 下载插件: https://docs.tenable.com/nessus/Content/Downlo ...
- java数组,遍历数组
数组:一组具有相同数据类型的集合(容器) 1.数组声明格式: 数据类型 [] 数组名 = new 数据类型[长度]: 数组长度一旦确定无法更改. 数组里的数据必须是相同类型或自动向上转型后兼容的类型 ...
- User-Based Collaborative Recommender System
Collaborative Recommender System基于User给Item的打分表,认为相似度很高的用户,会对同一个item给出相似的分数,找出K个相似度最高的用户,集合他们的打分,来推算 ...
- SQL计算两个时间段相隔时间
SQL语句: select cast(floor(datediff(minute,时间1,时间2) / 1440) as varchar)+'天'+ cast(floor((datediff(minu ...
- 洛谷P1168 中位数——set/线段树
先上一波链接 https://www.luogu.com.cn/problem/P1168 这道题我们有两种写法 第一种呢是线段树,我们首先需要将原本的数据离散化,线段树维护的信息就是区间内有多少个数 ...
- C# InterLock保证数据一致性
; i < ; i++) { c.Increment(); c.Decrement(); ...