817C. Really Big Numbers 二分

LINK

题意:给出两个数n, s,要求问1~n中\(x-bit(x)>=s\)的数有多少个。其中bit(x)指x的各位数之和

思路:首先观察能够发现,对于一个数如果满足了条件,由于x-bit(x)总是随x变大而变大的,所以只要求得最小的x能够满足条件,二分即可

/** @Date    : 2017-07-02 11:17:55
  * @FileName: 817C 二分.cpp
  * @Platform: Windows
  * @Author  : Lweleth (SoungEarlf@gmail.com)
  * @Link    : https://github.com/
  * @Version : $Id$
  */
#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

LL n, s;

LL trans(LL x)
{
	LL res = 0;
	while(x)
	{
		res += x % 10;
		x /= 10;
	}
	return res;
}
int main()
{
	while(cin >> n >> s)
	{
		LL l = 1, r = 1e18 + 20;//注意范围
		if(s >= n)//注意s可能大于n
		{
			printf("0\n");
			continue;
		}
		while(l < r)
		{
			LL mid = (l + r) >> 1;
			if(mid - trans(mid) >= s)
				r = mid;
			else l = mid + 1;

			//cout << l << '~' << r << endl;
			//cout << mid << "~" << trans(mid) << endl;
		}
		if(l > n)
			printf("0\n");
		else
			printf("%lld\n", n - l + 1);
	}
    return 0;
}
posted @ 2017-07-04 10:31  Lweleth  阅读(195)  评论(0编辑  收藏  举报