题目链接:
解题思路:
数位DP
设计状态
设 d p [ i ] [ j ] dp[i][j] dp[i][j]表示位数是 i i i(可以有前导零),且最高位是 j j j的所有数中,windy数的个数。
转移方程
d p [ i ] [ j ] = Σ d p [ i − 1 ] [ k ] dp[i][j]=\Sigma dp[i-1][k] dp[i][j]=Σdp[i−1][k],其中 k k k满足条件 ∣ j − k ∣ ≥ 2 |j-k|\geq2 ∣j−k∣≥2
答案存储
询问
[
l
,
r
]
[l,r]
[l,r]中windy数的个数,设
a
s
k
(
i
)
ask(i)
ask(i)表示
[
0
,
i
)
[0,i)
[0,i)中windy数的个数。
故
a
n
s
=
a
s
k
(
r
+
1
)
−
a
s
k
(
l
)
ans=ask(r+1)-ask(l)
ans=ask(r+1)−ask(l)
求
a
s
k
(
i
)
ask(i)
ask(i)可采用逐位确定的方法:
E
g
.
Eg.
Eg.
就是这样(详细的看代码)
详细代码
#define rg register
#define il inline
#define DEBUG printf("[Passing [%s] in line %d.]\n", __func__, __LINE__)
#define putline putchar('\n')
#define putsp putchar(' ')
#define Rep(a, s, t) for(rg int a = s; a <= t; a++)
#define Repdown(a, t, s) for(rg int a = t; a >= s; a--)
typedef long long ll;
#include<cstdio>
#define rs freopen("test.in", "r", stdin), freopen("test.out", "w", stdout)
struct IO
{
IO(int set = 0) {if(set) rs;}
template<typename T> il IO r(T& x)const
{
x = 0; T f = 1; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + int(ch - '0');
x *= f; return *this;
}
template<typename T> il IO w(T x)const
{
if(x < 0) {putchar('-'); x = -x;}
if(x >= 10) w(x / 10);
putchar(x % 10 + '0'); return *this;
}
template<typename T> il IO wl(const T& x)const {w(x), putline; return *this;}
template<typename T> il IO ws(const T& x)const {w(x), putsp; return *this;}
il IO l() {putline; return *this;}
il IO s() {putline; return *this;}
}io;
template<typename T> il T Max(const T& x, const T& y) {return y < x ? x : y;}
template<typename T> il T Min(const T& x, const T& y) {return y < x ? y : x;}
template<typename T> il void Swap(T& x, T& y) {T tmp = x; x = y; y = tmp;}
int dp[15][15];
int A, B;
int Abs(int a) {return a >= 0 ? a : -a;}
int ask(int x)//找所有<x的windy数
{
int a[15] = {0}, len = 0, ans = 0;
while(x)
{
a[++len] = x % 10;
x /= 10;
}//将x按位拆开
for(rg int i = 1; i < len; i++)
for(rg int j = 1; j <= 9; j++)
ans += dp[i][j];//先将所有长度<len的windy数求出
for(rg int i = 1; i < a[len]; i++)
ans += dp[len][i];//再将所有长度=len,但最高位比x的最高位小的windy数求出
for(rg int i = len - 1; i >= 1; i--)
{
for(rg int j = 0; j < a[i]; j++)
//枚举数的第i位的数字(这个数的第i位以上的数字与x相同)
{
if(Abs(j - a[i + 1]) >= 2) ans += dp[i][j];
}
if(Abs(a[i + 1] - a[i]) < 2) break;//再找下去永远无法满足条件
}//最后求长度=len,最高位等于x最高位的windy数。
//要采用逐位确定法
return ans;
}
int main()
{
//FileReset();
for(rg int i = 0; i <= 9; i++) dp[1][i] = 1;
for(rg int i = 2; i <= 10; i++)
for(rg int j = 0; j <= 9; j++)
for(rg int k = 0; k <= 9; k++)
if(Abs(j - k) >= 2)
{
dp[i][j] += dp[i - 1][k];
}
io.r(A).r(B);
io.wl(ask(B + 1) - ask(A));
return 0;
}