首页 > ACM题库 > HDU-杭电 > HDU 2929-Bigger is Better-动态规划-[解题报告]HOJ
2014
02-23

HDU 2929-Bigger is Better-动态规划-[解题报告]HOJ

Bigger is Better

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

6 3
5 6
0

样例输出:

Case 1: 111
Case 2: -1

题意:你的任务是用不超过n根火柴摆一个尽量大的,能被m整除的正整数,无解时输出-1

思路:先预处理出用不超过n根火柴能摆成的最大数有几位,那么只有当这个数是m的倍数,它的位数越大才有用,那么用一维记录余数,所以dp[i][j]表示用i根组成的数余数是j的最大位数是多少,接下来的就是打印各个位上的数了,试想一下满足的条件,

dp[i][j]+1==dp[newi][newj]满足的话,那么如果dp[newi][newj]存在的话,那么新增的K就是下一位,用next[i][j]表示当前状态的下一个数

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 110;
const int MAXM = 3010;
const int used[10] = {6,2,5,5,4,5,6,3,7,6};

int dp[MAXN][MAXM],next[MAXN][MAXM];
int n,m,maxlen;

int main(){
    int cas = 0;
    while (scanf("%d",&n) != EOF && n){
        scanf("%d",&m);
        memset(dp,-1,sizeof(dp));
        dp[0][0] = 0,maxlen = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                if (dp[i][j] >= 0)
                    for (int k = 9; k >= 0; k--)
                        if (i + used[k] <= n){
                            int newi = i+used[k],newj = (j*10+k)%m;
                            if (dp[i][j] + 1 > dp[newi][newj]){
                                dp[newi][newj] = dp[i][j] + 1;
                                if (dp[newi][newj] > maxlen && newj == 0)
                                    maxlen = dp[newi][newj];
                            }
                        }
        memset(next,-1,sizeof(next));
        for (int i = n; i >= 0; i--)
            for (int j = 0; j < m; j++)
                if (dp[i][j] >= 0){
                    if (dp[i][j] == maxlen && j == 0){
                        next[i][j] = 10;
                        continue;
                    }
                    for (int k = 9; k >= 0; k--){
                        if (i + used[k] <= n){
                            int newi = i + used[k];
                            int newj = (j*10+k)%m;
                            if (dp[newi][newj] == dp[i][j] + 1 && next[newi][newj] >= 0){
                                next[i][j] = k;
                                break;
                            }
                        }
                    }
                }
        printf("Case %d: ",++cas);
        int i,j,u,v;
        if (maxlen > 0){
            i = 0,j = 0;
            while (next[i][j] != 10){
                u = i + used[next[i][j]];
                v = (j*10+next[i][j]) % m;
                printf("%d",next[i][j]);
                i = u,j = v;
            }
            printf("\n");
        }
        else if (n >= used[0])
                printf("0\n");
             else printf("-1\n");
    }
    return 0;
}


解题参考:http://blog.csdn.net/u011345136/article/details/17654783


  1. #include <stdio.h>
    int main()
    {
    int n,p,t[100]={1};
    for(int i=1;i<100;i++)
    t =i;
    while(scanf("%d",&n)&&n!=0){
    if(n==1)
    printf("Printing order for 1 pages:nSheet 1, front: Blank, 1n");
    else {
    if(n%4) p=n/4+1;
    else p=n/4;
    int q=4*p;
    printf("Printing order for %d pages:n",n);
    for(int i=0;i<p;i++){
    printf("Sheet %d, front: ",i+1);
    if(q>n) {printf("Blank, %dn",t[2*i+1]);}
    else {printf("%d, %dn",q,t[2*i+1]);}
    q–;//打印表前
    printf("Sheet %d, back : ",i+1);
    if(q>n) {printf("%d, Blankn",t[2*i+2]);}
    else {printf("%d, %dn",t[2*i+2],q);}
    q–;//打印表后
    }
    }
    }
    return 0;
    }

  2. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there's even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at.