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 ...
随机推荐
- 浅谈ABB机器人(工具坐标,工件坐标,有效载荷)
工具坐标(tool): 使tcl坐标偏移到工具上,例如焊接工作,使机器人工作点切入焊枪点上 mass:工具的重量 xyz:偏移距离的大小 验证:通过手动模式,切换至自定义工具,重定向 工件坐标(wob ...
- noobSTL-1-配置器-0
noobSTL-1-配置器-0 0.前言 STL的配置器(allocator),也叫内存分配器,负责空间配置与管理,简单地说,就是负责管理内存的. 从实现的角度来看,配置器是一个实现了动态空间配置.空 ...
- Tensorflow版本更改所产生的问题及解决方案
1.module 'tensorflow' has no attribute 'mul' tf.mul已经在新版本中被移除,使用 tf.multiply 代替 解决方法 将tf.mul(input1, ...
- 线性筛-prime,强大O(n)
和朴素的素数筛法一样,flag数组,记录x是否为素数 flag[x]=0,x为合数 falg[x]=1,x为素数 flag[1],无定义 其核心思想是,用x筛除与之差异最小的y,达到时间上O(n)的目 ...
- 那些年做过的ctf之加密篇(加强版)
MarkdownPad Document *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...
- linux 网络数据收发网络流量监控
网卡流量 1.iftop命令 iftop可以用来监控网卡的实时流量(可以指定网段).反向解析IP.显示端口信息.TCP/IP连接等官网:http://www.ex-parrot.com/~pdw/if ...
- String - 字符串分割操作
如果我想将一个字符串按照每8位一组分为若干个块,然后存储在一个byte[ ]数组中,我首先需要确定这个byte数组的长度,但由于我无法确定这个字符串的长度是否可以被8整除,所以无法直接判断,因此需要对 ...
- 关于excuteQuery与execute()
excuteQuery是查询语句,如果是更新或者插入或报错,换成execute()就好了
- celery 使用 - 3
# celery 使用 1.broker 2.基础案例 使用redis作为broker和brokend. 创建tasks.py # tasks.py di = 'redis://:****@local ...
- Matlab geom3d函数注释
Matlab geom3d函数解析 geom3d函数库 geom3d库的目的是处理和可视化三维几何原语,如点.线.平面.多面体等.它提供了操作三维几何原语的底层功能,使得开发更复杂的几何算法变得更加容 ...