题目链接[SCOI2009]windy数
其实不是初探啦,很早以前就学习过然而并没有与刷题,现在想起来有这么一个坑
我们设dp[i][j]表示长度为i开头数字为j的windy数的个数,统计就随意了
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
LL a,b,dp[11][10],bin[11];
LL solve(LL x){
if (!x) return 0; LL ret=0;
int n=10; while (bin[n]>x) n--;
for (int i=1;i<n;++i)
for (int j=1;j<=9;++j)
ret+=dp[i][j];
int cur=x/bin[n];
for (int i=1;i<cur;++i) ret+=dp[n][i];
x%=bin[n]; int last=cur;
for (int i=n-1;i;i--){
cur=x/bin[i];
if (i>1){
for (int j=0;j<cur;++j)
if (abs(j-last)>=2) ret+=dp[i][j];
}else if (i==1){
for (int j=0;j<=cur;++j)
if (abs(j-last)>=2) ret+=dp[i][j];
}if (abs(cur-last)<2) break;
x%=bin[i]; last=cur;
}return ret;
}
int main(){
scanf("%lld%lld",&a,&b);
for (int i=0;i<=9;++i)
dp[1][i]=1;
for (int i=2;i<=10;++i)
for (int j=0;j<=9;++j)
for (int k=0;k<=9;++k)
dp[i][j]+=abs(j-k)>=2?dp[i-1][k]:0;
bin[1]=1;
for (int i=2;i<=10;++i) bin[i]=bin[i-1]*10;
printf("%lld",solve(b)-solve(a-1));
}