BZOJ 1026 windy数 (数位DP)

代码

[cpp] #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <string> #include <cstring> #include <vector> #include <set> #include <stack> #include <queue> #define MID(x,y) ((x+y)/2) #define MEM(a,b) memset(a,b,sizeof(a)) #define REP(i, begin, m) for (int i = begin; i < begin+m; i ++) using namespace std; typedef long long LL; typedef vector <int> VI; typedef set <int> SETI; typedef queue <int> QI; typedef stack <int> SI; const int oo = 0x7fffffff; int dp[12][10][2]; VI num; int dfs(int pos, int pre, bool flag, int limit){ if (pos == -1) return 1; if (!limit && ~dp[pos][pre][flag]) return dp[pos][pre][flag]; int end = limit?num[pos]:9, res = 0; for (int i = 0; i <= end; i ++){ if (flag || abs(i - pre) >= 2){ res += dfs(pos-1, i, flag&&(i == 0),limit&&(i==end)); } } return limit?res:dp[pos][pre][flag] = res; } int cal(int x){ num.clear(); while(x){ num.push_back(x%10); x /= 10; } return dfs(num.size()-1, 0, 1, 1); } int main(){ int a,b; MEM(dp, -1); while(scanf("%d %d", &a, &b) != EOF){ printf("%d\n", cal(b)-cal(a-1)); } return 0; } [/cpp]
posted @ 2013-08-12 07:19  AbandonZHANG  阅读(113)  评论(0编辑  收藏