ZOJ 3777 B - Problem Arrangement 状压DP
LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777
题意:有N(\( N <= 12 \))道题,排顺序,当某道题选择了放在第j个位置时,会获得Pij的点数,但其他题目就不能选择j位置了。要求总获得的点数要大于M(\(1 <= M <= 500\)),问在所有方案中选择出合法方案的次数的期望值。
思路:题目要求的期望值,比较简单,就是总方案数和合法方案数的比值。看题目就知道是个DP,重点在于状态设计上。首先我们考虑到每个j位置只能被选择一次,也就意味着所有N门课所具有的状态是不同,而且某题选择位置后就不用再考虑它其他位置上的点数,即无后效性,这时很容易想到状压,二进制位对应当前状态下某个位置是否已被选择。
dp[s][j]代表s状态,不小于j点数的方案数。转移上,枚举旧状态,再枚举下一状态所有可行的选择方案(没被选择过的位置),旧状态到新状态。嘛,就是个背包。dp[s | (1<<k)][j] += dp[s][j - p[cnt][k]] 其中cnt表示前cnt题已进行过选择。
- /** @Date : 2017-03-27-15.47
- * @Author : Lweleth (SoungEarlf@gmail.com)
- * @Link : https://github.com/
- * @Version :
- */
- #include<bits/stdc++.h>
- #define LL long long
- #define PII pair
- #define MP(x, y) make_pair((x),(y))
- #define fi first
- #define se second
- #define PB(x) push_back((x))
- #define MMG(x) memset((x), -1,sizeof(x))
- #define MMF(x) memset((x),0,sizeof(x))
- #define MMI(x) memset((x), INF, sizeof(x))
- using namespace std;
- const int INF = 0x3f3f3f3f;
- const int N = 1e5+20;
- const double eps = 1e-8;
- LL gcd(LL a, LL b)
- {
- return b?gcd(b, a % b):a;
- }
- LL fac(int x)
- {
- LL ans = 1;
- while(x)
- {
- ans *= x;
- x--;
- }
- return ans;
- }
- LL a[15][15];
- LL dp[(1 << 12)+20][510];
- int main()
- {
- int T;
- cin >> T;
- while(T--)
- {
- LL n, k;
- cin >> n >> k;
- for(int i = 0; i < n; i++)
- {
- for(int j = 0; j < n; j++)
- {
- scanf("%lld", &a[i][j]);
- }
- }
- MMF(dp);
- dp[0][0] = 1;
- for(int s = 0; s < (1 << n); s++)//枚举旧状态
- {
- int cnt = 0;
- int t = s;
- while(t)
- {
- if(t & 1)
- cnt++;
- t >>= 1;
- }
- for(int i = 0; i < n; i++)
- {
- if((1 << i) & s) continue;
- for(int j = k; j >= 0; j--)
- {
- if(j >= a[cnt][i])
- dp[s | (1 << i)][j] += dp[s][j - a[cnt][i]];//
- }
- }
- }
- LL ans = 0;
- for(int i = 0; i < k; i++)
- {
- ans += dp[(1 << n) - 1][i];//求不符合要去的
- }
- //cout << ans << endl;
- LL x = fac(n);
- LL y = x - ans;
- LL g = 1;
- g = gcd(x, y);
- if(y == 0)
- printf("No solution\n");
- else printf("%lld/%lld\n", x/g, y/g);
- }
- return 0;
- }
ZOJ 3777 B - Problem Arrangement 状压DP的更多相关文章
- ZOJ 3777 - Problem Arrangement - [状压DP][第11届浙江省赛B题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 Time Limit: 2 Seconds Me ...
- zoj3777 Problem Arrangement(状压dp,思路赞)
The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward i ...
- 2014 Super Training #4 B Problem Arrangement --状压DP
原题:ZOJ 3777 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 题意:给每个题目安排在每个位置的value ...
- FZU - 2218 Simple String Problem(状压dp)
Simple String Problem Recently, you have found your interest in string theory. Here is an interestin ...
- ZOJ 3777-Problem Arrangement(状压DP)
B - Problem Arrangement Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %l ...
- ZOJ 2563 Long Dominoes(状压DP)
给定一个m*n的方格子,要求用3*1的骨牌去覆盖,骨牌可以用横放或者竖放,问最终有多少种放置方式,将其铺满. 分析:由于最多30行,每行最多9列,所以可以按行来dp,设计每行的状态从而进行转移,考虑每 ...
- ZOJ 3471 Most Powerful (状压DP,经典)
题意: 有n个原子,每当两个原子碰撞时就会产生能量,并且消耗其中一个原子.已知每两个原子碰撞时消耗其中指定一个原子所产生的能量,问最多能产生多少能量? 思路: 一开始以为是找一个有序序列,使得能量最大 ...
- zoj 3471 Most Powerful(状压dp+Tsp问题+连续性问题)
上来直接一波敲键盘,直接套Tsp问题的代码 然后WA 发现貌似这道题没有连续性. Tsp问题是一条路径,一个点到另一个点,多了一个限制,所以就需要加多一维 而这道题没有限制,也就是说那一维不需要加,我 ...
- ZOJ 2563 Long Dominoes(状压DP)题解
题意:n*m的格子,用1 * 3的矩形正好填满它,矩形不能重叠,问有几种填法 思路:poj2411进阶版.我们可以知道,当连续两行的摆法确定,那么接下来的一行也确定.当第一行还有空时,这时第三行必须要 ...
随机推荐
- springMVC 流程
springMVC流程控制 SpringMVC流程 web.xml 中配置 org.springframework.web.servlet.DispatcherServlet 这一步其实和spring ...
- 团队项目-BUG挖掘
测试硬件: 华为畅享5 测试平台: 安卓5.1 测试项目Git地址: https://github.com/RABITBABY/We-have-bing 测试Apk来源地址: http://www.a ...
- 1029 C语言文法翻译(2)
program à external_declaration | program external_declaration 翻译:<源程序>→ <外部声明> | <源程序 ...
- ant build.xml 解释!
Ant的概念 Make命令是一个项目管理工具,而Ant所实现功能与此类似.像make,gnumake和nmake这些编译工具都有一定的缺陷,但是Ant却克服了这些工具的缺陷.最初Ant开发者在开发跨 ...
- 微信Web开发者工具-下载、安装和使用图解
开发和测试小程序,需要借助微信官方提供的微信Web开发者工具进行预览和调试代码,从下载安装到使用,大致的流程如下: 1.下载安装包 下载地址传送门:https://developers.weixin. ...
- Mybatis 类属性和字段映射小小分析
在上一篇 [Mybatis 点点滴滴]博客中,写到了 Mybatis 能够将类属性和表字段自动对应起来,在 parameterType属性值直接填写 POJO 类的名称即可(首字母不区分大小写),在 ...
- 【设计模式】C++中的单例模式
讲解来自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&id=4281275&uid=26611383 由于使用了POSIX函 ...
- 第134天:移动web开发的一些总结(二)
1.响应式布局 开发一个页面,在所有的设备上都能够完美展示. 媒体查询:@media screen and (max-width:100px) { } 媒体类型:screen(屏幕) print(打印 ...
- 【EF】EF Code-First数据迁移
Code-First数据迁移 首先要通过NuGet将EF升级至最新版本. 新建MVC 4项目MvcMigrationDemo 添加数据模型 Person 和 Department,定义如下: usi ...
- 下载文件 通过a 标签 请求某个servlet进行下载的
下载文件 通过a 标签 请求某个servlet进行下载的