hdu 5898 odd-even number


odd-even number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 84    Accepted Submission(s): 39


Problem Description
For a number,if the length of continuous odd digits is even and the length of continuous even digits is odd,we call it odd-even number.Now we want to know the amount of odd-even number between L,R(1<=L<=R<= 9*10^18).
 

Input
First line a t,then t cases.every line contains two integers L and R.
 

Output
Print the output for each case on one line in the format as shown below.
 

Sample Input
  
  
2 1 100 110 220
 

Sample Output
  
  
Case #1: 29 Case #2: 36
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5901  5900  5899  5897  5896 
题意:求l到r的数里面,有多少数符合奇数连续的数位长度是偶数,偶数连续的数位长度是奇数。

思路:很明显就是一个数位dp,我用dp[i][j]表示第i位数前一位数的状态是j。状态有4种,奇数长度为奇,奇数长度为偶,偶数长度为奇,偶数长度为偶,我外加一个状态0表示前面全是前导0,接下来的就是基本的数位dp思想,不多说了,下面给代码:

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
typedef long long LL;
using namespace std;
#define inf 0x3f3f3f3f
#define maxn 55
typedef long long LL;
int num[25];
LL dp[25][5];
LL dfs(int pos, int limit, int status){
	if (pos < 1){
		if (status == 2 || status == 3)
			return 1;
		else
			return 0;
	}
	if (!limit&&~dp[pos][status])
		return dp[pos][status];
	int end = limit ? num[pos] : 9;
	LL ans = 0;
	for (int i = 0; i <= end; i++){
		if (!status){
			if (!i){
				ans += dfs(pos - 1, 0, 0);
			}
			else if (i & 1){
				ans += dfs(pos - 1, limit&&i == end, 1);
			}
			else{
				ans += dfs(pos - 1, limit&&i == end, 3);
			}
		}
		else{
			if (status == 1){
				if (i & 1){
					ans += dfs(pos - 1, limit&&i == end, 2);
				}
			}
			else if (status == 2){
				if (i & 1){
					ans += dfs(pos - 1, limit&&i == end, 1);
				}
				else{
					ans += dfs(pos - 1, limit&&i == end, 3);
				}
			}
			else if (status == 3){
				if (i & 1){
					ans += dfs(pos - 1, limit&&i == end, 1);
				}
				else{
					ans += dfs(pos - 1, limit&&i == end, 4);
				}
			}
			else{
				if (!(i & 1)){
					ans += dfs(pos - 1, limit&&i == end, 3);
				}
			}
		}
	}
	dp[pos][status] = ans;
	return ans;
}
LL solve(LL x){
	memset(dp, -1, sizeof(dp));
	int len = 0;
	while (x){
		num[++len] = x % 10;
		x /= 10;
	}
	return dfs(len, 1, 0);
}
int main(){
	int t;
	scanf("%d", &t);
	for (int tcase = 1; tcase <= t; tcase++){
		LL l, r;
		scanf("%lld%lld", &l, &r);
		printf("Case #%d: %lld\n", tcase, solve(r) - solve(l - 1));
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值