結果

問題 No.167 N^M mod 10
ユーザー バカらっくバカらっく
提出日時 2019-11-29 05:53:52
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 347 ms / 1,000 ms
コード長 1,609 bytes
コンパイル時間 13,984 ms
コンパイル使用メモリ 446,984 KB
実行使用メモリ 57,388 KB
最終ジャッジ日時 2024-04-30 22:49:00
合計ジャッジ時間 23,952 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 281 ms
56,904 KB
testcase_01 AC 278 ms
57,076 KB
testcase_02 AC 347 ms
57,316 KB
testcase_03 AC 338 ms
57,388 KB
testcase_04 AC 341 ms
57,204 KB
testcase_05 AC 285 ms
56,944 KB
testcase_06 AC 280 ms
56,944 KB
testcase_07 AC 276 ms
56,944 KB
testcase_08 AC 273 ms
57,044 KB
testcase_09 AC 288 ms
57,008 KB
testcase_10 AC 291 ms
56,940 KB
testcase_11 AC 281 ms
56,924 KB
testcase_12 AC 279 ms
57,096 KB
testcase_13 AC 282 ms
57,040 KB
testcase_14 AC 271 ms
56,976 KB
testcase_15 AC 276 ms
56,920 KB
testcase_16 AC 280 ms
56,952 KB
testcase_17 AC 284 ms
56,936 KB
testcase_18 AC 287 ms
56,964 KB
testcase_19 AC 274 ms
56,996 KB
testcase_20 AC 277 ms
57,104 KB
testcase_21 AC 285 ms
57,076 KB
testcase_22 AC 334 ms
57,376 KB
testcase_23 AC 342 ms
57,312 KB
testcase_24 AC 337 ms
57,360 KB
testcase_25 AC 338 ms
57,300 KB
testcase_26 AC 309 ms
57,244 KB
testcase_27 AC 334 ms
57,224 KB
testcase_28 AC 343 ms
57,372 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