UVa 12105 Bigger is Better (DP)
题意:用不超过 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)的更多相关文章
- UVA - 10131Is Bigger Smarter?(DAG上的DP)
题目:UVA - 10131Is Bigger Smarter? (DAG) 题目大意:给出一群大象的体重和IQ.要求挑选最多的大象,组成一个序列.严格的体重递增,IQ递减的序列.输出最多的大象数目和 ...
- UVA.10066 The Twin Towers (DP LCS)
UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...
- UVA 10003 Cutting Sticks 区间DP+记忆化搜索
UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...
- UVA 10131 Is Bigger Smarter?(DP最长上升子序列)
Description Question 1: Is Bigger Smarter? The Problem Some people think that the bigger an elepha ...
- 【Uva 12105】Bigger is Better
[Link]: [Description] 让你用最多n根棍子,组成一个数字,使得它能够被m整除; 数字1..9分别需要用-根棍子. 要求这个数字尽可能地大; 然后输出这个数字. [Solution] ...
- uva 10131 Is Bigger Smarter ? (简单dp 最长上升子序列变形 路径输出)
题目链接 题意:有好多行,每行两个数字,代表大象的体重和智商,求大象体重越来越大,智商越来越低的最长序列,并输出. 思路:先排一下序,再按照最长上升子序列计算就行. 还有注意输入, 刚开始我是这样输入 ...
- UVA - 1625 Color Length[序列DP 代价计算技巧]
UVA - 1625 Color Length 白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束 和模拟赛那道环形DP很想,计算这 ...
- Uva 10891 经典博弈区间DP
经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...
- HDU2929 Bigger is Better[DP 打印方案 !]
Bigger is Better Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- angular 指令封装弹出框效果
就直接用bs的警告框啦~,Duang~ 功能 可以设置message和type,type就是bs内置的几种颜色 默认提示3秒框自动关闭,或者点击x号关闭 代码 模板 <div class=&qu ...
- 个人开发者帐号--我是如何实现在另一台mac上真机调试的
本文转载至 : http://blog.csdn.net/chenyong05314/article/details/8689534 注:本人有一台mac电脑,之前申请开发者帐号的时候直接就是在这 ...
- 1185: [HNOI2007]最小矩形覆盖
1185: [HNOI2007]最小矩形覆盖 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 1426 Solve ...
- non-blocking I/O
https://en.wikipedia.org/wiki/New_I/O_(Java) zh.wikipedia.org/wiki/Java_NIO Java NIO API提供在java.nio套 ...
- HDFS被设计成能够在一个大集群中跨机器可靠地存储超大文件
HDFS被设计成能够在一个大集群中跨机器可靠地存储超大文件.它将每个文件存储成一系列的数据块,除了最后一个,所有的数据块都是同样大小的.为了容错,文件的所有数据块都会有副本.每个文件的数据块大小和副本 ...
- Hibernate ManyToOne Mappings 多对一关联映射
Hibernate ManyToOne Mappings 多对一关联映射 Hibernate框架的使用步骤: 1.创建Hibernate的配置文件(hibernate.cfg.xml)2.创建持久化类 ...
- ReentrantReadWriteLock读写锁实现分析
排他锁在同一时刻只允许一个线程进行访问,而读写锁在同一时刻允许多个读线程访问,但是在写线程访问时,所有的读线程和其他的写线程均被阻塞.读写锁内部维护了一对锁,一个读锁和一个写锁,通过分离读锁和写锁,使 ...
- Partition算法及Partition算法用于快速排序
JavaScript简单方便,所以用JavaScript实现,可以在Chrome控制台下观察运行结果.主要实现Partition算法,比如输入为 var array = [4, 2, 1, 3, ...
- 手把手教你打造ImageView支持手势放大缩小
写在前面 最近有了新的任务,学习的时间比以前少了不少,Java回炉的文估计是得缓缓了,不过每周一篇尽量保质保量.最近感觉我文写的有点不好,因为我写东西除非必要,不然概念性的东西我基本上都是一笔带过…… ...
- 用margin还是用padding?(3)—— 负margin实战
看过一篇文章是关于我知道你不知道的负Margin,里面对margin做了总结: 当margin四个值都为正数值的话,那么margin按照正常逻辑同周围元素产生边距.当元素margin的top和left ...