[NOI2009] Pipeline to take beads

Topic description

Pipe Beading is a game that Xiao X likes very much. In this problem, we will consider a simple modification of the game. The game screen is shown in Figure 1:

(figure 1)

At the beginning of the game, the upper and lower pipes on the left have a certain number of small balls (there are two types of dark balls and light balls), while the output pipe on the right is empty. For each operation, you can select a pipe from the left and push the rightmost ball in that pipe into the output pipe on the right.

For example: we first move a ball from the lower pipe to the output pipe, and we will get the situation shown in Figure 2.

(figure 2)

Assuming that there are n balls in the upper pipe and m balls in the lower pipe, the entire game process needs to perform n+m operations, that is, move all the balls in the left pipe into the output pipe. The final n+m balls form the output sequence from right to left in the output pipeline.

The math-loving little X knows that he has a total of C(n+m,n) different operations, and different operations may lead to the same output sequence. For example, for the game situation shown in Figure 3:

(image 3)

We use A for light balls and B for dark balls. Assuming that the operation of moving the ball on the right side of the upper pipe is U, and the operation of moving the ball on the right side of the lower pipe is D, there are C(2+1,1)=3 different operation modes, namely UUD, UDU, DUU; The final output sequence formed in the output pipeline (from right to left) is BAB, BBA, BBA. It can be found that the latter two operations will result in the same output sequence.

It is assumed that there are K kinds of different kinds of output sequences that may be generated in the end, among which: there are ai of the generation modes of the i-th output sequence (that is, the number of different operation modes). The clever little X already knew that,

Σai=C(n+m,n)

Therefore, little X wants to calculate:

Σ(ai)^2

Can you help him calculate this value? Since this value can be large, just output the value modulo 1024523 (i.e. the remainder of dividing by 1024523).

Explanation: C(n+m, n) in the text represents the number of combinations. The number of combinations C(a, b) is equivalent to the number of options for selecting b among a different items.

Input and output format

Input format:

 

The first line in the input file contains two integers n and m, which represent the number of balls in the upper and lower pipes respectively.

The second line is an AB string with a length of n, indicating the type of the ball from left to right in the upper pipeline. Among them: A represents the light-colored ball, B represents the dark-colored ball.

The third line is an AB string with a length of m, which represents the situation in the lower pipeline.

 

Output format:

 

Only one line in the output file is an integer, which is the remainder after dividing by 1024523.

 

Input and output example

Input example #1: 
2 1
AWAY
B
Sample output #1:
5

illustrate

【Example description】

The example is in the text (Figure 3). There are two different output sequence forms, the sequence BAB is produced in 1 way, and the sequence BBA is produced in 2 ways, so the answer is 5.

【Data scale and convention】

For 30% of the data, satisfy: m, n<=12;

For 100% of the data, satisfy: m, n<=500.

 

 

   A new dp pose qwq, counting ∑a[i] ^ 2 is equivalent to counting how many ordered schemes are the same, or simply take twice, the second sequence is the same as the first sequence Number, just force four-dimensional dp directly, because the last dimension is limited, in fact, only three-dimensional, transfer O(1), you can roll the array. . .

 

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int ha=1024523,maxn=505;
inline void ADD(int &x,int y){ x+=y; if(x>=ha) x-=ha;}
int f[2][maxn][maxn],n,m,now,nxt;
char A[maxn],B[maxn];

inline void dp(){
	f[now=0][m][n]=1,nxt=1;
	for(int i=n;i>=0;i--,now=nxt,nxt^=1){
		memset(f[nxt],0,sizeof(f[nxt]));
		for(int j=m;j>=0;j--)
		    for(int k=n,o;k>=0;k--) if(f[now][j][k]){
		    	o=i+j-k;
		    	if(i){
				    if(A[i]==A[k]) ADD(f[nxt][j][k-1],f[now][j][k]);
		    	    if(A[i]==B[o]) ADD(f[nxt][j][k],f[now][j][k]);
		    	}
		    	if(j){
                    if(B[j]==A[k]) ADD(f[now][j-1][k-1],f[now][j][k]);
		    	    if(B[j]==B[o]) ADD(f[now][j-1][k],f[now][j][k]);
		    	}
			}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	scanf("%s%s",A+1,B+1);
	A[0]='~',B[0]='@',dp();
	printf("%d\n",f[nxt][0][0]);
	return 0;
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325299051&siteId=291194637