链接:

https://codeforces.com/contest/1228/problem/E

题意:

You have n×n square grid and an integer k. Put an integer in each cell while satisfying the conditions below.

All numbers in the grid should be between 1 and k inclusive.

Minimum number of the i-th row is 1 (1≤i≤n).

Minimum number of the j-th column is 1 (1≤j≤n).

Find the number of ways to put integers in the grid. Since the answer can be very large, find the answer modulo (109+7).

These are the examples of valid and invalid grid when n=k=2.

思路:

Dp[i][j] 表示前i行有j列有1同时保证每一行都有1,考虑转移, 当转移上下两行列的1数相等时.

单独考虑, 1的列可以是任意值,但是必须存在一个1保证当前行存在1.

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const int MOD = 1e9+7;
  5. LL C[300][300];
  6. LL Dp[300][300];
  7. LL M1[300], M2[300];
  8. LL n, k;
  9. int main()
  10. {
  11. C[0][0] = C[1][0] = C[1][1] = 1;
  12. for (int i = 2;i <= 250;i++)
  13. {
  14. C[i][0] = C[i][i] = 1;
  15. for (int j = 1;j < i;j++)
  16. C[i][j] = (C[i-1][j]+C[i-1][j-1])%MOD;
  17. }
  18. M1[0] = M2[0] = 1;
  19. cin >> n >> k;
  20. for (int i = 1;i <= n;i++)
  21. M1[i] = (M1[i-1]*k)%MOD, M2[i] = (M2[i-1]*(k-1))%MOD;
  22. //k^i
  23. for (int i = 1;i <= n;i++)
  24. Dp[1][i] = (C[n][i]*M2[n-i])%MOD;
  25. for (int i = 2;i <= n;i++)
  26. {
  27. for (int j = 1;j <= n;j++)
  28. {
  29. for (int p = j;p <= n;p++)
  30. {
  31. LL res = ((C[n-j][p-j]*M2[n-p])%MOD*M1[j])%MOD;
  32. if (p == j)
  33. res = ((M1[j]-M2[j])*M2[n-j])%MOD;
  34. LL sum = (Dp[i-1][j]*res)%MOD;
  35. Dp[i][p] = (Dp[i][p]%MOD + sum + MOD)%MOD;
  36. }
  37. }
  38. }
  39. cout << Dp[n][n] << endl;
  40. return 0;
  41. }

Codeforces Round #589 (Div. 2) E. Another Filling the Grid(DP, 组合数学)的更多相关文章

  1. Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理

    Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理 [Problem Description] 在\(n\times n\) ...

  2. Codeforces Round #589 (Div. 2) (e、f没写)

    https://codeforces.com/contest/1228/problem/A A. Distinct Digits 超级简单嘻嘻,给你一个l和r然后寻找一个数,这个数要满足的条件是它的每 ...

  3. Codeforces Round #267 (Div. 2) C. George and Job(DP)补题

    Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...

  4. Codeforces Round #589 (Div. 2)

    目录 Contest Info Solutions A. Distinct Digits B. Filling the Grid C. Primes and Multiplication D. Com ...

  5. Codeforces Round #589 (Div. 2) B. Filling the Grid

    链接: https://codeforces.com/contest/1228/problem/B 题意: Suppose there is a h×w grid consisting of empt ...

  6. Codeforces Round #589 (Div. 2) Another Filling the Grid (dp)

    题意:问有多少种组合方法让每一行每一列最小值都是1 思路:我们可以以行为转移的状态 附加一维限制还有多少列最小值大于1 这样我们就可以不重不漏的按照状态转移 但是复杂度确实不大行(减了两个常数卡过去的 ...

  7. Codeforces Round 589 (Div. 2) 题解

    Is that a kind of fetishism? No, he is objectively a god. 见识了一把 Mcdic 究竟出题有多神. (虽然感觉还是吹过头了) 开了场 Virt ...

  8. Codeforces Round #589 (Div. 2) D. Complete Tripartite(染色)

    链接: https://codeforces.com/contest/1228/problem/D 题意: You have a simple undirected graph consisting ...

  9. Codeforces Round #589 (Div. 2) C - Primes and Multiplication(数学, 质数)

    链接: https://codeforces.com/contest/1228/problem/C 题意: Let's introduce some definitions that will be ...

随机推荐

  1. mybatis+mysql insert添加数据后返回数据主键id

    1.根据useGeneratedKeys获取返回值,部分数据库不支持 修改mybatis xml <insert id="insertUser" useGeneratedKe ...

  2. Http请求头和响应头(Get和Post)

    HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送 ...

  3. React—组件生命周期详解

    React—组件生命周期详解 转自 明明的博客  http://blog.csdn.net/slandove/article/details/50748473 (非原创) 版权声明:转载请注明出处,欢 ...

  4. Jquery中数组转字符串,c:foreach自动将带","字符串进行拆分赋值

    1.数组转字符串,逗号分割 a.push()将元素依次添加至数组: b.join()将数组转换成字符串,里面可以带参数分隔符,默认[,] <script type = text/javascri ...

  5. 如何使用RedisTemplate访问Redis数据结构之list

    Redis的List数据结构 这边我们把RedisTemplate序列化方式改回之前的 Jackson2JsonRedisSerializer<Object> jackson2JsonRe ...

  6. (二十二)自定义简化版JDBC(Dbutils框架的设计思想)

    目录 元数据概念 DataBaseMetaData ParameterMetaData ResultSetMetaData 编写简化版的JDBC O-R Mapping 概念 自定义简化版JDBC 元 ...

  7. python爬取网页数据并存储到mysql数据库

    #python 3.5 from urllib.request import urlopen from urllib.request import urlretrieve from bs4 impor ...

  8. Secret的三种形式

    Secret ConfigMap这个资源对象是Kubernetes当中非常重要的一个对象,一般情况下ConfigMap是用来存储一些非安全的配置信息,如果涉及到一些安全相关的数据的话用ConfigMa ...

  9. 阿里云Centos7 配置二级域名

    之前在自己的服务器上安装了laravel,现在给它个二级域名!结果发现了个小坑= =.不说了,上步骤 首先你要有个自己的域名,可以在万网上买一个,我的还是蛮便宜的... 进入你的阿里云管理台 选择云解 ...

  10. Python字符串常用的方法——真心觉得比java,c好用

    # Strings have many methods wo can use rand_string=" life is a beautiful struggle " print( ...