[codeforces 1332E] Height All the Same parity construction + permutation + fast power + binomial theorem + multiplicative inverse

Codeforces Round # 630 (Div. 2)   Number of matches 12012

[codeforces 1332E] Height All the Same parity construction + permutation + fast power + binomial theorem + multiplicative inverse

See https://blog.csdn.net/mrcrack/article/details/103564004 for the general catalog

Online evaluation address https://codeforces.com/contest/1332/problem/E

Problem Just Verdict Time Memory
E - Height All the Same GNU C ++ 11 Accepted 31 ms 100 KB

1. Understanding the question

How to put squares, the understanding of the title is also a big problem.

Stack up one cube in two adjacent cells; how to understand this sentence is still difficult to understand according to Operation 1 in the figure below.

What is needed at this time is patience , or to grasp the keywords.

Initially ai, j cubes are stacked up in the cell (i, j). This sentence is very important. The cell defines the location, and many cubes can be placed in the same cell.

Combining the two sentences above with English, you should be able to read the meaning of the question :

There is now a n * m square with a [i] [j] squares in row i and column j.

You can perform the following operations any number of times:

1.1. Select (i, j) to add 2 to a [i] [j].

1.2. Select two adjacent squares and add 1 to the number of squares.

Now ask the initial a [i] [j] can be any number in [L, R], how many initial schemes can make all a [i] [j] equal after any number of operations.

 

2. The idea is taken from https://www.cnblogs.com/qieqiemin/p/12614527.html and individual errors are corrected

2.1 Because Operation 2 does not change the parity, and the number of the same parity must be changed into an equal number by Operation 2, so for an n ∗ m

Only the parity of the lattice is considered. The question can be transformed into whether two adjacent lattices can be selected to reverse their parity, and finally make the parity of the entire lattice consistent.

This is a classic problem, the solution is: it is not feasible that all odd and even numbers are odd, otherwise it is feasible.

You can watch the picture below. Red represents odd numbers and white represents even numbers.

We can make it into one operation (Operation 1):

After moving the odd number to some special position, because the number of remaining even numbers, cnt, is even, you can use cnt / 2 flip operations (Operation 1) to become:

All become red (odd numbers), 1 in the figure above represents the first operation, 2 represents the second operation, 3 represents the third operation, and 4 represents the fourth operation.

As long as at least one of the odd number and the even number is an even number, it can be changed to the same parity by this method.

(R-L + 1) ^ nm is explained as follows: each position can take values ​​as follows

L, L + 1, L + 2, ......, R-1, R, there are R-L + 1 ways, there are a total of nm positions, according to the multiplication principle of arrangement and combination, the number of types is (R -L + 1) ^ nm

2.2 Otherwise, at this time n ∗ m is even , let a be the number of odd numbers in [L, R], and let b be the number of even numbers in [L, R].

If you have mastered the Binomial Theorem , the rightmost equation in the above equation should be understandable.

2.3.a+b=R-L+1

If R-L + 1 is odd, | ab | = 1; if R-L + 1 is even, | ab | = 0.

The origin of the value of | ab | can be studied as an example :

There are 2 even numbers in 1,2,3,4,5, 3 odd numbers, R-L + 1 = 5, | ab | = 1

There are 3 even numbers and 3 odd numbers in 1,2,3,4,5,6, R-L + 1 = 6, | ab | = 0

There are 3 even numbers in 2, 3, 4, 5, 6 and 2 odd numbers, R-L + 1 = 5, | ab | = 1

There are 3 even numbers and 3 odd numbers in 2,3,4,5,6,7, R-L + 1 = 6, | ab | = 0

2.4. Don't forget to calculate the multiplicative inverse of 2.

AC code is as follows

#include <stdio.h>
#define LL long long
#define mod 998244353
LL quick_pow(LL a,LL b){//快速幂
	LL ans=1;
	while(b){
		if(b&1)ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
int main(){
	LL n,m,L,R,x,ans;
	scanf("%lld%lld%lld%lld",&n,&m,&L,&R);
	x=R-L+1;
	if(n*m%2==1)printf("%lld\n",quick_pow(x,n*m));
	else{//n*m%2==0
		//a奇数个数,b偶数个数  a+b=R-L+1,  |a-b|=1或0,若R-L+1奇数,为1,否则为0
		ans=quick_pow(x,n*m)+((x&1)?1:0);//((x&1)?1:0)计算(a-b)^nm
		ans=ans*quick_pow(2,mod-2)%mod;//关于2的乘法逆元
		printf("%lld\n",ans);
	}
	return 0;
}

 

Published 660 original articles · praised 562 · 480,000 views

Guess you like

Origin blog.csdn.net/mrcrack/article/details/105270405