码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 12105 Bigger is Better (DP)

时间:2017-02-23 23:58:33      阅读:366      评论:0      收藏:0      [点我收藏+]

标签:system.in   close   ase   out   next   bigger   debug   int   integer   

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

标签:system.in   close   ase   out   next   bigger   debug   int   integer   

原文地址:http://www.cnblogs.com/dwtfukgv/p/6436159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!