HDU 5898 (数位DP)

HDU 5898 odd-even number

题意:如果一个数连续的奇数之和为偶数,连续的偶数之和为奇数则满足条件,问某一区间内满足条件的数字的个数。

思路:数位DP,dp[i][j][k][u]表示计算到底i位,是否为临界值,上一位是奇还是偶,连续奇或偶有u个,在dfs是多记录一下该数前面是不是全为0以及该数是不是已近不满足条件。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long LL;
vector<int> v;
LL l,r;
LL dp[25][2][2][25];//第i位,是否为临界值,上一位是奇还是偶,连续或偶的个数
LL dfs(int pos,int pan,int status,int num,int ok1,int ok2){
    if(ok1) return 0;
    if(pos<0){
        if(ok2==0) return 0;
        if(status==1&&num%2==0)
            return 1;
        if(status==0&&num%2==1)
            return 1;
        return 0;
    }
    if(dp[pos][pan][status][num]!=-1)
        return dp[pos][pan][status][num];
    LL cnt=0;
    int k=pan?v[pos]:9;
    for(int i=0;i<=k;i++){
        int nowpan,nowstatus,nownum;
        int nowok1=0,nowok2=0;
        nowpan=(pan&&(i==k))?1:0;
        if(i==0){//该位为0
            if(ok2==0){//前面全是0
                nowok2=0;
                nownum=0;
                nowstatus=0;
            }
            else{//前面并非全是0
                nowok2=1;
                nowstatus=0;
                if(status==1){//前一个是奇,这个发生改变
                    nownum=1;
                    if(num%2==1)
                        nowok1=1;
                }
                else{//前一个是偶,这个继续
                    nownum=num+1;
                }
            }
        }
        else if(i%2==1){//非0且为奇
            nowok2=1;
            nowstatus=1;
            if(status==1){//前面为奇,继续
                nownum=num+1;
            }
            else{//前面为偶,发生改变
                nownum=1;
                if(num%2==0&&num!=0)
                    nowok1=1;
            }
        }
        else{//非0且为偶
            nowok2=1;
            nowstatus=0;
            if(status==1){//前面为奇,发生改变
                nownum=1;
                if(num%2==1&&num!=0)
                    nowok1=1;
            }
            else{//前面为偶,继续
                nownum=num+1;
            }
        }
        //printf("%d %d %d %d %d\n",nowpan,nowstatus,nownum,nowok1,nowok2);
        cnt+=dfs(pos-1,nowpan,nowstatus,nownum,nowok1,nowok2);
    }
    return dp[pos][pan][status][num]=cnt;
}
int main(){
    int t; cin>>t;
    int T=t;
    while(t--){
        cin>>l>>r;

        l--;
        v.clear();
        memset(dp,-1,sizeof(dp));
        while(l){
            v.push_back(l%10);
            l/=10;
        }
        int len=v.size()-1;
        LL a=dfs(len,1,1,0,0,0);

        v.clear();
        memset(dp,-1,sizeof(dp));
        while(r){
            v.push_back(r%10);
            r/=10;
        }
        len=v.size()-1;
        LL b=dfs(len,1,1,0,0,0);
        printf("Case #%d: ",T-t);
        cout<<b-a<<endl;
    }

    return 0;
}

Psong

时间: 2024-11-08 02:43:53

HDU 5898 (数位DP)的相关文章

2016 ACM/ICPC Asia Regional Shenyang Online 1007/HDU 5898 数位dp

odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 388    Accepted Submission(s): 212 Problem Description For a number,if the length of continuous odd digits is even and the length

hdu 4734 数位dp

http://acm.hdu.edu.cn/showproblem.php?pid=4734 Problem Description For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. Now you are given two numbers A and B, plea

hdu 4352 数位dp(最长上升子序列的长度为k的个数)

http://acm.hdu.edu.cn/showproblem.php?pid=4352 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefully reading the entire description is very important. As the strongest fighting force in UESTC, xhxj grew

hdu 3709 数位dp(小思维)

http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit.

hdu 4352 数位dp + 状态压缩

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2265    Accepted Submission(s): 927 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

hdu 4507 数位dp(求和,求平方和)

http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都讨厌! 吉哥观察了214和77这两个数,发现: 2+1+4=7 7+7=7*2 77=7*11 最终,他发现原来这一切归根到底都是因为和7有关!所以,他现在甚至讨厌一切和7有关的数! 什么样的数和7有关呢? 如果一个整数符合下面3个条件之一,那么我们就说

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6156 数位DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:如题. 解法:数位DP,暴力枚举进制之后,就转化成了求L,R区间的回文数的个数,这个直接做一个数位DP就好了.dp[jz][start][cur][state]表示jz进制下以start位起始到cur位状态为state(1表示已经回文,0表示没有回文)时回文数的个数. #include <bits/stdc++.h> using namespace std; typedef long

hdu 5179 数位dp

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5179 beautiful number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 198    Accepted Submission(s): 116 Problem Description Let A=∑ni=1ai?10n?i(1

HDU 4352 数位dp

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3081    Accepted Submission(s): 1291 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefu

HDU 3709 数位dp

Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 5276    Accepted Submission(s): 2523 Problem Description A balanced number is a non-negative integer that can be balanced if a pi