BZOJ 1566 管道取珠(DP)

求方案数的平方之和。这个看起来很难解决。如果转化为求方案数的有序对的个数。那么就相当于求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-9
# define MOD 1024523
# define INF 1000000000
# 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<<1,l,mid
# define rch p<<1|1,mid+1,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=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void Out(int a) {
    if(a<0) {putchar('-'); a=-a;}
    if(a>=10) Out(a/10);
    putchar(a%10+'0');
}
const int N=505;
//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=0;
    if (x>0&&z>0&&s1[x]==s1[z]) res+=dfs(x-1,y,z-1);
    if (x>0&&x+y>z&&s1[x]==s2[x+y-z]) res+=dfs(x-1,y,z);
    if (y>0&&z>0&&s2[y]==s1[z]) res+=dfs(x,y-1,z-1);
    if (y>0&&x+y>z&&s2[y]==s2[x+y-z]) res+=dfs(x,y-1,z);
    return dp[x][y][z]=res%MOD;
}
int main ()
{
    int n, m;
    scanf("%d%d%s%s",&n,&m,s1+1,s2+1);
    mem(dp,-1); dp[0][0][0]=1;
    printf("%d\n",dfs(n,m,n));
    return 0;
}
View Code

 

posted @ 2017-04-29 12:54  free-loop  阅读(147)  评论(0编辑  收藏  举报