CF1228E. Another Filling the Grid(容斥原理+排列组合)

2024-04-16 02:38

本文主要是介绍CF1228E. Another Filling the Grid(容斥原理+排列组合),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

You have ?×? square grid and an integer ?. Put an integer in each cell while satisfying the conditions below.

All numbers in the grid should be between 1 and ? inclusive.
Minimum number of the ?-th row is 1 (1≤?≤?).
Minimum number of the ?-th column is 1 (1≤?≤?).
Find the number of ways to put integers in the grid. Since the answer can be very large, find the answer modulo (109+7).

These are the examples of valid and invalid grid when ?=?=2.
Input
The only line contains two integers ? and ? (1≤?≤250, 1≤?≤109).

Output
Print the answer modulo (109+7).

Examples
inputCopy
2 2
outputCopy
7
inputCopy
123 456789
outputCopy
689974806
Note
In the first example, following 7 cases are possible.

In the second example, make sure you print the answer modulo (109+7).

题意: nXn的矩阵,每个格子可以填1~k的数。要求使得每行每列的数字最小值都为1.问有多少种填数方案
思路:
我是看了这篇博客
https://www.cnblogs.com/birchtree/p/11614243.html
可以有n^3的dp写法,这里是排列组合+容斥。

在这里插入图片描述
这里i(j)代表至少多少个最小值不为1的行(列)。

而关于为什么这样容斥,我的想法是:
在这里插入图片描述
容斥原理的原始公式是划分出集和的,本题也需要划分集合。假设只有一行的情况下,你只需要考虑列。设 f [ i ] f[i] f[i]表示第 i i i列不为1。
那么 f [ 1 ] , f [ 2 ] , f [ 3 ] , . . . , f [ i ] f[1],f[2],f[3],...,f[i] f[1],f[2],f[3],...,f[i]就是集合所需的所有集合了。

答案就是 U − ( ∑ f [ i ] − ∑ f [ i ] ∩ f [ j ] + ∑ f [ i ] ∩ f [ j ] − ∑ f [ i ] ∩ f [ j ] ∩ f [ k ] . . . ) U - (∑f[i]-∑f[i]∩f[j]+∑f[i]∩f[j]-∑f[i]∩f[j]∩f[k]...) U(f[i]f[i]f[j]+f[i]f[j]f[i]f[j]f[k]...)
这个式子是不是有原始公式的味道啦?

当然,行是不得不考虑的,那么就多划分出一些集合。
定义 f [ i ] f[i] f[i]为第 i i i列不存在1, g [ i ] g[i] g[i]为第 i i i行不存在1.
f [ 1 ] , f [ 2 ] , f [ 3 ] , . . . . , f [ n ] , g [ 1 ] , g [ 2 ] , g [ 3 ] , . . . , g [ n ] f[1],f[2],f[3],....,f[n],g[1],g[2],g[3],...,g[n] f[1],f[2],f[3],....,f[n],g[1],g[2],g[3],...,g[n]就是所需的所有集合。

答案就是 U − ( ∑ f [ i ] + ∑ g [ i ] − ∑ f [ i ] ∩ f [ j ] − ∑ f [ i ] ∩ g [ j ] − ∑ g [ i ] ∪ g [ j ] . . . ) U-(∑f[i]+∑g[i] - ∑f[i]∩f[j] - ∑f[i]∩g[j]-∑g[i]∪g[j] ...) U(f[i]+g[i]f[i]f[j]f[i]g[j]g[i]g[j]...)

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>using namespace std;typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 7;
struct Point
{ll x,y;
}p[5005];
ll fac[maxn],inv[maxn],f[maxn];int cmp(Point a,Point b)
{if(a.x == b.x)return a.y < b.y;return a.x < b.x;
}ll qpow(ll a,ll b)
{ll res = 1;while(b){if(b & 1){res = (res * a) % mod;}a = (a * a) % mod;b = b >> 1;}return res % mod;
}ll C(ll n,ll m)
{if(m > n || m < 0)return 0;return fac[n] * ((inv[n - m] * inv[m]) % mod) % mod;
}void init()
{fac[0] = 1;inv[0] = 1;for(int i = 1;i <= maxn - 2;i++){fac[i] = (fac[i - 1] * i) % mod;inv[i] = qpow(fac[i],mod - 2);}
}int main()
{init();ll n,k;scanf("%lld%lld",&n,&k);ll ans = 0;for(int i = 0;i <= n;i++){for(int j = 0;j <= n;j++){ll cnt = n * i + n * j - i * j;ans = (ans + qpow(-1,i+j) * C(n,i) % mod * C(n,j)%mod * qpow(k - 1,cnt)%mod * qpow(k,n * n - cnt)%mod + mod * 100) % mod;}}printf("%lld\n",ans);return 0;
}

