CF1332E Height All the Same

Height All the Same

题面翻译

题目描述

最近,Alice 迷上了一款名为 Sirtet 的游戏。

在 Sirtet 中,玩家会得到一个 n × m n \times m n×m 的网格。初始时,格 ( i , j ) (i, j) (i,j) 上码放有 a i , j a_{i, j} ai,j 个方块。若两个格子有一条公共边,我们称这两个格子时相邻的。玩家可以进行以下两种操作:

  • 在两个相邻的格子上各码上一个方块。
  • 在一个格子上码上两个方块。

上述中所提到的所有方块都具有相同的高度。

以下是该游戏的一个图例说明。图中右侧的状态是由图中左侧的状态经过一次上述操作得到,灰色的方块表示操作中新加入的方块。

题目中的图

玩家的目标是通过这些操作,使得所有的格子拥有同样的高度(也就是说,每个格子上堆放的方块数相同)。

然而,Alice 发现存在有某些初始局面,使得无论她采用什么策略,都无法达到目标。因此,她希望知道有多少初始局面,满足,

  • 对于所有的 1 ≤ i ≤ n 1 \le i \le n 1in 1 ≤ j ≤ m 1 \le j \le m 1jm L ≤ a i , j ≤ R L \le a_{i, j} \le R Lai,jR
  • 玩家可以通过执行这些操作,达到目标。

请帮助 Alice 解决这个问题。注意答案可能很大,请输出所求答案对 998 , 244 , 353 998, 244, 353 998,244,353 取模的值。

输入格式

输入一行四个整数 n , m , L , R   ( 1 ≤ n , m , L , R ≤ 1 0 9 ; L ≤ R ; n ⋅ m ≥ 2 ) n, m, L, R ~ (1 \le n, m, L, R \le 10 ^ 9; L \le R; n \cdot m \ge 2) n,m,L,R (1n,m,L,R109;LR;nm2)

输出格式

输出一个整数,表示所求答案对 998 , 244 , 353 998, 244, 353 998,244,353 取模的值。

说明/提示

在第一个样例中,唯一一种符合要求的初始局面是 a 1 , 1 = a 2 , 1 = a 1 , 2 = a 2 , 2 = 1 a_{1, 1} = a_{2, 1} = a_{1, 2} = a_{2, 2} = 1 a1,1=a2,1=a1,2=a2,2=1。因此答案为 1 1 1

在第二个样例中,符合要求的初始局面有 a 1 , 1 = a 1 , 2 = 1 a_{1, 1} = a_{1, 2} = 1 a1,1=a1,2=1 a 1 , 1 = a 1 , 2 = 2 a_{1, 1} = a_{1, 2} = 2 a1,1=a1,2=2。因此答案为 2 2 2

题目描述

Alice has got addicted to a game called Sirtet recently.

In Sirtet, player is given an $ n \times m $ grid. Initially $ a_{i,j} $ cubes are stacked up in the cell $ (i,j) $ . Two cells are called adjacent if they share a side. Player can perform the following operations:

  • stack up one cube in two adjacent cells;
  • stack up two cubes in one cell.

Cubes mentioned above are identical in height.

Here is an illustration of the game. States on the right are obtained by performing one of the above operations on the state on the left, and grey cubes are added due to the operation.

Player’s goal is to make the height of all cells the same (i.e. so that each cell has the same number of cubes in it) using above operations.

Alice, however, has found out that on some starting grids she may never reach the goal no matter what strategy she uses. Thus, she is wondering the number of initial grids such that

  • $ L \le a_{i,j} \le R $ for all $ 1 \le i \le n $ , $ 1 \le j \le m $ ;
  • player can reach the goal using above operations.

Please help Alice with it. Notice that the answer might be large, please output the desired value modulo $ 998,244,353 $ .

输入格式

The only line contains four integers $ n $ , $ m $ , $ L $ and $ R $ ( $ 1\le n,m,L,R \le 10^9 $ , $ L \le R $ , $ n \cdot m \ge 2 $ ).

输出格式

Output one integer, representing the desired answer modulo $ 998,244,353 $ .

样例 #1

样例输入 #1

2 2 1 1

样例输出 #1

1

样例 #2

样例输入 #2

1 2 1 2

样例输出 #2

2

提示

In the first sample, the only initial grid that satisfies the requirements is $ a_{1,1}=a_{2,1}=a_{1,2}=a_{2,2}=1 $ . Thus the answer should be $ 1 $ .

In the second sample, initial grids that satisfy the requirements are $ a_{1,1}=a_{1,2}=1 $ and $ a_{1,1}=a_{1,2}=2 $ . Thus the answer should be $ 2 $ .

观察到如果 n m nm nm 为奇数,无论如何均有解,此时解为 ( r − l + 1 ) n m (r-l+1)^{nm} (rl+1)nm
n m nm nm 为偶数,当且仅当 ∑ a i % 2 = 0 \sum a_i\%2=0 ai%2=0 时有解。
[ l , r ] [l,r] [l,r] 中奇数个数为 O d d Odd Odd,偶数个数为 E v e n Even Even
则解为 ∑ i = 0 , 2 ∣ i n m ( n m i ) O d d i ∗ E v e n n m − i \sum_{i=0,2|i}^{nm}{nm \choose i}Odd^i*Even^{nm-i} i=0,2∣inm(inm)OddiEvennmi
类比二项式定理,我们尝试展开 ( O d d + E v e n ) n m (Odd+Even)^{nm} (Odd+Even)nm ( O d d − E v e n ) n m (Odd-Even)^{nm} (OddEven)nm
( O d d + E v e n ) n m = ∑ i = 0 n m ( n m i ) O d d i ∗ E v e n n − i                   ① (Odd+Even)^{nm}=\sum_{i=0}^{nm}{nm\choose i}Odd^i*Even^{n-i}~~~~~~~~~~~~~~~~~~① (Odd+Even)nm=i=0nm(inm)OddiEvenni                  
( O d d − E v e n ) n m = ∑ i = 0 n m ( n m i ) O d d i ∗ ( − E v e n ) n − i             ② (Odd-Even)^{nm}=\sum_{i=0}^{nm}{nm\choose i}Odd^i*(-Even)^{n-i}~~~~~~~~~~~~② (OddEven)nm=i=0nm(inm)Oddi(Even)ni            
① + ②得 : ①+②得: +:
( O d d + E v e n ) n m + ( O d d − E v e n ) n m = ∑ i = 0 , 2 ∣ i n m ( n m i ) O d d i ∗ E v e n n m − i (Odd+Even)^{nm}+(Odd-Even)^{nm}=\sum_{i=0,2|i}^{nm}{nm \choose i}Odd^i*Even^{nm-i} (Odd+Even)nm+(OddEven)nm=i=0,2∣inm(inm)OddiEvennmi
即为所求。

Code:

#include <bits/stdc++.h>
using namespace std;

#define de(x) cout << #x << " = " << x << endl
#define int long long
const int mod = 998244353;

int n, m, l, r, res;

int qpow(int x, int n, const int mod) {
	int res = 1;
	while (n) {
		if (n & 1) res = res * x % mod;
		x = x * x % mod;
		n >>= 1;
	} return res;
}


signed main() {
	cin >> n >> m >> l >> r;
	n *= m;

	if (n & 1) {
		res = qpow(r - l + 1, n, mod);
	} else {
		int E = r / 2 - (l - 1) / 2, O = r - l + 1 - E;
		res = (qpow((O + E) % mod, n, mod) + qpow((O-E + mod) % mod, n, mod) + mod) % mod * (499122177) % mod;
	}
	cout << res << "\n";
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值