Little Elephant and Interval
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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 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 %I64dspecifier.
Output
On a single line print a single integer — the answer to the problem.
Sample Input
Input
2 47
Output
12
Input
47 1024
Output
98 简单题意: 给你两个数字, 判断两个数字之间有多少个首位与末位相同的数字个数,,范围1 -- 10^18 思路分析: 本来想用字符串,,发现__int64可以存储数字,。。 给出两个数字,先求从(0 -- 小数)的数字个数, 再求(0 -- 大树)的数字个数,然后相减。。
#include<stdio.h> #include<string.h> #include<math.h> __int64 l, r, sum1, sum2; __int64 a[20]; int lnn[22]; //保存每一位; int getlen(__int64 n) { int len = 0; while(n) { lnn[len] = n % 10; n = n / 10; len++; } return len; } //注意一下这里求阶乘,不要用那个pow函数,因为会有误差; __int64 fac(int s){ int i; __int64 ans=1; for(i = 1; i <= s; i++) ans = ans * 10; return ans; } //这里是为了求出中间的那个数字; __int64 getn(int ll,int rr) { int i; __int64 j = 1, ans = 0; for(i = ll; i <= rr; i++) { ans += lnn[i] * j; j = j * 10; } return ans; } int main() { int i, j, k; int len1 = 0,len2 = 0; scanf("%I64d%I64d", &l,&r); a[1] = 10; a[2] = 9; a[3] = 90; for(i = 4; i <= 18; i++) a[i] = a[i - 1] * 10; sum1 = sum2 = 0; len2 = getlen(r); //要单独讨论长度只有1的情况; if(len2 == 1) sum2 += r + 1; for(i = 1; i < len2; i++) sum2 += a[i]; __int64 s2 = fac(len2 - 1); __int64 ans = 0; if(r / s2 > 1 && len2 != 1) { sum2 += a[len2] / 9 * (r / s2 - 1); } sum2 += getn(1, len2 - 2); //这里是首位小于等于末尾的情况,那么要加1; if(r / s2 <= r % 10 && len2 != 1) sum2++; memset(lnn, 0, sizeof(lnn)); len1 = getlen(l - 1); if(len1 == 1) sum1 += l; for(i = 1;i < len1; i++) sum1 += a[i]; __int64 s1 = fac(len1 - 1); if((l - 1) / s1 > 1 && len1 != 1) { sum1 += a[len1] / 9 * ((l - 1) / s1 - 1); } sum1 += getn(1, len1 - 2); if((l - 1) / s1 <= (l - 1) % 10 && len1 != 1) sum1++; printf("%I64d\n",sum2 - sum1); }