(Noip提高组及以下),有意者请联系Lydsy2012@163.com,仅限教师及家长用户。

2560: 串珠子

Time Limit: 10 Sec Memory Limit: 128 MB

Submit: 915 Solved: 603

[Submit][Status][Discuss]

Description

  铭铭有n个十分漂亮的珠子和若干根颜色不同的绳子。现在铭铭想用绳子把所有的珠子连接成一个整体。

  现在已知所有珠子互不相同,用整数1到n编号。对于第i个珠子和第j个珠子,可以选择不用绳子连接,或者在ci,j根不同颜色的绳子中选择一根将它们连接。如果把珠子看作点,把绳子看作边,将所有珠子连成一个整体即为所有点构成一个连通图。特别地,珠子不能和自己连接。

  铭铭希望知道总共有多少种不同的方案将所有珠子连成一个整体。由于答案可能很大,因此只需输出答案对1000000007取模的结果。

 

Input

 标准输入。输入第一行包含一个正整数n,表示珠子的个数。接下来n行,每行包含n个非负整数,用空格隔开。这n行中,第i行第j个数为ci,j。

 

Output

 标准输出。输出一行一个整数,为连接方案数对1000000007取模的结果。

Sample Input

3

0 2 3

2 0 4

3 4 0

Sample Output

50

HINT

  对于100%的数据,n为正整数,所有的ci,j为非负整数且不超过1000000007。保证ci,j=cj,i。每组数据的n值如下表所示。

编号 1 2 3 4 5 6 7 8 9 10

n 8 9 9 10 11 12 13 14 15 16

Source

2012国家集训队Round 1 day1

题意:



思路:



状压DP,

我们把n个点压成二进制数代表n个点的状态信息。

我们设两个数组,

g[s] 代表s状态下的所有情况(s中为1的节点不一定联通情况)

即s状态下的点两两之间任意连边(可以不连,所以边数应该+1)

f[s] 代表s状态下的合法情况(即s中为1的节点之间一定联通)方案数。

容易知道答案就是 f[(2^n)-1]

通过枚举二进制状态信息,我们可以在(nn2^n)的时间复杂度来求出g数组,

然后我们通过容斥原理来求f[s]

我们枚举所有状态s,然后枚举s的所有子集,设子集为i,那么不合法的情况就是

g[i]*f[s^i] ,我们减去这些情况,就得到f[s] 了。这里的 s^i 是 i关于集合s的补集。

