H - Mountain Number FZU - 2109(前导0)

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<cstdio>
#include<cstring>
#define bug(x) printf("%d***\n",x)
using namespace std;

const int maxn=100;
int dp[10][10][2]; 
int num[20];

int dfs(int odd,int pre,int limit,int pos){
	if(pos==-1)	return 1;
	if(!limit&&dp[pos][pre][odd]!=-1) return dp[pos][pre][odd];
	int ed=limit?num[pos]:9;
	int ans=0;
	for(int i=0;i<=ed;i++){
		if(odd&&i>=pre){
			ans+=dfs(odd^1,i,limit&&i==ed,pos-1);
		}
		if(!odd&&i<=pre)
			ans+=dfs(odd^1,i,limit&&i==ed,pos-1);
	}
	if(!limit)
		dp[pos][pre][odd]=ans;
	return ans;
}

int solve(int n){
	if(n<0)return 0;
	if(n==0) return 1;
	int cnt=0;
	while(n){
		num[cnt++]=n%10;
		n/=10;
	}
	return dfs(0,10,1,cnt-1);
}

void init(){
	memset(dp,-1,sizeof(dp));
	solve(1e9);
}
int main(){
	init();
	int T;
	scanf("%d",&T);
	while(T--){
		int ans=0;
		int st,ed;
		scanf("%d %d",&st,&ed); 
		int ans1=solve(ed);
		int ans2=solve(st-1);
		printf("%d\n",ans1-ans2); 
	}
    return 0;
}
#include<cstdio>
#include<cstring>
#define bug(x) printf("%d***\n",x)
using namespace std;

const int maxn=100;
int dp[10][10][2]; 
int num[20];
/*
还是差一点吧
一个是状态,知道是pre,不敢想另一个是奇偶,但是状态不压缩的话
肯定就会超时,只要我们不重不漏的写完,肯定就是对的了 
*/

int dfs(int odd,int zero,int pre,int limit,int pos){
	if(pos==-1)	return 1;
	if(!limit&&dp[pos][pre][odd]!=-1) return dp[pos][pre][odd];
	int ed=limit?num[pos]:9;
	int ans=0;
	for(int i=0;i<=ed;i++){
		if(zero&&!i){//如果之前是0,当前位置也是0 
			ans+=dfs(0,1,10,0,pos-1); 
		}
		else if(odd&&i>=pre){
			ans+=dfs(odd^1,0,i,limit&&i==ed,pos-1);
		}
		else if(!odd&&i<=pre)
			ans+=dfs(odd^1,0,i,limit&&i==ed,pos-1);
	}
	if(!limit)
		dp[pos][pre][odd]=ans;
	return ans;
}

int solve(int n){
	if(n<0)return 0;
	if(n==0) return 1;
	int cnt=0;
	while(n){
		num[cnt++]=n%10;
		n/=10;
	}
	return dfs(0,1,10,1,cnt-1);
}

void init(){
	memset(dp,-1,sizeof(dp));
	solve(1e9);
}
int main(){
	init();
	int T;
	scanf("%d",&T);
	while(T--){
		int ans=0;
		int st,ed;
		scanf("%d %d",&st,&ed); 
		int ans1=solve(ed);
		int ans2=solve(st-1);
		printf("%d\n",ans1-ans2); 
	}
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值