2025/2/20

关于动态规划处理模数问题

两层dp,代表前i个字符,模数为j的方案数有几个

#include<bits/stdc++.h>
#define int long long
#define x first
#define y second
#define endl '\n'
#define pq priority_queue
using namespace std;
typedef pair<int,int> pii;
const int mod = 1e9 + 7;

void solve(){
    string s;
    cin>>s;
    int n=s.size();
    s=" "+s;
    vector<vector<int>>dp(n+1,vector<int>(13,0));
    dp[0][0]=1;
    for(int i=0;i<n;i++){
        for(int j=0;j<=12;j++){
            if(s[i+1]=='0'){
                dp[i+1][j*10%13]+=dp[i][j];
            }
            else if(s[i+1]=='1'){
                dp[i+1][(j*10+1)%13]+=dp[i][j];
               
            }else if(s[i+1]=='?'){
                dp[i+1][j*10%13]+=dp[i][j];
                dp[i+1][(j*10+1)%13]+=dp[i][j];
            }
            dp[i+1][j*10%13]%=mod;
            dp[i+1][(j*10+1)%13]%=mod;
        }
    }
    cout<<dp[n][0]<<'\n';
}


signed main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t = 1;
    while(t--)
    {
        solve();
    }
    return 0;
}
  

 

posted on 2025-02-20 20:59  临江柔  阅读(11)  评论(0)    收藏  举报