uva12105 Bigger is Better
更简单的做法:
定义状态dp[i][j]表示在已经用了i根火柴的情况下拼出来了剩余部分(是剩余部分,不是已经拼出来了的)为j(需要%m)的最大长度,一个辅助数组p[i][j]表示状态[i][j]的最高位
是往后添加k
dp[i][j]=max(dp[i][j],dp[i-c[k]][(j*10+k)%m]+1)
(这里应该要证明dp[i-c[k]][(j*10+k)%m]加了d%m=j,没证出来...
倒着枚举k,这样保证位数相同存更大的数
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; const int maxn = ;
const int maxm = ; // dp[i][j] is the maximal length of the integer whose remainder is j (with at most i matches)
// p[i][j] is the maximal digit for state (i,j)
int n, m, dp[maxn][maxm], p[maxn][maxm]; int needs[] = { , , , , , , , , , }; int main(){
int kase = ;
while(scanf("%d%d", &n, &m) == ) {
printf("Case %d: ", ++kase); for(int i = ; i <= n; i++) //dp[i][j]=max(dp[i][j],dp[i-c[k]][(j*10+k)%m]+1)
for(int j = ; j < m; j++){ //注意枚举的是火柴数不是位数
int& ans = dp[i][j];
ans = p[i][j] = -;
if (j == ) ans = ;
for(int d = ; d >= ; d--)
if (i >= needs[d]){
int t = dp[i - needs[d]][(j * + d) % m]; if (t >= && t + > ans){ //t exist
ans = t + ;
p[i][j] = d;
}
}
} if (p[n][] < ) printf("-1");
else {
int i = n, j = ;
for(int d = p[i][j]; d >= ; d = p[i][j]){
printf("%d", d);
i -= needs[d];
cout<<"x"<<j<<endl;
j = (j * + d) % m;
//printf("%d", j);
}
}
printf("\n");
}
return ;
}
uva12105 Bigger is Better的更多相关文章
- spark - tasks is bigger than spark.driver.maxResultSize
Error ERROR TaskSetManager: Total size of serialized results of 8113 tasks (1131.0 MB) is bigger tha ...
- MYSQL 内存报错 Use 'mysqld --thread_stack=#' to specify a bigger stack.
今天在使用mysql的过程中,连接数据库始终无法成功 最后发现是数据库无法执行增加修改的操作 :错误代码 Thread stack overrun: 11552 bytes used of a 13 ...
- 数据库执行sql报错Got a packet bigger than 'max_allowed_packet' bytes及重启mysql
准备在mysql上使用数据库A,但mysql5经过重装后,上面的数据库已丢失,只得通过之前备份的A.sql重新生成数据库A. 1.执行sql报错 在执行A.sql的过程中,出现如下错误:Got a p ...
- HDU2929 Bigger is Better[DP 打印方案 !]
Bigger is Better Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDOJ 2929 Bigger is Better
DP....好难的DP... Bigger is Better Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- don't forget the bigger picture
Imagine a circle that contains all of human knowledge: By the time you finish elementary school, you ...
- Got a packet bigger than 'max_allowed_packet' bytes
昨天用导入数据的时候发现有的地方有这个错误.后来才发现我用RPM包装的MYSQL配置文件里面有old_passwords=1去掉就可以了. Got a packet bigger than ‘max_ ...
- Navicat导入数据时发生了报错 --- 1153 - Got a packet bigger than 'max_allowed的处理办法
今天我在使用Navicat导入.sql文件数据时,发现本来是80万条的数据,结果只导入了10万条左右,而且在其错误信息日志中,我发现了这样一条错误:1153 - Got a packet bigger ...
- Navicat 导入数据报错 --- 1153 - Got a packet bigger than 'max_allowed_packet' bytes
在用Navicat导入SQL文件时报错:MySql 错误 Err [Imp] 1153 - Got a packet bigger than 'max_allowed_packet' bytes 查了 ...
随机推荐
- UVa 1611 Crane (构造+贪心)
题意:给定一个序列,让你经过不超过9的6次方次操作,变成一个有序的,操作只有在一个连续区间,交换前一半和后一半. 析:这是一个构造题,我们可以对第 i 个位置找 i 在哪,假设 i 在pos 位置, ...
- Linux ubuntu 下寻找 texlive 缺失包 texlive 缺失包(转载)
转自:http://blog.sina.com.cn/s/blog_87315ca60101d4d1.html 在Linux下用 latex 编译 ××.tex 文件有时候时会提示: ! LaTeX ...
- Oracle Escape
select * from tablewhere col like '%\_'escape'\'
- 腾讯视频API --关闭广告推荐
官方文档:http://v.qq.com/open/doc/tvpapi2.0.pdf 使用: <script src="http://imgcache.qq.com/tencentv ...
- Swift3.0 UITextView写反馈界面
效果图 适配用的 SnapKit 使用介绍: http://www.hangge.com/blog/cache/detail_1097.html private func creationTextV ...
- HDU 5101
hdoj5101 lower_bound函数: 题意: 从两个不同集合拿出两个数,加的和大于k的可行的方案数 思路: 答案=从所有数中选择的两个加和大于k的数的方案数-在同一个集合中选择的两个加和大于 ...
- poj 2960 S-Nim【SG函数】
预处理出SG函数,然后像普通nim一样做即可 #include<iostream> #include<cstdio> using namespace std; const in ...
- autolayout UIImageView 根据 UILabel的宽度变换位置
仅个人学习笔记,大牛勿喷 代码写法 使用Masonry //昵称 _nameLableView = [[UILabel alloc]init]; [_nameLableView setTextColo ...
- C++面向对象程序设计举例
[例8.1]最简单的例子. #include <iostream> using namespace std; class Time //定义Time类 { public : //数据成员为 ...
- python之url编码
import urllib.parsempp='besttest 自动化测试'print(urllib.parse.quote_plus(mpp)) #url编码print(urllib.parse. ...