天天看点

Codeforces 204 A (五一训练 A)+想法题

A. Little Elephant and Interval time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

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 %I64d specifier.

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. 

分析:10^18,数据非常大,想了好久都没有解决。以下是别人写的代码,好6的感觉。

题意:有一种个位数与最高位数字相等的数字,求在l,r的范围内,这种数字的个数。

做法:可以把设下界为1,然后将新得到的两个区间合并。求n-0时,当n大于十时,个位数视最高位而定,所以这样的数有n/10个,个位数每个都符合题意所以还要加上9,最后判断一样离n最近的那个数是否可以在降位之前取得。因为n/10取的时候假设n所在的那个十位也可以取得。

#include<cstdio>
#include<iostream>
#define LL long long
using namespace std;
//区间合并法
LL work(LL x)
{
    LL ret=0,wei;
    if(x<10)return x;
    wei=x%10;
    ret=x/10+9;
    while(x>=10)x/=10;
    if(x>wei)ret--;
    return ret;
}
int main()
{
    LL l,r;
    scanf("%I64d%I64d",&l,&r);
    printf("%I64d\n",work(r)-work(l-1));
    return 0;
}