Codeforces Round #589 (Div. 2)E(组合数,容斥原理,更高复杂度做法为DP)
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int f[257],fac[257],ifac[257];
const long long mod = 1e9+7;
int qpow(int x,int y){
int tamp=1;
while(y){
if(y&1)
tamp=1ll*tamp*x%mod;
x=1ll*x*x%mod;
y>>=1;
}
return tamp;
}
int combination(int n,int m){
return 1ll*ifac[m]*ifac[n-m]%mod*fac[n]%mod;//n!/(m!*(n-m)!)
}
int main(){
int n,k;
cin>>n>>k;
fac[0]=1;
for(int i=1;i<=n;++i){
f[i]=qpow((qpow(k,i)-qpow(k-1,i)+mod)%mod,n);//f[i]表示有i列格子填上了数字,每一列都有1
//qpow(k,i)表示一列中每一格都用1~k填充,即全排列
//qpow(k-1,i)表示一列中每一格都用2~k填充,即这一列没有1
//相减得到这一列定有1,n次方表示n列每一列都定有1
fac[i]=1ll*fac[i-1]*i%mod;//fac[i]表示sum[1,i](f[i])
}
ifac[n]=qpow(fac[n],mod-2);//费马小定理求逆元
for(int i=n-1;i>=0;--i)
ifac[i]=1ll*ifac[i+1]*(i+1)%mod;//ifac[i]=1/((n!*(n-i)!)/n!)
int ans=0;
for(int i=0;i<=n;++i)
ans=(ans+((i&1)?mod-1ll:1ll)*combination(n,i)%mod*qpow(k-1,1ll*n*i)%mod*f[n-i]%mod)%mod;
//容斥,用全排列的情况减去只有1列没有1加上只有2列没有1减去只有3列没有1加上只有4列没有1......
//多集合取交集容斥问题,公式类似二项式定理即ans=sum[0,n]((-1)^i*(C(n,i)(k-1)^(n*i))*f[n-i])
cout<<ans<<endl;
return 0;
}
Codeforces Round #589 (Div. 2)E(组合数,容斥原理,更高复杂度做法为DP)的更多相关文章
- Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理
Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理 [Problem Description] 在\(n\times n\) ...
- Codeforces Round #205 (Div. 2)C 选取数列可以选择的数使总数最大——dp
http://codeforces.com/contest/353/problem/C Codeforces Round #205 (Div. 2)C #include<stdio.h> ...
- Codeforces Round 589 (Div. 2) 题解
Is that a kind of fetishism? No, he is objectively a god. 见识了一把 Mcdic 究竟出题有多神. (虽然感觉还是吹过头了) 开了场 Virt ...
- Codeforces Round #589 (Div. 2)
目录 Contest Info Solutions A. Distinct Digits B. Filling the Grid C. Primes and Multiplication D. Com ...
- 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 ...
- Codeforces Round #589 (Div. 2) D. Complete Tripartite(染色)
链接: https://codeforces.com/contest/1228/problem/D 题意: You have a simple undirected graph consisting ...
- Codeforces Round #589 (Div. 2) C - Primes and Multiplication(数学, 质数)
链接: https://codeforces.com/contest/1228/problem/C 题意: Let's introduce some definitions that will be ...
- 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 ...
- Codeforces Round #589 (Div. 2) A. Distinct Digits
链接: https://codeforces.com/contest/1228/problem/A 题意: You have two integers l and r. Find an integer ...
随机推荐
- Makefile export
1) 1.在(parent,上层的)makefile中export出来变量,子makefile(sub make)中,是可以访问的. 2. 而同一级别的makefile(可通过makefile中内置变 ...
- 2019 ICPC 徐州网络赛 B.so easy (并查集)
计蒜客链接:https://nanti.jisuanke.com/t/41384 题目大意:给定n个数,从1到n排列,其中有q次操作,操作(1) 删除一个数字 // 操作(2)求这个数字之后第一个没有 ...
- redis解决方案之三种集群模式的概念与部署
上篇文章为大家总结了redis命令并讲述了持久化,今天我们来看一下redis的三种集群模式:主从复制,哨兵集群,Cluster集群 本篇文章先介绍redis-cluster集群模式,然后再依次介绍它的 ...
- Eugeny and Array(思维)
Eugeny has array a = a1, a2, ..., an, consisting of n integers. Each integer ai equals to -1, or to ...
- 关于反射中如何剔除实体类中serialVersionUID字段
Field[] fields = t.getClass().getDeclaredFields(); //这里为了剔除serialVersionUID Field[] tempArr = new Fi ...
- Servlet获取配置信息(ServletConfig)
ServletConfig ServletConfig:当Servlet容器初始化Servlet时,Servlet容器会给Servlet的init方法传入一个ServletConfig.Servlet ...
- 【PAT甲级】1104 Sum of Number Segments (20 分)
题意:输入一个正整数N(<=1e5),接着输入N个小于等于1.0的正数,输出N个数中所有序列的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC ...
- 每天进步一点点------如何实现Sobel Edge Detector? (Image Processing) (C/C++)
使用C與C++/CLI實現Sobel Edge Detector. http://www.cnblogs.com/oomusou/archive/2008/07/23/sobel_edge_detec ...
- Nodejs的Gruntjs使用一则
Gruntjs是前端项目构建工具,基于nodejs命令.有些js项目是基于Gruntjs构建的,如Jquery. Gruntjs主要功能有: 1.合并文件. 2.压缩html,js,css,图片文件. ...
- Git的安装和使用(转载)
转载博客连接:https://www.cnblogs.com/qijunjun/p/7137207.html 实际项目开发中,我们经常会用一些版本控制器来托管自己的代码,今天就来总结下Git的相关用法 ...