HDU 5898 odd-even number (数位DP) 2016 ACM/ICPC Asia Regional Shenyang Online

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5898




odd-even number

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


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




题目大意:一个数,每连续的奇数位以及连续的偶数位为一段,要求每一个奇数段(连续奇数组成的段)要有偶数个奇数,每一个偶数段(连续偶数组成的段)要有奇数个偶数,求区间[L,R]有多少个满足条件的。


解题思路:简单数位DP。



/* ***********************************************
┆  ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆  ┃ 勒 ┃  ┆      
┆  ┃ 戈 ┗━━━┓ ┆
┆  ┃ 壁     ┣┓┆
┆  ┃ 的草泥马  ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <bitset>
using namespace std;

#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
#define pb push_back
#define mp make_pair
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define all(a) a.begin(), a.end()
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")

int bit[20];
LL dp[20][20][2][2];


LL dfs(int pos,int num,bool e,bool isodd,bool z) //e上限,num个数,isodd是否是奇数
{
    if(!pos)
    {
        return ((num&1) != (isodd&1));
    }
    if(!e && dp[pos][num][isodd][z] != -1) return dp[pos][num][isodd][z];
    int digit = e ? bit[pos] : 9;
    LL ans = 0;
    for(int i=0;i<=digit;i++)
    {
        if(z==1)
        {
            if(i==0) ans += dfs(pos-1,1,e && digit==i,0,1);
            else ans += dfs(pos-1,1,e && digit==i,i&1,0);
        }
        else
        {
            if(isodd==(i&1))
            {
                ans += dfs(pos-1,num+1,e && digit==i,i&1,z);
            }
            else
            {
                if((num&1)== isodd)continue;
                ans += dfs(pos-1,1,e && digit==i,i&1,z);
            }
        }
    }
    if(!e) dp[pos][num][isodd][z] = ans;
    return ans;
}

LL solve(LL n)
{
    int len = 0;
    if(!n) return 1LL;
    while(n)
    {
        bit[++len] = n%10;
        n /= 10;
    }
    return dfs(len,0,1,0,1);
}


int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	memset(dp,-1,sizeof dp);
	LL L,R;
	int t;
	int cas = 1;
	t = read();
	while(t--)
    {
        scanf("%I64d%I64d",&L,&R);
        printf("Case #%d: %I64d\n",cas++,solve(R)-solve(L-1));
    }
	return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值