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

相关文章

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代

Golang HashMap实现原理解析

《GolangHashMap实现原理解析》HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持高效的插入、查找和删除操作,:本文主要介绍GolangH... 目录HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持

Spring Boot循环依赖原理、解决方案与最佳实践(全解析)

《SpringBoot循环依赖原理、解决方案与最佳实践(全解析)》循环依赖指两个或多个Bean相互直接或间接引用,形成闭环依赖关系,:本文主要介绍SpringBoot循环依赖原理、解决方案与最... 目录一、循环依赖的本质与危害1.1 什么是循环依赖?1.2 核心危害二、Spring的三级缓存机制2.1 三

C#中async await异步关键字用法和异步的底层原理全解析

《C#中asyncawait异步关键字用法和异步的底层原理全解析》:本文主要介绍C#中asyncawait异步关键字用法和异步的底层原理全解析,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录C#异步编程一、异步编程基础二、异步方法的工作原理三、代码示例四、编译后的底层实现五、总结C#异步编程

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

Go 语言中的select语句详解及工作原理

《Go语言中的select语句详解及工作原理》在Go语言中,select语句是用于处理多个通道(channel)操作的一种控制结构,它类似于switch语句,本文给大家介绍Go语言中的select语... 目录Go 语言中的 select 是做什么的基本功能语法工作原理示例示例 1:监听多个通道示例 2:带

鸿蒙中@State的原理使用详解(HarmonyOS 5)

《鸿蒙中@State的原理使用详解(HarmonyOS5)》@State是HarmonyOSArkTS框架中用于管理组件状态的核心装饰器,其核心作用是实现数据驱动UI的响应式编程模式,本文给大家介绍... 目录一、@State在鸿蒙中是做什么的?二、@Spythontate的基本原理1. 依赖关系的收集2.

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

Python中随机休眠技术原理与应用详解

《Python中随机休眠技术原理与应用详解》在编程中,让程序暂停执行特定时间是常见需求,当需要引入不确定性时,随机休眠就成为关键技巧,下面我们就来看看Python中随机休眠技术的具体实现与应用吧... 目录引言一、实现原理与基础方法1.1 核心函数解析1.2 基础实现模板1.3 整数版实现二、典型应用场景2