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. Mac版 Intellij IDEA 激活

    第一步:修改这两个文件夹 进入跳转路径,输入 /private/etc/ 点击[前往] 同时加上: # Intellij IDEA0.0.0.0 account.jetbrains.com 第二部:在 ...

  2. 远程服务器上的weblogic项目管理(三)常用指令及常见错误

    weblogic的管理流程已在前两节整理完毕,接下来汇总一下linux环境下的weblogic管理常用指令及常见错误: 常用指令: ./startWebLogic.sh 启动weblogic ./st ...

  3. 我的Android进阶之旅------>Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法

    开发中常常需要自己定义Listview,去继承BaseAdapter,在adapter中按照需求进行编写,问题就出现了,可能会发生点击每一个item的时候没有反应,无法获取的焦点. 如果你的自定义Li ...

  4. 【python】使用python发送文本内容邮件

    下面提供了一个使用python做的发送文本内容的邮件代码,能够在邮件内容中设置文字颜色,大小,换行等功能. #auther by zls #_*_coding:utf-8_*_ import sys ...

  5. NiFi汉化

    ①在源文件中的 source-nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-ui/src中修 ...

  6. 一次react滚动列表的实践---兼容ios安卓

    一.背景 近期项目改版,对原有的h5页面进行了重新设计,数据呈现变成了瀑布流.希望新版兼容ios和安卓两端的情况下,无限制的刷新加载数据.大致效果如下: 整个页面分4部分: 顶部导航 步数状态卡片 用 ...

  7. vue.use解析

    vue.use源码: Vue.use = function (plugin) { /*判断插件是否已引入*/ if (plugin.installed) { return } //倒序为一个数组[] ...

  8. 使用git连接到Github

    直奔主题,使用git连接到Github步骤如下: 1. 安装git yum install git 或者 sudo get-apt install git git-core 2. 全局配置 git c ...

  9. poj The Settlers of Catan( 求图中的最长路 小数据量 暴力dfs搜索(递归回溯))

    The Settlers of Catan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1123   Accepted: ...

  10. c语言学习的第13天2

    #include <stdio.h> #include <malloc.h> void f(int **q) { *q=(int *)malloc(sizeof(int)); ...