社区讨论

求佬帮忙看下,数位dp过了70分还有不知道哪里错了

P10424[蓝桥杯 2024 省 B] 好数参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mhjup3os
此快照首次捕获于
2025/11/04 08:48
4 个月前
此快照最后确认于
2025/11/04 08:48
4 个月前
查看原帖
CPP
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;

int n;
int dp[9][2][3];
//pre为上一位的奇偶性质 0 为奇 1为偶数 2为没有参与过
//free为是否自由选择 0 不自由 1自由
int f(int len,int offset,int free,int pre){
    if(len==0){
        return pre==2 ? 0 : 1;
    }
    if(dp[len][free][pre]!=-1){
        return dp[len][free][pre];
    }
    int ans=0;
    int cur = (n/offset)%10;
    if(free==0){
        if(pre==2){
            ans+=f(len-1,offset/10,1,2);
            for(int i=1;i<cur;i++){
                if(len%2==0){
                    if(i%2==0){
                        ans+=f(len-1,offset/10,1,1);
                    }
                }else{
                    if(i%2==1){
                        ans+=f(len-1,offset/10,1,0);
                    } 
                }
            }
            if(cur%2==0){
                ans+=f(len-1,offset/10,0,1);
            }else{
                ans+=f(len-1,offset/10,0,0);
            }
        }else{
            for(int i=0;i<cur;i++){
                if(pre==1){
                    if(i%2==1){
                        ans+=f(len-1,offset/10,1,0);
                    }
                }else{
                    if(i%2==0){
                        ans+=f(len-1,offset/10,1,1);
                    }
                }
            }
            if(pre==1){
                if(cur%2==1){
                    ans+=f(len-1,offset/10,0,0);
                }
            }else{
                if(cur%2==0){
                    ans+=f(len-1,offset/10,0,1);
                }
            }
        }
    }else{
        if(pre==2){
            ans+=f(len-1,offset/10,1,2);
            for(int i=1;i<=9;i++){
                if(len%2==0){
                    if(i%2==0){
                        ans+=f(len-1,offset/10,1,1);
                    }
                }else{
                    if(i%2==1){
                        ans+=f(len-1,offset/10,1,0);
                    }
                }
            }
        }else{
            for(int i=0;i<=9;i++){
                if(pre==1){
                    if(i%2==1){
                        ans+=f(len-1,offset/10,1,0);
                    }
                }else{
                    if(i%2==0){
                        ans+=f(len-1,offset/10,1,1);
                    }
                }
            }
        }
    }
    dp[len][free][pre]=ans;
    return ans;
}
signed main(){ 
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    int tmp = n;
    int len=0;
    int offset=1;
    while(tmp!=0){
        len++;
        offset*=10;
        tmp/=10;
    }
    for(int i=0;i<=len;i++){
        for(int j=0;j<2;j++){
            for(int k=0;k<3;k++){
                dp[i][j][k]=-1;
            }
        }
    }
    offset/=10;
    int ans = f(len,offset,0,2);
    cout<<ans;
    return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...