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 ...
随机推荐
- 车牌识别--OMAP4430处理器上測试
OMAP4430(ME865) arm-linux-gcc 4.5.1(FriendlyARM) 软浮点执行结果: root@lj:/workspace/carid# arm-linux-gcc ca ...
- caffe学习--Lenet5的应用和原理、实现----ubuntu16.04.2+caffe+mnist+train+test
Lenet5的应用和原理.实现 ----------------------------------------------ubuntu16.04.2------------------------- ...
- MongoDB连接数与连接优化
默认每个连接数占用10M内存 ulimit -a 查看stack size MongoDB服务器内存要满足 connection overhead + data size + index size 即 ...
- Oracle比较时间大小
1,比较当前时间与指定时间相差分钟数: select sysdate, sysdate - to_date('2007-04-03 13:45:39','yyyy-mm-dd hh24:mi: ...
- 【TensorFlow-windows】(四) CNN(卷积神经网络)进行手写数字识别(mnist)
主要内容: 1.基于CNN的mnist手写数字识别(详细代码注释) 2.该实现中的函数总结 平台: 1.windows 10 64位 2.Anaconda3-4.2.0-Windows-x86_64. ...
- 针对Web.config敏感信息加密
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 ==在appSettings节点上添加== <configProtectedData> & ...
- 九度OJ 1092:Fibonacci (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1923 解决:1378 题目描述: The Fibonacci Numbers{0,1,1,2,3,5,8,13,21,34,55...} ...
- 大数据之ES系列——第一篇 ElasticSearch2.2 集群安装部署
第一部分 安装准备 准备三台主机节点: hc11.spads 192.168.160.181 hc12.spads 192.168.160.182 hc13.spads 192.168.160 ...
- 数据结构之 字符串---字符串匹配(kmp算法)
串结构练习——字符串匹配 Time Limit: 1000MS Memory limit: 65536K 题目描述 给定两个字符串string1和string2,判断string2是否为strin ...
- UVA11752 The Super Powers —— 数论、枚举技巧
题目链接:https://vjudge.net/problem/UVA-11752 题意: 一个超级数是能够至少能表示为两个数的幂,求1~2^64-1内的超级数. 题解: 1.可知对于 n = a^b ...