Codeforces Round #589 (Div. 2) E. Another Filling the Grid(DP, 组合数学)
链接:
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.
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
LL C[300][300];
LL Dp[300][300];
LL M1[300], M2[300];
LL n, k;
int main()
{
C[0][0] = C[1][0] = C[1][1] = 1;
for (int i = 2;i <= 250;i++)
{
C[i][0] = C[i][i] = 1;
for (int j = 1;j < i;j++)
C[i][j] = (C[i-1][j]+C[i-1][j-1])%MOD;
}
M1[0] = M2[0] = 1;
cin >> n >> k;
for (int i = 1;i <= n;i++)
M1[i] = (M1[i-1]*k)%MOD, M2[i] = (M2[i-1]*(k-1))%MOD;
//k^i
for (int i = 1;i <= n;i++)
Dp[1][i] = (C[n][i]*M2[n-i])%MOD;
for (int i = 2;i <= n;i++)
{
for (int j = 1;j <= n;j++)
{
for (int p = j;p <= n;p++)
{
LL res = ((C[n-j][p-j]*M2[n-p])%MOD*M1[j])%MOD;
if (p == j)
res = ((M1[j]-M2[j])*M2[n-j])%MOD;
LL sum = (Dp[i-1][j]*res)%MOD;
Dp[i][p] = (Dp[i][p]%MOD + sum + MOD)%MOD;
}
}
}
cout << Dp[n][n] << endl;
return 0;
}
Codeforces Round #589 (Div. 2) E. Another Filling the Grid(DP, 组合数学)的更多相关文章
- 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\) ...
- Codeforces Round #589 (Div. 2) (e、f没写)
https://codeforces.com/contest/1228/problem/A A. Distinct Digits 超级简单嘻嘻,给你一个l和r然后寻找一个数,这个数要满足的条件是它的每 ...
- 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 ...
- Codeforces Round #589 (Div. 2)
目录 Contest Info Solutions A. Distinct Digits B. Filling the Grid C. Primes and Multiplication D. Com ...
- 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 ...
- Codeforces Round #589 (Div. 2) Another Filling the Grid (dp)
题意:问有多少种组合方法让每一行每一列最小值都是1 思路:我们可以以行为转移的状态 附加一维限制还有多少列最小值大于1 这样我们就可以不重不漏的按照状态转移 但是复杂度确实不大行(减了两个常数卡过去的 ...
- Codeforces Round 589 (Div. 2) 题解
Is that a kind of fetishism? No, he is objectively a god. 见识了一把 Mcdic 究竟出题有多神. (虽然感觉还是吹过头了) 开了场 Virt ...
- Codeforces Round #589 (Div. 2) D. Complete Tripartite(染色)
链接: https://codeforces.com/contest/1228/problem/D 题意: You have a simple undirected graph consisting ...
- Codeforces Round #589 (Div. 2) C - Primes and Multiplication(数学, 质数)
链接: https://codeforces.com/contest/1228/problem/C 题意: Let's introduce some definitions that will be ...
随机推荐
- LUA的table实现
数据结构 下面的结构体是lua中所定义的table typedef struct Table { CommonHeader; lu_byte flags; /* 1<<p means ta ...
- windows scala helloworld例子详解
[学习笔记] windows scala helloworld例子详解: 在操作系统中,我们的Test3.scala会生成Test3.class,然后class文件被虚拟机加载并执行, 这一点和jav ...
- python中int是什么类型
python中的基本数据类型 1:虽然python中的变量不需要声明,但使用时必须赋值整形变量浮点型变量字符型2:可以一个给多个变量赋值,也可以多个给多个变量赋值3:python3中有6个标准数据类型 ...
- Numpy学习之——数组创建
Numpy学习之--数组创建 过程展示 import numpy as np a = np.array([2,3,9]) a array([2, 3, 9]) a.dtype dtype('int32 ...
- fiddler笔记:统计选项卡(Statistics)
Request Count 选中的Session数. Bytes sent Http请求头和请求体中向外发送的字节总数. Bytes received HTTP请求头和请求体中接收到的所有字节数. R ...
- JS基础_全局作用域
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- ajax 请求二进制流 图片
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> ...
- 15 Django之Celery发送邮件
异步任务--celery发送邮件 安装两个python包: pip install celery==3.1.25 pip install django-celery==3.2.1 pip instal ...
- SpringCloud之Feign声明式调用原理及配置
1 什么是Feign Feign是一种声明式.模板化的HTTP客户端(仅在Application Client中使用).声明式调用是指,就像调用本地方法一样调用远程方法,无需感知操作远程http请求. ...
- Qt 中配置 c99的问题
Qt 5.3 版本 报错原因是c99标准问题的话,可以尝试下面方法 打开项目中xxx.pro工程文件 加入如下语句: QMAKE_CFLAGS += -std=c99