codeforces 817C Really Big Numbers 二分

来源:互联网 发布:网页游戏源码网 编辑:程序博客网 时间:2024/05/18 01:01

题意:用f(x)=x-(x的各个位之和),求出小于n,且f(x)>=s满足条件x的个数。

思路:每个x都有唯一对应的f(x)的,并且是递增的,所以可以用二分来找第一个大于或等于s的值

#include<cstdio>#define ll long longll get(ll num){ll d=0;ll temp=num;while(temp){d+=temp%10;temp/=10;}return num-d;}int main(){ll n,s;while(~scanf("%I64d%I64d",&n,&s)){ll l=0,r=n;ll mid,i=0;while(l<=r){mid=(l+r)/2;ll temp=get(mid);if(temp>=s)r=mid-1;else if(temp<s)l=mid+1;}ll temp=get(l);if(temp<s){printf("0\n");}else{printf("%I64d\n",n-l+1);}}}


原创粉丝点击