G - odd-even number HDU - 5898 数位dp

odd-even number

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


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

题意是区间满足要求的数的个数

要求是数位的连续奇数的长度要求是偶数,数位上连续偶数的长度要求是奇数

很明显的数位dp,

flog记录前一位是奇数还是偶数,len记录已经有多长了。

注意要记录前导零,因为前导零会影响连续偶数的长度

#include<bits/stdc++.h>
using namespace std;
long long dp[2][100];
int a[100];
long long dfs(int pos,int flog,int len,int state,int limit,int zero)
{
	if(pos==-1)
	{
		return state;
	}
	if(dp[flog][pos]!=-1&&state&&!limit)
	return dp[flog][pos];
	int up=limit?a[pos]:9;
	long long ans=0;
	for(int i=0;i<=up;i++)
	{
		if(zero&&i==0)
		{
			ans+=dfs(pos-1,1,0,0,limit&&a[pos]==0,zero);
		}
		else if(flog&&(len&1))
		{
			if(i&1)
			{
				ans+=dfs(pos-1,flog,(len+1)%2,1,limit&&i==a[pos],0);
			}
			else
			continue;
		}
		else if(!flog&&!(len&1))
		{
			if(!(i&1))
			{
				ans+=dfs(pos-1,flog,(len+1)%2,1,limit&&i==a[pos],0);
			}
			else 
			continue;
		}
		else
		{
			if(flog)
			{
				if(i&1)
				{
					ans+=dfs(pos-1,flog,(len+1)%2,0,limit&&i==a[pos],0);
				}
				else
				{
					ans+=dfs(pos-1,0,1,1,limit&&i==a[pos],0);
				}
			}
			else
			{
				if(i&1)
				{
					ans+=dfs(pos-1,1,1,0,limit&&i==a[pos],0);
				}
				else
				{
					ans+=dfs(pos-1,0,(len+1)%2,0,limit&&i==a[pos],0);
				}
			}
		}
	}
	if(!limit&&state&&dp[flog][pos]==-1)
	dp[flog][pos]=ans;
	return ans;
}
long long solve(long long x)
{
	int pos=0;
	while(x)
	{
		a[pos++]=x%10;
		x/=10;
	}
	return dfs(pos-1,1,0,0,1,1);
}
int main()
{
	memset(dp,-1,sizeof(dp));
	int t;
	long long x,y;
	cin>>t;
	for(int j=1;j<=t;j++)
	{
		scanf("%lld %lld",&x,&y);
		printf("Case #%d: %lld\n",j,solve(y)-solve(x-1));
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值