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.
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.
On a single line print a single integer — the answer to the problem.
2 47
12
47 1024
98
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 */
CF 205 C Little Elephant and Interval(模拟)
原文:http://blog.csdn.net/u014737310/article/details/44573079