超级坑的水题!!!想了两天没一点思路,看了题解第一段话就做出来了
刚开始一直在想找到通项就是例如an*10^n+...+a0*10^0-an-...-a0>=s,然后从这个里面找到规律,结果走进死胡同了
Let's prove that if x is really big, then x + 1 is really big too.
证明很容易,我就不证了,直接上代码,二分答案就行了
还有一点要注意,因为是10E18范围,所以平时循环50次是不够用的,加到了70次就ac了,也可以用l<r-1这个来判断
#include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cassert> #include<iomanip> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define pi acos(-1) #define ll long long #define mod 1000000007 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-9; const int N=100000+10,maxn=111117,inf=11111; ll n,s; bool ok(ll x) { ll t=x,p=x; while(p){ t-=(p%10); p/=10; } return t>=s; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin>>n>>s; ll l=1,r=n+1; for(int i=0;i<70;i++) { ll m=(l+r)/2; if(ok(m))r=m; else l=m; } cout<<n-l<<endl; return 0; }