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 ...
随机推荐
- python 字符串是否包含某个子字符串
方法如下:以后再整理 if str1 in str2: 包含的话,True if str1.find(str2)>=0: 包含的话,返回第一次出现的位置,没有的话为负数 https://www. ...
- JAVA中fail-fast机制
在JDK的Collection中我们时常会看到类似于这样的话: 例如,ArrayList: 注意,迭代器的快速失败行为无法得到保证,因为一般来说,不可能对是否出现不同步并发修改做出任何硬性保证.快速失 ...
- redis常用配置参数
首先弄清楚当前redis读取的是哪个配置文件,然后去配置文件修改,例如windows 打开服务列表,点击Redis,右键属性 "D:\Program Files\Redis\redis-se ...
- Windows引用opencv静态库
参考博客:https://www.cnblogs.com/sysuzyq/p/6183568.html
- python爬虫-----Python访问http的几种方式
爬取页面数据,我们需要访问页面,发送http请求,以下内容就是Python发送请求的几种简单方式: 会使用到的库 urllib requests 1.urlopen import urllib. ...
- linux 配置LVM
1. 阿里云挂载LVM目录(创建LVM,以及配置第一块硬盘) fdisk /dev/vdb 输入 n p 1 enter enter wq pvcreate /dev/vdb1 vgcreate lv ...
- 转载:AAC编解码概述
转自:http://www.cnblogs.com/gaozehua/archive/2012/05/03/2479960.html 编码概述 其整体AAC 编解码系统,如图所示,其编码流程概述如下: ...
- 【使用python urllib时出现[SSL: CERTIFICATE_VERIFY_FAILED]报错的解决方案】
"首先,这个报错是告诉你,你的证书有问题. 其次,出现这个问题的原因,在于Python本身. 问题原因 Python升级到2.7.9以后,引入了一个新特性. 当使用urllib打开https ...
- 使用Canvas画布的注意事项
1.开始一个路径时要使用beiginPath()方法 ,不然会发生意想不到的事件. 2.图片加载完成后才能按照顺序依次绘图 (巧用onload时间)
- Linux04——手动修改IP和关闭防火墙服务
两种方法手动修改IP: 命令行方式 2. 图形化界面修改 点击应用即可(地址自行设置) 关闭防火墙服务,否则连不上Linux