abc135D 模13余5的数的个数
给定一个由0~9以及?组成的字符串,其中的?可以替换成0~9
中任意1个数字,问有多少种情况使得这个数字模13的余数为5?结果对1e9+7
取模。注意允许s有前导0。
1<=|s|<=1e5
记dp[i][j]表示前i个数字构成的数,模13余j的方案数。如果s[i]是数字,直接转移;如果是问题,枚举0~9
所有可能分别枚举,总时间复杂度O(13n)。这里使用的刷表法。
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i,a,b) for(int i=a;i<=b;i++) #define per(i,a,b) for(int i=b;i>=a;i--) template<int MOD> struct MInt { int x; int norm(int u) const {u%=MOD; if(u<0) u+=MOD; return u;} MInt(int v=0):x(norm(v)) {} int val() const {return x;} MInt operator-() const {return MInt(norm(MOD-x));} MInt inv() const {assert(x!=0); return power(MOD-2);} MInt &operator*=(const MInt &o) {x=norm(x*o.x); return *this;} MInt &operator+=(const MInt &o) {x=norm(x+o.x); return *this;} MInt &operator-=(const MInt &o) {x=norm(x-o.x); return *this;} MInt &operator/=(const MInt &o) {*this *= o.inv(); return *this;} friend MInt operator*(const MInt &a, const MInt &b) {MInt ans=a; ans*=b; return ans;} friend MInt operator+(const MInt &a, const MInt &b) {MInt ans=a; ans+=b; return ans;} friend MInt operator-(const MInt &a, const MInt &b) {MInt ans=a; ans-=b; return ans;} friend MInt operator/(const MInt &a, const MInt &b) {MInt ans=a; ans/=b; return ans;} friend std::istream &operator>>(std::istream &is, MInt &a) {int u; is>>u; a=MInt(u); return is;} friend std::ostream &operator<<(std::ostream &os, const MInt &a) {os<<a.val(); return os;} MInt power(int b) const {int r=1, t=x; while(b){if(b&1) r=r*t%MOD; t=t*t%MOD; b/=2;} return MInt(r);} }; using mint = MInt<1000000007>; const int N = 100005; mint dp[N][13]; string s; void solve() { cin >> s; int n = s.size(); dp[0][0] = 1; rep(i,0,n-1) rep(j,0,12) { if (s[i] == '?') { rep(k,0,9) { int u = (j * 10 + k) % 13; dp[i+1][u] += dp[i][j]; } } else { int k = (j * 10 + s[i] - '0') % 13; dp[i+1][k] += dp[i][j]; } } cout << dp[n][5] << "\n"; } signed main() { cin.tie(0)->sync_with_stdio(0); int t = 1; while (t--) solve(); return 0; }
标签:
线性dp
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· DeepSeek本地性能调优
· 一文掌握DeepSeek本地部署+Page Assist浏览器插件+C#接口调用+局域网访问!全攻略