您的位置:首页 > 其它

【二分】【codeforces817C】Really Big Numbers

2017-09-08 20:20 337 查看
诶嘿嘿,题目传送门

题目描述

给定n和s,求出1——n中有多少数满足该数减去其所有数位数字之和大于或等于s的性质

输出满足数的个数 (1 ≤ n, s ≤ 10^18).

思路

首先我们观察一下这题其实和个位没有什么关系,因为个位已经减去,所以每十个数得到的结果是一样的,我们通过规律可知,该数减去其所有数位数字之和是一个单调递增的函数,所以我们二分答案即可

代码

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#define ll long long
using namespace std;
ll n,S,an;
int a[10001];
inline void fj(ll x){
an=0;
while(x>0)a[++an]=x%10,x/=10;
}
inline ll check(ll w){
if(w==0) return 0;
fj(w);
for(int i=1;i<=an;++i)w-=a[i];
return w;
}
int main(){
scanf("%lld%lld",&n,&S);
ll l=0,r=n;
while(l<r){
ll mid=(l+r)>>1;
if(check(mid)<S)l=mid+1;
else r=mid;
}
r=r-r%10;
if(check(r)>=S)cout<<n-r+1;
else cout<<0;
return 0;
}


是不是很简单呢??

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: