hdu5898odd-even number

来源:互联网 发布:搞笑词语网络流行语言 编辑:程序博客网 时间:2024/06/02 06:29

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5898

题意:求L~R之间有多少个数满足条件。条件为:奇数的长度为偶数,偶数的长度为奇数,如132(13为奇数长度为2,2为偶数长度为1)如235(2为偶数长度为1,35为奇数长度为2)。

分析:简单的数位dp,自己处理好细节就行了。

代码:

#include<map>#include<set>#include<cmath>#include<queue>#include<bitset>#include<math.h>#include<vector>#include<string>#include<stdio.h>#include<cstring>#include<iostream>#include<algorithm>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;const int N=40010;const int M=50010;const int mod=1000000007;const int MOD1=1000000007;const int MOD2=1000000009;const double EPS=0.00000001;typedef long long ll;const ll MOD=1000000007;const int INF=1000000010;const ll MAX=1ll<<55;const double eps=1e-5;const double inf=~0u>>1;const double pi=acos(-1.0);typedef double db;typedef unsigned int uint;typedef unsigned long long ull;int d[25],q[25][11][2];ll dp[25][11][2];ll dfs(int len,int pre,int odd,int bo) {    if (len==0) {        if (pre==10) return 0;        if ((pre&1)==odd) return 0;        else return 1;    }    if (bo&&q[len][pre][odd]) return dp[len][pre][odd];    if (bo) {        ll &ret=dp[len][pre][odd];        q[len][pre][odd]=1;        if (pre==10) {            ret+=dfs(len-1,10,0,1);            for (int i=1;i<=9;i++) ret+=dfs(len-1,i,1,1);        } else {            for (int i=pre&1;i<=9;i+=2) ret+=dfs(len-1,i,odd^1,1);            if ((pre&1)!=odd) {                for (int i=pre&1^1;i<=9;i+=2) ret+=dfs(len-1,i,1,1);            }        }        return ret;    } else {        ll ret=0;        if (pre==10) {            ret+=dfs(len-1,10,0,1);            for (int i=1;i<d[len];i++) ret+=dfs(len-1,i,1,1);            ret+=dfs(len-1,d[len],1,0);        } else {            for (int i=pre&1;i<d[len];i+=2) ret+=dfs(len-1,i,odd^1,1);            if ((pre&1)!=odd) {                for (int i=pre&1^1;i<d[len];i+=2)ret+=dfs(len-1,i,1,1);            }            if ((pre&1)==(d[len]&1)) ret+=dfs(len-1,d[len],odd^1,0);            else if ((pre&1)!=odd) ret+=dfs(len-1,d[len],1,0);        }        return ret;    }}ll solve(ll x) {    int len=0;    while (x) d[++len]=x%10,x/=10;    return dfs(len,10,0,0);}int main(){    int ca,T;    ll l,r;    scanf("%d", &T);    memset(q,0,sizeof(q));    memset(dp,0,sizeof(dp));    for (ca=1;ca<=T;ca++) {        scanf("%lld%lld", &l, &r);        printf("Case #%d: %lld\n", ca, solve(r)-solve(l-1));    }    return 0;}


0 0
原创粉丝点击