結果

問題 No.219 巨大数の概算
ユーザー uafr_csuafr_cs
提出日時 2015-08-31 03:00:37
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,951 bytes
コンパイル時間 2,606 ms
コンパイル使用メモリ 85,436 KB
実行使用メモリ 86,068 KB
最終ジャッジ日時 2023-09-25 15:49:27
合計ジャッジ時間 36,462 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 AC 213 ms
59,208 KB
testcase_06 AC 615 ms
62,508 KB
testcase_07 AC 183 ms
57,040 KB
testcase_08 AC 613 ms
64,120 KB
testcase_09 AC 179 ms
57,224 KB
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 TLE -
testcase_30 TLE -
testcase_31 TLE -
testcase_32 TLE -
testcase_33 TLE -
testcase_34 TLE -
testcase_35 TLE -
testcase_36 TLE -
testcase_37 TLE -
testcase_38 TLE -
testcase_39 TLE -
testcase_40 TLE -
testcase_41 TLE -
testcase_42 TLE -
testcase_43 TLE -
testcase_44 TLE -
testcase_45 TLE -
testcase_46 TLE -
testcase_47 TLE -
testcase_48 TLE -
testcase_49 TLE -
testcase_50 TLE -
testcase_51 AC 182 ms
57,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class Main {
	
	public static class Value {
		long val, zero;
		
		
		public Value(long val, long zero){
			//System.out.println(val);
			this.val = val;
			this.zero = zero;
		}
		
		public Value(long val){
			this(val, 0);
		}
		
		public Value(Value v){
			this(v.val, v.zero);
		}
		
		public static Value mul(Value a, Value b){
			long zero = a.zero + b.zero;
			BigInteger value = BigInteger.valueOf(a.val).multiply(BigInteger.valueOf(b.val));
			
			while(value.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0){
				// 18桁
				zero += value.toString().length() - 18;
				value = new BigInteger(value.toString().substring(0, 18));
			}
			
			return new Value(value.longValue(), zero);
		}
		
		public String toString(){
			String str = Long.toString(this.val);
			//System.out.println(str);
			
			return str.charAt(0) + " " + (str.length() == 1 ? "0" : str.charAt(1)) + " " + (this.zero + Math.max(0, str.length() - 1)) ;
		}
	}
	
	public static Value pow(Value A, long B){
		if(B == 1){ return new Value(A); }
		if(B % 2 != 0){ return Value.mul(A, pow(A, B - 1)); }
		
		//System.out.println("A = " + A);
		//System.out.println("B = " + B);
		final Value ret = pow(A, B / 2);
		//System.out.println("RET = " + ret);
		
		return Value.mul(ret, ret);
	}
	
	
	
	public static BigDecimal pow(BigDecimal A, long B){
		if(B == 1){ return A; }
		if(B % 2 != 0){ return A.multiply(pow(A, B - 1)); }
		final BigDecimal ret = pow(A, B / 2);
		
		return ret.multiply(ret);
	}
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		
		for(int tt = 0; tt < N; tt++){
			final long A = sc.nextLong();
			final long B = sc.nextLong();
			
			System.out.println(pow(new Value(A), B));
		}
		
	}
	
}
0