听说3^n也能水过去。。

其实应该是个经典题,求图染色这个np问题。

把问题拆成独立集来进行dp可以在3^n之内水过去。

拆成独立集的时候就发现,等价与一个经典的反演dp问题

然后复杂度就变成了 n*n*2^n

另外,偷到一套头文件宏定义。

#include <math.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <iostream>
#include <algorithm>
#define pb push_back
#define fi first
#define se second
#define icc(x) (1<<(x))
#define lcc(x) (1ll<<(x))
#define lowbit(x) (x&-x)
#define debug(x) cout<<#x<<"="<<x<<endl
#define rep(i,s,t) for(int i=s;i<t;++i)
#define per(i,s,t) for(int i=t-1;i>=s;--i)
#define mset(g, x) memset(g, x, sizeof(g))
using namespace std; typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> veci;
const int mod=(int)1e9+,inf=0x3fffffff,rx[]={-,,,},ry[]={,,,-};
const ll INF=1ll<<;
const db pi=acos(-),eps=1e-; template<class T> void rd(T &res){
res = ; int ch,sign=;
while( (ch=getchar())!='-' && !(ch>=''&&ch<=''));
if(ch == '-') sign = ; else res = ch-'';
while((ch=getchar())>=''&&ch<='') res = (res<<)+(res<<)+ch-'';
res = sign?-res:res;
}
template<class T>void rec_pt(T x){
if(!x)return;
rec_pt(x/);
putchar(x%^);
}
template<class T>void pt(T x){
if(x<) putchar('-'),x=-x;
if(!x)putchar('');
else rec_pt(x);
}
template<class T>inline void ptn(T x){ pt(x),putchar('\n'); }
template<class T>inline void Max(T &a,T b){ if(b>a)a=b; }
template<class T>inline void Min(T &a,T b){ if(b<a)a=b; }
template<class T>inline T mgcd(T b,T d){ return b?mgcd(d%b,b):d; }//gcd模板,传入的参数必须是同一类型
//-------------------------------主代码--------------------------------------// int mat[];
int mark[<<];//记录是否为独立集
int dp[][<<];
int A[<<],B[<<];
int n;
void gao(int _A[],int _B[],int C[])
{
rep(i, , icc(n)){
A[i] = _A[i];
B[i] = _B[i];
}
//传说中的or卷积
rep(i, , n){
rep(j, , icc(n)){
if( (icc(i)&j)){
A[j] += A[j^icc(i)];
B[j] += B[j^icc(i)];
}
}
}
rep(i, , icc(n)) C[i] = A[i]*B[i];
//然后是逆着卷机
rep(i, , n){
rep(j, , icc(n)){
if( (icc(i)&j)){
C[j] -= C[j^icc(i)];
}
}
} }
int main()
{
int T;
rd(T);
while(T--)
{
rd(n);
rep(i, , n){
mat[i] = ;
rep(j, , n){
char tmp;
cin>>tmp;
if(tmp == '')
mat[i] |= icc(j);
}
}
//mset(dp, 0);
//然后求出独立集
mark[] = ;
rep(i, , n){
rep(j, , icc(i)){
dp[][icc(i)|j] = mark[icc(i)|j] = mark[j]==?(mat[i]&j)==:;
}
} //然后开始dp rep(i, , n){
if(dp[i][icc(n)-]) break;//这步优化很重要啊
gao(dp[i],mark,dp[i+]);
rep(j, , icc(n)) {
dp[i+][j] = (dp[i+][j]!=);
//pt(dp[i+1][j]); putchar(' ');
}
//puts("");
} ui ans = ;
ui tmp = ;
rep(i, , icc(n))
{
ui mj = ;
tmp *= ;
rep(j, , n+){
if(dp[j][i]!=){
mj = j;
break;
}
}
ans += mj*tmp;
}
ptn(ans);
}
return ;
} /*
//-----------Test Case------------// */

