Codeforces Round #129 (Div. 2) C

Description

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 and47, 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.

Examples
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.

题意:有这种数字,第一位和最后一位相同,问你两个数字范围内有多少符合要求的数字~

解法:当然是找规律啊,很容易看出规律是9*10^n,然后考虑两个端点的情况,他们是不是也是符合要求的(这里的代码写的不清楚QAQ,最有价值的只有cmd函数了)

#include<bits/stdc++.h>
using namespace std;
long long a[100005];
int pos;
int n;
int d;
long long cmd(string s)
{
    long long sum1=0,sum2=0;
    if(s.length()==1)
    {
        sum1=0;
    }
    else if(s.length()==2)
    {
        sum1=9;
    }
    else
    {
        sum1=10;
        for(int i=1;i<s.length()-2;i++)
        {
            sum1*=10;
        }
     //   cout<<sum1<<endl;
        sum1=sum1-1;
      //  cout<<sum1<<endl;
        sum1+=9;

    }
    int ans1=s[0]-'0'-1;
    long long sum3=0;
    long long pos1=1;
    if(s.length()>=3)
    {
        for(int i=1; i<=s.length()-2; i++)
        {
            sum3=sum3*10+(s[i]-'0');
        }
       // sum3=sum3+1;
        for(int i=0; i<s.length()-2; i++)
        {
            pos1*=10;
        }
        if((s[0]-'0')<=(s[s.length()-1]-'0'))
        {
            sum1=sum1+ans1*pos1+sum3+1;
        }
        else
        {
            sum1=sum1+ans1*pos1+sum3;
        }

    }
    else
    {
        if(s[0]<=s[s.length()-1])
        {
            sum1=sum1+ans1*pos1+sum3+1;
        }
        else
        {
            sum1=sum1+ans1*pos1+sum3;
        }
    }
    return sum1;
}
int main()
{
    string s1,s2;
    cin>>s1>>s2;
   // cout<<cmd(s1)<<endl;
   // cout<<cmd(s2)<<endl;
    if(s1[0]==s1[s1.length()-1]&&s2[0]<=s2[s2.length()-1])
    {
        cout<<cmd(s2)-cmd(s1)+1<<endl;
    }
    else if(s1[0]==s1[s1.length()-1]&&s2[0]>s2[s2.length()-1])
    {
        cout<<cmd(s2)-cmd(s1)+1<<endl;
    }
    else if(s1[0]<=s1[s1.length()-1]&&s2[0]<=s2[s2.length()-1])
    {
        cout<<cmd(s2)-cmd(s1)<<endl;
    }
    else if(s1[0]<=s1[s1.length()-1]&&s2[0]>s2[s2.length()-1])
    {
        cout<<cmd(s2)-cmd(s1)<<endl;
    }
    else if(s1[0]>s1[s1.length()-1]&&s2[0]<=s2[s2.length()-1])
    {
        cout<<cmd(s2)-cmd(s1)<<endl;
    }
    else if(s1[0]>s1[s1.length()-1]&&s2[0]>s2[s2.length()-1])
    {
        cout<<cmd(s2)-cmd(s1)<<endl;
    }
    else if(s1[0]>s1[s1.length()-1]&&s2[0]<=s2[s2.length()-1])
    {
        cout<<cmd(s2)-cmd(s1)<<endl;
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/yinghualuowu/p/5721373.html