码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 204A Little Elephant and Interval 数位DP

时间:2015-02-09 14:10:22      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:


#include <cstdio>
#include <cstring>
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;
}


CodeForces 204A Little Elephant and Interval 数位DP

标签:

原文地址:http://blog.csdn.net/u011686226/article/details/43669783

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!