windy数
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
windy定义了一种windy数。
不含前导零且相邻两个数字之差至少为 2 的正整数被称为windy数。
windy想知道,在 A 和 B 之间,包括 A 和 B ,总共有多少个windy数?
Input
包含两个整数, A B 。
满足 1≤A≤B≤2000000000 .
Output
Sample input and output
Sample Input | Sample Output |
---|---|
1 10 | 9 |
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
ll dp[70][10];
ll bits[70];
int cur;
ll dfs(ll pos,int pre,bool flag,bool zeros)
{
if(pos <= 0)
{
return 1;
}
if(!flag && ~dp[pos][pre]) return dp[pos][pre];
ll end = flag?bits[pos] : 9;
ll ret = 0;
for(int i=0;i<=end;i++)
{
if(abs(pre - i) >= 2)
{
int tmp = i;
if(zeros && tmp == 0) tmp = 100;
ret += dfs(pos-1,tmp,flag && i == end,zeros && i == 0);
}
}
return flag ? ret : dp[pos][pre] = ret;
}
ll solve(ll x)
{
cur = 0;
ll tt = x;
while(tt > 0)
{
bits[++cur] = tt % 10;
tt /= 10;
}
return dfs(cur,100,true,true);
}
int main()
{
ll a,b;
while(scanf("%lld%lld",&a,&b) != EOF)
{
memset(dp,-1,sizeof(dp));
printf("%lld\n",solve(b)-1-(solve(a-1) -1));
}
return 0;
}