# E. Height All the Same-Codeforces Round #630 (Div. 2)

Keywords: number theory cf

# E. Height All the Same

### General idea of the topic

Given the grid of \ (n * m \), cubes can be placed on each grid. Initially, there are \ (a_{i,j} \) cubes on the grid with coordinates of \ ((i,j) \). Now there are two operations:

• Select two adjacent squares and place a cube on top of each
• Select a person's Square and place two cubes on it

It is required to make the number of cubes on each grid equal within a limited number of operations.
Now give the value \ ([L,R] \) of \ (a_{i,j} \), and ask how many initial quantity schemes can meet the above requirements.

### Problem solving ideas

Because the operation can be performed any number of times, it is not necessary to consider the specific operation times, and it can be found that we can select any two squares to add \ (1 \) by operating \ (1 \).
Then I think that because the final quantity needs to be the same, each time I add \ (1 \) or \ (2 \), I consider the parity of the initial state. Assuming that all numbers are odd or even, the goal can be achieved by operation \ (2 \).
It is assumed that initially there are \ (x \) odd numbers and \ (y \) even numbers.
Because the operation \ (2 \) will not change the parity of the number of cubes on the overall grid, only the operation \ (1 \) on the grid with the same parity can change the overall parity. For example, if you select two odd grids for operation 1, the overall odd number will decrease \ (2 \), and the even number will increase \ (2 \), while selecting the grid with different parity is equivalent to no change (parity does not change).
And each change is \ (2 \), so one of the initial odd number or even number must be an even number to meet the requirements.

• If \ (n * m \) is an odd number, then one of the odd and even numbers must be an even number. The answer is \ ((R - L + 1)^{n * m} \)
• If it is an even number, it needs to be calculated and obtained by combinatorial mathematics \ (\ sum {I = 0} ^ {n * m} C {n * m} ^ I * x ^ I * y ^ {(n * m - I)} * [I \% 2 = 0] \)

For the second case,
According to the binomial theorem, \ ((x - y) ^ {(n * m)} = \ sum {I = 0} ^ {n * m} ^ {I} x ^ I * y ^ {(n * m - I)} * (- 1) ^ {n * m - I} = \ sum {I = 0} ^ {n * m} ^ {I} x ^ I * y ^ {(n * m - I)} * (- 1) ^ {I} \)
Therefore, odd items are \ (- 1 \) and even items are \ (1 \). We only need the value of even items.
$$(x + y)^{n*m} = \sum_{i = 0}^{n * m} C_{n * m}^{i} x^i * y^{(n *m - i)}$$
So \ (ans = \cfrac{(x-y)^{n * m} + (x + y^{n * m})}{2} \)

Therefore:
When \ (n *m \) is an odd number, \ (ANS = (R - L + 1) ^ {n *m} \)
Otherwise \ (ans = \cfrac{(x-y)^{n * m} + (x + y^{n * m})}{2}\ \ (x is the number of odd numbers and Y is the number of even numbers) \)

### Code

#include <bits/stdc++.h>
#define ll long long
#define qc ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define fi first
#define se second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define pb push_back
#define V vector
using namespace std;
const int N = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 998244353;

inline char nc() {
static char buf[1000000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++;
}
template <typename _Tp> inline void read(_Tp&sum) {
char ch = nc(); sum = 0;
while (!(ch >= '0'&&ch <= '9')) ch = nc();
while (ch >= '0'&&ch <= '9') sum = (sum << 3) + (sum << 1) + (ch - 48), ch = nc();
}

ll n,m,l,r;

ll ksm(ll x,ll y)
{
ll p = 1;
while(y)
{
if(y & 1)p = p * x % mod;
x = x * x % mod;
y >>= 1;
}
return p;
}

void solve(){
if((n * m) & 1){
printf("%lld\n",ksm((r - l + 1), n * m));
}else{
ll x = 0, y = 0;
if((l & 1) && (r & 1)){
x = (r - l) / 2;
y = (r - l) / 2 + 1;
}else if(!(l & 1) && !(r & 1)){
x = (r - l) / 2 + 1;
y = (r - l) / 2;
}else{
x = y = (r - l + 1) / 2;
}
printf("%lld\n",((ksm(abs(x - y), n * m) + ksm(x + y, n * m)) % mod) * ksm(2ll, mod - 2) % mod);
}
}

int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif

int T = 1;
// scanf("%d",&T);
while(T--){
solve();
}
return 0;
}


Posted by Adarmo on Mon, 01 Nov 2021 04:53:03 -0700