結果

問題 No.1161 Many Powers
ユーザー 箱星
提出日時 2020-08-19 18:04:36
言語 Kotlin
(2.1.0)
結果
AC  
実行時間 418 ms / 2,000 ms
コード長 1,364 bytes
コンパイル時間 12,591 ms
コンパイル使用メモリ 433,224 KB
実行使用メモリ 56,916 KB
最終ジャッジ日時 2024-10-12 05:07:30
合計ジャッジ時間 22,403 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.io.PrintWriter
import java.util.*

fun PrintWriter.solve(sc: FastScanner) {
    val a = sc.nextLong()
    val b = sc.nextLong()
    val c = sc.nextInt()
    val arr = Array(c) { 0L }
    for (i in 1 until c) {
        arr[i] = modpow(i.toLong(), b, c)
    }
    val sum = Array(c + 1) { 0L }
    for (i in 0 until c) {
        sum[i + 1] = sum[i] + arr[i]
    }
    var ans = sum[c] * (a / c) % c + sum[((a + 1) % c).toInt()]
    ans %= c
    println(ans)
}

fun modpow(a:Long, b:Long, c:Int): Long {
    if (b == 0L) return if (a % c == 0L) 0 else 1
    if (b == 1L) return a % c
    val p = modpow(a, b / 2, c)
    val pp = p * p % c
    return if (b % 2 == 0L) pp else pp * a % c
}

fun main() {
    val writer = PrintWriter(System.out, false)
    writer.solve(FastScanner(System.`in`))
    writer.flush()
}

class FastScanner(s: InputStream) {
    private var st = StringTokenizer("")
    private val br = BufferedReader(InputStreamReader(s))

    fun next(): String {
        while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())

        return st.nextToken()
    }

    fun nextInt() = next().toInt()
    fun nextLong() = next().toLong()
    fun nextLine() = br.readLine()
    fun nextDouble() = next().toDouble()
    fun ready() = br.ready()
}
0