hdu 2929 有n根火柴, 要求组成一个七段数码组成的十进制数, 要求此数是m的倍数, 问此数最大为多少 DP

这是一个使用动态规划(DP)解决的问题,目标是用n根火柴组合成一个十进制数,该数是m的倍数。文章通过定义f[i][j]表示使用i根火柴且对m取模为j时能组成的最大数,通过状态转移找到最大可能的组合。最后输出符合条件的最大数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
Bob has n matches. He wants to compose numbers using the following scheme (that is, digit 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 needs 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 matches):

Write a program to make a non-negative integer which is a multiple of m. The integer should be as big as possible.
 

Input
The input consists of several test cases. Each case is described by two positive integers n (n ≤ 100) and m (m ≤ 3000), as described above. The last test case is followed by a single zero, which should not be processed.
 

Output
For each test case, print the case number and the biggest number that can be made. If there is no solution, output -1.Note that Bob don't have to use all his matches.
 

Sample Input
  
  
6 3 5 6 0
 

Sample Output
  
  
Case 1: 111 Case 2: -1

//  仰慕秦牛 不解释

//秦牛's code  我的java没法过了

f[i][j]为用了i根火柴对m取模为j时能组成的最大的数. 
状态转移: f[i+a[k]][(j*10+k)%m] = max(f[i+a[k]][(j*10+k)%m], f[i][j]*10+k) 


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;


int n,m;


struct bignum
{
    int l;
    char a[60];
    void stlize()
    {
        while (a[l-1]==0) l--;
    }
    void set(char v)
    {
        memset(a,0,sizeof(a));
        l=1;
        a[0]=v;
        if (v==0) l=0;
    }
    void add(char v)
    {
        char i;
        a[0]+=v;
        for (i=0;i<l;++i)
        {
            if (a[i]>=10)
            {
                a[i+1]++;
                a[i]-=10;
            }
            else break;
        }
        if (a[l]>0) l++;
        stlize();
    }
    void mul10()
    {
        for (int i=l;i>0;--i)
            a[i]=a[i-1];
        a[0]=0;
        l++;
        stlize();
    }
    bool bigger(bignum ta)
    {
        ta.stlize();stlize();
        if (ta.l!=l) return l>ta.l;
        for (int i=l-1;i>=0;--i)
            if (ta.a[i]!=a[i]) return a[i]>ta.a[i];
    }
    void give(bignum ta)
    {
        l=ta.l;
        memcpy(a,ta.a,sizeof(char)*60);
    }
    void output()
    {
        if (a[0]==-1||l==0)
        {
            if (n>=6) puts("0");
            else puts("-1");
            return;
        }
        for (int i=l-1;i>=0;--i)
            printf("%d",(char)a[i]);
        puts("");
    }
};


bignum dp[101][3003];


long long neednum[10]={6,2,5,5,4,5,6,3,7,6};


int nc=1;
int i,j,k;


int main()
{
    while (scanf("%d",&n),n)
    {
        scanf("%d",&m);
        for (i=1;i<=n;++i)
            for (j=0;j<m;++j)
                dp[i][j].set(-1);
        for (i=0;i<10;++i)
            dp[neednum[i]][i%m].set((char)i);
        for (i=1;i<=n;++i)
            for (j=0;j<m;++j)
                if (dp[i][j].a[0]!=-1)
                {
                    for (k=0;k<10;++k)
                    {
                        int ns=i+neednum[k];
                        int nn=(j*10%m+k)%m;
                        if (ns>n) continue;
                        bignum temp;
                        temp.give(dp[i][j]);
                        temp.mul10();
                        temp.add((char)k);
                        if (temp.bigger(dp[ns][nn]))
                            dp[ns][nn].give(temp);
                    }
                }
        bignum ans;
        ans.set(-1);
        for (i=1;i<=n;++i)
            if (dp[i][0].bigger(ans)) ans.give(dp[i][0]);
        printf("Case %d: ",nc++);
        ans.output();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值