[HDU4734] not 62 (the number of bits dp entry)

> Portal <

The meaning of problems: statistical interval [a, b] does not contain the numbers 4 and 62 how many.

Ideas: Digital dp

Just can not have a digital 4 can not have consecutive 62, no 4, then at the time of enumeration to determine what, does not enumerate 4 can ensure state legal, so this constraint is not necessary memories of, and for 62, then, involves to two, the current one is not 6 or 6 I count these two different situations are not the same, so use the state to record the number of different schemes.
dp [pos] [sta] represents the current first pos position, whether it is a former 6 states, where sta only two states 0 and 1 to go on it, is not the case 6 can be regarded as the same species, will not affect count.


Code

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int a[20];
int dp[20][2];

int dfs(int pos, int pre, int sta, int limit){
    if (pos==0) return 1;
    if (!limit&&dp[pos][sta]!=-1) return dp[pos][sta];
    int up = limit?a[pos]:9;
    int tmp = 0;
    for (int i = 0; i <= up; i++) {
        if (pre==6&&i==2) continue;
        if (i==4) continue;
        tmp += dfs(pos-1, i, i==6, limit&&i==a[pos]);
    }
    if(!limit) dp[pos][sta] = tmp;
    return tmp;
}
int solve(int x) {
    int pos = 1;
    while (x){
        a[pos++] = x%10;
        x /= 10;
    }
    return dfs(pos-1, -1, 0, 1);
} 

int main()
{
    int l, r;
    memset(dp, -1, sizeof(dp));
    while (~scanf("%d%d", &l, &r)&&l+r)
        printf("%d\n", solve(r)-solve(l-1));
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/wizarderror/p/11348918.html