結果

問題 No.344 ある無理数の累乗
ユーザー 37zigen37zigen
提出日時 2016-02-13 11:53:00
言語 Java19
(openjdk 21)
結果
AC  
実行時間 141 ms / 2,000 ms
コード長 1,760 bytes
コンパイル時間 4,971 ms
コンパイル使用メモリ 78,008 KB
実行使用メモリ 57,712 KB
最終ジャッジ日時 2023-10-22 04:56:42
合計ジャッジ時間 9,008 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
57,504 KB
testcase_01 AC 141 ms
57,704 KB
testcase_02 AC 131 ms
57,600 KB
testcase_03 AC 130 ms
57,520 KB
testcase_04 AC 130 ms
57,608 KB
testcase_05 AC 131 ms
57,312 KB
testcase_06 AC 131 ms
57,584 KB
testcase_07 AC 135 ms
57,560 KB
testcase_08 AC 132 ms
57,576 KB
testcase_09 AC 132 ms
57,636 KB
testcase_10 AC 132 ms
57,372 KB
testcase_11 AC 133 ms
57,460 KB
testcase_12 AC 130 ms
57,460 KB
testcase_13 AC 118 ms
56,284 KB
testcase_14 AC 133 ms
57,712 KB
testcase_15 AC 132 ms
57,536 KB
testcase_16 AC 129 ms
55,692 KB
testcase_17 AC 129 ms
57,592 KB
testcase_18 AC 132 ms
57,492 KB
testcase_19 AC 132 ms
55,548 KB
testcase_20 AC 132 ms
57,432 KB
testcase_21 AC 130 ms
57,560 KB
testcase_22 AC 129 ms
57,528 KB
testcase_23 AC 118 ms
56,356 KB
testcase_24 AC 115 ms
54,224 KB
testcase_25 AC 135 ms
57,544 KB
testcase_26 AC 120 ms
56,288 KB
testcase_27 AC 130 ms
55,496 KB
testcase_28 AC 132 ms
57,476 KB
testcase_29 AC 133 ms
57,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.BigInteger;
import java.util.Scanner;
public class main{
	static Scanner sc=new Scanner(System.in);
	public static void main(String args[]){
		int n=sc.nextInt();
		long[][] A={
				{1,3},
				{1,1}
		};
		A=MtPow(n,A);
		long a=A[0][0];
		if(n%2==0){
			System.out.print((2*a-1)%1000);
		}else{
			System.out.print((2*a)%1000);
		}
	}

//int matrix^matrix(becareful about the size)
public static long[][] MtPrd(long[][] A,long[][] B){
	long[][] C=new long[A.length][B[0].length];
	for(int i=0;i<A.length;i++){
		for(int j=0;j<B[0].length;j++){
			for(int k=0;k<B[0].length;k++){
				C[i][j]+=A[i][k]*B[k][j];
				C[i][j]%=1000;
			}
		}
	}
	return C;
}
//A+B
public static long[][] MtSum(long[][] A,long[][] B){
	long[][] C=new long[A[0].length][A.length];
	for(int i=0;i<A.length;i++){
		for(int j=0;j<A[0].length;j++){
			C[i][j]=A[i][j]+B[i][j];
		}
	}
	return C;
}
//convert to binary
public static BigInteger bi(int n){
	BigInteger b=new BigInteger("0");
	BigInteger digit=new BigInteger("10");
	for(int i=0;n!=0;i++){
		//System.out.println("n="+n+"\nb="+b);
		if(n%2==0){
			n/=2;
		}else{
			n-=1;
			n/=2;
			b=b.add(digit.pow(i));
		}
	}
	return b;
}

//R=A^n
public static long[][] MtPow(int n,long[][] A){
	BigInteger b=new BigInteger("0");
	b=bi(n);
	long[][] B=new long[A.length][A[0].length];
	for(int i=0;i<A.length;i++){
		B[i][i]=1;
	}
	while(b.compareTo(BigInteger.valueOf(1))>=0){
		if((b.remainder(BigInteger.valueOf(10)).compareTo(BigInteger.valueOf(1)))==0){
			B=MtPrd(A,B);
			b=b.subtract(BigInteger.valueOf(-1));
			b=b.divide(BigInteger.valueOf(10));
		}else if((b.remainder(BigInteger.valueOf(10)).compareTo(BigInteger.valueOf(1)))!=0){
			b=b.divide(BigInteger.valueOf(10));
		}
		A=MtPrd(A,A);
	}
	return B;
}
}
0