Problem:


Score : 400 points

Problem Statement

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 10^9+7.

Constraints

S is a string consisting of digits (0, ..., 9) and ?.

1 \leq |S| \leq 10^5

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of integers satisfying the condition, modulo 10^9+7.

Sample Input 1

??2??5

Sample Output 1

768

For example, 482305, 002865, and 972665 satisfy the condition.

Sample Input 2

?44

Sample Output 2

1

Only 044 satisfies the condition.

Sample Input 3

7?4

Sample Output 3

0

We may not be able to produce an integer satisfying the condition.

Sample Input 4

?6?42???8??2??06243????9??3???7258??5??7???????774????4?1??17???9?5?70???76???

Sample Output 4

153716888



Sample input:
??2??5


Sample output:
768

Submission status: Accepted

Solution code:
import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

S = input()

dp = [[0] * 13 for _ in range(len(S))]
mod = 10 ** 9 + 7

if S[0] == '?':
    for i in range(10):
        dp[0][i] = 1
else:
    dp[0][int(S[0])] = 1

for i in range(1, len(S)):
    if S[i] == '?':
        for j in range(13):
            for k in range(10):
                dp[i][(j * 10 + k) % 13] += dp[i-1][j]
                dp[i][(j + 10 + k) % 13] %= mod
    else:
        for j in range(13):
            dp[i][(j * 10 + int(S[i])) % 13] += dp[i-1][j]
            dp[i][(j * 10 + int(S[i])) % 13] %= mod
print(dp[len(S) - 1][5])

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline
geta = lambda fn: map(fn, readline().split())
gete = lambda fn: fn(input())

# sys.setrecursionlimit(10**5)


def main():
    S = gete(str)[::-1]
    m, mod = 13, 10**9 + 7
    prev, curr = [0] * m, [0] * m
    base = []

    s0, b = 0, 1
    for i, c in enumerate(S):
        if c == "?":
            base.append(b)
        else:
            s0 = (s0 + b * int(c)) % m
        b = (b * 10) % m
    curr[s0] = 1

    for b in base:
        prev, curr = curr, [0] * m

        for d in range(10):
            ad = d * b
            for r, p in enumerate(prev):
                curr[(r + ad) % m] += p

            for r, c in enumerate(curr):
                curr[r] = c % mod

    print(curr[5])


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline
geta = lambda fn: map(fn, readline().split())
gete = lambda fn: fn(input())

# sys.setrecursionlimit(10**5)


def main():
    S=gete(str)[::-1]
    m=13
    mod=10**9 + 7
    prev, curr=[0]*m, [0]*m
    base,  wild=[], []

    s0, b=0, 1
    for i, c in enumerate(S):
        base.append(b)
        if c == "?":
            wild.append(i)
        else:
            s0=(s0 + b * int(c)) % m
        b=(b * 10) % m

    curr[s0]=1

    for wi in wild:
        prev, curr=curr, [0]*m

        for d in range(10):
            ad=d*base[wi]
            for r, p in enumerate(prev):
                curr[(r + ad) % m] += p

            for r, c in enumerate(curr):
                curr[r]=c % mod

    print(curr[5])


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
M = (10**9+7)
s = list(input())[::-1]

mo = []
fixed = 0
for i in range(len(s)):
  if s[i]!='?':
    t = int(s[i])
    fixed += (t*pow(10,i,13))
    fixed %= 13
  else:
    t = []
    for j in range(10):
      t.append((j*pow(10,i,13)%13))
    mo.append(tuple(t))

if len(mo)==0:
  if fixed == 5:
    print(1)
  else:
    print(0)
  exit()
  
dp = [[-1]*13 for _ in range(len(mo))]

for m in mo[0]:
  dp[0][m] = 1
for i in range(len(mo)-1):
  for j in range(13):
    if dp[i][j]==-1:
      continue
    dp_i = dp[i+1]
    dp_j = dp[i]
    for m in mo[i+1]:
      p = (j+m)%13
      if dp_i[p] == -1:
        dp_i[p] = 0
      dp_i[p] += dp_j[j]
      dp_i[p]  %= M

ans = dp[-1][(18-fixed)%13] % M 
if dp[-1][(18-fixed)%13] == -1:
  print(0)
  exit()
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
mod = 10**9 + 7

