传送门

看到 $n=250$ 显然考虑 $n^3$ 的 $dp$

设 $f[i][j]$ 表示填完前 $i$ 行,目前有 $j$ 列的最小值是 $1$ 的合法方案数

那么对于 $f[i][j]$ ,枚举 $f[i-1][k]$ ,有 $f[i][j]=\sum_{k=0}^{j}\binom{n-k}{j-k}f[i-1][k](m-1)^{n-j}m^k$

这里 $m$ 就是题目的 $k$

$\binom{n-k}{j-k}$ 是因为多出来的 $j-k$ 列 $1$ 可以任选

$(m-1)^{n-j}$ 是保证没有 $1$ 的列不能填 $1$ ,只有 $m-1$ 种填的数

$m^k$ 是那些原本有保证为 $1$ 的列怎么填都行

当然剩下的那 $j-k$ 个位置显然都是 $1$ ,方案数只有 $1$

然后这样就可以做到 $n^3 \log n$ 然后发现竟然 $T$ 了,所以预处理一下 $k \in [0,n],m^k$ 和 $k \in [0,n],(m-1)^k$ 即可

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int mo=1e9+,N=;
inline int fk(int x) { return x>=mo ? x-mo : x; }
int n,m;
int C[N][N],f[N][N];
int mi[N],mi_1[N];
int main()
{
n=read(),m=read();
for(int i=;i<=n;i++)
{
C[i][]=;
for(int j=;j<=i;j++)
C[i][j]=fk(C[i-][j]+C[i-][j-]);
}
mi[]=mi_1[]=;
for(int i=;i<=n;i++)
{
mi[i]=1ll*mi[i-]*m%mo;
mi_1[i]=1ll*mi_1[i-]*(m-)%mo;
}
for(int j=;j<=n;j++) f[][j]=1ll*C[n][j]*mi_1[n-j]%mo;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
for(int k=;k<=j;k++)
{
int x=1ll*f[i-][k]*C[n-k][j-k]%mo;
int y=1ll*mi_1[n-j]*mi[k]%mo;
f[i][j]=fk(f[i][j]+1ll*x*y%mo);
if(j==k) f[i][j]=fk(f[i][j]-1ll*mi_1[n]*f[i-][k]%mo+mo);
}
}
printf("%d\n",f[n][n]);
return ;
}

正常的做法

但是有些神仙看完数据说:" $n$ 太小了,可以出到 $10^5$ 级别"

所以考虑一下神仙的做法

看到有限制的方案数,考虑容斥!

总方案 - (一行不合法+一列不合法) + (两行不合法+两列不合法+一行一列不合法) - ......

那么写成式子就是长这个样子:

$\sum_{i=0}^{n}\sum_{j=0}^{n}(-1)^{i+j} \binom{n}{i}\binom{n}{j}m^{n^2-n(i+j)+ij}(m-1)^{n(i+j)-ij}$

上面 $m^{n^2-n(i+j)+ij}$ 就是没限制的位置顺便填,$(m-1)^{n(i+j)-ij}$ 就是强制 $i$ 行 $j$ 列的格子不能填 $1$

然后同样预处理一下 $m$ 和 $m-1$ 的幂次就可以做到 $n^2$

对着这个式子继续搞:

$\sum_{i=0}^{n}\sum_{j=0}^{n}(-1)^{i+j} \binom{n}{i}\binom{n}{j}m^{n^2-n(i+j)+ij}(m-1)^{n(i+j)-ij}$

$\sum_{i=0}^{n}(-1)^i\binom{n}{i}\sum_{j=0}^{n}(-1)^j\binom{n}{j}m^{(n-i)(n-j)}(m-1)^{(n-i)j}(m-1)^{ni}$

$\because \sum_{j=0}^{n}(-1)^j\binom{n}{j}m^{(n-i)(n-j)}(m-1)^{(n-i)j}=(m^{n-i}-(m-1)^{n-i})^n$

$\therefore \sum_{i=0}^{n}(-1)^i\binom{n}{i}(m-1)^{ni}(m^{n-i}-(m-1)^{n-i})^n$

$\sum_{i=0}^{n}(-1)^i\binom{n}{i}(m^{n-i}(m-1)^i-(m-1)^n)^n$

然后就可以 $n \log n$ 解决了

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=,mo=1e9+;
inline int fk(int x) { return x>=mo ? x-mo : x; }
int n,m,Ans;
int C[N][N],mi[N],m_1i[N];
inline int ksm(int x,int y)
{
int res=;
while(y) { if(y&) res=1ll*res*x%mo; x=1ll*x*x%mo; y>>=; }
return res;
}
int main()
{
n=read(),m=read();
for(int i=;i<=n;i++)
{
C[i][]=;
for(int j=;j<=i;j++) C[i][j]=fk(C[i-][j]+C[i-][j-]);
}
mi[]=m_1i[]=;
for(int i=;i<=n;i++)
{
mi[i]=1ll*mi[i-]*m%mo;
m_1i[i]=1ll*m_1i[i-]*(m-)%mo;
}
for(int i=;i<=n;i++)
{
int t=1ll*C[n][i]*ksm( fk(1ll*mi[n-i]*m_1i[i]%mo - m_1i[n] +mo) , n )%mo;
i& ? Ans=fk(Ans-t+mo) : Ans=fk(Ans+t);
}
printf("%d\n",Ans);
return ;
}

