【HDU 5471】Count the Grid
来源:互联网 发布:企业文档数据加密 编辑:程序博客网 时间:2024/05/29 18:36
题意:一个矩阵中可以任意填m个数。给你N个小矩阵并且告诉你此矩阵中的最大值v,求有多少种大矩阵满足所给条件。
分析:考虑到n很小,因此我们可以将所有不同的矩阵重叠的情况(最多2 ^ n种)的最大值和块数先处理出来。其中第i种的最大值为vi,块数为ci。
然后对于每一种重叠情况,我们有两种选择,其一是不取最大值,则方案数为(vi - 1) ^ ci。第二种是取最大值,则方案数为vi ^ ci - (vi - 1) ^ ci。
则可以设dp[i][j]为前i种重叠块其中有j这些状态的矩阵的最大值被满足了的方案数,则我们现在考虑第i+1块的取的情况。不妨设当第i+1块取最大值时能使得s[i + 1]的矩阵被满足。
则当取最大值时,把对应方案数转移到dp[i + 1][j | s[i + 1]],否则转移到dp[i + 1][j]。
故dp[(1 << n)][(1 << n) - 1]为最终的方案。
具体可见代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const int N = 2222;const int mod = 1e9 + 7;#define MM(a,b,c) memset(a,b,sizeof(a[0]) * ((c) + 2));struct Node{ static int h,w,m; int x1,y1,x2,y2,v; void add(){ scanf("%d%d%d%d",&x1,&y1,&x2,&y2); scanf("%d",&v); } Node operator + (const Node &a)const{ Node ret; ret.x1 = max(x1,a.x1); ret.x2 = min(x2,a.x2); ret.y1 = max(y1,a.y1); ret.y2 = min(y2,a.y2); ret.v = min(v,a.v); return ret; } int getarea(){ if(x2 < x1) return 0; if(y2 < y1) return 0; return (x2 - x1 + 1) * (y2 - y1 + 1); } void init(){ x1 = 1,y1 = 1; x2 = h;y2 = w; v = m; }}nd1[11],nd2[N];int cnt[N],ok[N];int dp[2][N],txt = 1;int Node::h = 0,Node::w = 0,Node::m = 0;int Pow(int a,int b){ int ret = 1; if(a == 0 || a == 1) return a; while(b){ if(b & 1) ret = 1ll * ret * a % mod; a = 1ll * a * a % mod; b >>= 1; } return ret;}inline void add(int &x,int y){ x += y; if(x >= mod) x -= mod;}void solve(){ int h,w,m,n; scanf("%d%d%d%d",&h,&w,&m,&n); Node::h = h;Node::w = w;Node::m = m; int lim = 1 << n; for(int i = 0 ; i < n ; i ++) nd1[i].add(); for(int i = 0 ; i < n ; i ++) nd2[(1 << i)] = nd1[i]; nd2[0].init(); for(int i = 1 ; i < lim ; i ++){ if((i - (i & -i)) == 0) continue; nd2[i] = nd2[i - (i & -i)] + nd2[(i & -i)]; } for(int i = lim - 1 ; i >= 0 ; i --){ ok[i] = 0; cnt[i] = nd2[i].getarea(); for(int j = i + 1 ; j < (1 << n) ; j ++){ if((j ^ i) & i) continue; cnt[i] -= cnt[j]; } for(int j = 0 ; j < n ; j ++) if((i & (1 << j)) && nd2[i].v == nd1[j].v) ok[i] |= (1 << j); } int cur = 1; MM(dp[cur],0,(1 << n)); dp[cur][0] = 1; for(int i = 0 ; i < (1 << n) ; i ++){ if(cnt[i] == 0) continue; cur ^= 1; MM(dp[cur],0,(1 << n)); int t1 = Pow(nd2[i].v,cnt[i]); int t2 = Pow(nd2[i].v - 1,cnt[i]); for(int j = 0 ; j < (1 << n) ; j ++){ if(dp[cur ^ 1][j] == 0) continue; add(dp[cur][j | ok[i]],1ll * dp[cur ^ 1][j] * ((t1 - t2 + mod) % mod) % mod); add(dp[cur][j],1ll * dp[cur ^ 1][j] * t2 % mod); } } printf("Case #%d: %d\n",txt ++,dp[cur][(1 << n) - 1]);}int main(){ int _; scanf("%d",&_); while(_--) solve(); return 0;}
0 0
- 【HDU 5471】Count the Grid
- hdu 5471 Count the Grid(容斥)
- DP HDOJ 5471 Count the Grid
- hdu 1705 Count the grid (皮克定理)
- HDU 1705 Count the grid && jisuanke 35 三角形内点
- HDU 1705 Count the grid(皮克定理的应用)
- Count the Buildings HDU
- Count The Pairs HDU
- Count the string HDU
- Count the string HDU
- Count the Tetris HDU
- HDU 3336 Count the string
- hdu 1131 Count the Trees
- hdu 3336 Count the string
- hdu 4372 Count the Buildings
- hdu 3336 Count the string
- hdu 3336 Count the string
- hdu 1131 Count the Trees
- BNUoj Rectangle 在矩形中查找矩形的种类
- php中使用memcache扩展的性能问题
- hdu 5480(前缀和)
- Swift教程-视频拍摄教程
- java菜鸟1:jdk 安装
- 【HDU 5471】Count the Grid
- ***Shortest Palindrome
- POJ 3262 Protecting the Flowers(贪心)
- 黑马程序员——String类及其常见操作
- cocos2dx and c++ 的小知识点
- SQL Server2008报错:值不能为空
- 自省随想
- POJ 3216 Repairing Company (floyd + 最小路径覆盖)
- Android 阻止反编译和 如何反编译重写打包