結果

問題 No.16 累乗の加算
ユーザー tsunabittsunabit
提出日時 2019-09-03 16:57:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 136 ms / 5,000 ms
コード長 988 bytes
コンパイル時間 4,079 ms
コンパイル使用メモリ 72,352 KB
実行使用メモリ 56,468 KB
最終ジャッジ日時 2023-08-25 20:28:10
合計ジャッジ時間 7,074 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,748 KB
testcase_01 AC 122 ms
55,676 KB
testcase_02 AC 133 ms
55,788 KB
testcase_03 AC 127 ms
56,032 KB
testcase_04 AC 129 ms
55,728 KB
testcase_05 AC 130 ms
55,748 KB
testcase_06 AC 132 ms
56,468 KB
testcase_07 AC 131 ms
55,776 KB
testcase_08 AC 136 ms
55,920 KB
testcase_09 AC 136 ms
55,572 KB
testcase_10 AC 134 ms
56,096 KB
testcase_11 AC 123 ms
55,980 KB
testcase_12 AC 131 ms
56,228 KB
testcase_13 AC 127 ms
55,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
import java.math.*;

public class No16 {
	public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int x = sc.nextInt(), n = sc.nextInt();
    	long w = 1000003;
    	long total = 0;
    	
    	for(int i = 0; i < n; i++) {
    		//double t = Math.pow(x, sc.nextInt());
    		double t = pow(x, sc.nextLong(), w);
//    		System.out.println(t);
    		total += t;
    		
    	}
//    	System.out.println("total = " + (total % w));
    	System.out.println((total % w));
	}
	// 繰り返し2乗法
    static public long pow(long x, long n, long w) {
    	long ans = 1;
    	while(n > 0) {
    		if((n & 1) == 1) {
    			ans = ans * x % w;
    		}
    		// 2進数で考え、桁を上げるたびに底数をかけていく。一周する度にx, x^2, x^4, x^8となる
    		x = x * x % w;
//    		System.out.println(x);
    		// 桁をずらす。「n = n >> 1」と同じ
    		n >>= 1;
    	}
    	return ans;
    }
}
0