您的位置:首页 > 其它

CodeForces 204A Little Elephant and Interval 数位DP

2015-02-09 13:18 369 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: