链接:

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;
}

最新文章

  1. Signalr系列之虚拟目录详解与应用中的CDN加速实战
  2. VC程序获取管理员权限
  3. MySQL时间分组查询
  4. C# 在執行程式目錄下產生文件夾
  5. Java并发编程实例(synchronized)
  6. 深度解析Java8 – AbstractQueuedSynchronizer的实现分析(下)
  7. 【Android】Activity生命周期(亲测)
  8. jstack命令(Java Stack Trace)
  9. Spring 3.x企业应用开发实战(14)----事务
  10. 网易云课堂_程序设计入门-C语言_期末考试编程题
  11. 自己实现的Boost库中的lexical_cast随意类型转换
  12. 简单实现Android平台多语言
  13. threading多线程总结
  14. cnblogs第一天
  15. 实战开发-》融云tp3.2.3
  16. Pycharm 2018.2.1最新版破解到2099年图解教程
  17. C++ STL标准容器插入删除算法的复杂度
  18. 应收事物处理删除 SQL 语句
  19. jvm高级特性(5)(1)(原子性,可见性,有序性,volatile,概述)
  20. 使用CAReplicatorLayer [2]

热门文章

  1. java23种设计模式之七: 观察者模式
  2. vue中使用第三方插件animate.css实现动画效果
  3. oracle分区表原理学习
  4. 小木棒HDU1455(DFS+剪枝)
  5. MySQL SQL Training
  6. MyBatis 示例-动态 SQL
  7. Idea+Maven部署打包JavaFX项目遇到的坑
  8. Django中 auto_now_add 和 auto_now 的区别
  9. Ubuntu18.04通过网线共享网络
  10. plsql developer字符集和oracle字符集不一致的解决方法(转)