[AtCoder Grand Contest 043-B] 123 Triangle

Description of the meaning of problems

A given length of \ (n-\) sequence \ (A [] \) , \ (= a_i l, 2,3 \)
defines \ (x_ {i, j} \) is:

\[\begin{equation*} x_{i,j}= \begin{cases} a_j & i=1,j \in [1,n] \\ |x_{i-1,j}-x_{i-1,j+1}| & {i \in [2,n],j \in [1,n+1-i]} \end{cases} \end{equation*} \]

Seeking (x_ {n, 1} \ ) \ value.
\ (n \ leq 10 ^ 6 \)


idea

First, obviously \ (X_ {I, J = 0,1,2} \) . \ (x_ {i, j} \) values related only to the difference, it is possible to \ (A [] \) all values are decremented by one, so that \ (= a_i 0,1,2 \) , multiple neat .

Then do not seem to know how to do it. Consider a case where the weakened so that \ (A [] \) is only two values.
Without loss of generality disposed \ (a [i] \) of the two values is \ (0 \) and \ (T \) , all \ (x_ {i, j} \) values only \ (0 \) , \ (t \)
again without loss of generality can set these two values is \ (0 \) and \ (1 \) .

In this case the problem is converted to, a given by the \ (0 \) and \ (1 \) array configuration \ (A [] \) , seeking \ (x_ {n, 1}
\) We note that 0 and 1 the difference is the difference between 0 and 1, 1 and 0 or 1 is 0. This is precisely an exclusive-OR operation.
Then there are \ (X_ {I, J} = X_ {I-. 1, J} \ Oplus X_ {I-. 1, J-. 1} \) , \ (\ Oplus \) represents an exclusive OR
final \ (x_ {n , i} \) is composed of several \ (a [i] \) XOR obtained.
Specific XOR How many times do?
To facilitate said predetermined symbol \ (the XOR (m, I) = m \ m Oplus \ Oplus ... \ Oplus m \) , represents \ (I \) a \ (m \) attached XOR.
It-yourself painting a picture can be found in the law (the figure represents ^ XOR).

Each \ (x_ {i, j} \) are two right and left of the following line numbers contribute, from the bottom upwards is a recursion found Pascal triangle.

Thus the number of combinations come to easily calculate the modulo 2 can know \ (x_ {n, 1} \) is the parity.
Note that the number of factorial combinations in the D 2 recording needs to 2:00.

Knowing the parity can be judged that the 1.

Then consider how to determine 0 and 2.
Here there is a small nature:
If the original sequence ( \ (A [I] -1 \) ) in 1, then the final answer can only be 0.
Prove it simple: if the original sequence all 1, then the final answer must be 0; 1 if not all, then there must be 1 and 2 or 1 position adjacent to the adjacent 0, the difference between these neighboring locations is 1, and The next line is a must have.
In this recursion continues, in the event of a line full before 1, all lines are all 1's. Until a full line 1, it will be after 1 line, the line and after the whole is zero.
Is it possible that the final answer is 1 do? As the front of the judges had the answer is even, so can only be 0.

After determining such a case, even when the residual prosequence only \ (0 \) and \ (2 \) . Analyzing available above \ (0 \) and \ (1 \) method determines the final answer.

Miao Miao title title ah!


to sum up

thought

Consider parity.
Binary.
Have the courage to discuss the classification! !

achieve

Number of combinations of small primes modulo: record the number.


Code

#include<cstdio>
#include<iostream>
#include<algorithm>
 
using namespace std;
 
const int N = 1000005;
 
int n;
char s[N];
int t[N];
 
int main()
{
	scanf("%d",&n);
	scanf("%s",s+1);
	for(int i=1;i<=n;i++) s[i]--;
	
	int sum=0,c=0;
	t[0]=t[1]=0;
	for(int i=2;i<=n;i++)
		if(i&1) t[i]=0;
		else t[i]=t[i/2]+1;
	for(int i=1;i<=n;i++){
		if(i>1) c+=t[n-i+1]-t[i-1];
		if(s[i]=='1' && c==0) sum^=1;
	}
	if(sum==1) { printf("1\n"); return 0; }
	
	for(int i=1;i<=n;i++)
		if(s[i]=='1') { printf("0\n"); return 0; }
	
	sum=0; c=0;
	for(int i=1;i<=n;i++){
		if(i>1) c+=t[n-i+1]-t[i-1];
		if(s[i]=='2' && c==0) sum^=1;
	}
	if(sum==1) printf("2\n");
	else printf("0\n");
	
	return 0;
}

Guess you like

Origin www.cnblogs.com/lindalee/p/12576020.html