FZU

Mountain Number

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

给定范围寻找山数的个数。
标准的数位dp。从首位枚举到末尾,当前项与前一项符合,却不一定不满足条件,还需要与后一项进行比对,所以需要记录pre。dp的三种状态dp[pos][pre][sta](当前位置,前一位,当前项奇偶)。套板子就行,注意首位的处理。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#define MAX 105
#define INF 0x3f3f3f3f
using namespace std;

typedef long long ll;

int a[MAX];
int poss;
ll dp[MAX][10][2];
ll dfs(int pos,int pre,int sta,bool limit){
    int i;
    if(pos==-1) return 1;
    if(!limit&&dp[pos][pre][sta]!=-1) return dp[pos][pre][sta];
    int up=limit?a[pos]:9;
    int cnt=0;
    for(i=0;i<=up;i++){
        if(sta==1&&pre>i) continue;
        if(pos!=poss&&sta==0&&pre<i) continue;
        cnt+=dfs(pos-1,i,!sta,limit&&i==a[pos]);
    }
    if(!limit) dp[pos][pre][sta]=cnt;
    return cnt;
}
ll solve(ll x){
    int pos=0;
    while(x){
        a[pos++]=x%10;
        x/=10;
    }
    poss=pos-1;
    return dfs(pos-1,-1,0,true);
}
int main()
{
    int t;
    ll l,r;
    scanf("%d",&t);
    memset(dp,-1,sizeof(dp));  //初始化一次(优化)
    while(t--){
        scanf("%lld%lld",&l,&r);
        printf("%lld
",solve(r)-solve(l-1));
    }
    return 0;
}
 
原文地址:https://www.cnblogs.com/yzm10/p/8822559.html