Codeforces Round # 630 (Div. 2) E. Height All the Same (a combination of mathematical inverse fast power)

He said face a lot of problems, can be abstracted as direct a matrix of n * m, each of a [i] [j] representative of the height of the grid (i, j) of. You can perform two operations:
1. to any one of a [i] [j] plus 2.
2. to two adjacent grid 1 are added
now given n, m, l, r, n ask you * m matrix initial value of each lattice point is [l, r] of any one ask how many kinds of the initial values can be made through the above two modes of operation, the same value possessive point.

How to determine whether the above-described operation such that all of equal weight matrix? Parity from the viewpoint of initial size and weight of the matrix.

1, if n * m is an odd number, then there must be an even number (or even number of unit weight value of the weight value is an even odd units), will be constructed by the above-described operation of the weight of the same parity. The total number of the program in this case is (L-R & lt +. 1) m * n- this part can be processed directly by flash power.

2, the difficulty lies in the n * m is an even number. Is initially assumed that a matrix is ​​an odd number the number of unit weight, b is an even number is the number of weights. If a is odd, no matter how the above-described operations are not all a and b such that the same parity.

Only when a and b are both even, before they can become the same parity.




 1 #include<cstring>
 2 #include<iostream>
 3 #include<cstdio>
 4 using namespace std;
 5 typedef long long ll;
 6 const int maxn = 265000;
 7 const ll mod = 998244353;
 8 ll q_pow(ll x,ll n){
 9     ll res = 1;
10     while(n){
11         if(n&1) res = (res * x)%mod;
12         x = x*x%mod;
13         n>>=1;
14     }
15     return res;
16 }
17 int main() {
18     ll n,m,l,r;
19     cin>>n>>m>>l>>r;
20     ll ans;
21     if(ll(n*m)%2){
22         ans = q_pow(r-l+1,n*m);
23     }
24     else{
25         ll cnt = r - l + 1;
26         ll inv2 = q_pow(2,mod-2);
27         years = (q_pow (cnt, m * n) + q_pow (cnt & 1 ? 1 : 0 , m * n))% mod;
28          years = years * inv2% mod;
29      }
 30      cout << years;
31      return  0 ;
32 }


Guess you like