FZU2109:Mountain Number(数位DP)
来源:互联网 发布:淘宝网雪纺白色长裙 编辑:程序博客网 时间:2024/05/19 17:51
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
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
#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cmath>#include<map>#include<cstring>#include<string>#include<set>#include<stack>#include<queue>using namespace std;typedef long long LL;int dp[20][20][2],a[20];int dfs(int h,int o,int d,int s){//h当前位置 o前一个数 d当前位置奇偶性 0是偶1是奇 s是否有限制//是否有限制的意思是当前位置能否遍历所有的数,打个比方1567这个数在5这一位遍历到5的时候就不能遍历下一位的9 这就是有限制,当遍历的时候是4就能遍历下一位的9比如1499这就是没有限制 if(!h)return 1;//如果超出最后一位时每一位都满足条件这个数就满足 if(!s&&dp[h][o][d]!=-1)return dp[h][o][d];//如果没有限制的并且已经搜索就直接返回搜索过的//这里为什么是没限制呢,当有限制的时候比如1567这个数,在5的时候是有限制的,如果1570满足的话就没有被记入,所以不能直接用搜索过的 int k; if(s)k=a[h]; else k=9; int sum=0; for(int i=0;i<=k;i++){ if(d){//如果奇数位 if(o<=i)sum+=dfs(h-1,i,!d,s&&i==k);//如果前一位小于这一位继续搜索 } else{//偶数位同理 if(o>=i)sum+=dfs(h-1,i,!d,s&&i==k); } } if(!s)dp[h][o][d]=sum;//如果没有限制就可以记住 return sum;}int solve(int n){ memset(a,0,sizeof(a)); int h=0; while(n){ a[++h]=n%10; n/=10; } int sum=dfs(h,9,0,1);//前一位是9这样就省去了第一次的特判 return sum;}int main(){ int t; cin>>t; memset(dp,-1,sizeof(dp)); while(t--){ long long l,r; cin>>l>>r; cout<<solve(r)-solve(l-1)<<endl; } return 0;}
0 0
- FZU2109:Mountain Number(数位DP)
- FZU2109:Mountain Number(数位DP)
- FZU2109 Mountain Number (数位DP)
- Mountain Number (数位dp)
- FZU2109数位DP
- FZU2109(数位DP)
- FZU2109(数位dp)
- FZU 2109 Mountain Number ---数位dp
- FZU - 2109 Mountain Number 数位DP
- FZU-2109 Mountain Number(数位dp)
- FZU 2109 Mountain Number (数位DP)
- 【数位DP】模板+入门题HDU2089 FZU2109
- fzu 2109 Mountain Number 数位DP 记忆化搜索
- FZU 2109 Mountain Number [数位DP]【动态规划】
- fzu 2109 Mountain Number 数位DP 递推
- FZU 2109 Mountain Number_数位DP
- Round Number --数位DP
- hdu3652B-number (数位dp)
- 九度 OJ 1050:完数
- nginx-tomcat负载均衡redis-session共享,静态资源分离
- AndroidUI——InputFilter的使用(3)
- StringUtils工具类用法
- error LNK2001: unresolved external symbol __imp__closesocket@4解决
- FZU2109:Mountain Number(数位DP)
- VC++之 CreateEvent和SetEvent及WaitForSingleObject的用法
- suse 11设置开机自启动路由
- Google Eventbus优缺点
- 51nod1084 更难难得矩阵取数问题
- rtmp协议分析---HTTP_get分析
- centos 6.5 连接MySQL 提示:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password:
- Mac MAMP安装Memcached
- 树莓派 常用软件及配置(opencv安装,中文环境设置,VNC配置,截屏)