Accept: 189    Submit: 461
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 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

 Source

“高教社杯”第三届福建省大学生程序设计竞赛

 
某次群赛的一个很水的数位dp,但脑抽了,不知道想到哪里去了
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int bits[];
ll dp[][][][];
ll dfs(int pos, int pre, int iso, int doing) {
if(pos == -) return ;
int End = doing ? bits[pos] : ;
ll& ans = dp[pos][pre][iso][doing];
if(ans != -) return ans;
ans = ;
for(int i = ; i <= End; ++i) {
if(iso && i <= pre) ans += dfs(pos - , i, , doing && i == End);
else if(!iso && i >= pre) ans += dfs(pos - , i, , doing && i == End);
}
return ans;
}
ll calc(ll x) {
int ls = ;
memset(dp, -, sizeof dp);
while(x) {
bits[ls++] = x % ;
x /= ;
}
return dfs(ls - , , , );
}
int main() {
ll n, m;
int _;
cin >> _;
while(_ --) {
cin >> n >> m;
cout << calc(m) - calc(n - ) << endl;
}
return ;
}

Fzu2109 Mountain Number 数位dp的更多相关文章

  1. FZU - 2109 Mountain Number 数位dp

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

  2. 多校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 用作标记,当现在枚举的数小 ...

  3. hdu 5898 odd-even number 数位DP

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

  4. codeforces Hill Number 数位dp

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

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

  6. HDU 3709 Balanced Number (数位DP)

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

  7. beautiful number 数位DP codeforces 55D

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

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

  9. 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. 【leetcode】Binary Search Tree Iterator(middle)

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  2. 【leetcode】Substring with Concatenation of All Words (hard) ★

    You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...

  3. 【转】CV_EXPORT定义的作用,lib及dll的区别

    http://blog.csdn.net/viewcode/article/details/8021989 在core.hpp中,CV_EXPORT是出现频率最高的词之一. 1. CV_EXPORT是 ...

  4. [Android ] linux命令英文缩写的含义(方便记忆)

    du -sh */ reference to : http://blog.chinaunix.net/uid-27164517-id-3299073.html linux常用命令的英文单词缩写 命令缩 ...

  5. jquery.validation.js 表单验证

    jquery.validation.js 表单验证   官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuer ...

  6. NYOJ题目113字符串替换

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAr4AAAHvCAIAAAA930vtAAAgAElEQVR4nO3dPVLjysIG4G8T5CyE2A ...

  7. NYOJ题目766回文数

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsgAAAHaCAIAAACSPygsAAAgAElEQVR4nO3dO3LqSheG4X8S5AyEWB ...

  8. 关于ActionContext.getContext()的用法心得

    转: 为了避免与Servlet API耦合在一起,方便Action类做单元测试,Struts 2对HttpServletRequest.HttpSession和ServletContext进行了封装, ...

  9. php单双引号

    1.定义字符串 定义字符串时,只有一种引号被视为定义符,即单引号或双引号.于是,如果一个字符串由双引号开始,那么只有双引号被分析器解析.这样,你就可以在双引号串中包含任何其他字符,甚至单引号.下面的引 ...

  10. JQ JSON数据类型

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...