Problem Description

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。。那肯定不会。。。

感觉有点像记忆化搜索吧。cal(n)求小于等于n且满足条件的数。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int N = 15;
int num[N];
int dp[N][N][2]; // dp[i][j][1] i位数 前一位为j int dfs(int nowPos, int preNum, bool isPeak, bool isFirst, bool isMax)
{
if (nowPos == -1) return 1; if (!isMax && dp[nowPos][preNum][isPeak]) return dp[nowPos][preNum][isPeak]; int ans = 0;
int limit = isMax ? num[nowPos] : 9;
for (int i = 0; i <= limit; ++i) {
// isFirst 是说前面都是0 也就是该位是第一位
if (isFirst && i == 0) ans += dfs(nowPos - 1, 9, false, true, false);
else if (isPeak && i >= preNum) ans += dfs(nowPos - 1, i, false, false, (isMax && i == limit));
else if (!isPeak && i <= preNum) ans += dfs(nowPos - 1, i, true, false, (isMax && i == limit));
}
if (!isMax) dp[nowPos][preNum][isPeak] = ans; return ans;
} int cal(int n)
{
int idx = 0;
while (n) {
num[idx++] = n % 10;
n /= 10;
}
return dfs(idx - 1, 9, false, true, true);
} int main()
{
int t;
scanf("%d", &t);
while (t--) {
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", cal(r) - cal(l - 1));
}
return 0;
}

  

fzu2109--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 数位dp

    Mountain Number One integer number x is called "Mountain Number" if: (1) x>0 and x is a ...

  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 ...

  10. hdu 5898 odd-even number(数位dp)

    Problem Description For a number,if the length of continuous odd digits is even and the length of co ...

随机推荐

  1. Oracle控制文件丢失,日志文件丢失

    控制文件丢失: alter database backup controlfile to traces; shutdown immediate; @j:\db\script\orcl_ora_ctl_ ...

  2. T[]与List<T>的使用时机

    所有的数组类型都隐式地从System.Array这个抽象类派生,而System.Array又派生自System.Object.也就是说数组是引用类型.通过如下方式创建数组: int[] arrInt ...

  3. Unity3d Shader开发(三)Pass(Pass Tags,Name,BindChannels )

    Pass Tags 通过使用tags来告诉渲染引擎在什么时候该如何渲染他们所期望的效果. Syntax 语法 Tags { "TagName1" = "Value1&qu ...

  4. 如何组织css,写出高质量的css代码

    !如何组织css一:css的API 属于基础部分,这部分的能力用“对”和“错”来评判. 比如说把文字设置为红色,只能用color:red:这种写法是对的,其他任何写法都是错的. 二:css框架 不能用 ...

  5. 1006: [HNOI2008]神奇的国度

    图上的最小的染色方案: 学习了陈丹绮的论文: MCS算法 #include<cstdio> #define maxn 10005 #define maxm 2000005 using na ...

  6. Firefly官方教程之Distributed使用文档

    原地址:http://bbs.gameres.com/thread_224191.html distributed使用文档1.distributed说明该模块主要封装了各个服务进程间进行通信的方法.n ...

  7. IOS 接ShareSDK问题

    如果报错AGCommon 等错误 引用libicucore.A.dylib ShareSDK 官网 管理中心 → 创建一个新的应用 获得key之后  啥都别做.! - (BOOL)applicatio ...

  8. 1090-Rock, Paper, Scissors

    描述 Rock, Paper, Scissors is a classic hand game for two people. Each participant holds out either a ...

  9. easyui源码翻译1.32--Slider(滑动条)

    前言 使用$.fn.slider.defaults重写默认值对象.下载该插件翻译源码 滑动条允许用户从一个有限的范围内选择一个数值.当滑块控件沿着轨道移动的时候,将会显示一个提示来表示当前值.用户可以 ...

  10. eCos中断模型

    http://blog.csdn.net/chychc/article/details/8313458 http://www.cnblogs.com/RandyQ/archive/2013/04/14 ...