1.题目描述:
Ivan likes to learn different things about numbers, but he is especially interested in really big numbers. Ivan thinks that a positive integer number x is really big if the difference between x and the sum of its digits (in decimal representation) is not less than s. To prove that these numbers may have different special properties, he wants to know how rare (or not rare) they are — in fact, he needs to calculate the quantity of really big numbers that are not greater than n.
Ivan tried to do the calculations himself, but soon realized that it's too difficult for him. So he asked you to help him in calculations.
The first (and the only) line contains two integers n and s (1 ≤ n, s ≤ 1018).
Print one integer — the quantity of really big numbers that are not greater than n.
12 1
3
25 20
0
10 9
1
In the first example numbers 10, 11 and 12 are really big.
In the second example there are no really big numbers that are not greater than 25 (in fact, the first really big number is 30: 30 - 3 ≥ 20).
In the third example 10 is the only really big number (10 - 1 ≥ 9).
2.题意概述:
定义really big数就是那种本身与各项数字之和的差大于等于s的数,给你一个n和s,问不大于n的really big数个数有多少个?
3.解题思路:
我们先证明如果x是really big,那么x+1也是really big。
数学归纳法: 假设x是reall big,那么设x的各项和为sumd(x),由条件有x - sumd(x) ≥ s。
那么对于 x + 1 有sumd(x + 1) = sumd(x) + 1,由不等式性质 x + 1 - sumd(x + 1) = x + 1 - sumd(x) - 1 = x - sumd(x) ≥ s也成立!
于是我们只要找到最小的x是reall big,那么结果就是n - x了,找的方法可以通过二分。复杂度是
4.AC代码:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100010
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
bool check(ll x, ll s)
{
ll sumd = 0, tmpx = x;
while (x)
{
sumd += x % 10;
x /= 10;
}
// printf("%I64d: %I64d\n", tmpx, x - sumd);
return tmpx - sumd >= s;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
ll n, s;
while (~scanf("%I64d%I64d", &n, &s))
{
ll l = 1, r = n, ans = 0;
while (l <= r)
{
ll mid = l + (r - l) / 2;
if (check(mid, s))
{
ans = n - mid + 1;
r = mid - 1;
}
else l = mid + 1;
}
printf("%I64d\n", ans);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}