s = input().rstrip()
n = len(s)
s = s[::-1]

dp = [[0]*13 for _ in range(n + 1)] # dp[i][j] : i桁目まで見て13で割ってj余る個数
dp[0][0] = 1
mul = 1
for i in range(n):
    if i > 0:
        mul *= 10
        mul %= 13
    if s[i] != '?':
        s_ = int(s[i]) * mul
        for j in range(13):
            dp[i + 1][(j + s_) % 13] += dp[i][j]
            dp[i + 1][(j + s_) % 13] %= mod
    else:
        for j in range(13):
            for k in range(10):
                k *= mul
                dp[i + 1][(j + k) % 13] += dp[i][j]
                dp[i + 1][(j + k) % 13] %= mod
print(dp[-1][5])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

S = list(input()[:-1])
S.reverse()
dp = [[0]*13 for _ in range(len(S)+1)]
dp[0][0] = 1
MOD = 10**9+7

for i in range(len(S)):
    p = pow(10, i, 13)
    
    if S[i]=='?':
        for j in range(10):
            for k in range(13):
                dp[i+1][(k+j*p)%13] += dp[i][k]
                dp[i+1][(k+j*p)%13] %= MOD
    else:
        for j in range(13):
            dp[i+1][(j+int(S[i])*p)%13] += dp[i][j]
            dp[i+1][(j+int(S[i])*p)%13] %= MOD

print(dp[len(S)][5])

Submission status: Accepted

Solution code:
s=input()
lim=10**9+7
dp = [[0 for _ in range(13)] for _ in range(len(s))]
if s[0] =="?":
    for i in range(10):
        dp[0][i] = 1
else:
    dp[0][int(s[0])] = 1

for i in range(1,len(s)):
    for j in range(13):
        if s[i] ==  "?":
            for k in range(10):
                dp[i][(j*10 + k)%13] = (dp[i][(j*10 + k)%13]+dp[i-1][j])%lim
        else:
            dp[i][(j*10 + int(s[i]))%13] = (dp[i][(j*10 + int(s[i]))%13]+dp[i-1][j])%lim
print(dp[-1][5])

Submission status: Accepted

Solution code:





S = input()
N = len(S)
MOD = 10**9 + 7

# dp[i+1][j]: i桁目まで見たとき、j=1でSより厳密に小さく、13で割った時あまりがjになる個数
dp = [[0 for _ in range(13)] for _ in range(N+1)]
dp[0][0] = 1

"""
遷移
dp[i][j] -> dp[i+1][j+m] : i-1まで見た時あまりがkになる数字について、末尾にS[i]が追加されると、i-1までのあまりは10倍になるから、10*j+mを13で割ったあまりが新たなあまり
: S[i]が？の場合可能
dp[i][j] -> dp[i+1][k]   : S[i]もS[i-1]も？ではない

"""

for i in range(N):
    for j in range(13):
        if S[i] == "?":
            # ?に0~9がいれられる
            for num in range(10):
                dp[i+1][(10*j + num) % 13] += dp[i][j]
                dp[i+1][(10*j + num) % 13] %= MOD
        else:
            # i桁目の値は固定
            dp[i+1][(10*j + int(S[i])) % 13] += dp[i][j]
            dp[i+1][(10*j + int(S[i])) % 13] %= MOD


print(dp[-1][5])

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    MOD = 10 ** 9 + 7
    S = rl().rstrip()
    
    N = len(S)
    dp = [[0 for _ in range(13)] for _ in range(N + 1)]
    dp[0][0] = 1
    
    for i in range(N):
        if S[i] == '?':
            for j in range(10):
                for k in range(13):
                    dp[i + 1][(k * 10 + j) % 13] += dp[i][k] % MOD
        else:
            num = int(S[i])
            for j in range(13):
                dp[i + 1][(j * 10 + num) % 13] += dp[i][j] % MOD
    print(dp[N][5] % MOD)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
def solve():
    S = input()
    N = len(S)
    MOD = 10 ** 9 + 7

    dp = [[0] * 13 for _ in range(N+1)]
    dp[0][0] = 1
    for i in range(N):
        for j in range(13):
            if S[i] != '?':
                dp[i+1][(10 * j + int(S[i])) % 13] += dp[i][j] % MOD
            else:
                for k in range(0,10):
                    dp[i+1][(10 * j + k) % 13] += dp[i][j] % MOD
    
    print(dp[-1][5] % MOD)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
