HDU 5456 Matches Puzzle Game [数位DP]

Problem Description
As an exciting puzzle game for kids and girlfriends, the Matches Puzzle Game asks the player to find the number of possible equations  AB=C with exactly  n (5n500) matches (or sticks).

In these equations,  A,B and  C are positive integers. The equality sign needs two matches and the sign of subtraction needs just one. Leading zeros are not allowed.

Please answer the number, modulo a given integer  m (3m2×109).
 

Input
The input contains several test cases. The first line of the input is a single integer  t which is the number of test cases. Then  t (1t30) test cases follow.

Each test case contains one line with two integers  n (5n500)          m (3m2×109).
 

Output
For each test case, you should output the answer modulo  m.
 

题意:

给出每个数字的火柴拼接数,问用N个火柴棍构恰好造出一个无前导0的A-B=C的方程式的方案数(减号和等号算3个棍子) 

解法:

转换为A+B=C,然后数位DP求解,DP[I][J][2][3][3] 分别表示  棒数 位数 进位状态 A的状态,B的状态 :状态0表示前导为1-9,1表示前导为0,2表示前导为空格

(状态比较丑,A,B的状态其实可以只表示成2种:0:没到最高位,1:已经到过最高位)

可以发现每多一位(即J+1),I都是增加的(除非A,B的前导都是空格),那么J这一维可以省略(这个优化完全想不到,一开始跑了900MS,后来才知道这一维可以优化掉,然后就变成了60MS)

注意取模数最大为20亿,所以加法会暴int,需要定义成unsigned int

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#pragma comment(linker, "/STACK:1024000000,1024000000")
template 
bool scanff(T &ret){ //Faster Input
    char c; int sgn; T bit=0.1;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    if(c==' '||c=='\n'){ ret*=sgn; return 1; }
    while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
    ret*=sgn;
    return 1;
}
#define inf 1073741823
#define llinf 4611686018427387903LL
#define PI acos(-1.0)
#define lth (th<<1)
#define rth (th<<1|1)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)
#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)
#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)
#define mem(x,val) memset(x,val,sizeof(x))
#define mkp(a,b) make_pair(a,b)
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
typedef pair pii;
#define NN 2200000

unsigned int dp[501][2][3][3]; 
//棒数 进位 A的状态,B的状态 :状态0表示前导为1-9,1表示前导为0,2表示前导为空格

int mod,n;
int cot[11]={6,2,5,5,4,5,6,3,7,6,0};

int main(){
    tdata{
        mem(dp,0);
        scanff(n);scanff(mod);
        n-=3;
        rep(a,0,9)
        rep(b,0,9){
            int c=a+b,k=0,as=0,bs=0;
            if(c>=10){
                c-=10;
                k=1;
            }
            if(a==0)as=1;
            if(b==0)bs=1;
            int tot=cot[a]+cot[b]+cot[c];
            dp[tot][k][as][bs]++;
            dp[tot][k][as][bs]%=mod;
        }
        rep(i,0,n)
            rep(k,0,1)
            rep(as,0,2)
            rep(bs,0,2){
                if(as==2&&bs==2)continue;
                if(dp[i][k][as][bs]==0)continue;
                int start_a=0,start_b=0;
                int c,nk,nas,nbs,tot;
                if(as==2)start_a=10;
                if(bs==2)start_b=10;
                rep(a,start_a,10){
                    if(a==10&&as==1)continue;
                    if(a==0)nas=1;
                    else if(a==10)nas=2;
                    else nas=0;
                    rep(b,start_b,10){
                        if(b==10&&bs==1)continue;
                        if(b==10&&a!=10)c=a+k;
                        if(a==10&&b!=10)c=b+k;
                        if(a!=10&&b!=10)c=a+b+k;
                        if(a==10&&b==10)c=k;
                        nk=0;
                        if(c>=10){
                            c-=10;
                            nk=1;
                        }
                        if(a==10&&b==10&&k==0){
                            c=10;
                            nk=0;
                        }
                        if(b==0)nbs=1;
                        else if(b==10)nbs=2;
                        else nbs=0;
                        tot=cot[a]+cot[b]+cot[c];
                        if(i+tot<=n){
                            dp[i+tot][nk][nas][nbs]+=dp[i][k][as][bs];
                            dp[i+tot][nk][nas][nbs]%=mod;
                        }
                    }
                }
            }
        printf("Case #%d: %u\n",cas,dp[n][0][2][2]);
    }
    return 0;
}


你可能感兴趣的:(动态规划--数位DP)