uva11472 Beautiful Numbers (数位DP)

dp[l][x][used]表示长度为l尾部为x的串用了那些数字的种数

#include <iostream>
#include <cstring>
#include <cstdio>
#define mod 1000000007
using namespace std;
int t,n,m;
int dp[110][12][1<<10|1];
int g(int x){
   return x>=0 ? x:(-x);
}
int DP(int l,int x,int used){
    if(l==0){
        if(used==(1<<n)-1) return 1;
        else return 0;
    }
    if(dp[l][x][used]!=-1) return dp[l][x][used];
    int ret=0;
    for(int i=0;i<n;i++){
        if(g(x-i)==1) ret=(ret+DP(l-1,i,used|(1<<i)))%mod;
    }
    return dp[l][x][used]=ret;
}
int main(){
    scanf("%d",&t);
    for(int ca=1;ca<=t;ca++){
        scanf("%d%d",&n,&m);
        memset(dp,-1,sizeof dp);
        int ans=0;
        for(int j=1;j<=m;j++){
            for(int i=1;i<n;i++){
                ans=(ans+DP(j-1,i,1<<i))%mod;
                //cout<<j-1<<" "<<i<<" "<<DP(j-1,i,1<<i)<<endl;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
uva11472

 

posted @ 2014-02-08 16:49  wonderzy  阅读(202)  评论(0编辑  收藏  举报