uvalive4794(集合+状态压缩)
2015-02-13 13:41
309 查看
题意:
有一块x*y的巧克力,每次可以切一刀,问切若干刀后(不能两块同时切),可不可以切成n块,面积分别是a1,a2...an;的巧克力:
思路:
看到final的题就怂了;
最后还是参考了大白,还有网上的题解:
http://www.cnblogs.com/hlmark/p/4059654.html
有一块x*y的巧克力,每次可以切一刀,问切若干刀后(不能两块同时切),可不可以切成n块,面积分别是a1,a2...an;的巧克力:
思路:
看到final的题就怂了;
最后还是参考了大白,还有网上的题解:
http://www.cnblogs.com/hlmark/p/4059654.html
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 16; const int M = 100 + 10; int n, part , sum[1<<N], f[1<<N][M], vis[1<<N][M]; int bitcount(int x) { return x == 0 ? 0 : bitcount(x / 2) + (x & 1); } int dp(int sta, int x) { if(vis[sta][x]) return f[sta][x]; vis[sta][x] = 1; int& ans = f[sta][x]; if(bitcount(sta) == 1) return ans = 1; int y = sum[sta] / x; for(int sta0 = (sta - 1) & sta; sta0 != 0; sta0 = (sta0 - 1) & sta) { int sta1 = sta-sta0; if(sum[sta0] % x == 0 && dp(sta0, min(x, sum[sta0] / x)) && dp(sta1, min(x, sum[sta1] / x))) return ans = 1; if(sum[sta0] % y == 0 && dp(sta0, min(y, sum[sta0] / y)) && dp(sta1, min(y, sum[sta1] / y))) return ans = 1; } return ans = 0; } int main() { int cas = 0, n, x, y; while(scanf("%d", &n) && n) { scanf("%d%d", &x, &y); for(int i = 0; i < n; i++) scanf("%d", &part[i]); memset(sum, 0, sizeof(sum)); for(int i = 0; i < (1<<n); i++) for(int j = 0; j < n; j++) if(i & (1 << j)) sum[i] += part[j]; memset(vis, 0, sizeof(vis)); int ans; if(sum[(1 << n) - 1] != x*y || sum[(1 << n) - 1] % x != 0) ans = 0; else ans = dp((1 << n) - 1, min(x,y)); printf("Case %d: %s\n", ++cas, ans ? "Yes" : "No"); } return 0; }
相关文章推荐
- UVALive4794[Sharing Chocolate] 状态压缩动态规划
- UVALive 6432 —— Influence(记忆化搜索 + 状态压缩)
- uvalive 7834 状态压缩dp
- UVaLive 2965 Jurassic Remains (状态压缩)
- UVAlive 4999 状态压缩DP+最短路
- UVALive 7721 K - 2-ME Set 集合dp,所有数的位或来表示状态。
- UVALive 2031 Dance Dance Revolution (舞步转移,状态压缩DP,4级)
- UVALive - 3693 Balancing the Scale 枚举 + 状态压缩
- UVALive 2031 Dance Dance Revolution (舞步转移,状态压缩DP,4级)
- UVA - 1252 UVALive - 4643 状态压缩dp
- UVA11795——Mega Mans Missions(集合DP,状态压缩)
- UVALive 3953 Strange Billboard (状态压缩+枚举)
- 例题1.25 侏罗纪 Jurassic Remains UVALive - 2965 状态压缩 + 中途相遇法
- UVa 11795 - Mega Man's Mission (集合DP 状态压缩)
- UVALive 6625 Diagrams & Tableaux (状态压缩DP)
- uvalive2965(状态压缩)
- UVALive 6625_状态压缩
- uvalive 2965(状态压缩)
- UVALive - 4643 Twenty Questions (状态压缩)
- UVA 11825 集合枚举 状态压缩 dp