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


【Problem Description】

在\(n\times n\)的格子中填入\([1,k]\)之间的数字,并且保证每一行至少有一个\(1\),每一列至少有一个\(1\),问有多少种满足条件的填充方案。

【Solution】

令\(R[i]\)表示为第\(i\)行至少有一个\(1\)的方案数,\(C[i]\)表示第\(i\)列至少有一个\(1\)的方案数。则题目要求的就是\(\bigcap_{i=1}^nR[i]\cap C[i]\)。由容斥定理得:

\[\sum_{i=0}^{n} \sum_{j=0}^{n} (-1)^{i+j} \cdot {n\choose j} \cdot {n\choose i} \cdot k^{n^2 - n \cdot (i+j) + i \cdot j} \cdot (k-1)^{n \cdot (i+j) - i \cdot j}
\]

表示从\(n\)行中,选\(i\)行,从\(n\)列中选\(j\)列,选出\(n\cdot(i+j)-i\cdot j\)个格子不能放\(1\),这些格子有\((k-1)^{n\cdot (i+j)-i\cdot j}\)种放置方案,剩余的\(n^2-n\cdot (i+j)+i\cdot j\)有\(k^{n^2-n\cdot (i+j)+i\cdot j}\)种放置方案。


【Code】

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef int Int;
#define int long long
#define maxn 1005
#define INF 0x3f3f3f3f
const int mod=1e9+7;
int bit[maxn][maxn];
int fpow(int a,int b){
int ans=1;a%=mod;
while(b){
if(b&1) (ans*=a)%=mod;
(a*=a)%=mod;
b>>=1;
}
return ans;
}
Int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,k;cin>>n>>k;
for(int i=0;i<=n;i++) bit[i][0]=1;
for(int i=1;i<=n;i++){ //预处理组合数
for(int j=1;j<=i;j++){
bit[i][j]=(bit[i-1][j]+bit[i-1][j-1])%mod;
}
}
int ans=0;
for(int i=0;i<=n;i++){ //直接套公式即可
for(int j=0;j<=n;j++){
ans+=((i+j)&1?-1:1)*bit[n][i]%mod*bit[n][j]%mod*fpow(k,n*n-n*(i+j)+i*j)%mod*fpow(k-1,n*(i+j)-i*j)%mod;
ans%=mod;
}
}
cout<<(ans+mod)%mod<<endl;
return 0;
}

Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理的更多相关文章

  1. 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 ...

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

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

  3. Codeforces Round #589 (Div. 2)

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

  4. Educational Codeforces Round 37 G. List Of Integers (二分,容斥定律,数论)

    G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  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. [LeetCode] 495. Teemo Attacking 提莫攻击

    In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...

  2. CentOS 7.5二进制部署Kubernetes1.12(加密通信)(五)

    一.安装方式介绍 1.yum 安装 目前CentOS官方已经把Kubernetes源放入到自己的默认 extras 仓库里面,使用 yum 安装,好处是简单,坏处也很明显,需要官方更新 yum 源才能 ...

  3. 【神经网络与深度学习】【计算机视觉】SPPNet-引入空间金字塔池化改进RCNN

    转自: https://zhuanlan.zhihu.com/p/24774302?refer=xiaoleimlnote 继续总结一下RCNN系列.上篇RCNN- 将CNN引入目标检测的开山之作 介 ...

  4. Altera FPGA 远程升级有关的几个IP的使用

    在做在线远程升级的时候,一般需要两步:1.将数据写到外挂的flash中.2重新启动FPGA配置. 不过要做到远程升级,一般需要在原始程序中就考虑到加入远程升级模块,remote updata IP, ...

  5. SOC中的DMIPS_GFLOPS_GMACS的含义

    l  DMIPS全称叫Dhrystone MIPS 这项测试是用来计算同一秒内系统的处理能力,它的单位以百万来计算,也就是(MIPS) 上面的意思也就是,这个处理器测整数计算能力为(200*100万) ...

  6. vscode failed to excute git

    将代码提交到github时 提示 “failed to excute git” 解决:配置git git config --global user.email "youremailid@ex ...

  7. Appium移动端自动化测试--录制测试用例并运行

    目录 文章目录如下 录制用例并执行 1.使用Appium desktop录制用例 2.安装Pythony依赖Appium-Python-Client 3.增加隐式等待增强稳定性 4.重新运行 文章目录 ...

  8. 19.Python略有小成(面向对象Ⅰ)

    Python(面向对象Ⅰ) 一.面向对象初识 回顾面向过程编程与函数式编程 # 面向过程编程 测量对象的元素个个数. s1 = 'fjdsklafsjda' count = 0 for i in s1 ...

  9. 18 IO流(十五)——RandomAccessFile随机访问文件及使用它进行大文件切割的方法

    本文部分内容转自:https://blog.csdn.net/nightcurtis/article/details/51384126 1.RandomAccessFile特点 RandomAcces ...

  10. flink linux安装 单机版

    1.下载二进制的Flink,根据你喜欢的Hadoop/Scala版本选择对应的Flink版本. https://flink.apache.org/downloads.html2.选择存放目录 解压 f ...