• hdu 5898 odd-even number 数位DP


    传送门:hdu 5898 odd-even number

    思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze]

    pos:当前处理的位

    pre:上一位的奇偶性

    status:截止到上一位的连续段的奇偶性

    ze:是否有前导0

    /**************************************************************
        Problem:hdu 5898 odd-even number
        User: youmi
        Language: C++
        Result: Accepted
        Time:0MS
        Memory:1580K
    ****************************************************************/
    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    //#include<bits/stdc++.h>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <map>
    #include <stack>
    #include <set>
    #include <sstream>
    #include <cmath>
    #include <queue>
    #include <deque>
    #include <string>
    #include <vector>
    #define zeros(a) memset(a,0,sizeof(a))
    #define ones(a) memset(a,-1,sizeof(a))
    #define sc(a) scanf("%d",&a)
    #define sc2(a,b) scanf("%d%d",&a,&b)
    #define sc3(a,b,c) scanf("%d%d%d",&a,&b,&c)
    #define scs(a) scanf("%s",a)
    #define sclld(a) scanf("%I64d",&a)
    #define pt(a) printf("%d
    ",a)
    #define ptlld(a) printf("%I64d
    ",a)
    #define rep(i,from,to) for(int i=from;i<=to;i++)
    #define irep(i,to,from) for(int i=to;i>=from;i--)
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    #define lson (step<<1)
    #define rson (lson+1)
    #define eps 1e-6
    #define oo 0x3fffffff
    #define TEST cout<<"*************************"<<endl
    const double pi=4*atan(1.0);
    
    using namespace std;
    typedef long long ll;
    template <class T> inline void read(T &n)
    {
        char c; int flag = 1;
        for (c = getchar(); !(c >= '0' && c <= '9' || c == '-'); c = getchar()); if (c == '-') flag = -1, n = 0; else n = c - '0';
        for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) n = n * 10 + c - '0'; n *= flag;
    }
    ll Pow(ll base, ll n, ll mo)
    {
        ll res=1;
        while(n)
        {
            if(n&1)
                res=res*base%mo;
            n>>=1;
            base=base*base%mo;
        }
        return res;
    }
    //***************************
    
    int n;
    const int maxn=100000+10;
    const ll mod=1000000007;
    int digit[30];
    ll dp[30][2][2][2];
    int tot=0;
    ll dfs(int pos,int pre,int status,int ze,int limit)
    {
        if(pos<0)
        {
            if(pre%2==1&&status%2==0)
                return 1;
            else if(pre%2==0&&status%2==1)
                return 1;
            else
                return 0;
        }
        if(!limit&&dp[pos][pre][status][ze]!=-1)
            return dp[pos][pre][status][ze];
        int ed=limit?digit[pos]:9;
        ll res=0;
        if(ze)
            res+=dfs(pos-1,0,0,1,limit&&(0==ed));
        else
        {
            if(pre%2==0)
                res+=dfs(pos-1,0,status^1,ze,limit&&(0==ed));
            else if(pre%2==1&&status==0)
                res+=dfs(pos-1,0,1,ze,limit&&(0==ed));
        }
        rep(i,1,ed)
        {
            if(i%2&&pre)
                res+=dfs(pos-1,1,status^1,0,limit&&(i==ed));
            else if(i%2==0&&!pre)
                res+=dfs(pos-1,0,status^1,0,limit&&(i==ed));
            else if(i%2&&!pre&&(status==1||ze))
                res+=dfs(pos-1,1,1,0,limit&&(i==ed));
            else if(i%2==0&&pre&&(status==0||ze))
                res+=dfs(pos-1,0,1,0,limit&&(i==ed));
        }
        if(!limit)
            dp[pos][pre][status][ze]=res;
        return res;
    }
    void work(ll num)
    {
        tot=0;
        while(num)
        {
            digit[tot++]=num%10;
            num/=10;
        }
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        int T_T;
        scanf("%d",&T_T);
        for(int kase=1;kase<=T_T;kase++)
        {
            printf("Case #%d: ",kase);
            ll num;
            sclld(num);
            work(num-1);
            ones(dp);
            ll temp=dfs(tot-1,0,0,1,1);
            sclld(num);
            work(num);
            ones(dp);
            ptlld(dfs(tot-1,0,0,1,1)-temp);
        }
        return 0;
    }
    不为失败找借口,只为成功找方法
  • 相关阅读:
    Qt编写控件属性设计器12-用户属性
    C#中通过三边长判断三角形类型(三角形测试用例)
    C#中通过Selenium定位<a>标签的问题
    SharePoint自动化系列——Manage "Site Subscriptions" using PowerShell
    SharePoint API测试系列——Records.BypassLocks测试
    SharePoint API测试系列——对Recorded Item做OM操作(委托的妙用)
    放松时刻——C#分割字符串
    链表——PowerShell版
    栈——PowerShell版
    队列——PowerShell版
  • 原文地址:https://www.cnblogs.com/youmi/p/5910526.html
Copyright © 2020-2023  润新知