大整数
1 2
| 比较大小: a.compareTo(b): 小于返回-1,等于返回0,大于返回1 int/long转换: BigIntger a = BigInteger.valueOf(114514);
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| import java.util.*; import java.math.BigInteger; import java.math.*; public class Main{ public static void main(String[] args) { BigInteger number1=new BigInteger("347238462384523623645237465237415234165234615246742354"); BigInteger number2=new BigInteger("42673547263541874637462394142837645"); System.out.println("abs():"+number1.abs().toString()); System.out.println("divide():"+number1.divide(number2)); System.out.println("remainder():"+number1.remainder(number2)); System.out.println("add():"+number1.add(number2).toString()); System.out.println("shiftLeft():"+number1.shiftLeft(1)); System.out.println("shiftRight():"+number1.shiftRight(1)); System.out.println("signum():"+number1.signum()); System.out.println("sqrt():"+number1.sqrt()); System.out.println("and():"+number1.and(number2)); System.out.println("andNot():"+number1.andNot(number2)); System.out.println("bitCount():"+number1.bitCount()); System.out.println("bitLength():"+number1.bitLength()); System.out.println("clearBit:"+number2.clearBit(0)); System.out.println("compareTo():"+number1.compareTo(number2)); System.out.println("divideAndRemainder():"+Arrays.toString(number1.divideAndRemainder(number2))); System.out.println("doubleValue():"+number1.doubleValue()); System.out.println("floatValue():"+number1.floatValue());
System.out.println("max():"+number1.max(number2)); System.out.println("min():"+number1.min(number2)); System.out.println("mod():"+number1.mod(number2)); System.out.println("modInverse():"+number1.modInverse(number2)); System.out.println("modPow():"+number1.modPow(BigInteger.valueOf(1), number2)); System.out.println("multiply():"+number1.multiply(number2)); System.out.println("negate():"+number1.negate()); System.out.println("equals()就不说了,大家都明白...."); System.out.println("flipBit():"+number1.flipBit(1)); System.out.println("gcd():"+number1.gcd(number2)); System.out.println("getLowestSetBit():"+number1.getLowestSetBit()); System.out.println("hashCode():"+number1.hashCode()); System.out.println("intValue():"+number1.intValue()); System.out.println("isProbablePrime():"+number1.isProbablePrime(10)); System.out.println("longValue():"+number1.longValue()); System.out.println("nextProbablePrime():"+number1.nextProbablePrime()); System.out.println("not():"+number1.not()); System.out.println("or():"+number1.or(number2)); System.out.println("pow():"+number1.pow(2)); System.out.println("probablePrime():"+number1.probablePrime(10, new Random(10)));
System.out.println("setBit():"+number1.setBit(5)); BigInteger[] arr1=number1.sqrtAndRemainder(); System.out.println(arr1[0]+" "+arr1[1]); System.out.println("subtract():"+number1.subtract(number2)); System.out.println("testBit():"+number1.testBit(1)); System.out.println("toByteArray():"+Arrays.toString(number1.toByteArray())); System.out.println("toString()不多说了...."); https: System.out.println("toString(int radix):"+number1.toString(10)); System.out.println("valueOf():"+BigInteger.valueOf(8)); System.out.println("xor(BigInteger val):"+number1.xor(number2)); } }
|
使用例(国王游戏)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import java.util.*; import java.math.*; class Node implements Comparable <Node>{ public BigInteger a,b,w; Node(long A,long B){ a = BigInteger.valueOf(A); b = BigInteger.valueOf(B); w = a.multiply(b); } public int compareTo(Node X){ return w.compareTo(X.w); } } class Main{ static int maxn = (int)1e5+10; public static void main(String args[]){ Scanner sc = new Scanner(System.in); Node [] node = new Node[maxn]; int n = sc.nextInt(); for(int i = 0;i <= n;i++){ long a,b; a = sc.nextLong();b = sc.nextLong(); node[i] = new Node(a,b); } Arrays.sort(node,1,n+1); BigInteger ans = new BigInteger("-1"); BigInteger pre = node[0].a; for(int i = 1;i <= n;i++){ ans = ans.max(pre.divide(node[i].b)); pre = pre.multiply(node[i].a); } System.out.println(ans); } }
|
\[
\frac{1}{2} \cdot (-4)^n + \frac{1}{4}\cdot16^n
\]