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 ...
随机推荐
- IDEA的使用
1.设置字体file->setting->color$fonts->font 保存提示设置file->Settings -> Editor -> General - ...
- python 操作sqlite数据库
'''SQLite数据库是一款非常小巧的嵌入式开源数据库软件,也就是说 没有独立的维护进程,所有的维护都来自于程序本身. 在python中,使用sqlite3创建数据库的连接,当我们指定的数据库文件不 ...
- ping(1)
/* ping program for learning IP protocol author: jeff date: 2014/10/25 */ #include <stdio.h> # ...
- 几个不常见但非常出色的 .NET 开源库
NLog NLog 目前最为出色的 .NET 日志库,非常容易配置,且极具灵活性.最重要的是与 log4net 相比,NLog 的开发很活跃.顺带提一句,NLog 完全兼容 Mono. Mono.Ce ...
- Dynamic Programming (DP) 问题总结
所有的 DP 问题都可以简单得用 Recursion 的方式实现.这通常是最容易想到的思路. 问题是这种实现不够 efficient,存在 subproblem 被重复计算的情况.有两种解决这个问题的 ...
- 【综述】(MIT博士)林达华老师-"概率模型与计算机视觉”
[综述](MIT博士)林达华老师-"概率模型与计算机视觉” 距上一次邀请中国科学院的樊彬老师为我们撰写图像特征描述符方面的综述(http://www.sigvc.org/bbs/thread ...
- 对CURL的一些研究
http://www.kuqin.com/article/23candcplusplus/586014.html 前两天看到有人求客户端socket 发HTTP包的代码,受flw版主启发找了一些per ...
- POJ1269+直线相交
求相交点 /* 线段相交模板:判相交.求交点 */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...
- UDP模块(黑胶体)
UDP模块是采用PIP封装技术的U盘半成品模块,直接加上外壳,就是成品U盘. 它有以下特点: 防水.防尘.防震. 一体WAFER封装技术. 薄.轻.时尚. 产品装配方便.简单. 产品标准化,适合客户在 ...
- java1.8的几大新特性(二)
七.Date APIJava 8 在包java.time下包含了一组全新的时间日期API.新的日期API和开源的Joda-Time库差不多,但又不完全一样,下面的例子展示了这组新API里最重要的一些部 ...