linkkk
题意:
定义美丽数为数位和与数值的差大于等于s的数,问有多少个美丽数小于等于n
思路:
可以看出美丽数是满足单调性的,如果x是美丽数,那么x + 1也一定是美丽数,相当于等式两边都增加了1,等式依旧成立。二分找到最小的美丽数即可。
代码:
// Problem: C. Really Big Numbers // Contest: Codeforces - Educational Codeforces Round 23 // URL: https://codeforces.com/problemset/problem/817/C // Memory Limit: 256 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) #include<bits/stdc++.h> using namespace std; typedef long long ll; ll n,s; bool check(ll x){ ll sum=0,tmp=x; while(x){ sum+=x%10;x/=10; } return abs(sum-tmp)>=s; } int main(){ cin>>n>>s; ll l=1,r=2e18,ans=n; //cout<<check(11)<<" "<<check(12)<<" "<<check(13)<<"\n"; while(l<=r){ ll mid=(l+r)/2; if(check(mid)) ans=mid,r=mid-1; else l=mid+1; } cout<<max(0ll,n-ans+1)<<endl; return 0; }