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] 149. Max Points on a Line 共线点个数

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  2. POJ8469 特殊密码锁

    总时间限制: 1000ms 内存限制: 1024kB 描述 有一种特殊的二进制密码锁,由n个相连的按钮组成(n<30),按钮有凹/凸两种状态,用手按按钮会改变其状态. 然而让人头疼的是,当你按一 ...

  3. python基础(三)-- 文件操作

    一. 文件操作: 对文件操作流程 1.打开文件,得到文件句柄并赋值给一个变量 2.通过句柄对文件进行操作 3.关闭文件 现有文件如下 : Somehow, it seems the love I kn ...

  4. ubuntu16.04+cuda9.0_cudnn7.5+tensorflow-gpu==1.12.0

    1.查找可用的tensorflow源,该命令运行后终端会输出所有可用的源 anaconda search -t conda tensorflow 2.这里name是上一步中输出源的tensorflow ...

  5. nginx实现tcp负载均衡

    1 安装支持库 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel yum install pcre-d ...

  6. vue通过ajax加载json数据

    HTML <ul id="Hanapp"> <li class="styVue" v-for="item in actList&qu ...

  7. Asp.Net Core异常处理

    本文将介绍在ASP.Net Core中处理异常的几种方法 1使用开发人员异常页面(The developer exception page) 2配置HTTP错误代码页 Configuring stat ...

  8. IP核——RAM

    一.Quartus 1.打开Quartus ii,点击Tools---MegaWizard Plug-In Manager 2.弹出创建页面,选择Creat a new custom megafunc ...

  9. Huawei重新开启隐藏桌面功能

    在HUAWEI的EMUI系统7.0的时候我们都能发现桌面上靠手指操作的隐藏桌面的功能,像这样: 但是在之后的EMUI8.0.9.0,之后就没有办法用了.后来问了官方,这个功能确实是被去掉了.个人也很不 ...

  10. numpy模块之axis(转)

    转自:https://blog.csdn.net/fangjian1204/article/details/53055219