FZU2109:Mountain Number(数位DP)

 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

31 101 1001 1000

 Sample Output

954384

要求出区间内偶数为大于奇数位的数字的个数

dp[i][j][k] 表示在第i个位置时,前面是j,现在这位是奇数位还是偶数位的数目

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int t;
int bit[20],len,l,r;
int dp[20][10][2];

int dfs(int pos,int pre,int odd,int zero,int doing)//pos-当前位置,pre前一个数字,odd,现在位置的奇偶性,zero是否还是前导0,doing是否美剧导了边界
{
    if(pos==-1) return 1;
    if(dp[pos][pre][odd]!=-1 && !doing)
        return dp[pos][pre][odd];
    int end = doing?bit[pos]:9;
    int ans = 0;
    for(int i = 0; i<=end; i++)
    {
        if(!(i||zero))
            ans+=dfs(pos-1,9,0,zero||i,doing&&i==end);
        else if(odd && pre<=i)
            ans+=dfs(pos-1,i,!odd,zero||i,doing&&i==end);
        else if(!odd && pre>=i)
            ans+=dfs(pos-1,i,!odd,zero||i,doing&&i==end);
    }
    if(!doing)
        dp[pos][pre][odd] = ans;
    return ans;
}

int cal(int x)
{
    len = 0;
    while(x)
    {
        bit[len++] = x%10;
        x/=10;
    }
    return dfs(len-1,9,0,0,1);//刚刚开始在前面加个9,方便后面判断
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,-1,sizeof(dp));
        scanf("%d%d",&l,&r);
        printf("%d\n",cal(r)-cal(l-1));
    }

    return 0;
}

时间: 2024-10-11 07:09:10

FZU2109:Mountain Number(数位DP)的相关文章

fzu 2109 Mountain Number 数位DP

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2109 题意: 如果一个>0的整数x,满足a[2*i+1] >= a[2*i]和a[2*i+2],则这个数为Mountain Number. 给出L, R,求区间[L, R]有多少个Mountain Number. 思路: 数位DP,判断当前是偶数位还是奇数位(从0开始),如果是偶数位,那么它要比前一个数的值小, 如果是奇数位,那么它要比前一个数的值大. 1 #include <iostream>

Hdu3079Balanced Number数位dp

枚举支点,然后就搞,记录之前的点的力矩和. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <s

SPOJ MYQ10 Mirror Number 数位dp&#39;

题目链接:点击打开链接 MYQ10 - Mirror Number A number is called a Mirror number if on lateral inversion, it gives the same number i.e it looks the same in a mirror. For example 101 is a mirror number while 100 is not. Given two numbers a and b, find the number

多校5 HDU5787 K-wolf Number 数位DP

1 // 多校5 HDU5787 K-wolf Number 数位DP 2 // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d 3 // f 用作标记,当现在枚举的数小于之前的数时,就不用判断i与dig[pos]的大小 4 // 整体来说就,按位往后移动,每次添加后形成的数都小于之前的数,并且相邻k位不一样,一直深搜到cnt位 5 // http://blog.csdn.net/weizhuwyzc000/article/details/5209769

foj 2109 Mountain Number 位数dp

 题目链接  Problem 2109 Mountain Number Accept: 139    Submit: 357 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[le

hdu 5898 odd-even number 数位DP

odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 716    Accepted Submission(s): 385 Problem Description For a number,if the length of continuous odd digits is even and the length

hdu 5787 K-wolf Number 数位dp

数位DP 神模板 详解 为了方便自己参看,我把代码复制过来吧 // pos = 当前处理的位置(一般从高位到低位) // pre = 上一个位的数字(更高的那一位) // status = 要达到的状态,如果为1则可以认为找到了答案,到时候用来返回, // 给计数器+1. // limit = 是否受限,也即当前处理这位能否随便取值.如567,当前处理6这位, // 如果前面取的是4,则当前这位可以取0-9.如果前面取的5,那么当前 // 这位就不能随便取,不然会超出这个数的范围,所以如果前面取

codeforces Hill Number 数位dp

http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits:  200000 KB 64-bit interger IO format:  %lld   Java class name:  Main Description A Hill Number is a number whose digits possibly rise and then possibl

hdu3709---Balanced Number(数位dp)

Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit. When a pivot is placed at some digit of the nu