hdu 5456 Matches Puzzle Game(dp)

题意:给出n个火柴,要求拼出形如x-y=z的等式,x,y,z都为正数。问情况总数。

做法:用一个数组a[i]代表用了i根火柴拼成数字情况总数,数组b[i]代表用了i根火柴拼成2个数字(相差1)的情况总数。

然后dp[i][0]代表用了i根火柴不借位的情况数,dp[i][1]则代表借位,然后枚举前面放的新的数字是几然后分类讨论即可。

做法比较搓,细节问题多思考量比较大。

AC代码:

#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll __int64
#define ull unsigned __int64
#define eps 1e-8
#define NMAX 200000000
#define MOD 530600414
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
template<class T>
inline void scan_d(T &ret)
{
    char c;
    int flag = 0;
    ret=0;
    while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c == '-')
    {
        flag = 1;
        c = getchar();
    }
    while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
    if(flag) ret = -ret;
}

ll a[605],b[1105];
int shu[10] = {6,2,5,5,4,5,6,3,7,6};

ll dp[605][2];

int main()
{
#ifdef GLQ
    freopen("input.txt","r",stdin);
//    freopen("o.txt","w",stdout);
#endif
    int T,cas = 1;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        ll m;
        scanf("%d%I64d",&n,&m);
        memset(a,0,sizeof(a));
        a[0] = 1;
        for(int i = 0; i <= n; i++)
        {
            int lim = i == 0 ? 1 : 0;
            for(int j = lim; j <= 9; j++)
                a[i+shu[j]] = (a[i+shu[j]]+a[i])%m;
        }
        memset(b,0,sizeof(b));
        b[2] = 1;
        for(int i = 3; i <= n; i++)
        {
            for(int s1 = 1; s1 <= 9; s1++) if(i >= shu[s1]+shu[s1-1])
                {
                    int p = i-shu[s1]-shu[s1-1];
                    if(((s1 == 1 && p > 0) || s1 != 1) && p%2 == 0)
                        b[i] = (b[i]+a[p/2])%m;
                }
            if(i >= shu[0]+shu[9]) b[i] = (b[i]+b[i-shu[0]-shu[9]])%m;
        }
        n -= 3;
        memset(dp,0,sizeof(dp));
        dp[0][0] = 1;
        ll ans = 0;
        for(int i = 0; i < n; i++)
        {
            if(dp[i][0])
            {
                for(int s1 = 0; s1 <= 9; s1++)
                    for(int s2 = 0; s2 <= 9; s2++)
                    {
                        if(s1 > s2)
                        {
                            int ha = shu[s1]+shu[s2]+shu[s1-s2];
                            if(s2)
                            {
                                int wo = 2, p = n-i-ha;
                                if(p == 0) wo = 1;
                                if(p >= 0 && p%2 == 0) ans = (ans+a[p/2]*dp[i][0]%m*wo%m)%m;
                            }
                            else
                            {
                                int p = n-i-ha;
                                if(p > 0 && p%2 == 0) ans = (ans+a[p/2]*dp[i][0]%m)%m;
                            }
                            dp[i+ha][0] = (dp[i+ha][0]+dp[i][0])%m;
                        }
                        else if(s1 < s2)
                        {
                            int ha = shu[s1]+shu[s2]+shu[s1-s2+10];
                            if(n-i-ha >= 2)
                            {
                                int wo = n-i-ha == 2 ? 1 : 2;
                                ans = (ans+dp[i][0]*b[n-i-ha]%m*wo%m)%m;
                            }
                            dp[i+ha][1] = (dp[i+ha][1]+dp[i][0])%m;
                        }
                        else
                        {
                            int ha = shu[s1]+shu[s2]+shu[0];
                            int p = n-i-ha;
                            if(s1 != 0 && p > 0 && p%2 == 0) ans = (ans+a[p/2]*dp[i][0]%m)%m;
                            dp[i+ha][0] = (dp[i+ha][0]+dp[i][0])%m;
                        }
                    }
            }
            if(dp[i][1])
            {
                for(int s1 = 0; s1 <= 9; s1++)
                    for(int s2 = 0; s2 <= 9; s2++)
                    {
                        if(s1-1 >= s2)
                        {
                            int ha = shu[s1]+shu[s2]+shu[s1-1-s2];
                            if(s2 != 0 && s1-1-s2 != 0)
                            {
                                int wo = 2, p = n-i-ha;
                                if(p == 0) wo = 1;
                                if(p >= 0 && p%2 == 0) ans = (ans+dp[i][1]*a[p/2]%m*wo%m)%m;
                            }
                            else if((s2 == 0 && s1-1-s2 != 0) || (s2 != 0 && s1-1-s2 == 0))
                            {
                                int p = n-i-ha;
                                if(p > 0 && p%2 == 0) ans = (ans+dp[i][1]*a[p/2]%m)%m;
                            }
                            dp[i+ha][0] = (dp[i+ha][0]+dp[i][1])%m;
                        }
                        else
                        {
                            int ha = shu[s1]+shu[s2]+shu[s1+9-s2];
                            if(s2 == 0 || s1+9-s2 == 0)
                            {
                                int p = n-i-ha;
                                if(p > 2) ans = (ans+dp[i][1]*b[p]%m);
                            }
                            else
                            {
                                int p = n-i-ha;
                                int wo = p == 2 ? 1 : 2;
                                if(p >= 2) ans = (ans+dp[i][1]*b[p]%m*wo%m)%m;
                            }
                            dp[i+ha][1] = (dp[i+ha][1]+dp[i][1])%m;
                        }
                    }
            }
        }
        printf("Case #%d: %I64d\n",cas++,ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值