DP做法
定义 f [ i ] [ j ] f[i][j] f[i][j]表示填完前 i i i行,前 i i i行都合理(最小值为1),且正好 j j j行合理。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1000 + 7;int n,k;
ll pk[maxn],pk1[maxn];
ll f[maxn][maxn];
ll fac[maxn],inv[maxn];ll qpow(ll a,ll b)
{ll res = 1;while(b){if(b & 1){res = (res * a) % mod;}a = (a * a) % mod;b = b >> 1;}return res % mod;
}ll C(ll n,ll m)
{if(m > n || m < 0)return 0;return fac[n] * ((inv[n - m] * inv[m]) % mod) % mod;
}void init()
{fac[0] = 1;inv[0] = 1;pk[0] = 1;pk1[0] = 1;for(int i = 1;i <= maxn - 2;i++){fac[i] = (fac[i - 1] * i) % mod;inv[i] = qpow(fac[i],mod - 2);pk[i] = pk[i - 1] * k % mod;pk1[i] = pk1[i - 1] * (k - 1) % mod;}
}int main() {scanf("%d%d",&n,&k);init();for(int i = 1;i <= n;i++) {f[1][i] = C(n,i) * pk1[n - i] % mod;}for(int i = 2;i <= n;i++) {for(int j = 1;j <= n;j++) {for(int k = 1;k < j;k++) {f[i][j] = (f[i][j] + C(n - k,j - k) * pk[k] % mod * pk1[n - j] % mod * f[i - 1][k] % mod) % mod;}f[i][j] = (f[i][j] + (pk[j] - pk1[j] + mod) * pk1[n - j] % mod * f[i - 1][j] % mod) % mod;}}printf("%lld\n",f[n][n]);return 0;
}

这篇关于CF1228E. Another Filling the Grid(容斥原理+排列组合)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907602

相关文章

从原理到实战深入理解Java 断言assert

《从原理到实战深入理解Java断言assert》本文深入解析Java断言机制,涵盖语法、工作原理、启用方式及与异常的区别,推荐用于开发阶段的条件检查与状态验证,并强调生产环境应使用参数验证工具类替代... 目录深入理解 Java 断言(assert):从原理到实战引言:为什么需要断言?一、断言基础1.1 语

MySQL中的表连接原理分析

《MySQL中的表连接原理分析》:本文主要介绍MySQL中的表连接原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、表连接原理【1】驱动表和被驱动表【2】内连接【3】外连接【4编程】嵌套循环连接【5】join buffer4、总结1、背景

深度解析Spring AOP @Aspect 原理、实战与最佳实践教程

《深度解析SpringAOP@Aspect原理、实战与最佳实践教程》文章系统讲解了SpringAOP核心概念、实现方式及原理,涵盖横切关注点分离、代理机制(JDK/CGLIB)、切入点类型、性能... 目录1. @ASPect 核心概念1.1 AOP 编程范式1.2 @Aspect 关键特性2. 完整代码实

Java Stream的distinct去重原理分析

《JavaStream的distinct去重原理分析》Javastream中的distinct方法用于去除流中的重复元素,它返回一个包含过滤后唯一元素的新流,该方法会根据元素的hashcode和eq... 目录一、distinct 的基础用法与核心特性二、distinct 的底层实现原理1. 顺序流中的去重

Spring @Scheduled注解及工作原理

《Spring@Scheduled注解及工作原理》Spring的@Scheduled注解用于标记定时任务,无需额外库,需配置@EnableScheduling,设置fixedRate、fixedDe... 目录1.@Scheduled注解定义2.配置 @Scheduled2.1 开启定时任务支持2.2 创建

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

Mysql的主从同步/复制的原理分析

《Mysql的主从同步/复制的原理分析》:本文主要介绍Mysql的主从同步/复制的原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录为什么要主从同步?mysql主从同步架构有哪些?Mysql主从复制的原理/整体流程级联复制架构为什么好?Mysql主从复制注意

Nacos注册中心和配置中心的底层原理全面解读

《Nacos注册中心和配置中心的底层原理全面解读》:本文主要介绍Nacos注册中心和配置中心的底层原理的全面解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录临时实例和永久实例为什么 Nacos 要将服务实例分为临时实例和永久实例?1.x 版本和2.x版本的区别

apache的commons-pool2原理与使用实践记录

《apache的commons-pool2原理与使用实践记录》ApacheCommonsPool2是一个高效的对象池化框架,通过复用昂贵资源(如数据库连接、线程、网络连接)优化系统性能,这篇文章主... 目录一、核心原理与组件二、使用步骤详解(以数据库连接池为例)三、高级配置与优化四、典型应用场景五、注意事