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 ...
随机推荐
- ONESHELL
没有加 .ONESHELL 的时候,片段中的各行 shell 彼此独立. 加了 .ONESHELL 后,各行shell 可以看作一行 shell. <1> .PHONY: all all: ...
- opencv:轮廓匹配
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...
- 大数据的特征(4V+1O)
数据量大(Volume):第一个特征是数据量大,包括采集.存储和计算的量都非常大.大数据的起始计量单位至少是P(1000个T).E(100万个T)或Z(10亿个T). 类型繁多(Variety):第二 ...
- js 时间格式化工具类
/** * 返回示例:0 天 4 小时 7 分钟 57 秒 * @param second 毫秒数 * @returns {String} 时间html */ function secondToDay ...
- Atcoder Beginner Contest 147D(按位处理)
把每个数字每一位上二进制数字取出,求答案时直接用N个数里这一位是0的个数乘上这一位是1的个数然后乘上二的这一位次方,注意所有可能溢出的地方都要对mod取模. #define HAVE_STRUCT_T ...
- 【C语言】一元二次方程(求实根和虚根)
求一元二次方程:ax2+bx+c=0 的根. 输入三个实数a,b,c的值,且a不等于0. #include<stdio.h> #include<math.h> int main ...
- Python中的参数解包:`*`表达式和 `**`表达式
目录 1.参数解包:方法调用中的*表达式和**表达式 2.参数解包:方法定义中的*表达式和**表达式 3.在元组,列表,集合和字典中解包 4.Extended Unpacking:赋值表达式左边的*表 ...
- 初始css一
初始CSS 一.form表单补充 form表单关键性的属性补充 form表单 关键性的属性 action 控制的是数据的提交路径 1.不写 默认是朝着当前页面所在的地址提交 2.全路径 3.后缀(/i ...
- 【Math】高数-一个有趣的旋转体体积与面积
Go confidently in the direction of your dreams. Live the life you've imagined. 题目 设曲线 \(y = \tfrac{1 ...
- iframe多层嵌套时,Jquery获取元素
在项目中,尤其是后台管理项目,会使用到iframe嵌套的网页,说起iframe,真的是个让人头疼的东西,能避开是最好避开.不然要请随身备好氧气瓶哈(因为管理和调试过程中往往会被气缺氧!!!哈哈哈~~~ ...