結果

問題 No.1629 Sorting Integers (SUM of M)
ユーザー rulerruler
提出日時 2021-07-31 12:31:00
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 255 ms / 2,000 ms
コード長 1,711 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 10,860 KB
実行使用メモリ 50,724 KB
最終ジャッジ日時 2023-10-14 14:52:07
合計ジャッジ時間 5,483 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 228 ms
50,536 KB
testcase_01 AC 232 ms
50,556 KB
testcase_02 AC 228 ms
50,596 KB
testcase_03 AC 232 ms
50,656 KB
testcase_04 AC 239 ms
50,468 KB
testcase_05 AC 230 ms
50,724 KB
testcase_06 AC 232 ms
50,500 KB
testcase_07 AC 244 ms
50,568 KB
testcase_08 AC 237 ms
50,520 KB
testcase_09 AC 242 ms
50,592 KB
testcase_10 AC 238 ms
50,480 KB
testcase_11 AC 235 ms
50,628 KB
testcase_12 AC 241 ms
50,572 KB
testcase_13 AC 239 ms
50,512 KB
testcase_14 AC 236 ms
50,460 KB
testcase_15 AC 241 ms
50,560 KB
testcase_16 AC 238 ms
50,584 KB
testcase_17 AC 255 ms
50,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import typing 
from math import factorial
import numpy as np
import sys


class ModFactorial:
  def __call__(
    self, 
    n: int = 1 << 20,
  ) -> np.array:
    a = np.arange(n); a[0] = 1
    return self.cumprod(a)


  def cumprod(
    self, 
    a: np.array,
  ) -> np.array:
    m = self.__mod
    l = len(a)
    n = int(np.sqrt(l) + 1)
    a = np.resize(a, (n, n))
    for i in range(n-1):
      a[:, i + 1] *= a[:, i]
      a[:, i + 1] %= m
    for i in range(n-1):
      a[i + 1] *= a[i, -1]
      a[i + 1] %= m
    return np.ravel(a)[:l]


  def __init__(
    self,
    modulo: int,
  ) -> typing.NoReturn:
    self.__mod = modulo 
  

  def inv(
    self, 
    n: int = 1 << 20,
  ) -> np.array:
    a = np.arange(1, n + 1)
    m = self.__mod
    x = int(self(n)[-1])
    a[-1] = pow(x, m - 2, m)
    return self.cumprod(
      a[::-1],
    )[n::-1]


class Inverse():
  def __call__(
    self,
    i: int,
  ) -> int:
    return self[i]


  def __getitem__(
    self,
    i: int,
  ) -> int:
    return self.__a[i]


  def __init__(
    self,
    n: int,
    modulo: int,
  ) -> typing.NoReturn:
    m = modulo
    fn = ModFactorial(m)
    a = fn.inv(n)
    a[1:] *= fn(n - 1)
    self.__a = a % m


def main() -> typing.NoReturn:
  mod = 10 ** 9 + 7

  fn = ModFactorial(mod)
  fact = fn(1 << 18)
  ifact = fn.inv(1 << 18)
  inv = Inverse(1 << 18, mod)

  n = int(input())
  c = np.array(
    sys.stdin.readline()
    .split(),
    dtype=np.int64,
  )
  m = fact[n]
  for x in c:
    m *= ifact[x]
    m %= mod
  
  s = (np.arange(9) + 1) * c
  s = s.sum()
  s *= inv[n]
  s %= mod
  d = 0
  for _ in range(n):
    d *= 10
    d += 1
    d %= mod
  s = s * d % mod * m % mod 
  print(s)


main()

  
0