mod = 10**9+7
def main():
    s = list(input())
    n = len(s)
    dp = [[0 for _ in range(13)] for _ in range(n+1)]
    dp[0][0] = 1
    for i in range(n):
        if s[i] != '?':
            s[i] = int(s[i])
            for k in range(13):
                index = (k*10+s[i])%13
                dp[i+1][index] += dp[i][k]
        else:
            for j in range(10):
                for k in range(13):
                    index = (k*10+j)%13
                    dp[i+1][index] += dp[i][k]
        for j in range(13):
            dp[i+1][j] %= mod
    print(dp[n][5])

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
MOD = pow(10,9)+7
S = input()[::-1]
N = len(S)

DP = [[0]*13 for i in range(N)]
K = 1
for i in range(N):
    if S[i]=="?":
        for j in range(10):
            if i==0:
                DP[0][(j*K)%13] = 1
            else:
                for k in range(13):
                    DP[i][(j*K+k)%13] += DP[i-1][k]
    else:
        j = int(S[i])*K
        if i==0:
            DP[0][(j*K)%13] = 1
        else:
            for k in range(13):
                DP[i][(j+k)%13] += DP[i-1][k]
    K = (K*10)%13
    for j in range(13):
        DP[i][j] = DP[i][j]%MOD
print(DP[N-1][5]%MOD)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
MOD = 10 ** 9 + 7

def main():
    S = readline().rstrip()
    N = 13
    dp = [0] * 13  # dp[k]: 13で割った余りがkのパターン数
    dp[0] = 1
    
    mul = 1
    for s in S[::-1]:
        next_dp = [0] * N
        if s == '?':
            for k in range(10):
                for j in range(N):
                    next_dp[(k * mul + j) % N] += dp[j]
                    next_dp[(k * mul + j) % N] %= MOD
        else:
            k = int(s)
            for j in range(N):
                next_dp[(k * mul + j) % N] += dp[j]
                next_dp[(k * mul + j) % N] %= MOD
        mul *= 10
        mul %= N
        dp = next_dp

    print(dp[5])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict as dd
s = input()
#dp[keta][amari] = pattern_num
dp = dd(lambda: dd(lambda: 0))
nums = list(range(0, 10))

for keta in range(1, len(s)+1):
    digit = s[keta-1]
    if digit != "?":
        digit = int(digit)
        if keta == 1:
            dp[keta][digit] = 1
        else:
            for amari, val in dp[keta-1].items():
                dp[keta][(amari * 10 + digit) % 13] += val
                dp[keta][(amari * 10 + digit) % 13] %= 10**9 + 7

    else:
        if keta == 1:
            for digit in nums:
                dp[keta][digit] = 1
        else:
            for digit in nums:
                for amari, val in dp[keta-1].items():
                    dp[keta][(amari * 10 + digit) % 13] += val
                    dp[keta][(amari * 10 + digit) % 13] %= 10**9 + 7

print(dp[len(s)][5] % (10**9 + 7))

Submission status: Accepted

Solution code:
S = input()
N = len(S)
mod = 10**9+7
P = 13
 
dp = [[0 for _ in range(P)] for _ in range(N+1)]
dp[0][0] = 1
 
for i in range(N):
    x = S[-1-i]
    if x == '?':
        base = pow(10, i, P)
        for j in range(10):
            r = base*j % P
            for m in range(P):
                dp[i+1][(m+r)%P] = (dp[i+1][(m+r)%P] + dp[i][m]) % mod
    else:
        r = pow(10, i, P) * int(x) % P
        for j in range(P):
            dp[i+1][(j+r)%P] = max(dp[i+1][(j+r)%P], dp[i][j])
 
print(dp[-1][5])

Submission status: Accepted

Solution code:
S = input()
N = len(S)
mod = 10**9+7
P = 13
 
dp = [[0 for _ in range(P)] for _ in range(N)]
 
# 1桁目だけ事前計算
if S[N-1] == '?':
    tmp = [0 for _ in range(P)]
    for i in range(10):
        tmp[i % P] = 1
    dp[0] = tmp
