题意:用不超过 n 根火柴,组成一个尽可能大的数。

析:很明显的一个DP题,首先不难想到这个dp[i][j] 表示前 i 根火柴,所能拼出的取模 m 为 j 的数,状态转移方程也很好写,

dp[i][j] ==> dp[i+c[k]][(j*10+k)%m] 其中 k 为在后面添加的数,c 数组是用的火柴数,这个方程没问题,就是效率有点低,

因为有一个高精度问题,可以用Java来实现,也能够AC的。

第二种方法就是换一个表示方法,不过确实不太容易想到。dp[i][j] 表示用最多 i 根火柴,取模 m 的数的最大位数。毕竟位数越大,数越大。

状态转移方程也是和上面差不多就是变成位数了而已。

代码如下:

import java.math.BigInteger;
import java.util.*; public class Main{
public static final int maxn = 100 + 10;
public static final int maxm = 3000 + 10;
public static BigInteger[][] ans;
public static int[] c; public static BigInteger solve(int n, int m){
for(int i = 0; i <= n; ++i)
for(int j = 0; j < m; ++j)
ans[i][j] = BigInteger.valueOf(-1); ans[0][0] = BigInteger.ZERO;
BigInteger res = BigInteger.valueOf(-1);
for(int i = 0; i <= n; ++i){
for(int j = 0; j < m; ++j){
if(ans[i][j].equals(BigInteger.valueOf(-1))) continue;
for(int k = 0; k < 10; ++k){
if(i +c[k] > n) continue;
ans[i+c[k]][(j*10+k)%m] = ans[i+c[k]][(j*10+k)%m].max(ans[i][j].multiply(BigInteger.valueOf(10)).add(BigInteger.valueOf(k)));
}
}
if(i > 1 && res.compareTo(ans[i][0]) < 0) res = ans[i][0];
}
return res;
} public static void main(String []args){
c = new int[15];
ans = new BigInteger[maxn][maxm];
c[0] = 6; c[1] = 2; c[2] = 5; c[3] = 5;
c[4] = 4; c[5] = 5; c[6] = 6; c[7] = 3;
c[8] = 7; c[9] = 6;
Scanner cin = new Scanner(System.in);
int kase = 0;
while(cin.hasNext()){
int n = cin.nextInt();
if(0 == n) break;
int m = cin.nextInt();
System.out.println("Case " + (++kase) +": " + solve(n, m));
}
cin.close();
}
}

第二种方法:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-5;
const int maxn = 2600 + 10;
const int mod = 1e6;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int dp[110][3010], p[110][3010];
const int c[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 }; int main(){
int kase = 0;
while(scanf("%d %d", &n, &m) == 2 && n){
printf("Case %d: ", ++kase); for(int i = 0; i <= n; ++i)
for(int j = 0; j < m; ++j){
int &ans = dp[i][j];
ans = p[i][j] = -1;
if(!j) ans = 0;
for(int k = 9; k >= 0; --k) if(i >= c[k]){
int t = dp[i-c[k]][(j*10+k)%m] + 1;
if(t > 0 && t > ans){
ans = t;
p[i][j] = k;
}
}
} if(dp[n][0] <= 0) printf("-1");
else{
int i = n, j = 0;
for(int d = p[i][j]; d >= 0; d = p[i][j]){
printf("%d", d);
i -= c[d];
j = (j*10 + d) % m;
}
}
printf("\n");
}
return 0;
}

UVa 12105 Bigger is Better (DP)的更多相关文章

  1. UVA - 10131Is Bigger Smarter?(DAG上的DP)

    题目:UVA - 10131Is Bigger Smarter? (DAG) 题目大意:给出一群大象的体重和IQ.要求挑选最多的大象,组成一个序列.严格的体重递增,IQ递减的序列.输出最多的大象数目和 ...

  2. UVA.10066 The Twin Towers (DP LCS)

    UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...

  3. UVA 10003 Cutting Sticks 区间DP+记忆化搜索

    UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...

  4. UVA 10131 Is Bigger Smarter?(DP最长上升子序列)

    Description   Question 1: Is Bigger Smarter? The Problem Some people think that the bigger an elepha ...

  5. 【Uva 12105】Bigger is Better

    [Link]: [Description] 让你用最多n根棍子,组成一个数字,使得它能够被m整除; 数字1..9分别需要用-根棍子. 要求这个数字尽可能地大; 然后输出这个数字. [Solution] ...

  6. uva 10131 Is Bigger Smarter ? (简单dp 最长上升子序列变形 路径输出)

    题目链接 题意:有好多行,每行两个数字,代表大象的体重和智商,求大象体重越来越大,智商越来越低的最长序列,并输出. 思路:先排一下序,再按照最长上升子序列计算就行. 还有注意输入, 刚开始我是这样输入 ...

  7. UVA - 1625 Color Length[序列DP 代价计算技巧]

    UVA - 1625 Color Length   白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束   和模拟赛那道环形DP很想,计算这 ...

  8. Uva 10891 经典博弈区间DP

    经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...

  9. HDU2929 Bigger is Better[DP 打印方案 !]

    Bigger is Better Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. Java、C++、Python、Ruby、PHP、C#和JavaScript的理解

    Java.C++.Python.Ruby.PHP.C#和JavaScript和日本动漫里的那些大家熟悉的动漫人物结合起来.依据他们的身世.个人经历来生动的表达各编程语言的发展历程.原文内容例如以下:  ...

  2. 后台运行命令:&amp;和nohup command &amp; 以及关闭、查看后台任务

    当我们在终端或控制台工作时.可能不希望由于执行一个作业而占住了屏幕,由于可能还有更重要的事情要做,比方阅读电子邮件. 对于密集訪问磁盘的进程,我们更希望它可以在每天的非负荷高峰时间段执行(比如凌晨). ...

  3. # kubernetes调度之nodeName与NodeSelector

    系列目录 Kubernetes的调度有简单,有复杂,指定NodeName和使用NodeSelector调度是最简单的,可以将Pod调度到期望的节点上. 本文主要介绍kubernetes调度框架中的No ...

  4. longestIncreasingSequence最长上升子序列问题

    package dp; /** * 最长上升子序列问题 */ public class LongestIncreasingSubsequence { /** * 原始dp * @param arr * ...

  5. Finder 快捷键

    记录几个常用的 Finder 快捷键: 复制 Finder 里选中的路径:option+cmd+c 地址栏跳到指定路径:shift+cmd+g 增加标签:cmd+t 显示/隐藏 标签栏:shift+c ...

  6. centos编译 Compiling FFmpeg on CentOS RHEL Fedora

    This guide is based on a minimal installation of the latest CentOS release, and will provide a local ...

  7. storm是怎样保证at least once语义的

    背景 本篇看看storm是通过什么机制来保证消息至少处理一次的语义的. storm中的一些原语 要说明上面的问题,得先了解storm中的一些原语,比方: tuple和message 在storm中,消 ...

  8. swift3.0系列完整demo代码库

    https://github.com/soapyigu/Swift30Projects 感谢作者

  9. Python 008- 游戏2048

    #-*- coding:utf-8 -*- import curses from random import randrange, choice # generate and place new ti ...

  10. leetcode dfs Flatten Binary Tree to Linked List

    Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions Give ...