Codeforces Round #129 (Div. 1) A Little Elephant and Interval
2012-07-12 16:19
375 查看
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 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 %I64d specifier.
Output
On a single line print a single integer — the answer to the problem.
Sample test(s)
input
output
input
output
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了,话说直接统计的应该会很麻烦吧。。不知道直接统计的有木有简单的写法。。
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 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 %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) B. Little Elephant and Cards
- Codeforces Round #129 (Div. 2)A. Little Elephant and Rozdil
- Codeforces Round #129 (Div. 2) B. Little Elephant and Sorting
- Codeforces Round #129 (Div. 1) B. Little Elephant and Cards
- Codeforces Round #129 (Div. 1)B Little Elephant and Cards
- Codeforces Round #129 (Div. 1)B Little Elephant and Cards
- Codeforces Round #129 (Div. 2) D. Little Elephant and Cards
- Codeforces Round #129 (Div. 1)E. Little Elephant and Strings
- 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings
- Codeforces Round #136 (Div. 2) A. Little Elephant and Function
- Codeforces Round #136 (Div. 1)C. Little Elephant and Shifts multiset
- Codeforces Round #157 (Div. 2) B. Little Elephant and Magic Square
- CodeforcesRound #136 (Div. 2) D. Little Elephant andArray
- Codeforces Round #136 (Div. 1) B. Little Elephant and Array
- Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shift(序列)
- Codeforces Round #259 (Div. 2) B - Little Pony and Sort by Shift
- Codeforces Round #169 (Div. 2) D. Little Girl and Maximum XOR(贪心,中等)
- Codeforces Round #259 (Div. 2)-D. Little Pony and Harmony Chest
- Codeforces Round #259 (Div. 2) A-Little Pony and Crystal Mine
- 【Codeforces Round 169 (Div 2) D】【简单数位贪心】Little Girl and Maximum XOR 区间选两数使得异或值尽可能大