else:
    dp[0][int(S[N-1]) % P] = 1
 
for i in range(1, N):
    k = N-i-1
    if S[k] == '?':
        base = pow(10, i, P)
        for j in range(10):
            r = base*j % P
            for m in range(P):
                dp[i][(m+r)%P] = (dp[i][(m+r)%P] + dp[i-1][m]) % mod
    else:
        r = pow(10, i, P) * int(S[k]) % P
        for j in range(P):
            dp[i][(j+r)%P] = max(dp[i][(j+r)%P], dp[i-1][j])
 
print(dp[-1][5])

Submission status: Accepted

Solution code:




S = input()
N = len(S)
MOD = 10**9 + 7
"""
dp[i+1][j] : Sのi文字目まで見たときに、13で割って余りがjになるものの個数

遷移
i-1まで見たときの余りがjの個数がdp[i][j]
iまで見るとi-1までの部分を13でわった余りも10倍されるから、10*j
数字の後ろにS[i]が追加されるので 10*j + S[i]になり、これを13で割った余ると
S[i][j] -> S[i+1][(10*j + S[i]) % 13] みたいな遷移になる

"""
dp = [[0 for _ in range(13)] for _ in range(N+1)]
dp[0][0] = 1


for i in range(N):
    for j in range(13):
        if S[i] == "?":
            for k in range(10): # ?に0~9
                dp[i+1][(10*j + k) % 13] += dp[i][j]
                dp[i+1][(10*j + k) % 13] %= MOD

        else:
            dp[i+1][(10*j + int(S[i])) % 13] += dp[i][j]
            dp[i+1][(10*j + int(S[i])) % 13] %= MOD



print(dp[-1][5])

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

s = input()[::-1]
n = len(s)
now = [0] * 13
now[0] = 1
for i,x in enumerate(s):
    new = [0] * 13
    c = pow(10,i,13)
    if x == '?':
        for k in range(10):
            for j in range(13):
                new[(j+c*k)%13] += now[j]
                new[(j+c*k)%13] %= mod
    else:
        for j in range(13):
            new[(j+c*int(x))%13] += now[j]
            new[(j+c*int(x))%13] %= mod
    now = new[::]
print(now[5])

Submission status: Accepted

Solution code:
S = input()[:: -1]
mod = 10 ** 9 + 7
dp = [[0 for _ in range(13)] for _ in range(len(S) + 1)]

if S[0] == "?":
    for k in range(10):
        dp[1][k % 13] += 1
else:
    dp[1][int(S[0]) % 13] += 1

tmp = 1
for i in range(2, len(S) + 1):
    tmp *= 10
    tmp %= 13
    for j in range(13):
        if dp[i - 1][j] > 0:
            if S[i - 1] == "?":
                for k in range(10):
                    dp[i][(tmp * k + j) % 13] += dp[i - 1][j] % mod
            else:
                dp[i][(tmp * int(S[i - 1]) + j) % 13] += dp[i - 1][j] % mod

print(dp[len(S)][5] % mod)

Submission status: Accepted

Solution code:
m=10**9+7
s=list(input())
n=len(s)
s.reverse()
dp=[[0]*13 for _ in range(n)]
if s[0]=="?":
    for i in range(10):
        dp[0][i]+=1
else:
    dp[0][int(s[0])]=1
x=1
for i in range(1,n):
    x=(x*10)%13
    for j in range(13):
        if s[i]=="?":
            if dp[i-1][j]!=0:
                for k in range(10):
                    dp[i][(j+k*x)%13]+=dp[i-1][j]
                    dp[i][j]%=m
        else:
            if dp[i-1][j]!=0:
                dp[i][(j+int(s[i])*x)%13]+=dp[i-1][j]
                dp[i][j]%=m
print(dp[n-1][5]%m)

Submission status: Accepted

Solution code:
def main():
    MOD = 10 ** 9 + 7

    s = input()
    r = [0] * 13
    r[0] = 1
    p = 1
    for c in reversed(s):
        if c == '?':
            tank = []
            for x in range(10):
                x = x * p % 13
                tank.append(r[13 - x:] + r[:13 - x])
            r = map(sum, zip(*tank))
            *r, = map(lambda x: x % MOD, r)


        else:
            x = int(c)
            x = x * p % 13
            r = r[13 - x:] + r[:13 - x]
        p = p * 10 % 13

    print(r[5])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
