Educational Codeforces Round 90 (Rated for Div. 2) E - Sum of Digits

题目链接:https://codeforces.com/contest/1373/problem/E
枚举结果的后两位,从[0,100 - k),保证最后一个数不会对百位产生进位影响(至于为什么这样,打表观测后,猜测不会有进位到百位的情况);
最后两位确定了,然后这k+1个数的其他位是相同的,用x9999…填充,x可以计算得出;
比较取出最小的值即可;

ac代码:

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
using namespace std;

typedef long long ll;

int n, k;

int f(int x) {
	int res = 0;
	while (x > 0) {
		res += x % 10;
		x /= 10;
	}
	return res;
}

ll get(int p) {
	int s = 0;
	for (int i = 0; i <= k; i++)s += f(p + i);
	if (s > n)return -1;
	if ((n - s) % (k + 1))return -1;
	int r = (n - s) / (k + 1);
	ll res = r % 9;
	for (int i = 0; i < r / 9; i++)res = res * 10 + 9;
	return res;
}

void solve() {
	ll res = ((ll)1 << 62);
	for (int i = 0; i < 100 - k; i++) {
		ll m = get(i);
		if (m != -1) res = min(res, m * 100 + i);
	}
	if (res != ((ll)1 << 62))cout << res << endl;
	else cout << -1 << endl;
}

int main() {
	int T;
	cin >> T;
	while (T--) {
		cin >> n >> k;
		solve();
	}
	return 0;
}

你可能感兴趣的:(acm)