枚举子集可以看这个神仙的博客http://www.cnblogs.com/jffifa/archive/2012/01/16/2323999.html

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=700010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/ const ll mod=1000000007ll; ll g[maxn];
ll f[maxn];
ll a[20][20];
int n;
int main()
{
// freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\code_stream\\out.txt","w",stdout);
gbtb;
// cout<<(1<<16)<<endl;
cin>>n;
rep(i,0,n)
{
rep(j,0,n)
{
cin>>a[i][j];
}
}
int maxstate=(1<<n)-1;
for(int i=1;i<=maxstate;i++)
{
g[i]=1ll;
for(int j=0;j<=n;j++)
if(i&(1<<j))
{
for(int k=j+1;k<=n;++k)
if(i&(1<<k))
{
g[i]=(g[i]*(a[j][k]+1))%mod;
}
}
f[i]=g[i];
int upup = i & (i - 1);
for (int j = upup; j; j = upup & (j - 1))
{
f[i]=(f[i]-g[j]*f[j^i]%mod+mod)%mod;
}
}
cout<<f[maxstate]<<endl; return 0;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

BZOJ 2560: 串珠子 (状压DP+枚举子集补集+容斥)的更多相关文章

  1. [Luogu P3959] 宝藏 (状压DP+枚举子集)

    题面 传送门:https://www.luogu.org/problemnew/show/P3959 Solution 这道题的是一道很巧妙的状压DP题. 首先,看到数据范围,应该状压DP没错了. 根 ...

  2. bzoj2560串珠子 状压dp+容斥(?)

    2560: 串珠子 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 515  Solved: 348[Submit][Status][Discuss] ...

  3. 【bzoj2560】串珠子 状压dp+容斥原理

    题目描述 有 $n$ 个点,点 $i$ 和点 $j$ 之间可以连 $0\sim c_{i,j}$ 条无向边.求连成一张无向连通图的方案数模 $10^9+7$ .两个方案不同,当且仅当:存在点对 $(i ...

  4. 计蒜客习题:蒜头君的积木 (状压DP 枚举子集)

    问题描述 蒜头君酷爱搭积木,他用积木搭了 n 辆重量为 wi的小车和一艘最大载重量为 W 的小船,他想用这艘小船将 n 辆小车运输过河.每次小船运载的小车重量不能超过 W.另外,小船在运载小车时,每辆 ...

  5. 【BZOJ2560】串珠子 状压DP+容斥

    [BZOJ2560]串珠子 Description 铭铭有n个十分漂亮的珠子和若干根颜色不同的绳子.现在铭铭想用绳子把所有的珠子连接成一个整体. 现在已知所有珠子互不相同,用整数1到n编号.对于第i个 ...

  6. bzoj2560 串珠子 状压DP

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2560 题解 大概是这类关于无向图的联通性计数的套路了. 一开始我想的是这样的,考虑容斥,那么就 ...

  7. UVA 11825 Hackers’ Crackdown 状压DP枚举子集势

    Hackers’ Crackdown Miracle Corporations has a number of system services running in a distributed com ...

  8. BZOJ1195 [HNOI2006]最短母串 【状压dp】

    题目 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. 输入格式 第一行是一个正整数n(n<=12),表示给定的字符串的 ...

  9. BZOJ 2073: [POI2004]PRZ( 状压dp )

    早上这道题没调完就去玩NOI网络同步赛了.... 状压dp , dp( s ) 表示 s 状态下所用的最短时间 , 转移就直接暴力枚举子集 . 可以先预处理出每个状态下的重量和时间的信息 . 复杂度是 ...

随机推荐

  1. Linux小记 -- [已解决]Failed to connect to https://changelogs.ubuntu.com/meta-release-lts. Check your Internet connection or proxy settings

    问题描述 操作系统:Ubuntu Server 18.04 LTS Ubuntu每次启动时产生如下motd(message of today)输出 Failed to connect to https ...

  2. 《Effective Java》读书笔记 - 5.泛型

    Chapter 5 Generics Item 23: Don't use raw types in new code 虽然你可以把一个List<String>传给一个List类型(raw ...

  3. ubuntu下安装apatch

    在Ubuntu上安装Apache,有两种方式:1 使用开发包的打包服务,例如使用apt-get命令:2 从源码构建Apache.本文章将详细描述这两种不同的安装方式. 方法一:使用开发包的打包服务—— ...

  4. KVM 记录

    mkdir -p /home/hugepagesmount -t hugetlbfs hugetlbfs /home/hugepages 配置文件 vim /etc/libvirt/qemu.conf ...

  5. Jenkins Html Rport 使用frame报错解决办法

    对于Blocked script execution in '<URL>' because the document's frame is sandboxed and the 'allow ...

  6. 【HANA系列】SAP HANA ODBC error due to mismatch of version

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA ODBC er ...

  7. Python_ONLINE_习题集_02 函数封装

    2.1 封装函数实现如下要求 例如:输入2,5 则求:2 + 22+222 + 2222+22222的和 参考答案: https://www.bilibili.com/read/cv4185619 d ...

  8. Flask框架(一)—— Flask简介

    Flask框架(一)—— Flask简介 目录 Flask框架介绍 一.Flask简介 二.flask安装与使用 1.安装 2.使用 3.简单案例——flask实现用户登录 Flask框架介绍 一.F ...

  9. python 并发编程 查看进程的id pid与父进程id ppid

    查看进程id pid 不需要传参数 from multiprocessing import Process import time import os def task(): print(" ...

  10. 红帽学习笔记[RHCSA] 第八课[Nice值、时间同步、RPM与Yum软件安装]

    第八课 nice值 什么是nice值 给进程设置的优先级就是nice.nice的范围是-20~20.nice值越小占用的系统资源就越多,就是这个进程不nice. 如何查看nice值 # 使用top命令 ...