【 【henuacm2016级暑期训练】动态规划专题 K】 Really Big Numbers

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

会发现如果x是reallynumber那么x+1也会是reallynumber.... (个位数+1,各位数的和+1了但是整个数也+1了。而且如果发生了进位的话,各个数码的和只会更小。。 因此这个reallynumber肯定有一个下界的。。 二分得到这个下界就好。 然后用n-下界+1就是答案了。

【代码】

#include <bits/stdc++.h>
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;

const double pi = acos(-1);
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};

LL n,s,temp;

LL get_digitas_sum(LL x){
    LL sum = 0;
    while (x>0){
        sum += x%10;
        x/=10;
    }
    return sum;
}

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("rush_in.txt", "r", stdin);
	#endif
	ios::sync_with_stdio(0),cin.tie(0);
    cin >> n >> s;
    temp = -1;
    LL l = 1,r = n;
    while (l<=r){
        LL mid = (l+r)>>1;
        LL delta = mid - get_digitas_sum(mid);
        if (delta>=s){
            temp = mid;
            r = mid -1;
        }else l = mid + 1;
    }
    if (temp==-1)
        cout<<0<<endl;
    else
        cout<<n-temp+1<<endl;
	return 0;
}

原文地址:https://www.cnblogs.com/AWCXV/p/9322146.html