CodeForces 1332E Height All the Same
题意
对于一个\(n*m\)的矩阵,有两种操作
- 一个格子加二
- 一个格子和另一个相邻的格子同时加一
通过这两种操作最终使得所有矩阵元素相等
对于矩阵元素来说,有\(L\leq a_{i,j}\leq R(1\leq i\leq n,1\leq j\leq m)\)
问有多少种方案数,答案\(\mod 998244353\)
分析
由于最终相等的值与答案无关,所以我们不妨所有元素减去\(L\),即所有元素的值在\([0,R-L]\)区间内
而所有元素通过操作可以相差最多为\(1\)(仅仅分奇偶)
这种操作可以不断进行,所以我们只需看数字的奇偶性
为描述,我们不妨将所有元素视为\(0,1\)
对于\(1\)来说,其周围一定没有\(1\),否则可以填上使两元素变为\(0\),那么如果该\(1\)和旁边的\(0\)同时加\(1\),我们可以发现\(0\)和\(1\)互换位置了
这种操作的意义在于,对于任意的一个\(1\),我们可以通过操作使其变到其他任意的位置
那么如果矩阵中有奇数个\(1\),我们可以将其变为\(1\)个\(1\),而偶数个\(1\),我们一定可以将两个\(1\)进行配对,从而消去
我们思考\(n*m\)的奇偶性
- 若\(n*m\)为奇数,若有偶数个\(1\),则满足条件,若有奇数个\(1\),我们将其变为\(1\)个\(1\),并将其移动到边角上,通过蛇形配对,我们可以将除该元素的其他元素同时加上\(1\),所有元素相等,因此所有取值皆满足答案就是\((R-L+1)^{n*m}\)(每个数有\(R-L+1\)中取法)
- 若\(n*m\)为偶数,若有偶数个\(1\),则满足条件,若有奇数个\(1\),我们可以发现元素和为奇数,而\(n*m\)为偶数,元素和无论怎么增加(每次加二),一定是奇数,无法整除\(n*m\)一定不满足,因此答案为偶数个\(1\)的取值方式
接下来分析\(n*m\)为偶数时,有多少种偶数个\(1\)的取值方式:
如果\(R-L+1\)为奇数,则可以取\(\frac {R-L+2} {2}\)种奇数,否则为\(\frac {R-L+1} {2}\)种奇数,设为\(j\),设\(R-L+1\)为\(t\)
答案为\(C^0_{n*m}*j^0*(t-j)^{n*m}+C^2_{n*m}*j^2*(t-j)^{n*m-2}+\cdots+C^{n*m}_{n*m}*j^{n*m}*(t-j)^{0}\),意思为挑偶数(\(2*k\))个奇数(\(C^{2*k}_{n*m}\)),每个奇数有\(j\)中取值,其余偶数有\(t-j\)种取值
发现这个式子是二项展开式的偶数项,那么可以推导下(半小时无从下手,我对不起高中数学老师)
\]
\]
第二个式子偶数项是加号,奇数项是减号(从\(0\)开始计数)
两式相加除以二即为偶数项和,即
\]
#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
#define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define int ll
#define ls st<<1
#define rs st<<1|1
#define pii pair<int,int>
#define rep(z, x, y) for(int z=x;z<=y;++z)
#define com bool operator<(const node &b)
using namespace std;
const int maxn = (ll) 3e5 + 5;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
int qp(int x, int y) {
int ans = 1;
while (y) {
if (y & 1)
ans = ans * x % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
signed main() {
start;
int n, m, L, R;
cin >> n >> m >> L >> R;
int t = R - L + 1;
if ((n * m) & 1) {
cout << qp(t, n * m) % mod;
} else {
int j;
if (t & 1)
j = (t + 1) / 2;
else
j = t / 2;
int ans = ((qp(t, m * n) + qp((2 * j - t), m * n)) % mod + mod) % mod * qp(2, mod - 2) % mod;
cout << ans;
}
return 0;
}
CodeForces 1332E Height All the Same的更多相关文章
- [PyData] 03 - Data Representation
Ref: http://blog.csdn.net/u013534498/article/details/51399035 如何在Python中实现这五类强大的概率分布 考虑下在mgrid上画二维概率 ...
- Educational Codeforces Round 97 (Rated for Div. 2) D. Minimal Height Tree (贪心)
题意:有一个从根节点\(BFS\)得来的序列(每次\(bfs\)子节点的时候保证是升序放入队列的),现在让你还原树(没必要和之前相同),问能构造出的最小的树的深度. 题解:不看根节点,我们从第二个位置 ...
- CodeForces 548D 单调栈
Mike and Feet Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Subm ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #355 (Div. 2)-B
B. Vanya and Food Processor 题目链接:http://codeforces.com/contest/677/problem/B Vanya smashes potato in ...
- Codeforces Round #355 (Div. 2)-A
A. Vanya and Fence 题目连接:http://codeforces.com/contest/677/problem/A Vanya and his friends are walkin ...
- Codeforces Round #354 (Div. 2)-B
B. Pyramid of Glasses 题目链接:http://codeforces.com/contest/676/problem/B Mary has just graduated from ...
- Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树
C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...
- [codeforces 241]C. Mirror Box
[codeforces 241]C. Mirror Box 试题描述 Mirror Box is a name of a popular game in the Iranian National Am ...
- CodeForces 471C MUH and House of Cards
MUH and House of Cards Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & % ...
随机推荐
- Cannot read property ‘type‘ of undefined Occurred while linting **\index.jsx:1
今一个react 中使用mobx 老是提示Cannot read property 'type' of undefined Occurred while linting **\index.jsx:1 ...
- Cesium中监听MOUSE_MOVE事件获取经纬度和高度
有时候在这个圆球上获取精确的经度纬度还不容易,特别是高度 还好在cesium提供了接口,看 let selft = this; const scene = this.viewer.scene; var ...
- RSA 加密解密
from Crypto.Util.number import bytes_to_long, long_to_bytes, getPrime import libnum # 一.取两个素数 p = ge ...
- Python modbus_tk 库源码分析
modbus_tk 源代码分析 前言 modbus_tcp 协议是工业项目中常见的一种基于 TCP/IP 协议的设备数据交互协议. 作为 TCP/IP 协议的上层协议,modbus_tcp 协议涉及到 ...
- Curl 中 关于PUT, POST, DELETE, UPDATE 的使用
POST curl -H "Content-Type:application/json" -X POST --data '{"id":1, "text ...
- Python运维开发之路《WEB框架:Django》
一.Web框架的本质 所有的web框架.web请求:本质上都是:socket 浏览器:socket客户端 服务器:socket服务端 1. socket服务端 import socket def ha ...
- BeanDefinitionStoreException: Failed to read candidate component class
ssm 整合时出现问题 org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate ...
- AI重塑千行百业,华为云发布盘古大模型3.0和昇腾AI云服务
[中国,东莞,2023年7月7日]华为开发者大会2023(Cloud)7月7日在中国东莞正式揭开帷幕,并同时在全球10余个国家.中国30多个城市设有分会场,邀请全球开发者共聚一堂,就AI浪潮之下的产业 ...
- 配置k8s拉取Harbor镜像
创建Secret # 认证名称为:docker-harbor-registry kubectl create secret docker-registry docker-harbor-registry ...
- 即构携手智能对讲机品牌Runbo,打造可视化对讲通信系统
现代通信技术的发展,让信息的传递变得前所未有的便捷.作为双向移动通信工具,对讲机不需要网络支持就可以进行通话,且没有话费产生,尤其适合酒店.物业等使用区域固定.通话频繁的场景. 随着技术的不断迭代,对 ...