Lesson Timetable

题目链接https://www.codeforces.com/contest/37/problem/D

数据范围:略。


题解

根本就没想到可以动态规划。

首先从前往后处理,仔细一想是有道理的。

因为如果处理到了$i$,那么前面的所有值都是可以对当前值有贡献的。

即,我们设状态$dp_{i, j}$表示前$i$个教室有$j$个人(第二节课)的方案数。

最后用阶乘即可。

代码

  1. #include <bits/stdc++.h>
  2.  
  3. #define setIO(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
  4.  
  5. #define N 1010
  6.  
  7. using namespace std;
  8.  
  9. typedef long long ll;
  10.  
  11. const int mod = 1000000007;
  12.  
  13. char *p1, *p2, buf[100000];
  14.  
  15. #define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++ )
  16.  
  17. int rd() {
  18. int x = 0;
  19. char c = nc();
  20. while (c < 48) {
  21. c = nc();
  22. }
  23. while (c > 47) {
  24. x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
  25. }
  26. return x;
  27. }
  28.  
  29. int fac[N], inv[N];
  30.  
  31. int qpow(int x, int y) {
  32. int ans = 1;
  33. while (y) {
  34. if (y & 1) {
  35. ans = (ll)ans * x % mod;
  36. }
  37. y >>= 1;
  38. x = (ll)x * x % mod;
  39. }
  40. return ans;
  41. }
  42.  
  43. void init() {
  44. fac[0] = 1;
  45. inv[0] = 1;
  46. for (int i = 1; i < N; i ++ ) {
  47. fac[i] = (ll)fac[i - 1] * i % mod;
  48. inv[i] = qpow(fac[i], mod - 2);
  49. }
  50. }
  51.  
  52. int dp[1010][1010], bfr[1010], x[1010], y[1010];
  53.  
  54. int C(int x, int y) {
  55. return (ll)fac[x] * inv[y] % mod * inv[x - y] % mod;
  56. }
  57.  
  58. int main() {
  59. setIO("num");
  60. init();
  61. int m = rd();
  62. for (int i = 1; i <= m; i ++ ) {
  63. x[i] = rd(), bfr[i] = bfr[i - 1] + x[i];
  64. }
  65. for (int i = 1; i <= m; i ++ ) {
  66. y[i] = rd();
  67. }
  68. dp[0][0] = 1;
  69. for (int i = 1; i <= m; i ++ ) {
  70. for (int j = 0; j <= bfr[i]; j ++ ) {
  71. for (int k = 0; k <= min(j, y[i]); k ++ ) {
  72. (dp[i][j] += (ll)dp[i - 1][j - k] * C(bfr[i] - (j - k), k) % mod) %= mod;
  73. }
  74. }
  75. }
  76. // cout << dp[m][bfr[m]] << endl ;
  77. int ans = (ll)dp[m][bfr[m]] * fac[bfr[m]] % mod;
  78. for (int i = 1; i <= m; i ++ ) {
  79. ans = (ll)ans * inv[x[i]];
  80. }
  81. cout << ans << endl ;
  82. return 0;
  83. }

[CF37D]Lesson Timetable_动态规划的更多相关文章

  1. Codeforces 37D Lesson Timetable - 组合数学 - 动态规划

    题目传送门 神奇的门I 神奇的门II 题目大意 有$n$组学生要上课2次课,有$m$个教室,编号为$1$到$m$.要确定有多少种不同的安排上课的教室的方案(每组学生都是本质不同的),使得它们满足: 每 ...

  2. Codeforces 946 D.Timetable-数据处理+动态规划(分组背包) 处理炸裂

    花了两个晚上来搞这道题. 第一个晚上想思路和写代码,第二个晚上调试. 然而还是菜,一直调不对,我的队友是Debug小能手呀(真的是无敌,哈哈,两个人一会就改好了) D. Timetable   tim ...

  3. Lesson 18 He often does this!

    Text After I had had lunch at a village pub, I looked for my bag. I had left it on a chair beside th ...

  4. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

  5. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  6. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  7. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  8. C#动态规划查找两个字符串最大子串

     //动态规划查找两个字符串最大子串         public static string lcs(string word1, string word2)         {            ...

  9. C#递归、动态规划计算斐波那契数列

    //递归         public static long recurFib(int num)         {             if (num < 2)              ...

随机推荐

  1. 搭建自己的博客(二十二):通过ajax提交评论信息,并增加公式编辑功能

    编辑功能使用到了ckeditor的MathJax组件.ajax提交评论可以不用刷新浏览器. 1.变化的部分

  2. grafana+mysql忘记admin密码解决方法

    通过mysql修改admin的密码. 我之前注册了一个用户,所以我直接复制lvusyy这个用户的密码即可. update user inner join (select password,salt,r ...

  3. Python的is和==

    is是对比地址:==是对比值

  4. Codeforces Round #501 (Div. 3)

    A - Points in Segments 题意:implement #include<bits/stdc++.h> using namespace std; typedef long ...

  5. vue中class用法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. HttpClient学习(一)—— 基本使用

    HttpClient是支持Http协议的客户端编程工具包. 一.简单使用 1.1 引入依赖 <dependency> <groupId>org.apache.httpcompo ...

  7. IdentityServer4入门四:应用Implicit模式保护网站(上)

    我们先新增一个网站,名为“ClientMvc",也是asp.net core Web应用程序(模型视图控制器) 使用nuget安装以下引用 Microsoft.AspNetCore.Auth ...

  8. 标准6轴机器人正反解(1)-坐标系和MDH参数表

    刚来新公司不久,部门给安排了新人作业,我被分到的任务是求标准6轴机器人的正反解,以及利用就近原则选择最优解.从今天开始,逐步将这部分内容总结出来: 本文以及后续文章均使用改进DH法: 连杆坐标系: 坐 ...

  9. OpenResty之ngx.shared.DICT

    参考链接: resty.core.shdict ngx_shared.DICT 源码正文: dict.lua 部分源码如下: local ffi = require 'ffi' local base ...

  10. 利用iis创建网站后为什么不能设置主机名

    主机名 主机名就是网站的域名,通俗说就是网站地址(如:www.baidu.com). 设置了主机名,而IIS确不知道主机名对应的地址在哪里. 举个例子,把www.baidu.com做为IIS网站的主机 ...