java大数(以下为转)
用Java来处理高精度问题,相信对很多ACMer来说都是一件很happy的事,简单易懂。用Java刷了一些题,感觉Java还不错,在处理高精度和进制转换中,调用库函数的来处理。下面是写的一些Java中一些基本的函数的及其…… 头文件:import java.io.*; 读入: 输出: 定义: 数据类型: 数据类型 布尔型 字节型 字符型 短整型 整型 长整型 浮点型 双精度型 这里特别要提出出的两种类型: BigInteger 任意大的整数,原则上是,只要你的计算机的内存足够大,可以有无限位的 BigInteger 任意大的实数,可以处理小数精度问题。 BigInteger中一些常见的函数: A=BigInteger.ONE B=BigInteger.TEN C=BigInteger.ZERO 一些常见的数的赋初值。将int型的数赋值给BigInteger,BigInteger.valueOf(k); 基本的函数: valueOf:赋初值 add:+ subtract:- multiply:* divide:/ remainder:this % val divideAndRemainder:a[0]=this / val; a[1]=this % val pow:a.pow(b)=a^b gcd,abs:公约数,绝对值 negate:取负数 signum:符号函数 mod:a.mod(b)=a%b; shiftLeft:左移,this << n ,this*2^n; shiftRight:右移,this >> n,this/2^n; and:等同于c++的&&,且; or:||,或; xor:异或,BigInteger xor(BigInteger val),this^val not:!,非; bitLength:返回该数的最小二进制补码表示的位的个数,即 *不包括* 符号位 (ceil(log2(this <0 ? -this : this + 1)))。对正数来说,这等价于普通二进制表示的位的个数。 bitCount:返回该数的二进制补码表示中不包扩符号位在内的位的个数。该方法在 BigIntegers 之上实现位向量风格的集合时很有用。 isProbablePrime:如果该 BigInteger 可能是素数,则返回 true ;如果它很明确是一个合数,则返回 false 。 参数 certainty 是对调用者愿意忍受的不确定性的度量:如果该数是素数的概率超过了 1 - 1/2**certainty方法,则该方法返回 true 。执行时间正比于参数确定性的值。 compareTo:根据该数值是小于、等于、或大于 val 返回 -1、0 或 1; equals:判断两数是否相等,也可以用compareTo来代替; min,max:取两个数的较小、大者; intValue,longValue,floatValue,doublue:把该数转换为该类型的数的值。 今天参考课本写了一个关于二进制与十进制转换的程序,程序算法不难,但写完后测试发现不论是二转十还是十转二,对于大于21亿即超过整数范围的数不能很好的转换。都会变成0. 1,BigInteger属于java.math.BigInteger,因此在每次使用前都要import 这个类。偶开始就忘记import了,于是总提示找不到提示符。 2,其构造方法有很多,但现在偶用到的有: BigInteger(String val) 3,BigInteger类模拟了所有的int型数学操作,如add()==“+”,divide()==“-”等,但注意其内容进行数学运算时不能直接使用数学运算符进行运算,必须使用其内部方法。而且其操作数也必须为BigInteger型。 4,当要把计算结果输出时应该使用.toString方法将其转换为10进制的字符串,详细说明如下: 5,另外说明三个个用到的函数。 compareTo 将BigInteger的数转为2进制: public class TestChange { |
Problem D: Lets Play Another Game
Description
OK, if you finished previous game, lets play another game :D
Now n participants of this game sit around the table. Each minute one pair of neighbors can change their places. Find the minimum time (in minutes) required for all participants to sit in reverse order (so that left neighbors would become right, and right - left).
Input
The first line is the amount of tests. Each next line contains one integer n (1 <= n <= 1e18) - the amount of participants.
Output
For each number n of participants to game print on the standard output, on a separate line, the minimum time required for all participants to sit in reverse order.
Sample Input
Sample Output
246
这题通过找规律得出公式,重点在于大数
具体代码:
import java.math.BigInteger;
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sb=new Scanner(System.in);
int test;
test=sb.nextInt();
BigInteger one=BigInteger.ONE;
BigInteger zero=one.subtract(one);
BigInteger two=one.add(one);
BigInteger three=two.add(one);
BigInteger four=three.add(one);
while(test-->0)
{
BigInteger n=sb.nextBigInteger();
BigInteger sum;
if(n.mod(two).compareTo(one)==0)
{
BigInteger xiang=n.subtract(one).divide(two);
sum=xiang.add(xiang.multiply(xiang.subtract(one)));
}
else
{
BigInteger xiang=n.subtract(two).divide(two);
if(xiang.mod(two).compareTo(zero)==0)
{
BigInteger sbb=xiang.divide(two);
// System.out.println(sbb);
sum=sbb.multiply(three).add(sbb.multiply(sbb.subtract(one).multiply(two)));
//.out.println(sum);
sum=sum.multiply(two);
// System.out.println(sum);
}
else
{
BigInteger sbb=xiang.divide(two);
// System.out.println(sbb);
sum=sbb.multiply(three).add(sbb.multiply(sbb.subtract(one).multiply(two)));
// System.out.println(sum);
sum=sum.multiply(two);
// System.out.println(sum);
BigInteger kk=xiang.divide(two).multiply(four).add(three);
// System.out.println(kk);
sum=sum.add(kk).subtract(one);
// System.out.println(sum);
}
}
System.out.println(sum);
}
}
}