Codeforces Round #129 (Div. 1) A Little Elephant and Interval

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 101477474 or 9 will be included in the answer and 47253 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.




简单的数位dp。

原来开三维(pos,begin,end)tle了,去掉end这一维就ok了,话说直接统计的应该会很麻烦吧。。不知道直接统计的有木有简单的写法。。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int dig[20];
__int64 dp[20][10];

__int64 F(int pos,int begin,bool inf)
{
    int i;
    if (!inf && dp[pos][begin]!=-1) return dp[pos][begin];
    __int64 ans=0;
    int end=inf?dig[pos]:9;
    if (pos==0)
    {
        return begin<=end;
    }
    for (i=0;i<=end;i++)
    {
        ans+=F(pos-1,begin,inf && (i==end));
    }
    if (!inf)
    {
        dp[pos][begin]=ans;
    }
    return ans;
}

__int64 Cal(__int64 t)
{
    int i,pos=0;
    __int64 ans=0;
    if (t==0) return 0;
    while(t)
    {
        dig[pos++]=t%10;
        t/=10;
    }
    if (pos==1) return dig[pos-1];
    for (i=0;i<=dig[pos-1];i++)
    {
        ans+=F(pos-2,i,i==dig[pos-1]);
    }
    return ans+8;
}


int main()
{
    int i,j,n;
    __int64 l,r;
    memset(dp,-1,sizeof(dp));
    scanf("%I64d%I64d",&l,&r);
    printf("%I64d\n",Cal(r)-Cal(l-1));
    return 0;
}







你可能感兴趣的:(Codeforces Round #129 (Div. 1) A Little Elephant and Interval)