https://cn.vjudge.net/problem/FZU-2109
One integer number x is called "Mountain Number" if:
(1) x>0 and x is an integer;
(2) Assume x=a[0]a[1]...a[len-2]a[len-1](0≤a[i]≤9, a[0] is positive). Any a[2i+1] is larger or equal to a[2i] and a[2i+2](if exists).
For example, 111, 132, 893, 7 are "Mountain Number" while 123, 10, 76889 are not "Mountain Number".
Now you are given L and R, how many "Mountain Number" can be found between L and R (inclusive) ?
Input
The first line of the input contains an integer T (T≤100), indicating the number of test cases.
Then T cases, for any case, only two integers L and R (1≤L≤R≤1,000,000,000).
Output
For each test case, output the number of "Mountain Number" between L and R in a single line.
Sample Input
3 1 10 1 100 1 1000
Sample Output
9 54 384
x=a[0]a[1]...a[len-2]a[len-1](0≤a[i]≤9, a[0] is positive)
下标为奇数的要大于等于两边
数位dp,注意ll用I64d
#include<cstdio>
#include<cstring>
#pragma GCC optimize(3)
#define max(a,b) a>b?a:b
using namespace std;
typedef long long ll;
ll dp[15][2][10];
int a[15];
ll dfs(int pos,int sta,int pre,bool lead,bool limit){
if(pos==0) return 1;
if(!lead&&!limit&&dp[pos][sta][pre]!=-1) return dp[pos][sta][pre];
int up=limit?a[pos]:9;
ll ans=0;
for(int i=0;i<=up;i++){
if(lead&&i==0) ans+=dfs(pos-1,0,9,true,limit&&i==up);
else if(sta==0&&i<=pre) ans+=dfs(pos-1,1,i,false,limit&&i==up);
else if(sta==1&&i>=pre) ans+=dfs(pos-1,0,i,false,limit&&i==up);
}
if(!lead&&!limit) dp[pos][sta][pre]=ans;
return ans;
}
ll solve(ll x){
int pos=0;
while(x){
a[++pos]=x%10;
x/=10;
}
return dfs(pos,0,9,true,true);
}
int main(){
memset(dp,-1,sizeof(dp));
int T;
scanf("%d",&T);
while(T--){
ll L,R;
scanf("%I64d%I64d",&L,&R);
printf("%I64d\n",solve(R)-solve(L-1));
}
return 0;
}