1. Introduction to BigInteger
If an integer data has exceeded the maximum type length of the integer during operation, the data cannot be loaded, so the BigInteger class must be used for operation at this time. These big numbers willStringin the form of
Compared with Integer, BigInteger can indeed be described as big. It is used for scientific calculations. Integer can only accommodate one int, so the maximum value is 2 31 visits minus 1, and the decimal is 2147483647. However, if you need to calculate a larger number, 31 bits are obviously not enough, then BigInteger can meet our needs at this time.
The number of digits that BigInteger can accommodate is very large. I gave it a simple try.Thousands have no problems. In addition to large capacity, BigInteger also encapsulates some common operations, such asThe basic operation of ±*/, as well as absolute values, opposite numbers, greatest common divisors, and whether they are prime numbersAnd so on operations.
2. Common BigInteger functions
- BigInteger(String value): constructor,
- BigInteger add(BigInteger value): addition,
- BigInteger subtract(BigInteger value): subtraction,
- BigInteger multiply(BigInteger value): multiplication,
- BigInteger divide(BigInteger divider): division,
- BigInteger modInverse(BigInteger m): Find the model,
- BigInteger pow(int exponent): multiply,
- BigInteger max(BigInteger value): maximum number,
- BigInteger min(BigInteger value): minimum number,
- BigInteger abs(): absolute value,
- BigInteger negate(): opposite number,
- int intValue(): converts int, converts BigInteger type data into int.
- BigInteger valueOf(long val): convert to BigInteger, convert long type to BigIntege type
Sample code
【topic】
Enter an integer n 1<n<10^9
Output an integer
Find the sum of all elements in all non-empty subsets, then modulo 10^9+7, and output the result
For example, enter 2, there are 3 non-empty subsets: {1}, {2}, {1, 2}, and the sum of all elements is 4
The output result is 4
Ideas
You will definitely get better with int, you need to use BigInteger
For input n, find the sum of all elements as n*2^(n-1)
Then take the model for 10^7+7
import ; import ; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(); String n = (); BigInteger res = count(new BigInteger(n)); BigInteger m = (10).pow(7).add((7)); ((m));; } // Calculation formula n*2^(n-1) static BigInteger count(BigInteger n) { return ((2).pow(()-1)); } }
This is the end of this article about the detailed explanation of the BigInteger case of Java processing of super large numbers. For more related content of Java processing of super large numbers, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!