后来我写了一个数位DP汇总:http://blog.csdn.net/techmonster/article/details/50284375
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MS(x,y) memset(x,y,sizeof(x))
#define pi acos(-1.0)
using namespace std;
void fre(){freopen("t.txt","r",stdin);}
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1e9 + 7;
const int inf = (1<<63)-1;
const double eps = 1e-8;
int dp[11][11];
void init()
{
int i,j,k;
for(i = 0; i <=9; ++i) dp[1][i] = 1;
dp[1][4] = 0;
for(i = 2; i <= 6; ++i)
{
for(j = 0; j <= 9; ++j)
{
if(j==4)continue;
for(k = 0; k <= 9; ++k)
{
if(k!=4&&!(j==6&&k==2)) dp[i][j] += dp[i-1][k];
}
}
}
}
int solve(int n)
{
int i,j,k,digit[10];
int len = 0,ans = 0;
while(n!=0)
{
digit[++len] = n%10;
n/=10;
}
digit[len+1] = 0;
for(i = len; i > 0; --i)
{
for(j = 0; j < digit[i]; ++j)
{
if(j==4 || (digit[i+1]==6 && j == 2))continue;
ans += dp[i][j];
}
if(digit[i]==4||(digit[i]==2&&digit[i+1]==6))break; //!!!关键不能忘
}
return ans;
}
int main()
{
//fre();
init();
int l,r;
while(~scanf("%d%d",&l,&r))
{
if(l==0 && r==0) break;
printf("%d\n",solve(r+1)-solve(l));
}
return 0;
}