CodeForces - 817C Really Big Numbers(二分)

题目链接:http://codeforces.com/problemset/problem/817/C点击打开链接


C. Really Big Numbers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 1011 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 3030 - 3 ≥ 20).

In the third example 10 is the only really big number (10 - 1 ≥ 9).


一开始的时候找规律去写 找到一百而已以为发现规律 结果一百之后规律不同。。

二分查找最小的符合条件的端点

#include <bits/stdc++.h>
using namespace std;
long long int large(long long int x)
{
    long long int mid=x;
    while(mid)
    {
        x-=(mid%10);
        mid/=10;
    }
    return x;
}
int main()
{
    long long int n,s;
    cin >> n >> s;
    long long int l,r;
    l=1;
    r=n;
    long long int ans=0;
    while(l<=r)
    {
        long long int mid=(l+r)/2;
        if(large(mid)>=s)
        {
            ans=mid;
            r=mid-1;
        }
        else
        {
            l=mid+1;
        }
    }
    if(ans)
    cout << n-ans+1;
    else
        cout << 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值