题意:用不超过 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;
}

最新文章

  1. prototype继承(1)
  2. Beta阶段第一次Scrum Meeting
  3. HTTP版本进化过程
  4. hdu 1370 Biorthythms 中国剩余定理
  5. Google Volley框架源码走读
  6. VS2010 调试不会命中当前断点
  7. Git与Repo入门(转载)
  8. Intellij IDEA 构建Spring Web项目 — 用户登录功能
  9. UNION ALL vs UNION
  10. python单线程,多线程和协程速度对比
  11. 文本离散表示(一):词袋模型(bag of words)
  12. Python爬虫入门教程 5-100 27270图片爬取
  13. CloudFoundry 之 IBMCloud 项目部署java例子
  14. [官网]How to use distributed transactions with SQL Server on Docker
  15. 给uniGUI的表格控件uniDBGrid加上记录序号的列
  16. node 学习系列-hello world
  17. c/c++ 函数说明以及技巧总结
  18. Struts2技术详解
  19. NewSQL 介绍
  20. scala集合与数据结构

热门文章

  1. ZOJ 1516 Uncle Tom&amp;#39;s Inherited Land(二分匹配 最大匹配 匈牙利啊)
  2. php pack()函数详解与示例
  3. Android下强制显示ActionBar的overflowbutton
  4. Data Matrix Font and Encoder条码控件可以以字体的形式来打印DataMatrix条形码
  5. Hadoop集群_HDFS初探之旅
  6. 在与SQL Server 建立 连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器
  7. 在苹果iOS平台中获取当前程序进程的进程名等信息
  8. 2015年度新增开源软件排名TOP 100,EasyDarwin开源流媒体服务器排名第17
  9. superslider网站特效插件
  10. innodb的锁和高并发