Codeforces 1332E Height All the Same

Description

Given $ n, m, L, R $, seeking size $ n \ times m $ matrix $ a_ {i, j} $ satisfying $ L \ le a_ {i, j} \ le R $ and by together with adjacent elements $ + $ 1, $ 2 + $ element two operation of the entire matrix is ​​equal to the number of $ \ bmod ~ 998244353 $.

Solution

First, the results can be observed and the actual height is independent of the degree of parity and relevant.

This is well understood, because we can operate 2 such that the change in parity with the number of domains.

Now only consider the operation 1.

We $ [L, R] $ E $ with an even number of $, $ O $ represents $ [L, R] of $ odd number.

Next to the two cases.

If $ n \ cdot m $ is odd, then, it can prove, either way is legal.

why? Because it thought that the figure is either an even number $ a \ in \ text {Even} $, or there is an even number $ a \ in \ text {Odd} $, no matter what kind, we can make this part of the operation 1 it becomes the opposite parity, the parity are equal, the answer will be equal.

So then the answer is:

$$ \sum_{i = 1}^{n} \sum_{j = 1}^{m}(R-L+1) = (R-L+1)^{nm}$$

Otherwise it is $ n \ cdot m $ is even.

Provided the final layer is $ h $, then the final $ \ sum_ {i = 1} ^ {n} \ sum_ {j = 1} ^ {m} a_ {i, j} = nmh $, apparently even number. At this time we want to reverse the process, no matter how operations, $ \ sum_ {i = 1} ^ {n} \ sum_ {j = 1} ^ {m} a_ {i, j} $ is always an even number, so we just and keep the situation is even, certainly solvable.

Perhaps you would like to ask why this is necessary and sufficient conditions, and is even because, indicating that there must be an even number $ a \ in \ text {Odd} $, so the analogy can cicadas above.

But how does this demand?

Obviously, as long as we keep the situation on the $ a \ in \ text {Odd} $ is an even number on the line. Might 2i provided $ $ position is an odd number, $ [L, R] $ $ O $ has odd, $ E $ even number.

The answer is:

$$ \sum_{i = 0}^{\frac{nm}{2}}\left(\begin{matrix} nm \\ 2i \end{matrix} \right )O^{2i} E^{nm - 2i} $$

But how to find this formula it?

This allows us to think of the binomial theorem.

Binomial theorem expressed as:

$$ (x+y)^{z} = \sum_{i=0}^{z}\left(\begin{matrix} z \\i \end{matrix}\right)x^i y^{z-i} $$

Is not it more similar?

$$ (O + E)^{nm} = \sum_{i = 0}^{nm} \left(\begin{matrix} nm \\ i \end{matrix} \right) O^i E^{nm - i}$$

Then the odd and even term items are pulled out.

$$(O+E)^{nm} = \sum_{i = 0}^{\frac{nm}{2}} \left(\begin{matrix} nm \\ 2i \end{matrix} \right ) O^{2i} E^{nm - 2i} + \sum_{i = 1}^{\frac{nm}{2}} \left(\begin{matrix} nm \\ 2i-1 \end{matrix} \right ) O^{2i-1} E^{nm - (2i-1)}$$

Found that the left half is what we want.

How eliminate the right half? Find:

$$ (O-E)^{nm} = \sum_{i = 0}^{\frac{nm}{2}} \left(\begin{matrix} nm \\ 2i \end{matrix} \right ) O^{2i} E^{nm - 2i} - \sum_{i = 1}^{\frac{nm}{2}} \left(\begin{matrix} nm \\ 2i-1 \end{matrix} \right ) O^{2i-1} E^{nm - (2i-1)} $$

Adding the two equations to give:

$$ (O + E)^{nm} + (O - E)^{nm} = 2 \sum_{i = 0}^{\frac{nm}{2}} \left(\begin{matrix} nm \\ 2i \end{matrix} \right ) O^{2i} E^{nm - 2i}$$

So, the answer is:

$$ \ frac {(O + E) ^ {+} nm (O - E) ^ {} {} nm 2} $$

Use fast power can do $ \ mathcal O (\ log (nm)) $, it is worth noting that the use of Euler's theorem special judge should base number equal to $ 0 $ circumstances, otherwise they will be out of extreme data card, for example:

998244352 2 1 998244353

The answer is $ 49,912,217 $.

Details see the code.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 998244353; 
LL qpow(LL a, LL p)
{
    if(a == 0) return 0;
    LL res = 1;
    while(p)
    {
        if(p & 1) res = res * a % MOD;
        a = a * a % MOD;
        p >>= 1;
    }
    return res;
}
int main()
{
    LL n, m, l, r;
    cin >> n >> m >> l >> r; 
    if(n * m & 1) cout << qpow((r - l + 1) % MOD, n * m % (MOD - 1)) << endl;
    else
    {
        LL E = (r >> 1) - (l - 1 >> 1);
        LL O = r - l + 1 - E;
        cout << ((qpow ((O + E)% MOD, n * m% (MOD - 1)) + qpow ((O - E + MOD)% MOD, n * m% (MOD - 1))) * 499122177% mOD) << endl; // 499122177 998244353 is the inverse sense mod 2 
    } 
    return 0; 
}

Guess you like

Origin www.cnblogs.com/syksykCCC/p/CF1332E.html