Digits Parade(dp)

题目描述
Given is a string S. Each character in S is either a digit (0, …, 9) or ?.
Among the integers obtained by replacing each occurrence of ? with a digit, how many have a remainder of 5 when divided by 13? An integer may begin with 0.
Since the answer can be enormous, print the count modulo 109+7.

Constraints
·S is a string consisting of digits (0, …, 9) and ?.
·1≤|S|≤105

输入
Input is given from Standard Input in the following format:

S

输出
Print the number of integers satisfying the condition, modulo 109+7.

样例输入
【样例1】
??2??5
【样例2】
?44
【样例3】
7?4
【样例4】
?6?42???8??2??06243???9??3???7258??5??7???774???4?1??17???9?5?70???76???

样例输出
【样例1】
768
【样例2】
1
【样例3】
0
【样例4】
153716888

提示
样例1解释:For example, 482305,002865, and 972665 satisfy the condition.
样例2解释:Only 044 satisfies the condition.
样例3解释:We may not be able to produce an integer satisfying the condition.

思路
根据mod的性质,可以推出dp递推公式dp[i][(j*10+t)%13]=(dp[i][(j*10+t)%13]+dp[i-1][j])%mod;

代码实现

#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e5+5;
const int M=10005;
const int INF=0x3f3f3f;
const ull sed=31;
const ll mod=1e9+7;
const double eps=1e-8;
const double PI=acos(-1.0);
typedef pair<int,int>P;
 
char s[N];
ll dp[N][20];
 
int main()
{
    scanf("%s",s+1);
    int len=strlen(s+1);
    dp[0][0]=1;
    for(int i=1;i<=len;i++)
    {
        if(isdigit(s[i]))
        {
            int t=s[i]-'0';
            for(int j=0;j<13;j++) dp[i][(j*10+t)%13]=(dp[i][(j*10+t)%13]+dp[i-1][j])%mod;
        }
        else
        {
            for(int t=0;t<10;t++)
                for(int j=0;j<13;j++)
                    dp[i][(j*10+t)%13]=(dp[i][(j*10+t)%13]+dp[i-1][j])%mod;
        }
    }
    printf("%lld\n",dp[len][5]);
    return 0;
}
发布了235 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43935894/article/details/104217139