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题已进行过选择。

  1. /** @Date : 2017-03-27-15.47
  2. * @Author : Lweleth (SoungEarlf@gmail.com)
  3. * @Link : https://github.com/
  4. * @Version :
  5. */
  6. #include<bits/stdc++.h>
  7. #define LL long long
  8. #define PII pair
  9. #define MP(x, y) make_pair((x),(y))
  10. #define fi first
  11. #define se second
  12. #define PB(x) push_back((x))
  13. #define MMG(x) memset((x), -1,sizeof(x))
  14. #define MMF(x) memset((x),0,sizeof(x))
  15. #define MMI(x) memset((x), INF, sizeof(x))
  16. using namespace std;
  17.  
  18. const int INF = 0x3f3f3f3f;
  19. const int N = 1e5+20;
  20. const double eps = 1e-8;
  21.  
  22. LL gcd(LL a, LL b)
  23. {
  24. return b?gcd(b, a % b):a;
  25. }
  26.  
  27. LL fac(int x)
  28. {
  29. LL ans = 1;
  30. while(x)
  31. {
  32. ans *= x;
  33. x--;
  34. }
  35. return ans;
  36. }
  37. LL a[15][15];
  38. LL dp[(1 << 12)+20][510];
  39. int main()
  40. {
  41. int T;
  42. cin >> T;
  43. while(T--)
  44. {
  45. LL n, k;
  46. cin >> n >> k;
  47. for(int i = 0; i < n; i++)
  48. {
  49. for(int j = 0; j < n; j++)
  50. {
  51. scanf("%lld", &a[i][j]);
  52. }
  53. }
  54. MMF(dp);
  55. dp[0][0] = 1;
  56. for(int s = 0; s < (1 << n); s++)//枚举旧状态
  57. {
  58. int cnt = 0;
  59. int t = s;
  60. while(t)
  61. {
  62. if(t & 1)
  63. cnt++;
  64. t >>= 1;
  65. }
  66. for(int i = 0; i < n; i++)
  67. {
  68. if((1 << i) & s) continue;
  69. for(int j = k; j >= 0; j--)
  70. {
  71. if(j >= a[cnt][i])
  72. dp[s | (1 << i)][j] += dp[s][j - a[cnt][i]];//
  73. }
  74. }
  75. }
  76. LL ans = 0;
  77. for(int i = 0; i < k; i++)
  78. {
  79. ans += dp[(1 << n) - 1][i];//求不符合要去的
  80. }
  81. //cout << ans << endl;
  82. LL x = fac(n);
  83. LL y = x - ans;
  84. LL g = 1;
  85. g = gcd(x, y);
  86. if(y == 0)
  87. printf("No solution\n");
  88. else printf("%lld/%lld\n", x/g, y/g);
  89. }
  90. return 0;
  91. }

ZOJ 3777 B - Problem Arrangement 状压DP的更多相关文章

  1. ZOJ 3777 - Problem Arrangement - [状压DP][第11届浙江省赛B题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 Time Limit: 2 Seconds      Me ...

  2. zoj3777 Problem Arrangement(状压dp,思路赞)

    The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward i ...

  3. 2014 Super Training #4 B Problem Arrangement --状压DP

    原题:ZOJ 3777  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 题意:给每个题目安排在每个位置的value ...

  4. FZU - 2218 Simple String Problem(状压dp)

    Simple String Problem Recently, you have found your interest in string theory. Here is an interestin ...

  5. ZOJ 3777-Problem Arrangement(状压DP)

    B - Problem Arrangement Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %l ...

  6. ZOJ 2563 Long Dominoes(状压DP)

    给定一个m*n的方格子,要求用3*1的骨牌去覆盖,骨牌可以用横放或者竖放,问最终有多少种放置方式,将其铺满. 分析:由于最多30行,每行最多9列,所以可以按行来dp,设计每行的状态从而进行转移,考虑每 ...

  7. ZOJ 3471 Most Powerful (状压DP,经典)

    题意: 有n个原子,每当两个原子碰撞时就会产生能量,并且消耗其中一个原子.已知每两个原子碰撞时消耗其中指定一个原子所产生的能量,问最多能产生多少能量? 思路: 一开始以为是找一个有序序列,使得能量最大 ...

  8. zoj 3471 Most Powerful(状压dp+Tsp问题+连续性问题)

    上来直接一波敲键盘,直接套Tsp问题的代码 然后WA 发现貌似这道题没有连续性. Tsp问题是一条路径,一个点到另一个点,多了一个限制,所以就需要加多一维 而这道题没有限制,也就是说那一维不需要加,我 ...

  9. ZOJ 2563 Long Dominoes(状压DP)题解

    题意:n*m的格子,用1 * 3的矩形正好填满它,矩形不能重叠,问有几种填法 思路:poj2411进阶版.我们可以知道,当连续两行的摆法确定,那么接下来的一行也确定.当第一行还有空时,这时第三行必须要 ...

随机推荐

  1. springMVC 流程

    springMVC流程控制 SpringMVC流程 web.xml 中配置 org.springframework.web.servlet.DispatcherServlet 这一步其实和spring ...

  2. 团队项目-BUG挖掘

    测试硬件: 华为畅享5 测试平台: 安卓5.1 测试项目Git地址: https://github.com/RABITBABY/We-have-bing 测试Apk来源地址: http://www.a ...

  3. 1029 C语言文法翻译(2)

    program à external_declaration | program external_declaration 翻译:<源程序>→ <外部声明> | <源程序 ...

  4. ant build.xml 解释!

    Ant的概念  Make命令是一个项目管理工具,而Ant所实现功能与此类似.像make,gnumake和nmake这些编译工具都有一定的缺陷,但是Ant却克服了这些工具的缺陷.最初Ant开发者在开发跨 ...

  5. 微信Web开发者工具-下载、安装和使用图解

    开发和测试小程序,需要借助微信官方提供的微信Web开发者工具进行预览和调试代码,从下载安装到使用,大致的流程如下: 1.下载安装包 下载地址传送门:https://developers.weixin. ...

  6. Mybatis 类属性和字段映射小小分析

    在上一篇 [Mybatis 点点滴滴]博客中,写到了 Mybatis 能够将类属性和表字段自动对应起来,在 parameterType属性值直接填写 POJO 类的名称即可(首字母不区分大小写),在 ...

  7. 【设计模式】C++中的单例模式

    讲解来自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&id=4281275&uid=26611383 由于使用了POSIX函 ...

  8. 第134天:移动web开发的一些总结(二)

    1.响应式布局 开发一个页面,在所有的设备上都能够完美展示. 媒体查询:@media screen and (max-width:100px) { } 媒体类型:screen(屏幕) print(打印 ...

  9. 【EF】EF Code-First数据迁移

    Code-First数据迁移  首先要通过NuGet将EF升级至最新版本. 新建MVC 4项目MvcMigrationDemo 添加数据模型 Person 和 Department,定义如下: usi ...

  10. 下载文件 通过a 标签 请求某个servlet进行下载的

    下载文件 通过a 标签 请求某个servlet进行下载的