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. idea切换工作目录后无法重启问题记录

    1.idea每次重新打开新项目或者切换新的工作空间后,总是半天起不来.有时候知道是缓存或者其他的问题,有时候莫名其妙就好了. 本次原因是:

  2. [简短问答]lodop打印过慢或有进度条

    问法1:打印预览显示进度条,过慢出现进度条,打印过慢,可能和很多原因有关:打印内容或样式或图片等过多,有需要下载有脚步执行或本身网络慢:机器性能过低 系统ie有问题或缓存过多:或使用的是共享打印机.如 ...

  3. [编程开发]STB image读取学习

    为了便于学习图像处理并研究图像算法, 俺写了一个适合初学者学习的小小框架. 麻雀虽小五脏俱全. 采用的加解码库:stb_image 官方:http://nothings.org/ stb_image. ...

  4. 看看这5个最容易犯的Java错误,你犯了没?

    人非圣贤,孰能无过.都说Java语言是一门简单的编程语言,基于C++演化而来,剔除了很多C++中的复杂特性,但这并不能保证Java程序员不会犯错.那么对于广大的Java程序员来说,它们最容易犯的几个错 ...

  5. Spell It Right

    Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...

  6. python 浅拷贝和深拷贝(9)

    何谓浅拷贝/深拷贝,说得直白一点,其实就是数据拷贝,两者到底有什么区别呢?听着就挺迷糊的,python开发项目的时候说不定你就能碰上这样的坑~~     一.普通的变量赋值 我们平常使用的变量赋值就是 ...

  7. LeetCode 32. 最长有效括号(Longest Valid Parentheses) 31

    32. 最长有效括号 32. Longest Valid Parentheses 题目描述 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 每日一算法2019/6/ ...

  8. 如何创建Kafka客户端:Avro Producer和Consumer Client

    1.目标 - Kafka客户端 在本文的Kafka客户端中,我们将学习如何使用Kafka API 创建Apache Kafka客户端.有几种方法可以创建Kafka客户端,例如最多一次,至少一次,以及一 ...

  9. new/delete与命名空间

    目录 1. new/delete 2. 命名空间 1. new/delete C++中的动态内存分配 C++通过new关键字进行动态内存申请 C++中的动态内存申请是基于类型进行的 delete关键字 ...

  10. Django项目常见面试问题

    1.python中的lambda是什么意思,可以举例 匿名函数 a = lambda x:x+1 print(a(1)) 2.请写出以下代码执行的结果 class Parent(object): x ...