結果

問題 No.167 N^M mod 10
ユーザー バカらっくバカらっく
提出日時 2019-11-29 05:53:52
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 375 ms / 1,000 ms
コード長 1,609 bytes
コンパイル時間 20,027 ms
コンパイル使用メモリ 420,088 KB
実行使用メモリ 53,612 KB
最終ジャッジ日時 2023-08-13 04:38:35
合計ジャッジ時間 28,656 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 301 ms
52,940 KB
testcase_01 AC 305 ms
53,000 KB
testcase_02 AC 367 ms
53,612 KB
testcase_03 AC 361 ms
53,360 KB
testcase_04 AC 363 ms
53,352 KB
testcase_05 AC 302 ms
52,984 KB
testcase_06 AC 303 ms
52,944 KB
testcase_07 AC 302 ms
52,980 KB
testcase_08 AC 304 ms
52,956 KB
testcase_09 AC 305 ms
52,868 KB
testcase_10 AC 303 ms
53,032 KB
testcase_11 AC 302 ms
52,900 KB
testcase_12 AC 302 ms
52,932 KB
testcase_13 AC 301 ms
52,948 KB
testcase_14 AC 303 ms
52,996 KB
testcase_15 AC 300 ms
52,888 KB
testcase_16 AC 305 ms
52,880 KB
testcase_17 AC 302 ms
52,996 KB
testcase_18 AC 300 ms
52,908 KB
testcase_19 AC 302 ms
52,924 KB
testcase_20 AC 301 ms
52,944 KB
testcase_21 AC 303 ms
53,216 KB
testcase_22 AC 361 ms
53,324 KB
testcase_23 AC 365 ms
53,296 KB
testcase_24 AC 363 ms
53,240 KB
testcase_25 AC 367 ms
53,592 KB
testcase_26 AC 327 ms
53,184 KB
testcase_27 AC 375 ms
53,400 KB
testcase_28 AC 364 ms
53,408 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:4:10: warning: parameter 'arr' is never used
fun main(arr:Array<String>) {
         ^

ソースコード

diff #

import java.util.*
import kotlin.contracts.contract

fun main(arr:Array<String>) {
    val (n,m) = (1..2).map { readLine()!! }
    val nn = n.last().toString().toInt()

    val dic = mutableMapOf<Int, Int>()

    var subTotal = 1
    var prevIdx = -1
    var toIdx = -1
    for(i in (1 until  20)) {
        subTotal *= nn
        subTotal = subTotal % 10
        if(m.length <= 3 && i == m.toInt()) {
            println(subTotal)
            return
        }
        if(m.length > 3 || m.toInt() >= 20) {
            if(dic.containsKey(subTotal)) {
                prevIdx = dic[subTotal]!!
                toIdx = i
            } else {
                dic[subTotal] = i
            }
        }
    }
    val ans = getAns(m, nn, prevIdx, toIdx)
    println(ans)
}

fun getAns(m:String, nn:Int, prevIdx:Int, toIdx:Int): Int {
    val num = m.map { it.toString().toInt() }.toMutableList()

    var subNum = prevIdx
    for(i in num.indices.reversed()) {
        if(num[i] >= subNum) {
            num[i] -= subNum
            break
        } else {
            num[i] = num[i] - subNum + 10
            subNum = 1
        }
    }
    
    val divNum = toIdx - prevIdx
    while (num.size > 1) {
        if(num[0] > divNum) {
            num[0] = num[0] % divNum
            if(num[0] == 0) {
                num.removeAt(0)
            }
        } else {
            num[1] = (num[0] * 10 + num[1]) % divNum
            num.removeAt(0)
        }
    }
    
    val cnt = prevIdx + num[0]
    
    var ans = 1
    for(i in (1..cnt)) {
        ans = ans * nn
        ans = ans % 10
    }
    return ans
    
}
0