题目链接 http://www.acmore.net/problem.php?id=1488
#include<iostream> #include<stdio.h> #include<cstring> #include<algorithm> #define M 1000000007 using namespace std; long long arr[32],dp[32]; long long D2( long long num ) { long long ans = 1; for( int i = 1; i <= num; i++ ) ans *= 10; return ans; } long long D1( long long num ) { long long t = 1,ans = 0; for( int i = 0; i < num; i++ ) { ans += (arr[i]*t); t *=10; } return ans; } long long DFS( long long pos,long long limit ) { if( pos == -1 ){ return 0;} if( !limit && dp[pos] >= 0 ) return dp[pos]; long long end = limit?arr[pos]:9; long long res = 0; for( int i = 0; i <= end; i++ ) { long long ans = 0; if( i == 1 ) { if( limit && ( i == end ) ) ans = ( ans + 1 + D1( pos ) )%M; else ans = ( ans + D2( pos ) )%M; } res = ( res + ans)%M; res = ( res + DFS( pos-1,limit && i == arr[pos] ) )%M; } if( !limit )dp[pos] = res; return res%M; } long long work( long long N ) { int k = 0; while( N ) { arr[k++] = N%10; N /= 10; } memset( dp,-1,sizeof(dp) ); return DFS( k-1,1 ); } int main( ) { long long a,b; while( scanf("%lld %lld",&a,&b) != EOF ) printf("%lld\n",(work(b) - work(a-1)+M)%M ); return 0; }