链接:

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, 组合数学)的更多相关文章

  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. superslider样式

    .picScroll-left{ width: 1180px; position:relative; overflow: hidden; .bd{ ul{ li{ float: left; displ ...

  2. SQL SERVER创建表

    创建表 create table table_name ( column_name_1 data_type, column_name_2 data_type NOT NULL, column_name ...

  3. SQL0668N Operation not allowed for reason code "3" on table "TEST". SQLSTATE=57016

    问题描述: 查询,操作表都报如下错误 SQL0668N  Operation not allowed for reason code "3" on table "TEST ...

  4. ubuntu18上传代码到github

    其实在github上建仓库时候就提示你步骤了: 1.注册个github账号并登录 创建一个仓库 https://github.com/ 2.创建SSH Key ssh-keygen -t rsa -C ...

  5. Python何时执行装饰器

    装饰器的一个关键特性是,它们在被装饰的函数定义之后立即运行.这 通常是在导入时(即 Python 加载模块时),如示例 7-2 中的 registration.py 模块所示. 示例 7-2 regi ...

  6. 1、ECharts(中国地图篇)的使用

    一.网址:            http://echarts.baidu.com/download.html点击:            完整下载文件:        echarts.min.js ...

  7. 怎样写一个 "Hello, World!"

    第一步: 打开浏览器, 按 F12 键或 Ctrl + Shift + J.   注意:  1. 打开的这个界面是浏览器的开发者工具界面. 2. 顶部有许多Tab栏, 如: Elements / Co ...

  8. 怎样理解document的快捷方式属性

    所谓 "快捷方式属性" , 也就是说它们不是必须的, 只是在操作dom时可以更为方便地获取. 主要有下面8个: 1. 获取当前文档所属的window对象: document.def ...

  9. mac 下 vscode配置SFTP连接

    VScode中使用SFTP插件连接远程服务器进行文件修改 下载SFTP插件后,使用Ctrl+Shift+P.输入SFTP,选择第一个将会生成简短的默认配置文件 然后把sftp.json文件内内容换成以 ...

  10. 【ES6 】Promise

    Promise对象定义: 用来处理异步编程 Promise对象的特点 对象的状态不受外界影响 一旦状态改变,就不会再变,任何时候都可以得到这个结果 Promise对象的状态 pending(进行中) ...