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][][];
ll dfs(int pos,int pre,int sta,bool limit){
int i;
if(pos==-) return ;
if(!limit&&dp[pos][pre][sta]!=-) return dp[pos][pre][sta];
int up=limit?a[pos]:;
int cnt=;
for(i=;i<=up;i++){
if(sta==&&pre>i) continue;
if(pos!=poss&&sta==&&pre<i) continue;
cnt+=dfs(pos-,i,!sta,limit&&i==a[pos]);
}
if(!limit) dp[pos][pre][sta]=cnt;
return cnt;
}
ll solve(ll x){
int pos=;
while(x){
a[pos++]=x%;
x/=;
}
poss=pos-;
return dfs(pos-,-,,true);
}
int main()
{
int t;
ll l,r;
scanf("%d",&t);
memset(dp,-,sizeof(dp)); //初始化一次(优化)
while(t--){
scanf("%lld%lld",&l,&r);
printf("%lld\n",solve(r)-solve(l-));
}
return ;
}
 

FZU - 2109 Mountain Number 数位dp的更多相关文章

  1. Fzu2109 Mountain Number 数位dp

    Accept: 189    Submit: 461Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description One ...

  2. FZU 2109 Mountain Number

    http://acm.fzu.edu.cn/problem.php?pid=2109 题意:找出区间[l,r]内满足奇数位的数字大于相邻偶数位数字的个数. 典型的数位dp了,记录一下当前位是奇数位还是 ...

  3. 多校5 HDU5787 K-wolf Number 数位DP

    // 多校5 HDU5787 K-wolf Number 数位DP // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d // f 用作标记,当现在枚举的数小 ...

  4. hdu 5898 odd-even number 数位DP

    传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...

  5. codeforces Hill Number 数位dp

    http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits: ...

  6. HDU 5787 K-wolf Number 数位DP

    K-wolf Number Problem Description   Alice thinks an integer x is a K-wolf number, if every K adjacen ...

  7. HDU 3709 Balanced Number (数位DP)

    Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  8. beautiful number 数位DP codeforces 55D

    题目链接: http://codeforces.com/problemset/problem/55/D 数位DP 题目描述: 一个数能被它每位上的数字整除(0除外),那么它就是beautiful nu ...

  9. BNU 13024 . Fi Binary Number 数位dp/fibonacci数列

    B. Fi Binary Number     A Fi-binary number is a number that contains only 0 and 1. It does not conta ...

随机推荐

  1. android菜鸟学习笔记8----Activity(一)

    Activity是android应用程序中重要的组件之一,常听到的android四大组件是Activity.Service.BroadcastReceiver和ContentProvider.它间接继 ...

  2. IE浏览器的判断

    function compatibleIE8(){ var browser = navigator.appName; var b_version = navigator.appVersion; if( ...

  3. GOLANG 1.9 语言规范

    GOLANG 1.9 语言规范 - CSDN博客 https://blog.csdn.net/libing_thinking/article/details/77671607

  4. redis自启动

    $ vi /etc/init.d/redis # chkconfig: 2345 90 10 # description: Redis is a persistent key-value databa ...

  5. java中final与static的使用场景

    final Java关键词final有“无法改变”的含义,主要用于修饰非抽象类.方法或者变量.使用时注意: final类不能被继承,没有子类,final类中的方法默认是final的. final方法不 ...

  6. d3 - bar chart

    用 D3.js 做一个简单的柱形图. 做柱形图有很多种方法,比如用 HTML 的 div 标签,或用 svg . 推荐用 SVG 来做各种图形.SVG 意为可缩放矢量图形(Scalable Vecto ...

  7. Discuz!支持发布视频帖子设定 + 修改后台文件

    最近想做一个地方性论坛,果断在阿里巴巴的phpwind论坛程序与腾讯旗下的discuz论坛程序中选择,虽然phpwind大气,后面还是 决定选择了discuz程序用来构建这个平台,经过一番安装后,发现 ...

  8. Java for LeetCode 120 Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  9. save create

    其中 create 和 create!就等於 new 完就 save 和 save!,有無驚嘆號的差別 在於 validate 資料驗證不正確的動作,無驚嘆號版本會回傳布林值(true 或 false ...

  10. iOS define 宏定义 和 const定义常量区别

    const   const 是c++中的修饰符.  c++中常用来定义常量,修饰左值. #define 宏定义语句, 在预处理阶段直接做文本替换,不做类型检查. 它们之间的最大区别: 1.  对于co ...