結果

問題 No.167 N^M mod 10
ユーザー norioc
提出日時 2015-09-17 22:08:14
言語 Scala(Beta)
(3.6.2)
結果
WA  
実行時間 -
コード長 742 bytes
コンパイル時間 12,757 ms
コンパイル使用メモリ 258,168 KB
実行使用メモリ 66,008 KB
最終ジャッジ日時 2024-11-14 10:55:21
合計ジャッジ時間 41,682 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 25 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

import scala.collection.mutable.ArrayBuffer

object Main {
  // 1 桁の数字 d を繰り返しかけていったときの
  // d の 1 の位のサイクルを求める
  def calcCycle(d: Int): Array[Int] = {
    assert(0 <= d && d < 10)
    val used = Array.ofDim[Boolean](10)
    val xs = new ArrayBuffer[Int]()
    var x = d
    while (!used(x)) {
      used(x) = true
      xs.append(x)
      x = (x * d) % 10
    }
    xs.toArray
  }

  def main(args: Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    val N = sc.nextLine
    val M = sc.nextLine

    val d = N.last - '0'
    val cycle = calcCycle(d)
    val index = (BigInt(M) % cycle.size).toInt
    println(cycle(if (index == 0) cycle.size-1 else index-1))
  }
}
0