HDU 2929 Bigger is Better

Bigger is Better

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 767    Accepted Submission(s): 197


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
 

Source
2006 Asia Regional Xi-An
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   2937  2932  2930  2931  2933 


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;
const int N=222,M=3222,mod=100000000;
int a[]={6, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
typedef int BIG[7];
BIG dp[N][M];
#define big BIG
void big2b(BIG a,BIG b)
{
    for(int i=6;i>=0;i--)a[i]=b[i];
}
bool Less(BIG a,BIG b)
{
    for(int i=6;i>=0;i--)
    {
        if(a[i]<b[i])return 1;
        if(a[i]>b[i])return 0;
    }
    return 0;
}
void mult(BIG x,int k,BIG ret)
{
    for(int i=0;i<7;i++)ret[i]=x[i];
    for(int i=0;i<7;i++)
    {
        ret[i]=ret[i]*10+k;
        k=  ret[i]/mod;
        ret[i]%=mod;
    }
}
void get(int n,int m)
{
    memset(dp,0,sizeof dp);
    for(int i=0;i<N;i++)for(int j=0;j<M;j++)dp[i][j][0]=-1;
    dp[0][0][0]=0;
    big b,ret,t;
    memset(ret,0,sizeof ret);
    ret[0]=-1;
    for(int i=0;i<=n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(dp[i][j][0]==-1)continue;
            for(int k=0;k<10;k++)
            {
                if(i+a[k]>n)continue;
                mult(dp[i][j],k,t);
                if(Less(dp[i+a[k]][(j*10+k)%m],t))
                {
                    big2b(dp[i+a[k]][(j*10+k)%m],t);
                    if((j*10+k)%m==0&&Less(ret,t))
                        big2b(ret,t);
                }
            }
        }
    }
    if(ret[0]==-1)
    {
        puts("-1");
    }
    else{
        int i;
        for(i=6;i>=0;i--)if(ret[i])break;
        if(i==-1)
        {
            puts("0");return;
        }
        for(;i>=0;i--)cout<<ret[i];
        puts("");
    }
}
int main()
{
    int n,m,cas=1;
    while(1)
    {
        cin>>n;if(n==0)break;
        cin>>m;

        printf("Case %d: ",cas++);get(n,m);
    }
}


你可能感兴趣的:(HDU 2929 Bigger is Better)