結果
| 問題 |
No.219 巨大数の概算
|
| コンテスト | |
| ユーザー |
uafr_cs
|
| 提出日時 | 2015-08-31 03:13:30 |
| 言語 | Java (openjdk 23) |
| 結果 |
TLE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 4,073 bytes |
| コンパイル時間 | 2,191 ms |
| コンパイル使用メモリ | 87,200 KB |
| 実行使用メモリ | 75,896 KB |
| 最終ジャッジ日時 | 2024-07-18 11:35:56 |
| 合計ジャッジ時間 | 80,252 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 6 TLE * 45 |
ソースコード
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
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 BigInteger LONGMAX = BigInteger.valueOf(Long.MAX_VALUE);
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));
if(value.compareTo(LONGMAX) > 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 void main(String[] args) throws IOException{
FastScanner sc = new FastScanner();
final int N = (int)(sc.nextLong());
PrintWriter pw = new PrintWriter(System.out);
for(int tt = 0; tt < N; tt++){
final long A = sc.nextLong();
final long B = sc.nextLong();
pw.println(pow(new Value(A), B));
}
pw.flush();
}
public static class FastScanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
}else{
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
public boolean hasNext() { skipUnprintable(); return hasNextByte();}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while(isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while(true){
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
}else if(b == -1 || !isPrintableChar(b)){
return minus ? -n : n;
}else{
throw new NumberFormatException();
}
b = readByte();
}
}
}
}
uafr_cs