HDU - 5898 odd-even number (数位dp)

Posted SomnusMistletoe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU - 5898 odd-even number (数位dp)相关的知识,希望对你有一定的参考价值。

题意:求一个区间内,满足连续的奇数长度是偶数,连续的偶数长度是奇数的数的个数。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-9;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 20 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int digit[MAXN];
LL dp[MAXN][2][MAXN];
LL dfs(int pos, int pre, int len, bool leadingzero, bool limit){//pos--当前位,pre--更高一位的数是奇还是偶,len--连续长度,leadingzero--是否有前导零,limit--当前位的数字是否有限制
    if(!pos) return (pre & 1) != (len & 1);
    if(!limit && dp[pos][pre][len] != -1) return dp[pos][pre][len];
    LL ans = 0;
    int up = limit ? digit[pos] : 9;
    for(int i = 0; i <= up; ++i){
        if(leadingzero){
            if(i == 0){
                ans += dfs(pos - 1, 0, 0, true, limit && i == up);
            }
            else{
                ans += dfs(pos - 1, i & 1, 1, false, limit && i == up);
            }
        }
        else{
            if(i & 1){
                if(pre & 1){
                    ans += dfs(pos - 1, i & 1, len + 1, false, limit && i == up);
                }
                else{
                    if(len & 1){//若当前位是奇数,前一位是偶数,且已经有奇数长度的偶数,则可以继续延伸
                        ans += dfs(pos - 1, i & 1, 1, false, limit && i == up);
                    }
                }
            }
            else{
                if(pre & 1){
                    if(!(len & 1)){
                        ans += dfs(pos - 1, i & 1, 1, false, limit && i == up);
                    }
                }
                else{
                    ans += dfs(pos - 1, i & 1, len + 1, false, limit && i == up);
                }
            }
        }
    }
    if(!limit) dp[pos][pre][len] = ans;
    return ans;
}
LL solve(LL x){
    int cnt = 0;
    while(x){
        digit[++cnt] = x % 10;
        x /= 10;
    }
    return dfs(cnt, 0, 0, true, true);
}
int main(){
    int T;
    scanf("%d", &T);
    int kase = 0;
    memset(dp, -1, sizeof dp);
    while(T--){
        LL L, R;
        scanf("%lld%lld", &L, &R);
        printf("Case #%d: %lld\n", ++kase, solve(R) - solve(L - 1));
    }
    return 0;
}

  

以上是关于HDU - 5898 odd-even number (数位dp)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5898 odd-even number 数位DP

hdu 5898 odd-even number(数位dp)

hdu 5898 odd-even number 数位dp

hdu 5898 odd-even number 数位DP

[暑假集训--数位dp]hdu5898 odd-even number

数位dp HDU - 5898 odd-even number