BZOJ 1026 - 数位DP

omo省选前夜… I'm Crawling Back to U...
若干坑点:
solve(x)是指小于x的windy数数量… 所以答案应是solve(b+1) - solve(a)
还有数组大小问题… B了狗了…

// BZOJ 1026 数位DP

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

 #define read(x) scanf("%lld", &x)
 #define rep(i,a,b) for (int i = a; i <= b; i++)
 #define dep(i,a,b) for (int i = a; i >= b; i--)
 #define LL long long

 const int Len = 10;

 int d[Len+2][12], bit[Len+2];

 void init() {
 	memset(d, 0, sizeof(d));
 	rep(i, 0, 9) d[1][i] = 1;
 	rep(i, 2, Len)
 	  rep(j, 0, 9)
 	    rep(k, 0, 9)
 	      if (abs(j-k) >= 2) d[i][j] += d[i-1][k];
 }

 int solve(LL x) {  // x = abcd
 	int cnt = 0, ret = 0;
 	if (x <= 2) return 0; 
 		else if (x <= 9) return x-2;
 	while (x > 0) {
 		bit[++cnt] = x%10;
 		x /= 10;
 	}
 	dep(i, bit[cnt]-1, 1) ret += d[cnt][i];  //  1xxx~3xxx (x<当前位数字)
 	dep(i, cnt-1, 1)  // 0xxx
 	  rep(j, 1, 9)
 	    ret += d[i][j];
 	dep(i, cnt-1, 1) {  //  axxx, abxx, abcx
 	  rep(j, 0, bit[i]-1)
 	    if (abs(bit[i+1]-j) >= 2) ret += d[i][j];
 	  if (abs(bit[i]-bit[i+1]) <= 1) break;
 	}
 	return ret;
 }

 int a, b;

int main()
{
	read(a); read(b);
	init();
	printf("%d\n", solve(b+1)-solve(a));

	return 0;
}

posted @ 2016-04-15 21:55  Armeria  阅读(123)  评论(0编辑  收藏