Really Big Numbers CodeForces - 817C(思维)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Miranda_ymz/article/details/83867795

Really Big Numbers CodeForces - 817C(思维)

题意:输入n,s问s-n中有多少数减去自身所有位数值的和比s大。

思路:其实就是猜测,嗯然后猜测对了,一次跑完所有测试样例嘻嘻嘻。 其实主要方法用对就不会超时。想想从s开始增大肯定到某个阶段,肯定再怎么减去所有位数的和值都不会比s小。所以找这个情况就好了。然后就可以发现,当s的长度*9+s。的值就是分界线。然后就出来了。

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
int f(ll u)
{
    int ans=0;
    while(u){
        ans+=u%10;
        u/=10;
    }
    return ans;
}
int wei(ll u)
{
    int ans=0;
    while(u){u/=10;ans++;}
    return ans;
}
int main()
{
    ll n,s;
    scanf("%lld%lld",&n,&s);
    ll ans=0,i;
    int len=wei(s);
    for(i=s;i<=s+len*9;i++)
    {
        if(i>n) break;
        if(i-f(i)>=s) ans++;
    }
    if(i>n) printf("%lld\n",ans);
    else{printf("%lld\n",n-i+1+ans);}
    return 0;
}

yong'dui

猜你喜欢

转载自blog.csdn.net/Miranda_ymz/article/details/83867795