Little Elephant and Interval
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64dspecifier.
Output
On a single line print a single integer — the answer to the problem.
Sample test(s)
input
2 47
output
12
input
47 1024
output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
-------------------------------------------------------editorial-----------------------------------------------------------------
this type of questions can be done with the technique
f(l,r)=f(1,r)-f(1,l-1)
Now suppose we have the number r=85546
Here we can go up to combinations like
7_ _ _ 7
with all possible permutations.
But when we encounter the digit at MSB , we
see the combination is formed like
8 _ _ _ 8
so there are 554 ways to normally fill this 3 dashed but as LSB<MSB so the only combonation that cannot be
done is 85548 so we subtract 1 .
Had the number been 8 _ _ _ 9 we could have added 554 combinations in between .
-------------------------------------------code-------------------------------------------------------------------------------------------------------
#include<bits/stdc++.h>
typedef long long int LL;
using namespace std;
LL calc(LL a)
{
if(a<10)
return a;
int ld=a%10;
LL res=9+(a-10)/10;// 9 is added since numbers from 1-9 is consider special;
while(a>9)
a/=10;
if(ld>=a) // if lsb>=msb
++res;
return res;
}
int main()
{
//clock_t start=clock();
LL a,b;
cin >> a >> b;
cout << calc(b)-calc(a-1) <<endl;
//fprintf(stderr,"time=%.3lfsec\n",0.000001*(clock()-start));
return 0;
}
----------------------------------------------------------editorial-- event------------------------------------------------------------------------
It is well-known that for such problem you need to write function F(x) which solves the problem for the interval 0..x, and the answer then is F(r) - F(l - 1).
Now you need to write F(x) function. If x < 10, then answer is, of course, equal to x. Otherwise, let len be the length of x, x' — the integer x but without first and last digits, xi — the i-th digit of integer x (from left to right, starting from 0). Interate through all possible first digit d (which is the last at the same time) and through the length i of the number. Then if i < len - 2 or (i = len - 2 and d < x0) you need to add 10i to the answer. Otherwise, if i = len - 2 and d = x0 you need to add x' to the answer. Finally, if i = len - 2 and d = x0and xlen - 1 ≥ d add 1 to the answer.
No comments:
Post a Comment