FZU - 2109 Mountain Number 数位dp
Mountain Number
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
3
1 10
1 100
1 1000
Sample Output
9
54
384 给定范围寻找山数的个数。
标准的数位dp。从首位枚举到末尾,当前项与前一项符合,却不一定不满足条件,还需要与后一项进行比对,所以需要记录pre。dp的三种状态dp[pos][pre][sta](当前位置,前一位,当前项奇偶)。套板子就行,注意首位的处理。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#define MAX 105
#define INF 0x3f3f3f3f
using namespace std; typedef long long ll; int a[MAX];
int poss;
ll dp[MAX][][];
ll dfs(int pos,int pre,int sta,bool limit){
int i;
if(pos==-) return ;
if(!limit&&dp[pos][pre][sta]!=-) return dp[pos][pre][sta];
int up=limit?a[pos]:;
int cnt=;
for(i=;i<=up;i++){
if(sta==&&pre>i) continue;
if(pos!=poss&&sta==&&pre<i) continue;
cnt+=dfs(pos-,i,!sta,limit&&i==a[pos]);
}
if(!limit) dp[pos][pre][sta]=cnt;
return cnt;
}
ll solve(ll x){
int pos=;
while(x){
a[pos++]=x%;
x/=;
}
poss=pos-;
return dfs(pos-,-,,true);
}
int main()
{
int t;
ll l,r;
scanf("%d",&t);
memset(dp,-,sizeof(dp)); //初始化一次(优化)
while(t--){
scanf("%lld%lld",&l,&r);
printf("%lld\n",solve(r)-solve(l-));
}
return ;
}
FZU - 2109 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
		
http://acm.fzu.edu.cn/problem.php?pid=2109 题意:找出区间[l,r]内满足奇数位的数字大于相邻偶数位数字的个数. 典型的数位dp了,记录一下当前位是奇数位还是 ...
 - 多校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 ...
 
随机推荐
- 关于maven的profile
			
1 什么是profile profile本质上就是不同的环境对应不同的配置. 这样的好处是,在命令行中指定具体的profile的时候,会有自己独特的参数或者独特的配置来为不同的环境生成不同的目标代码. ...
 - $CLASS('page__hd')[0].style.backgroundColor="red"
			
const $ID = (p) => document.getElementById(p)const $CLASS = (p) => document.getElementsByClass ...
 - zookeeper curator ( 实战一)
			
目录 zookeeper 的伪集群搭建 写在前面 1.1. zookeeper 安装&配置 1.1.1. 创建数据目录和日志目录: 1.1.2. 创建myid文件 1.1.3. 创建和修改配置 ...
 - 使用python实现二分法查找
			
最近开始学习mit的python课程,其中手工实现的一个关于二分法查找的练习代码个人感觉比较有参考价值,贴上来分享交流一下. 主要功能是在1-100中自己猜测一个数值,随后系统产生数值看是否符合猜测, ...
 - distributed OSGI demo
			
今天继续<OSGi原理与最佳实践>.看到第四章.做 HelloWorld-cxf 的样例 照着样例敲来着,整个样例敲完了,执行.一直报错, ----------------这里是解决方法- ...
 - 在cocos2d-x中使用LUA
			
在cocos2d-x中使用LUA 1.注冊LUA脚本引擎 CCLuaEngine* pEngine = CCLuaEngine::defaultEngine(); CCScriptEngineMana ...
 - git 从远端拉取指定分支和推送本地某个分支到远端
			
如题,可以直接从远端拉取某个分支,也可以直接将本地某个分支推送到远端. 原文链接:https://www.cnblogs.com/hamsterPP/p/6810831.html
 - UVA - 10305 【全排列】
			
题意 要求给出一组 包含 1 - N 的数字的序列 要求这个序列 满足 题给的限制条件 比如 1 2 就是 1 一定要在 2 前面 思路 因为 数据规模较小 可以用 全排列 然后判断每个序列 是否满足 ...
 - Visual Studio 2012 与此版本的 Windows 不兼容 解决
			
警告: [Window Title] 程序兼容性助手[Main Instruction] 此程序存在已知的兼容性问题[Expanded Information] Visual Studio 2012 ...
 - Ubuntu 16.04上编译SkyEye的测试程序
			
一.首先确保Ubuntu系统上已经安装了Skyeye.skyeye-testsuite和arm-linux-gcc交叉编译工具链,如果没有安装请参考: 1.Skyeye的安装:http://www.c ...