嚯嚯嚯,这道题很显然是削弱版的51nod P1042。
那么显然我们需要使用数位DP解题。
思路大致是这样的:
对于每一个数字,考虑三种影响关系:
1. 它对低位的影响
2. 它对高位的影响
3. 高位对低位的影响
然后在递归中实现这三种关系的计算即可。
AC代码:
1 #include <stdio.h> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 #define CLR(a,b) memset(a,b,sizeof(a)) 6 #define INF 0x3f3f3f3f 7 #define LL long long 8 void dp(LL n , LL m , LL *c) 9 { 10 LL x = n % 10; 11 LL y = n / 10; 12 for (int i = 0 ; i <= x ; i++) 13 c[i] += m; //当前位对低位的影响,每个都是0~9的范围 14 for (int i = 0 ; i <= 9 ; i++) 15 c[i] += m * y; //当前位对的高位影响(高位不为0,在上一个循环中算过) 16 c[0] -= m; //排除前导0的情况 17 LL t = y; 18 while (t) //高位对低位的影响,即都为最大限制数 19 { 20 c[t % 10] += (x+1) * m; //算上0 21 t /= 10; 22 } 23 if (y) 24 dp(y-1 , m*10 , c); //y值在上个while中算过,算下一个未限制数 25 } 26 int main() 27 { 28 LL r,l; 29 LL a[10] = {0}; 30 LL b[10] = {0}; 31 scanf ("%lld %lld",&l,&r); 32 dp(r , 1 , a); 33 dp(l-1 , 1 , b); 34 printf("%lld\n",a[2]-b[2]); 35 return 0; 36 }