今天比赛碰到一题高精度,不想用c++码了,直接在jdk帮助文档上学习了一下BigInteger,并总结了该类的一些常用方法。
总结一些BigInteger类的常用方法:
1.基本运算
加、减、乘、除、绝对值、求余、左移、右移、求幂
abs()
add(BigInteger)
subtract(BigInteger)
multiply(BigInteger)
divide(BigInteger)
mod(BigInteger)
shiftLeft(int)
shiftRight(int)
pow(int)
2.位运算
and(BigInteger)
or(BigInteger)
not(BigInteger)
xor(BigInteger)
3.比较
compareTo(BigInteger)
这个函数返回值为int !!!!!
equals(Object)
max(BigInteger)
求最大值,等于c++的max(a,b), min同理
min(BigInteger)
4.求特殊值
gcd(BigInteger)
hashCode() 返回int类型的哈希值
negate() 取相反数
nextProbablePrime() 返回下一个可能的素数
还有一些BigInteger类的常量:
BigInteger.ONE
BigInteger.ZERO
BigInteger.TEN
将常数转为BigInteger有两种方法:
1.应用构造方法
BigInteger(byte[] val) 将包含 BigInteger 的二进制补码表示形式的 byte 数组转换为 BigInteger。 |
BigInteger(int signum, byte[] magnitude) 将 BigInteger 的符号-数量表示形式转换为 BigInteger。 |
BigInteger(int bitLength, int certainty, Random rnd) 构造一个随机生成的正 BigInteger,它可能是一个具有指定 bitLength 的素数。 |
BigInteger(int numBits, Random rnd) 构造一个随机生成的 BigInteger,它是在 0 到 (2numBits - 1)(包括)范围内均匀分布的值。 |
BigInteger(String val) 将 BigInteger 的十进制字符串表示形式转换为 BigInteger。 |
BigInteger(String val, int radix) 将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger |
2.使用valueOf
valueOf
public static BigInteger valueOf(long val)
-
返回其值等于指定
long
的值的 BigInteger。 -
-
参数:
-
val
- 要返回的 BigInteger 的值。
返回:
- 具有指定值的 BigInteger。
-
Java例题:UESTC 1668
传送门:http://acm.uestc.edu.cn/#/problem/show/1668
Drinks
Time Limit: 6000/2000MS (Java/Others) Memory Limit: 262144/262144KB (Java/Others)
Every weekend, amoondreamoon and azildrazil play in the park, and for each time, one of them has to buys drinks for both.
They agree to apply the following procedure to determine who is on the duty of buying drinks.
1.Put
n red balls and
m white balls in a paper bag.
2.reamoondreamoon and razildrazil draw a ball from the bag in turns. Note that once a ball is drawn, it is removed from the bag.
3.The person who draws a red ball first has to buy drinks.
azildrazil lets reamoondreamoon perform the first draw every week for reamoondreamoon is older than him.
One day, eamndreamoon suddenly wonders what is the probability that he draws a red ball first.
Can you help him to calculate it? You may assume when every ball has the same probability to be drawn
Input
The first line contains a positive integer T, ≤1770T≤1770, indicating the number of test cases. Each test case has one line contains
two positive integers
n and
m where
1≤n≤59, 1≤m≤59 and
n+m≤60.
n is the number of red balls, and
m is the number of white balls.
Output
For each test case, output one line containing the answer represented by a reduced fraction.
Sample input and output
Sample Input | Sample Output |
---|---|
2 1 1 1 2 | 1/2 2/3 |
package main;
import java.io.*;
import java.util.Scanner;
import java.math.BigInteger;
public class Main {
public static void main (String []arg){
Scanner in = new Scanner(System.in);
int cas,i;
cas=in.nextInt();
while (cas!=0) {
cas--;
BigInteger l,p,totl,totp,u,t,n,m;
n=in.nextBigInteger();
m=in.nextBigInteger();
l=n.add(m);
p=n;
u=l.gcd(p);
l.divide(u);
p.divide(u);
totl=l;totp=p;
for (i=2;i<=m.intValue();i+=2) {
l=l.multiply((n.add(m).subtract(BigInteger.valueOf(i))).multiply(
(n.add(m).subtract(BigInteger.valueOf(i-1)))));
p=p.multiply((m.subtract(BigInteger.valueOf(i-1))).multiply(
(m.subtract(BigInteger.valueOf(i-2)))));
u=l.gcd(p);
l.divide(u);
p.divide(u);
t=l.gcd(totl);
totl=totl.divide(t).multiply(l);
totp=totp.multiply(l.divide(t));
totp=totp.add(p.multiply(totl).divide(l));
u=totl.gcd(totp);
totl=totl.divide(u);
totp=totp.divide(u);
}
System.out.print(totp);
System.out.print("/");
System.out.println(totl);
}
}
}