hdu3709---Balanced Number(디지털 dp)
                                            
 7043 단어  dp
                    
Input The input contains multiple test cases. The first line is the total number of cases T (0 < T ≤ 30). For each case, there are two integers separated by a space in a line, x and y. (0 ≤ x ≤ y ≤ 1018).
Output For each case, print the number of balanced numbers in the range [x, y] in a line.
Sample Input
2 0 9 7604 24324
Sample Output
10 897
Author GAO, Yuan
Source 2010 Asia Chengdu Regional Contest
Recommend zhengfeng | We have carefully selected several similar problems for you: 3711 3715 3718 3713 3712
dp[i][j][k]는 i자리수를 나타내고 축은 j에 있고 k의 개수를 나타낸다. 그리고 j, 디지털 dp를 열거한다. 0은 매번 계산되고 마지막에 빼야 한다(len-1).
/************************************************************************* > File Name: hdu3709.cpp > Author: ALex > Mail: [email protected] > Created Time: 2015 03 01      18 42 38  ************************************************************************/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
LL dp[40][40][4000];
int bit[40];
LL dfs (int cur, int pro, int sum, bool flag, bool zero)
{
    if (cur == -1)
    {
        if (zero)
        {
            return 1;
        }
        return sum == 0;
    }
    if (!flag && ~dp[cur][pro][2000 - sum])
    {
        return dp[cur][pro][2000 - sum];
    }
    LL ans = 0;
    int end = flag ? bit[cur] : 9;
    for (int i = 0; i <= end; ++i)
    {
        if (zero && !i)
        {
            ans += dfs (cur - 1, pro, 0, flag && (i == end), 1);
        }
        else if (zero && i)
        {
            ans += dfs (cur - 1, pro, i * (cur - pro), flag && (i == end), 0);
        }
        else
        {
            ans += dfs (cur - 1, pro, sum + i * (cur - pro), flag && (i == end), 0);
        }
    }
    if (!flag)
    {
        dp[cur][pro][2000 - sum] = ans;
    }
    return ans;
}
LL calc (LL n)
{
    int cnt = 0;
    while (n)
    {
        bit[cnt++] = n % 10;
        n /= 10;
    }
    LL ans = 0;
    for (int i = 0; i < cnt; ++i)
    {
        ans += dfs (cnt - 1, i, 0, 1, 1);
    }
    ans -= (cnt - 1);
    return ans;
}
int main ()
{
    memset (dp, -1, sizeof(dp));
    int t;
    scanf("%d", &t);
    while (t--)
    {
        LL l, r;
        scanf("%I64d%I64d", &l, &r);
        printf("%I64d
", calc (r) - calc (l - 1));
    }
    return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【경쟁 프로 전형적인 90문】008의 해설(python)의 해설 기사입니다. 해설의 이미지를 봐도 모르는 (이해력이 부족한) 것이 많이 있었으므로, 나중에 다시 풀었을 때에 확인할 수 있도록 정리했습니다. ※순차적으로, 모든 문제의 해설 기사를 들어갈 예정입니다. 문자열...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.