您的位置:首页 > 其它

FZU 2109 奇偶

2017-07-24 22:24 253 查看
传送门

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


要求出区间内偶数为大于奇数位的数字的个数dp[i][j][k] 表示在第i个位置时,前面是j,现在这位是奇数位还是偶数位的数目

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
#define me(x,y) memset(x,y,sizeof(x));
#define LL long long
typedef long long ll;
int a[25];
int dp[25][15][4];

int dfs(int pos,int pre,int sta,int lead,int limit)//pos 数位 pre 上一个数
//sta 奇偶 lead前导0
{
int mod_x,sta_x;
if(pos<=0) return 1;
// if(sta<0) return 0;
if(!limit && dp[pos][pre][sta]!=-1) return dp[pos][pre][sta];
int up=limit ? a[pos] : 9;
int ans=0;

for(int i=0;i<=up;i++)
{
if(!(i||lead)) ans+=dfs(pos-1,9,0,lead||i,limit&&i==up);
else if(sta&&pre<=i) ans+=dfs(pos-1,i,!sta,lead||i,limit&&i==up);
else if(!sta&&pre>=i) ans+=dfs(pos-1,i,!sta,lead||i,limit&&i==up);
}
if(!limit) dp[pos][pre][sta]=ans;
return ans;
}

int solve(int x)
{
int pos=0;//
while(x)
{
a[++pos]=x%10;
x/=10;
}
return dfs(pos,9,0,0,1);//在最前弄一个9
}
int main()
{
int t;
int l,r;
cin>>t;
while(t--)
{
cin>>l>>r;
//while(~scanf("%d%d",&l,&r)&&(l+r))
//while(~scanf("%d",&r))
me(dp,-1);
printf("%d\n",solve(r)-solve(l-1));
}
return 0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: