【Codeforces 817C. Really Big Numbers】+ 二分
2017-06-17 20:58
501 查看
C. Really Big Numbers
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Input
The first (and the only) line contains two integers n and s (1 ≤ n, s ≤ 1018).
Output
Print one integer — the quantity of really big numbers that are not greater than n.
Examples
input
12 1
output
3
input
25 20
output
0
input
10 9
output
1
Note
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).
找到满足要求的最小数o(o - 所有位上的数值和 >= s), o ~ n 的数一定都满足
AC代码:
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Input
The first (and the only) line contains two integers n and s (1 ≤ n, s ≤ 1018).
Output
Print one integer — the quantity of really big numbers that are not greater than n.
Examples
input
12 1
output
3
input
25 20
output
0
input
10 9
output
1
Note
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).
找到满足要求的最小数o(o - 所有位上的数值和 >= s), o ~ n 的数一定都满足
AC代码:
#include<cstdio> typedef long long LL; LL n,s; int sf(LL m){ LL sum = 0,o = m; while(o) sum += o % 10, o /= 10; return m - sum >= s; } void ef(){ LL l = s,r = n,o = 0; while(l <= r){ LL m = (l + r) / 2; if(sf(m)) o = m,r = m - 1; else l = m + 1; } printf("%lld\n",o ? n - o + 1 : 0); } int main() { scanf("%lld %lld",&n,&s); ef(); return 0; }
相关文章推荐
- CF - 817C. Really Big Numbers - 二分+数学
- 【二分】【codeforces817C】Really Big Numbers
- CF817C Really Big Numbers
- 【Codeforces 817 C. Really Big Numbers】+ 构造
- Educational Codeforces Round 23 C. Really Big Numbers(二分 数学)
- Educational Codeforces Round 23 C. Really Big Numbers 暴力
- C. Really Big Numbers
- Educational Codeforces Round 23 C. Really Big Numbers
- Codeforces 385C Bear and Prime Numbers(素数打表,二分)
- 补题补题补题~~~~really big numbers
- B - Really Big Numbers
- CodeForces 359 D.Pair of Numbers (二分+ST)
- Codeforces 359D Pair of Numbers | 二分+ST表+gcd
- C. Really Big Numbers
- Codeforces 359D Pair of Numbers【思维+二分+区间Gcd+区间最小值RMQ】
- codeforces817c Really Big Numbers
- 【 【henuacm2016级暑期训练】动态规划专题 K】 Really Big Numbers
- C. Really Big Numbers
- CodeForces - 672D Robin Hood (思维二分)
- CodeForces 670D Magic Powder - 2 (二分)