https://www.lydsy.com/JudgeOnline/problem.php?id=1026

C++版本一

题解:

假设我们需要求0至x(用数组表示)的区间的windy数的个数,x有t位,我们先求出t-1位的windy数的个数,因为这些windy数绝对比x小,不会超过这个区间,然后求出长度为t,最高位小于x[0]的windy数的个数,同样不会超过这个区间.最后统计长度为t,最高位为x[0]的windy数的个数,怎么统计呢?枚举i从0到x[1]-1,加上长度为t-1的最高位为i的数,不会超过这个区间,然后同样的,再求最高位为x[1]的windy数的个数,类似于递归过程.如果abs(x[0] - x[1]) < 2,则最高位为x[0],次高位为x[1]的windy数再不存在了,直接退出,到最后一位时,如果还存在windy数,windy数的个数+1即可.

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=1000000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
ll t,n,m,k,q;
int ans,cnt,flag,temp;
int num[20],dp[20][12];

int dfs(int len, int last, bool shangxian)
{
    int p;
    if (len <= 0)
        return 1;
    if (!shangxian && dp[len][last] != -1&& last >= 0)
        return dp[len][last];
    int cnt = 0, maxx = (shangxian ? num[len] : 9);
    for (int i = 0; i <= maxx; i++)
    {
        if (abs(i - last) < 2)
            continue;
        p = i;
        if (i == 0 && last == -10)
            p = last;
        cnt += dfs(len - 1, p, shangxian && (i == maxx));
    }
    //return cnt;
    if (last >= 0 && !shangxian)
        dp[len][last] = cnt;
    return cnt;
}

ll solve(int x)
{
    int k = 0;
    while (x)
    {
        num[++k] = x % 10;
        x /= 10;
    }
    memset(dp, 255, sizeof(dp));
    return dfs(k, -10, true);
}
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    memset(dp,-1,sizeof(dp));
    scanf("%lld %lld",&n,&m);
    printf("%lld\n",solve(m) - solve(n - 1));
    //cout << "Hello world!" << endl;
    return 0;
}

C++版本二

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

long long t, a, b;

long long f[15][11],shu[15];

void init()
{
    memset(f, 0, sizeof(f));
    for (int i = 0; i <= 9; i++)
        f[1][i] = 1;
    for (int i = 2; i <= 10; i++)
        for (int j = 0; j <= 9; j++)
            for (int k = 0; k <= 9; k++)
                if (abs(j - k) >= 2)
                    f[i][j] += f[i - 1][k];
}

long long solve(long long x)
{
    memset(shu, 0, sizeof(shu));
    if (x == 0)
        return 0;
    long long k = 0,ans = 0;
    while (x)
    {
        shu[++k] = x % 10;
        x /= 10;
    }
    for (int i = 1; i <= k - 1; i++)
        for (int j = 1; j <= 9; j++)
            ans += f[i][j];
    for (int i = 1; i < shu[k]; i++)
        ans += f[k][i];
    for (int i = k - 1; i >= 1; i--)
    {
        for (int j = 0; j <= shu[i] - 1; j++)
            if (abs(j - shu[i + 1]) >= 2)
                ans += f[i][j];
        if (abs(shu[i + 1] - shu[i]) < 2)
            break;
        if (i == 1)
            ans += 1;
    }
    return ans;
}

int main()
{
    scanf("%lld%lld", &a, &b);
    init();
    printf("%lld", solve(b) - solve(a - 1));

    //while (1);
    return 0;
}