hdu5823(反演dp)的更多相关文章

  1. HDU - 5823:color II (状压DP 反演DP)

    题意:给定连通图,求出连通图的所有子图的颜色数. 一个图的颜色数,指最少的颜色数,给图染色,使得有边相邻的点之间颜色不同. 思路:首先想法是DFS枚举,然后计算颜色,发现对于给定图,求颜色不会求? 毕 ...

  2. 反演dp经典

    咋一看,至少要用3^n才能做到. 但. 首先定义: 可以发现只要求出a' b' 那么直接可以得出c' 那么如何求a'呢 //dp求a',其实就是分别用[0,n)来更新a' ; i < n; i+ ...

  3. CF809E Surprise me!(莫比乌斯反演+Dp(乱搞?))

    题目大意: 给你一棵树,树上的点编号为\(1-n\).选两个点\(i.j\),能得到的得分是\(\phi(a_i*a_j)*dis(i,j)\),其中\(dis(i,j)\)表示\(a\)到\(b\) ...

  4. BZOJ3622 已经没有什么好害怕的了 二项式反演+DP

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3622 题解 首先显然如果 \(n - k\) 为奇数那么就是无解.否则的话,"糖果& ...

  5. Codeforces 285E - Positions in Permutations(二项式反演+dp)

    Codeforces 题目传送门 & 洛谷题目传送门 upd on 2021.10.20:修了个 typo( 这是一道 *2600 的 D2E,然鹅为啥我没想到呢?wtcl/dk 首先第一步我 ...

  6. 【CSA35G】【XSY3318】Counting Quests DP 拉格朗日反演 NTT

    题目大意 zjt 是个神仙. 一天,zjt 正在和 yww 玩猜数游戏. zjt 先想一个 \([1,n]\) 之间的整数 \(x\),然后 yww 开始向他问问题. yww 每次给 zjt 一个区间 ...

  7. LOJ #2542. 「PKUWC 2018」随机游走(最值反演 + 树上期望dp + FMT)

    写在这道题前面 : 网上的一些题解都不讲那个系数是怎么推得真的不良心 TAT (不是每个人都有那么厉害啊 , 我好菜啊) 而且 LOJ 过的代码千篇一律 ... 那个系数根本看不出来是什么啊 TAT ...

  8. Codeforces.1139D.Steps to One(DP 莫比乌斯反演)

    题目链接 啊啊啊我在干什么啊.怎么这么颓一道题做这么久.. 又记错莫比乌斯反演式子了(╯‵□′)╯︵┻━┻ \(Description\) 给定\(n\).有一个初始为空的集合\(S\).令\(g\) ...

  9. P4859 已经没有什么好害怕的了(dp+二项式反演)

    P4859 已经没有什么好害怕的了 啥是二项式反演(转) 如果你看不太懂二项式反演(比如我) 那么只需要记住:对于某两个$g(i),f(i)$ ---------------------------- ...

随机推荐

  1. c# UDP通信 列子

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. JS实现base64编码与解码

    var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ...

  3. EasyUI DataGrid 复选框

    使用checkbox,用户可以选定/取消数据行.添加checkbox列,我们简单的添加列的checkbox属性,并且设置为true.代码像这样:<table id="tt"& ...

  4. 2017年1月8日 星期日 --出埃及记 Exodus 21:34

    2017年1月8日 星期日 --出埃及记 Exodus 21:34 the owner of the pit must pay for the loss; he must pay its owner, ...

  5. 【Linux】vi(vim)起步学起来有些困难,一步一步温习

    以Tomcat的配置文件service.xml为例,记录.学习vi的最常用操作. > 什么是vi or vim? [nicchagil@localhost bak]$ man vi VIM() ...

  6. #define用法集锦

    Definition: The #define Directive You can use the #define directive to give a meaningful name to a c ...

  7. 我的android学习经历30

    数据存储的持久化技术 数据存储主要有五种方式: (1)文件存储或者SD卡 (2)SharedPreferences (3)SQLite (4)ContentProvider (5)网络存储 下面简单介 ...

  8. WebRTC的学习(二)

    英文原文的链接地址为:https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Overview WebRTC是由一些关联的API和协议一 ...

  9. hdu 4946 Area of Mushroom(凸包)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4946 Area of Mushroom Time Limit: 2000/1000 MS (Java/Ot ...

  10. python tools: iPython Notebook

    Introducing IPython Notebook IPython isn't a different programming language, it's just a set of comp ...