結果

問題 No.1632 Sorting Integers (GCD of M)
ユーザー yudedakoyudedako
提出日時 2022-03-01 16:48:11
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 1,051 ms / 2,000 ms
コード長 1,437 bytes
コンパイル時間 10,588 ms
コンパイル使用メモリ 262,512 KB
実行使用メモリ 63,488 KB
最終ジャッジ日時 2023-09-22 06:45:20
合計ジャッジ時間 79,509 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,039 ms
62,480 KB
testcase_01 AC 1,030 ms
62,476 KB
testcase_02 AC 1,015 ms
62,520 KB
testcase_03 AC 1,013 ms
62,568 KB
testcase_04 AC 1,025 ms
62,488 KB
testcase_05 AC 1,015 ms
62,492 KB
testcase_06 AC 1,007 ms
62,404 KB
testcase_07 AC 998 ms
62,388 KB
testcase_08 AC 1,003 ms
62,508 KB
testcase_09 AC 1,005 ms
62,392 KB
testcase_10 AC 1,003 ms
62,160 KB
testcase_11 AC 1,034 ms
62,472 KB
testcase_12 AC 1,008 ms
62,656 KB
testcase_13 AC 1,004 ms
62,440 KB
testcase_14 AC 1,013 ms
63,488 KB
testcase_15 AC 1,009 ms
62,416 KB
testcase_16 AC 1,007 ms
62,664 KB
testcase_17 AC 1,023 ms
62,504 KB
testcase_18 AC 1,028 ms
62,280 KB
testcase_19 AC 1,010 ms
62,264 KB
testcase_20 AC 1,027 ms
62,560 KB
testcase_21 AC 1,015 ms
62,432 KB
testcase_22 AC 1,009 ms
62,144 KB
testcase_23 AC 1,020 ms
62,392 KB
testcase_24 AC 1,002 ms
62,736 KB
testcase_25 AC 997 ms
62,488 KB
testcase_26 AC 1,000 ms
62,100 KB
testcase_27 AC 1,006 ms
62,436 KB
testcase_28 AC 1,006 ms
62,400 KB
testcase_29 AC 994 ms
62,312 KB
testcase_30 AC 1,004 ms
62,520 KB
testcase_31 AC 1,017 ms
62,568 KB
testcase_32 AC 1,004 ms
62,860 KB
testcase_33 AC 1,010 ms
62,528 KB
testcase_34 AC 1,008 ms
62,652 KB
testcase_35 AC 1,010 ms
62,484 KB
testcase_36 AC 1,008 ms
62,552 KB
testcase_37 AC 1,021 ms
62,408 KB
testcase_38 AC 1,012 ms
62,360 KB
testcase_39 AC 1,009 ms
62,476 KB
testcase_40 AC 1,019 ms
62,052 KB
testcase_41 AC 1,003 ms
62,592 KB
testcase_42 AC 1,028 ms
62,372 KB
testcase_43 AC 1,015 ms
62,452 KB
testcase_44 AC 1,051 ms
62,520 KB
testcase_45 AC 1,006 ms
62,568 KB
testcase_46 AC 1,005 ms
62,488 KB
testcase_47 AC 1,010 ms
62,740 KB
testcase_48 AC 1,006 ms
62,840 KB
testcase_49 AC 1,006 ms
62,476 KB
testcase_50 AC 1,006 ms
62,516 KB
testcase_51 AC 1,001 ms
62,204 KB
testcase_52 AC 1,004 ms
62,544 KB
testcase_53 AC 1,014 ms
62,376 KB
testcase_54 AC 1,019 ms
62,572 KB
testcase_55 AC 1,010 ms
62,712 KB
testcase_56 AC 1,027 ms
62,872 KB
testcase_57 AC 1,021 ms
62,576 KB
testcase_58 AC 1,008 ms
62,484 KB
testcase_59 AC 1,007 ms
62,456 KB
testcase_60 AC 1,013 ms
62,496 KB
testcase_61 AC 1,017 ms
62,476 KB
testcase_62 AC 1,013 ms
62,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import scala.collection.mutable.*
import scala.io.StdIn.*
import scala.util.chaining.*
import scala.math.*
import scala.reflect.ClassTag
import scala.util.*
import scala.annotation.tailrec
import scala.collection.mutable

@tailrec def gcd(a: Int, b: Int): Int =
  b match
    case 0 => a
    case _ => gcd(b, a % b)

def powMod(base: Int, exp: Int, mod: Int): Long =
  var result = 1L
  var b = base.toLong % mod
  var e = exp
  while e > 0 do
    if (e & 1) == 1 then
      result = result * b % mod
    b = b * b % mod
    e = e >> 1
  result

def calcRest(digitCount: Array[Int], mod: Int): Long =
  var r = 0L
  for i <- 1 to 9 if digitCount(i - 1) > 0 do
    val length = digitCount(i - 1)
    r = (r * powMod(10, length, mod)) % mod
    var value = 0L
    for d <- 29 to 0 by -1 do
      value = (value + value * powMod(10, length >> (d + 1), mod)) % mod
      if ((length >> d) & 1) == 1 then
        value = (value * 10 + i) % mod
    r = (r + value) % mod
  r

@main def main =
  val n = readLine.toInt
  val digitCount = readLine.split(' ').map(_.toInt)
  val diff = for
    i <- 1 to 9 if digitCount(i - 1) > 0
    j <- 1 until i if digitCount(j - 1) > 0
  yield
    9 * (i - j)
  val g = diff.fold(0)(gcd)
  val result = (1 to g).filter(g % _ == 0).findLast{ calcRest(digitCount, _) == 0 }
  result match
    case Some(d) => println(d)
    case None => println(calcRest(digitCount, 1000000007))


0