CodeForces 204A Little Elephant and Interval 数位DP


#include 
#include 
using namespace std;
typedef __int64 LL;
int a[33];
LL dp[33][10];

LL dfs(int x, int s, int e, int flag, int first)
{
    if(x == -1)
        return s == e;
    if(!flag && dp[x][s]!= -1)
        return dp[x][s];
    int end = 9;
    if(flag)
        end = a[x];
    LL sum = 0;
    for(int i = 0; i <= end; i++)
    {
    	int st = s, ed = e;
        if(!first && i)
        	st = i;
       	if(x == 0)
       		ed = i;
        sum += dfs(x-1, st, ed, flag && i == end, first||i);
    }
    if(!flag)
        dp[x][s] = sum;
    return sum;
}
LL cal(LL x)
{
	if(x == 0)
		return 1;
    int l = 0;
    while(x)
    {
        a[l++] = x%10;
        x /= 10;
    }
    return dfs(l-1, 0, -1, 1, 0);
}
int main()
{
    int T;
    memset(dp, -1, sizeof(dp));
    LL x, y;
    while(scanf("%I64d %I64d", &x, &y) != EOF)
    {    
        if(!x && !y)
            break;
        printf("%I64d\n", cal(y)-cal(x-1));
    }
    return 0;
}


你可能感兴趣的:(数位DP)