p = 10**9+7
S = input().strip()
A = [[0 for k in range(13)] for j in range(13)]
for j in range(13):
    for k in range(13):
        for m in range(10):
            if (10*k+m)%13==j:
                A[j][k] = 1
                break
B = [[0 for _ in range(10)] for _ in range(13)]
for j in range(13):
    for m in range(10):
        for k in range(13):
            if (10*k+m)%13==j:
                B[j][m] = k
                break
N = len(S)
dp = [[0 for _ in range(13)] for _ in range(N+1)]
for j in range(13):
    if S[0]!="?":
        dp[1][j] = int(int(S[0])==j)
    else:
        if j<10:
            dp[1][j] = 1
for i in range(2,N+1):
    for j in range(13):
        if S[i-1]!="?":
            dp[i][j] = (dp[i][j]+dp[i-1][B[j][int(S[i-1])]])%p
        else:
            for k in range(13):
                dp[i][j] = (dp[i][j]+dp[i-1][k]*A[j][k])%p
print(dp[N][5])

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# https://atcoder.jp/contests/abc135/tasks/abc135_d
# 5あまるじゃなくてn余るできないかな？

'''
dp[i][j] ... [0,i)桁の数字で、余りがjに成る通りの総数
更新則
?じゃないとき
dp[i+1][(j+S[i]*pow(10,i,13))%13] = dp[i][j]
?のとき
dp[i+1][j] += dp[i][k] #すべてのjとkで
'''
mod = 13
S = list(reversed(read()[:-1]))

dp = [[0] * mod for _ in range(len(S) + 1)]
dp[0][0] = 1
pow10imod = 1
for i in range(len(S)):
    for j in range(mod):
        if S[i] != '?':
            dp[i + 1][(j + int(S[i]) * pow10imod) % mod] += \
                dp[i][j] % MOD
            # dp[i + 1][(j + int(S[i]) * pow(10, i, mod)) % mod] += \
            #     dp[i][j] % MOD
        else:
            for k in range(10):
                dp[i + 1][(j + k * pow10imod) % mod] += \
                    dp[i][j] % MOD
                # dp[i + 1][(j + k * pow(10, i, mod)) % mod] += \
                #     dp[i][j] % MOD
    pow10imod = pow10imod * 10 % mod

print(dp[-1][5] % MOD)
# print(*dp, sep='\n')

Submission status: Accepted

Solution code:
S=input()
n=len(S)
t=[]
for i in range(13):
  t.append(0)
t[0]=1
jkmap=[]
for j in range(10):
  jkmap.append([])
  for k in range(13):
    jkmap[j].append((10*k+j)%13)
for i in range(n):
  if S[i]=='?':
    tcopy=[0]*13
    for j in range(10):
      for k in range(13):
        tcopy[jkmap[j][k]]=(tcopy[jkmap[j][k]]+t[k])%(10**9+7)
    for j in range(13):
      t[j]=tcopy[j]
  else:
    j=int(S[i])
    tcopy=[0]*13
    for k in range(13):
      tcopy[jkmap[j][k]]=(tcopy[jkmap[j][k]]+t[k])%(10**9+7)
    for j in range(13):
      t[j]=tcopy[j]
print(t[5])

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

s = read().rstrip().decode()
dp = [[0] * 13 for _ in range(len(s))]
mod = 10 ** 9 + 7
if s[0] == '?':
    dp[0] = 10 * [1] + [0] * 3
else:
    dp[0][int(s[0])] += 1
for i, ss in enumerate(s[1:]):
    if ss == '?':
        for j in range(13):
            for k in range(10):
                dp[i + 1][(j * 10 + k) % 13] += dp[i][j]
    else:
        for j in range(13):
            dp[i + 1][(int(ss) + j * 10) % 13] += dp[i][j]
    for j in range(13):
        dp[i + 1][j] %= mod
print(dp[-1][5])

Submission status: Accepted

Solution code:
S=input()
N=len(S)
C=13
MOD=10**9+7

