## 【HDU-5898】odd-even number【记忆化搜索】

【HDU-5898】odd-even number【记忆化搜索】

Solution:

```#include <iostream>
#include <cstring>

using namespace std;

typedef long long LL;

enum {ENE ,ONE, ONO, ENO, START};
const int NIL = -1;
const int LIM = 20;
const int SIZE = 5;
int digit[LIM];
LL dp[LIM][SIZE];

inline bool isOdd(int x) { return x & 1; }

LL dfs(int crt, bool islead, bool hasLimit, int state) {
if(crt == NIL) return (state == ONE || state == ENO) && !islead;
if(!hasLimit && !islead && dp[crt][state] != NIL) return dp[crt][state];
int upBound = hasLimit ? digit[crt] : 9;
LL cnt = 0;
for(int i = 0; i <= upBound; i++)
{
cnt += dfs(crt - 1,islead && !i,hasLimit && (i == upBound), isOdd(i) ? ONO : ONE);
} else {
if(state == ENO) cnt += dfs(crt - 1, false, hasLimit && (i == upBound), isOdd(i) ? ONO : ONE);
if(state == ONO && isOdd(i)) cnt += dfs(crt - 1, false, hasLimit && (i == upBound), ENO);
if(state == ONE) cnt += dfs(crt - 1, false, hasLimit && (i == upBound), isOdd(i) ? ONO : ENE);
if(state == ENE && !isOdd(i)) cnt += dfs(crt - 1, false ,hasLimit && (i == upBound), ONE);
}
}
if(!hasLimit && !islead) dp[crt][state] = cnt;
return cnt;
}
LL getNum(LL x) {
memset(dp, NIL, sizeof(dp));
int crt = 0;
while(x) {
digit[crt++] = x % 10;
x /= 10;
}
return dfs(crt - 1, true, true, START);
}

int main() {
int t;
scanf("%d",&t);
for(int k = 1; k <= t; k++) {
LL left, right;
scanf("%lld%lld", &left, &right);
printf("Case #%d: %lld\n", k, getNum(right) - getNum(left - 1));
}
return 0;
}
```