CF 205 C Little Elephant and Interval(模拟)

本文介绍了一个算法问题,即计算指定区间内所有首尾数字相同的整数的数量。通过解析输入输出示例及核心代码逻辑,阐述了解决方案的设计思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


题意:给出两个数,n,m判断两个数间 满足头尾数字相同的数的个数



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>


#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
#define pi acos(-1)

typedef __int64 ll;


#define fre(i,a,b)  for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define N 100

ll a[N],b[20];

ll op[17];


void inint()
{
  int i;
   op[0]=1;
   op[1]=10;
   fre(i,2,18)
      op[i]=op[i-1]*10;

   b[0]=0;
   b[1]=9;
   b[2]=b[1]+9;
   fre(i,3,19)
    b[i]=b[i-1]+9*op[i-2];     //存下不大于长度为i个9的满足数目


}

ll hello(ll x)
{
   ll i=0;

   while(x)
   {
       a[i++]=x%10;
       x/=10;
   }

   return i;
}

ll fdd(ll x)
{
    ll i;

    ll len=hello(x);


    if(len==1) return x;
    if(len==0) return 0;
    ll le=a[len-1];
    ll ri=a[0];

    if(len==2)     //防止 8548 中8538等情况
	{
        ll ans=0;

        if(ri>=le) ans++;
        le--;
        if(ri) ans+=le;
        ans+=9;
	  return ans;
	}

    ll temp=0;

    //pf("%I64d %I64d %I64d\n",len,ri,le);

    if(ri>=le) temp=1;

    ri=le-1;

	le=temp;

   // pf("%I64d\n",le);

	for(i=1;i<len-1;i++)      //防止 8548 中8538等情况
	  {
        le=le+a[i]*op[i-1];

        //pf("%I64d %I64d %I64d %I64d\n",i,a[i],op[i-1],le);
      }

   // pf("%I64d\n",le);
    if(ri>0)
	  le+=ri*op[len-2];

    le=le+b[len-1]; //当前数目算完后加上位数少一个的数目
   return le;
}

int main()
{
    int i,j;
    inint();
    ll le,ri;
   //freopen("in.txt","r",stdin);

   while(~scanf("%I64d%I64d",&le,&ri))
   {

    le=fdd(le-1);

	ri=fdd(ri);
   // pf("%I64d %I64d\n",le,ri);
    printf("%I64d\n",ri-le);
   }

    return 0;
}

/*

47 8545

2 47

47 1024

1000 1000

1 1000

47 74


*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值