DP=[[0]*C for _ in range(N+1)]#DP[i][j]はi番目まで見て、あまりがjノヤツの個数
DP[0][0]=1

for i,s in enumerate(S,1):
    if s=="?":
        for k in range(C):
            for j in range(10):
                new_mod=k*10+j
                DP[i][new_mod%13]+=DP[i-1][k]
    else:
        for k in range(C):
            new_mod=k*10+int(s)
            DP[i][new_mod%13]+=DP[i-1][k]
    for k in range(C):
        DP[i][k]%=MOD

print(DP[-1][5])

Submission status: Accepted

Solution code:
def main():
    S = input()
    N = len(S)
    dp = [[0]*13 for i in range(N+1)]

    mod = 10**9 + 7
    dp[0][0] = 1
    for i, x in enumerate(S):
        for j in range(13):
            if x != '?':
                x = int(x)
                k = j*10 + x
                dp[i+1][k % 13] += dp[i][j]
                dp[i+1][k % 13] %= mod
            else:
                for k in range(10):
                    dp[i+1][(j*10+k) % 13] += dp[i][j]
                    dp[i+1][(j*10+k) % 13] %= mod
    print(dp[-1][5])


main()

Submission status: Accepted

Solution code:
MOD = 10**9 + 7


def main():
    S = input()
    N = len(S)
    dp = [[0]*13 for _ in range(N+1)]
    dp[0][0] = 1
    from itertools import product
    for i, mod13 in product(range(N), range(13)):
        if S[i] == "?":
            lim = 9
            for d in range(lim+1):
                dp[i+1][(mod13*10 + d) % 13] \
                    += dp[i][mod13]
                dp[i+1][(mod13*10 + d) % 13] \
                    %= MOD
        else:
            d = int(S[i])
            dp[i+1][(mod13*10 + d) % 13] \
                += dp[i][mod13]
            dp[i+1][(mod13*10 + d) % 13] \
                %= MOD
    print(dp[N][5] % MOD)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()
MOD = 10**9 + 7

M = 13
dp = [0] * M
dp[0] = 1

for s in S:
    newDp = [0] * M
    if s == '?':
        for s in range(10):
            for m in range(M):
                newDp[(m * 10 + s) % M] += dp[m]
    else:
        s = int(s)
        for m in range(M):
            newDp[(m * 10 + s) % M] += dp[m]
    dp = newDp
    for m in range(M):
        dp[m] %= MOD

print(newDp[5] % MOD)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
S = list(input())[: -1]
mod = 10 ** 9 + 7
N = len(S)
dp = [[0] * 13 for _ in range(N + 1)]
dp[0][0] = 1
for i in range(N):
  for j in range(13):
    if S[i] != "?":
      x = int(S[i])
      dp[i + 1][(j * 10 + x) % 13] += dp[i][j]
      dp[i + 1][(j * 10 + x) % 13] %= mod
    else:
      for x in range(10):
        dp[i + 1][(j * 10 + x) % 13] += dp[i][j]
        dp[i + 1][(j * 10 + x) % 13] %= mod
print(dp[-1][5])

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from copy import copy 
def main():
    S = readline().decode().rstrip()
    P = int(1e9)+7
    DP = [0 for _ in range(13)]
    DP[0] = 1
    mul = 0
    for k in reversed(S):
        nxDP = [0 for _ in range(13)]
        if k == '?':
            for j in range(10):
                r = (j*pow(10, mul, 13))%13
                for i in range(13):
                    nxDP[(r+i)%13] += DP[i] 
                    nxDP[(r+i)%13] %= P  
        else:
            r = ((ord(k)-ord('0'))*pow(10, mul, 13))%13
            for i in range(13):
                nxDP[(r+i)%13] += DP[i] 
                nxDP[(r+i)%13] %= P
        mul += 1
        DP = copy(nxDP)
    print(DP[5])
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

##### 割り算の切り上げ #####
# tmp = -(-4 // 3)