Codeforces 1228E. Another Filling the Grid的更多相关文章

  1. [Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理)

    [Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理) 题面 一个\(n \times n\)的格子,每个格子里可以填\([1,k]\)内的整数. ...

  2. codeforces#1228E. Another Filling the Grid(容斥定理,思维)

    题目链接: https://codeforces.com/contest/1228/problem/E 题意: 给n*n的矩阵填数,使得每行和每列最小值都是1 矩阵中可以填1到$k$的数 数据范围: ...

  3. [Codeforces 1228E]Another Filling the Grid(组合数+容斥)

    题目链接 解题思路: 容斥一下好久可以得到式子 \(\sum_{i=0}^{n}\sum_{j=0}^{n}(-1)^{i+j}C_n^iC_n^j(k-1)^{ni+nj-ij}k^{n^2-(ni ...

  4. Another Filling the Grid

    E. Another Filling the Grid 参考:Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理 容斥这个东 ...

  5. [Codeforces 1245D] Shichikuji and Power Grid (最小生成树)

    [Codeforces 1245D] Shichikuji and Power Grid (最小生成树) 题面 有n个城市,坐标为\((x_i,y_i)\),还有两个系数\(c_i,k_i\).在每个 ...

  6. 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 ...

  7. 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 ...

  8. Codeforces Round #589 (Div. 2) Another Filling the Grid (dp)

    题意:问有多少种组合方法让每一行每一列最小值都是1 思路:我们可以以行为转移的状态 附加一维限制还有多少列最小值大于1 这样我们就可以不重不漏的按照状态转移 但是复杂度确实不大行(减了两个常数卡过去的 ...

  9. Codeforces 679C Bear and Square Grid

    Bear and Square Grid 枚举k * k 的位置, 然后接上它周围白色连通块的数量, 再统计完全在k * k范围里的连通块, 这个只要某个连通块全部的方格 在k * k里面就好, 并且 ...

随机推荐

  1. Leetcode题目39.组合总和(回溯+剪枝-中等)

    题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无 ...

  2. Netfilter 之 连接跟踪初始化

    基础参数初始化 nf_conntrack_init_start函数完成连接跟踪基础参数的初始化,包括了hash,slab,扩展项,GC任务等: int nf_conntrack_init_start( ...

  3. 2.jdk1.8+springboot中http1.1之tcp连接复用实现

    接上篇:https://www.cnblogs.com/Hleaves/p/11284316.html 环境:jdk1.8 + springboot 2.1.1.RELEASE + feign-hys ...

  4. 1.springboot启动流程

    SpringBoot版本:2.1.2.RELEASE 1.maven <parent> <groupId>org.springframework.boot</groupI ...

  5. python 牛顿迭代法

    使用牛顿迭代法求方程  在x附近的一个实根. 赋值X,即迭代初值:用初值x代入方程中计算此时的f(x)=(a * x * x * x + b * x * x + c * x + d)和f’(x)=(3 ...

  6. xshell链接vbox 上 nat 方式链接虚拟机 - 端口转发

    使用场景 某些不可解原因导致 centos7通过桥接方式进行外网资源访问无法实现, 但是 nat 方式是没问题的, 因此考虑直接基于这个的方式进行操作, 但是xshell 的链接需要ip地址, 因此提 ...

  7. 排错:Unable to create a new session key. It is likely that the cache is unavailable.

    排错:Unable to create a new session key. It is likely that the cache is unavailable. 问题 登录openstack页面, ...

  8. JAVA 基础编程练习题5 【程序 5 判断分数等级】

    5 [程序 5 判断分数等级] 题目:利用条件运算符的嵌套来完成此题:学习成绩>=90 分的同学用 A 表示,60-89 分之间的用 B 表示, 60 分以下的用 C 表示. 程序分析:(a&g ...

  9. Vue 子组件与子组件之间传值

    可以借用公共父元素.子组件A  this.$emit("eventName", data) 触发事件,父组件监听事件,更改父组件 data , 通过Props 传值到子组件B,子组 ...

  10. NSubstitute.Analyzers检测NSubstitute用法冲突

    NSubstitute是一个.Net环境使用的,简洁,语法友好的Mock库.语法简洁的缺点是有一些失败的用法很难察觉和检测.比如试图mock一个非虚拟成员-NSubstitute不能看到这些成员所以不 ...