結果

問題 No.344 ある無理数の累乗
ユーザー hiromi_ayasehiromi_ayase
提出日時 2016-02-13 06:18:03
言語 Java
(openjdk 23)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 850 bytes
コンパイル時間 2,188 ms
コンパイル使用メモリ 77,068 KB
実行使用メモリ 54,308 KB
最終ジャッジ日時 2024-09-22 06:11:35
合計ジャッジ時間 6,944 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner scanner  = new Scanner(System.in);
		int n = scanner.nextInt();
		int orgN = n;
		
		long[] x = {1, 0, 0, 1};
		long[] k = {1, 1, 3, 1};
		
		long[] tmp = k;
		while (n > 0) {
			if (n % 2 == 0) {
				mul(tmp, tmp, 1000);
				n >>= 1;
			} else {
				mul(x, tmp, 1000);
				n --;
			}
		}
		long ans = (x[0] * 2 - ((orgN % 2 == 0) ? 1 : 0)) % 1000;
		System.out.println(ans);
	}

	public static void mul(long[] mat1, long[] mat2, long mod) {
		long a = mat1[0];
		long b = mat1[1];
		long c = mat1[2];
		long d = mat1[3];

		long e = mat2[0];
		long f = mat2[1];
		long g = mat2[2];
		long h = mat2[3];

		mat1[0] = (a * e + b * g) % mod;
		mat1[1] = (a * f + b * h) % mod;
		mat1[2] = (c * e + d * g) % mod;
		mat1[3] = (c * f + d * h) % mod;
	}
}
0