結果

問題 No.1417 100の倍数かつ正整数(2)
ユーザー marroncastle
提出日時 2021-03-05 23:58:30
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 2,742 bytes
コンパイル時間 537 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 852,000 KB
最終ジャッジ日時 2024-10-07 06:12:56
合計ジャッジ時間 8,829 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31 MLE * 1 -- * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9+7

class ModInt:
  def __init__(self, x):
    self.x = x % MOD

  def __str__(self):
    return str(self.x)

  __repr__ = __str__

  def __add__(self, other):
    return (
      ModInt(self.x + other.x) if isinstance(other, ModInt) else
      ModInt(self.x + other)
    )

  def __sub__(self, other):
    return (
      ModInt(self.x - other.x) if isinstance(other, ModInt) else
      ModInt(self.x - other)
    )

  def __mul__(self, other):
    return (
      ModInt(self.x * other.x) if isinstance(other, ModInt) else
      ModInt(self.x * other)
    )

  def __truediv__(self, other):
    return (
      ModInt(
        self.x * pow(other.x, MOD - 2, MOD)
      ) if isinstance(other, ModInt) else
      ModInt(self.x * pow(other, MOD - 2, MOD))
    )

  __floordiv__ = __truediv__

  def __pow__(self, other):
    return (
      ModInt(pow(self.x, other.x, MOD)) if isinstance(other, ModInt) else
      ModInt(pow(self.x, other, MOD))
    )

  __radd__ = __add__

  def __rsub__(self, other):
    return (
      ModInt(other.x - self.x) if isinstance(other, ModInt) else
      ModInt(other - self.x)
    )

  __rmul__ = __mul__

  def __rtruediv__(self, other):
    return (
      ModInt(
        other.x * pow(self.x, MOD - 2, MOD)
      ) if isinstance(other, ModInt) else
      ModInt(other * pow(self.x, MOD - 2, MOD))
    )

  def __rpow__(self, other):
    return (
      ModInt(pow(other.x, self.x, MOD)) if isinstance(other, ModInt) else
      ModInt(pow(other, self.x, MOD))
    )

N = list(map(int,list(input())))[::-1]
digit = len(N)
dp = [[[[0]*10 for _ in range(3)] for _ in range(3)] for _ in N+[0]]
dp[0][0][0] = [1]*10
for i in range(digit): #配るDP
  for j in range(3): #2
    for k in range(3): #5
      for l in range(1,10):
        dp[i+1][j][k][l] += dp[i+1][j][k][l-1]
        dp[i+1][j][k][l] += dp[i][j][k][-1]*(int(l%2==1)-int(l%5==0))
        dp[i+1][min(2,j+1)][k][l] += dp[i][j][k][-1]*(int(l%2==0)-int(l%4==0))
        dp[i+1][min(2,j+2)][k][l] += dp[i][j][k][-1]*int(l%4==0)
        dp[i+1][j][min(2,k+1)][l] += dp[i][j][k][-1]*int(l%5==0)
dp1 = [[[0]*3 for _ in range(3)] for _ in N+[0]]
ans = ModInt(0)
for i in range(digit-1):
  ans += dp[i+1][-1][-1][-1]
for i in range(digit):
  if N[i-1]>0 and N[i]>0:
    for j in range(3): #2
      for k in range(3): #5
        dp1[i+1][j][k] += (dp[i][j][k][N[i-1]-1]+dp1[i][j][k])*(int(N[i]%2==1)-int(N[i]%5==0))
        dp1[i+1][min(2,j+1)][k] += (dp[i][j][k][N[i-1]-1]+dp1[i][j][k])*(int(N[i]%2==0)-int(N[i]%4==0))
        dp1[i+1][min(2,j+2)][k] += (dp[i][j][k][N[i-1]-1]+dp1[i][j][k])*int(N[i]%4==0)
        dp1[i+1][j][min(2,k+1)] += (dp[i][j][k][N[i-1]-1]+dp1[i][j][k])*int(N[i]%5==0)
ans += dp1[-1][-1][-1]+dp[-1][-1][-1][N[-1]-1]
print(ans)
0