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
Sample Input
Sample Output
看到题的第一反应是:数位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)的更多相关文章
- Fzu2109 Mountain Number 数位dp
Accept: 189 Submit: 461Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description One ...
- FZU - 2109 Mountain Number 数位dp
Mountain Number One integer number x is called "Mountain Number" if: (1) x>0 and x is a ...
- 多校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 用作标记,当现在枚举的数小 ...
- hdu 5898 odd-even number 数位DP
传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...
- codeforces Hill Number 数位dp
http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits: 5000 MS Memory Limits: ...
- 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 ...
- HDU 3709 Balanced Number (数位DP)
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- beautiful number 数位DP codeforces 55D
题目链接: http://codeforces.com/problemset/problem/55/D 数位DP 题目描述: 一个数能被它每位上的数字整除(0除外),那么它就是beautiful nu ...
- 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 ...
- 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 ...
随机推荐
- oracle dblink 配置两个ip
create database link test_link connect to xx identified by xx using '(DESCRIPTION = (ADDRESS_LIST = ...
- 关于hibernate的实体类中有集合类型转化成JSON的工具类 - 怀念今天的专栏 - 博客频道
Json 来源:http://blog.csdn.net/zczzsq/article/details/18697045#1536434-hi-1-4387-42d97150898b1af15ddaa ...
- PHP漏洞全解(六)-跨网站请求伪造
本文主要介绍针对PHP网站的跨网站请求伪造.在CSRF所有攻击方式中包含攻击者伪造一个看起来是其他用户发起的HTTP 请求,事实上,跟踪一个用户发送的HTTP请求才是攻击者的目的. CSRF(Cros ...
- Unity3D研究院之在把代码混淆过的游戏返混淆回来
最近一直在找如何在MAC上混淆Android的DLL,至今没能找到合适的,有大神知道记得告诉我喔.今天群里有人说了一个混淆代码和返混淆代码的工具de4dot ,不查不知道一查吓一跳.这玩意可以把别人混 ...
- 练习PYTHON之EVENTLET
以下是重点,要会运用: eventlet是一个用来处理和网络相关的python库函数,而且可以通过协程来实现并发,在eventlet里,把“协程”叫做 greenthread(绿色线程).所谓并发,就 ...
- eclipse查看.project .class隐藏文件
fileter ->*.resource勾选:
- JavaSE replaceAll 方法
private String srcStr = "index\\.php\\?action=";//要替换的原字符串 private String destStr = " ...
- char 与 unsigned char的本质区别
在C中,默认的基础数据类型均为signed,现在我们以char为例,说明(signed) char与unsigned char之间的区别 首先在内存中,char与unsigned char没有什么不同 ...
- char和QChar(Unicode的编码与内存里的值还不是一回事)
char类型是c/c++中内置的类型,描述了1个字节的内存信息的解析.比如: char gemfield=’g’; 那么在由gemfield标记的这块内存的大小就是1个字节,信息就是01100111, ...
- Android开发UI之Navigation Drawer
http://blog.csdn.net/xyz_lmn/article/details/12523895