Atcoder099 - C 题解

题意:

有若干种货币,他们的面值分别为1^n,6^n,9^n,且在 int 范围内,给一个价值,问最少需要多少种面值的货币才能凑齐这个价值。

思路:

一个裸的dp,有点像今年ACM四川省赛热身赛的C题,我写过上题的题解,如下:https://blog.csdn.net/ericgipsy/article/details/80578991

这题和它如出一辙,甚至比那题简单些,因为它只有1e5的数据,不需要预处理,就看以下两点即可:

当前状态的上一个状态有几种;

每种上一状态转移到当前状态,分别需要几步。

代码注释很详细。

本人AC代码:

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;
int n;
int dp[maxn];

int main() {
    cin >> n;
    dp[0] = 0;
    for(int i = 1; i <= n; i++) {
        dp[i] = dp[i - 1] + 1; //由上一状态加1转移过来, 所需1步
        for(int j = 9; j <= i; j *= 9) dp[i] = min(dp[i], dp[i - j] + 1); //由上一状态加6^n转移过来, 所需1步
        for(int j = 6; j <= i; j *= 6) dp[i] = min(dp[i], dp[i - j] + 1); //由上一状态加9^n转移过来, 所需1步
    }
    cout << dp[n] << endl;
}

猜你喜欢

转载自blog.csdn.net/ericgipsy/article/details/80698277
今日推荐