##### dict の for文 #####
# for k, v in d.items():
#     print(k, v)

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	S = inputStr()
	S_len = len(S)

	DP = [[0 for j in range(13)] for i in range(S_len)]
	S = S[::-1]

	mut = 10
	for i,val in enumerate(S):
		if i == 0:
			if val == "?":
				for j in range(0,10):
					DP[i][j] += 1
			else:
				DP[i][int(val)] += 1
		else:
			if val == "?":
				for j in range(0,10):
					tmp = (mut * j) % 13
					for v,vol in enumerate(DP[i-1]):
						if vol == 0:
							continue
						tmptmp = (v + tmp) % 13
						DP[i][tmptmp] = (DP[i][tmptmp] + vol) % mod
			else:
				tmp = (mut * int(val)) % 13
				for j,vol in enumerate(DP[i-1]):
					if vol == 0:
						continue
					tmptmp = (j + tmp) % 13
					DP[i][tmptmp] = (DP[i][tmptmp] + vol) % mod
			mut = (mut * 10) % 13

	#print(DP)
	print(DP[S_len-1][5] % mod)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# N 個のボールを K グループに分ける場合のパターン数
def sunuke(N, K, mod=10**9+7):
	if N < K or K-1 < 0:
		return 0
	else:
		return combination(N-1, K-1, mod)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)


class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# dfs のサンプル
def dfs(graph,parent,counter,edge):
    stk = []
    stk.append(edge)
    while len(stk) > 0:
        p = stk.pop()
        for e in graph[p]:
            if parent[p] == e:
                continue
            else:
                parent[e] = p
                counter[e] += counter[p]
                stk.append(e)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
def main():
    MOD = 10 ** 9 + 7
    S = input()
    N = len(S)
    mod13 = [0] * 13
    mod13[0] = 1
    exponent = 1
    for i in range(1, N + 1):
        mod13_new = [0] * 13
        c = S[-i]
        if c == "?":
            for n in range(10):
                m = n * exponent % 13
                for r in range(13):
                    mod13_new[(r + m) % 13] += mod13[r]
        else:
            for r in range(13):
                mod13_new[(r + int(c) * exponent) % 13] = mod13[r]
        exponent = exponent * 10 % 13
        for r in range(13):
            mod13_new[r] %= MOD
        mod13 = mod13_new
    print(mod13[5])

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    MOD = 10 ** 9 + 7
    S = input()
    N = len(S)
    mod13 = [[0] * 13 for _ in range(N + 1)]
    mod13[0][0] = 1
    exponent = 1
    for i in range(1, N + 1):
        c = S[-i]
        if c == "?":
            for n in range(10):
                m = n * exponent % 13
                for r in range(13):
                    mod13[i][(r + m) % 13] += mod13[i - 1][r]
        else:
            m = int(c) * exponent % 13
            for r in range(13):
                mod13[i][(r + m) % 13] = mod13[i - 1][r]
        exponent = exponent * 10 % 13
        for r in range(13):
            mod13[i][r] %= MOD
    print(mod13[N][5])

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)

def main():
    S = input()
    N = len(S)
    MOD = 10**9+7

    S = [int(s) if s!="?" else -1 for s in S ]
    dp = [[0] * 13 for _ in range(N+1)]

    s = S[0]
    if s == -1:
        for j in range(10):
            dp[0][j] += 1
    else:
        dp[0][s%13] += 1
    for i in range(1,N):
        s = S[i]
        X = [s]
        if s == -1:
            X = range(10)

        for j in range(0,13):
            ten_j = j*10
            for x in X:
                k = (ten_j+x)%13
                dp[i][k] += dp[i-1][j]
                dp[i][k] %= MOD

    # print(dp)
    print(dp[N-1][5])



if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)

def main():
    S = input()
    N = len(S)
    MOD = 10**9+7

    # S = [int(s) if s!="?" else -1 for s in S ]
    dp = [[0] * 13 for _ in range(N+1)]

    s = S[0]
    if s == "?":
        for j in range(10):
            dp[0][j] += 1
    else:
        dp[0][int(s)%13] += 1
    for i in range(1,N):
        s = S[i]
        if s == "?":
            for j in range(0,13):
                for x in range(10):
                    k = (j*10+x)%13
                    dp[i][k] += dp[i-1][j]
                    dp[i][k] %= MOD
        else:
            for j in range(0,13):
                k = (j*10+int(s))%13
                dp[i][k] += dp[i-1][j]
                dp[i][k] %= MOD


    print(dp[N-1][5])


if __name__ == "__main__":
  main()