結果

問題 No.1161 Many Powers
ユーザー tenten
提出日時 2020-08-17 11:38:12
言語 Java
(openjdk 23)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 841 bytes
コンパイル時間 3,478 ms
コンパイル使用メモリ 74,160 KB
実行使用メモリ 41,400 KB
最終ジャッジ日時 2024-10-11 10:55:26
合計ジャッジ時間 9,019 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long a = sc.nextLong();
        long b = sc.nextLong();
        int c = sc.nextInt();
        long div = a / c % c;
        long mod = a % c;
        long ans = 0;
        for (int i = 1; i < c; i++) {
            if (i <= mod) {
                ans += powMod(i, b, c) * (div + 1) % c;
            } else {
                ans += powMod(i, b, c) * div % c;
            }
            ans %= c;
        }
        System.out.println(ans);
   }
   
   static long powMod(long x, long y, long mod) {
       if (y == 0) {
           return 1;
       } else if (y % 2 == 0) {
           return powMod(x * x % mod, y / 2, mod);
       } else {
           return powMod(x, y - 1, mod) * x % mod;
       }
   }
}
0