# [BZOJ1026][SCOI2009]windy数

[BZOJ1026][SCOI2009]windy数

windy定义了一种windy数。不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。 windy想知道，

25 50

20

100%的数据，满足 1 <= A <= B <= 2000000000 。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cmath>
using namespace std;

int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}

#define maxn 15
int f[maxn][maxn];

int calc(int x) {
int num[maxn], cnt = 0;
while(x) num[++cnt] = x % 10, x /= 10;
int ans = 0;
for(int i = 1; i < cnt; i++)
for(int j = 1; j <= 9; j++) ans += f[i][j];
for(int i = cnt; i; i--) {
for(int j = (i == cnt ? 1 : 0); j < num[i]; j++) if(i == cnt || abs(j - num[i+1]) >= 2) ans += f[i][j];
if(i < cnt && abs(num[i+1] - num[i]) < 2) break;
}
return ans;
}

int main() {
for(int i = 0; i <= 9; i++) f[1][i] = 1;
for(int i = 1; i <= 10; i++)
for(int j = 0; j <= 9; j++) {
for(int k = 0; k <= j - 2; k++) f[i+1][k] += f[i][j];
for(int k = j + 2; k <= 9; k++) f[i+1][k] += f[i][j];
//			printf("%d %d: %d\n", i, j, f[i][j]);
}

int a = read(), b = read();
printf("%d\n", calc(b + 1) - calc(a));

return 0;
}
/*
1 2000000000
123456 928374445
*/


posted @ 2016-11-11 21:28  xjr01  阅读(282)  评论(0编辑  收藏