链接
题意
有 n 种货币,问你对于给定的整数 k,找出不能用 k 或更少的钞票表示的最小正数 s
思路
因为是求对于给定的整数 k,找出不能用 k 或更少的钞票表示的最小正数 s,所以要尽可能使用多的小面值货币,什么叫尽可能多呢?尽可能多就是指除了最大面值的货币外,其余货币与比他大一点的面值的货币之差,比如有 2 种面值 $1 和 $10,尽可能多的使用 $1 货币,这里的尽可能多就是最多使用 9 张 $1 面值的货币,因为使用 10 张得到的面值即为 10,但这个面值可以只用一张 $10 的货币即可达到,因此最多只能使用 9 张 $1 面值的货币
代码
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <cmath>
typedef long long ll;
using namespace std;
#define ioClose() ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define endl '\n'
const int maxn = 1e9 + 5;
const int mod = 998244353;
// 货币的面值,因为题目规定 n 不超过 10,所以面值最大为 10e10,可以用 long long 存储
ll type[15];
// cnt[i]: 最多使用 type[i] 面值的货币的数量
ll cnt[15];
int main() {
ioClose();
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
type[i] = pow(10, temp);
if (i != 0) {
// type[i] - type[i - 1]:相邻 2 种货币的面值差
// (type[i] - type[i - 1]) / type[i - 1]:最多使用 type[i - 1] 面值的货币的数量
cnt[i - 1] = (type[i] - type[i - 1]) / type[i - 1];
}
}
ll ans = 0;
// 在该循环中,k 代表还需要使用的货币数,ans 代表当前价值
for (int i = 0; i < n - 1; i++) {
if (k > cnt[i]) {
k -= cnt[i];
ans += type[i] * cnt[i];
} else if (k == cnt[i]) {
k -= cnt[i];
ans += type[i] * cnt[i];
ans += type[i + 1];
break;
} else {
ans += type[i] * (k + 1);
k = 0;
break;
}
}
// 如果在使用除了最大面值的货币的其余货币之后仍未达到要求,则剩下的全部使用最大面值的货币
if (k > 0) ans += type[n - 1] * (k + 1);
cout << ans << endl;
}
return 0;
}