Is this O(1) solution possible with little modification

Revision en1, by todi_kunal, 2017-06-17 06:40:45

Question is 817C

Ivan likes to learn different things about numbers, but he is especially interested in really big numbers. Ivan thinks that a positive integer number x is really big if the difference between x and the sum of its digits (in decimal representation) is not less than s. To prove that these numbers may have different special properties, he wants to know how rare (or not rare) they are — in fact, he needs to calculate the quantity of really big numbers that are not greater than n.

Ivan tried to do the calculations himself, but soon realized that it's too difficult for him. So he asked you to help him in calculations.

Constraints: 1<s,n<10^18

The solution given is O(logn) but i was trying O(1) and my solution ran for test cases.Can someone suggest some modification in it that it becomes valid.

include

include <bits/stdc++.h>

using namespace std;

define LL long long int

int main() {

LL n,s;
cin>>n>>s;
if(s%9)
{
    LL x=(s-s%9+9);
    LL y=(x-x%10+10)-1;
    if(n-y<=0)
    cout<<0;
    else
    cout<<n-y;
}
else
{
    LL x=(s-s%9);
    LL y=(x-x%10+10)-1;
    if(n-y<=0)
    cout<<0;
    else
    cout<<n-y;
}
// your code goes here
return 0;

}

Tags #math, educa

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English todi_kunal 2017-06-17 06:42:01 28 Tiny change: 'valid.\n\n\n#define LL long long int\nint main' -> 'valid.\n\nint main'
en2 English todi_kunal 2017-06-17 06:41:33 67
en1 English todi_kunal 2017-06-17 06:40:45 1320 Initial revision (published)