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] 270. Closest Binary Search Tree Value 最近的二叉搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  2. PCL读取PCD文件的数据

    1.pcd文件——rabbit.pcd 链接:https://pan.baidu.com/s/1v6mjPjwd7fIqUSjlIGTIGQ提取码:zspx 新建项目pcl rabbit.pcd 和p ...

  3. mysql查询之 用户行程的取消率,人流量高峰时段

    1.用户行程的取消率 Trips 表中存所有出租车的行程信息.每段行程有唯一键 Id,Client_Id 和 Driver_Id 是 Users 表中 Users_Id 的外键.Status 是枚举类 ...

  4. 基于travis和git tag 实现npm自动化发版

    最近又把烂尾的开源项目alfred-femine拾起来了,这个项目旨在开发一系列前端常用的alfred workflow,提供前端开发的查询效率.时隔这么久,再次搞起,希望自己能够一直维护下去,也欢迎 ...

  5. Java集合对比

    1.array和ArrayList 的区别?1.1:ArrayList是Array的复杂版本1.2:数组不能扩容集合可以扩容1.3:存储的数据类型:Array只能存储相同数据类型的数据,而ArrayL ...

  6. Dubbo快速入门 一

    1.分布式基础理论 1.1).什么是分布式系统? “分布式系统是若干独立计算机的集合,这些计算机对于用户来说就像单个相关系统” 分布式系统(distributed system)是建立在网络之上的软件 ...

  7. AS3放大镜工具类

    package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Display ...

  8. Elasticsearch-6.7.0系列(八)开启kibana监控

    修改ES配置: 修改elasticsearch.yml,添加如下xpack配置: xpack.security.enabled: true xpack.ml.enabled: true xpack.l ...

  9. Self-paced Clustering Ensemble自步聚类集成论文笔记

    Self-paced Clustering Ensemble自步聚类集成论文笔记 2019-06-23 22:20:40 zpainter 阅读数 174  收藏 更多 分类专栏: 论文   版权声明 ...

  10. python多线程一些知识点梳理

    学习python的进程和线程以来,对这两个概念一直都处于模糊状态,所以决定花点时间好好学习一下这块知识.以下是我自己在学习过程中形成的一些疑问以及搜集的一些相应的比较好的答案,整理如下,方便复习自查. ...