求方案数的平方之和。这个看起来很难解决。如果转化为求方案数的有序对的个数。那么就相当于求A和B同时取,最后序列一样的种数。
令dp[i][j][k]表示A在上管道取了i个,下管道取了j个,B在上管道取了k个,下管道取了i+j-k个珠子的序列相同的种数。
那么状态转移方程显然可得。
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
//# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... int dp[N][N][N];
char s1[N], s2[N]; int dfs(int x, int y, int z){
if (~dp[x][y][z]) return dp[x][y][z];
int res=;
if (x>&&z>&&s1[x]==s1[z]) res+=dfs(x-,y,z-);
if (x>&&x+y>z&&s1[x]==s2[x+y-z]) res+=dfs(x-,y,z);
if (y>&&z>&&s2[y]==s1[z]) res+=dfs(x,y-,z-);
if (y>&&x+y>z&&s2[y]==s2[x+y-z]) res+=dfs(x,y-,z);
return dp[x][y][z]=res%MOD;
}
int main ()
{
int n, m;
scanf("%d%d%s%s",&n,&m,s1+,s2+);
mem(dp,-); dp[][][]=;
printf("%d\n",dfs(n,m,n));
return ;
}