题目链接:http://codeforces.com/problemset/problem/817/C点击打开链接
二分查找最小的符合条件的端点
#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;
}