How Many Pieces of Land ? UVA - 10213

欧拉平面公式证明:点击打开链接

题目链接:点击打开链接

import java.math.BigInteger;
import java.util.*;
/*
第一道java题,简单在不用模拟,但是要记住这一堆规则
1. 头文件 import java.math.BigInteger; import java.util.*;//大数头文件和 一些标准头文件,
2. 函数名必须写Main(),函数前面都加上 public static
3. 输入   Scanner cin=new Scanner(System.in), 后面就都是int n=cin.nextInt(),BigInteger a=cin.nextBigInteger();
4. 多组输入,直接while(cin.hasNext()){},不管几个都是这样写
5 .输出   System.out.println()
*/

public class Main {

	public static BigInteger get(BigInteger n) {
		BigInteger v1=n.subtract(BigInteger.valueOf(2));
		BigInteger v2=n.subtract(BigInteger.valueOf(3));
		BigInteger v3=n.multiply(BigInteger.valueOf(2)).subtract(BigInteger.valueOf(5));
		return v1.multiply(v1).multiply(v2).divide(BigInteger.valueOf(2)).subtract(v1.multiply(v2).multiply(v3).divide(BigInteger.valueOf(6)));
		
	}
	public static BigInteger getv(BigInteger n) {
		BigInteger ans=n;
		BigInteger tmp=get(n);
		ans=ans.add(n.multiply(tmp).divide(BigInteger.valueOf(4)));
		return ans;
		
	}
	
	public static BigInteger gete(BigInteger n) {
		BigInteger ans=n.multiply(BigInteger.valueOf(2));
		BigInteger tmp=get(n);
		tmp=tmp.add(n.subtract(BigInteger.valueOf(3)));
		ans=ans.add(n.multiply(tmp).divide(BigInteger.valueOf(2)));
		return ans;
	}
	
	public static void main(String[] args) {
		int ans[]= {1,1,2,4,8,16,31};
		Scanner cin=new Scanner(System.in);
		while(cin.hasNext()) {//多组输入,一直到EOF
			int m=cin.nextInt();
			for(int i=1;i<=m;i++) {
				int n=cin.nextInt();
				if(n<5) {
					System.out.println(ans[n]);
					continue;
				}
				BigInteger V=getv(BigInteger.valueOf(n));
				BigInteger E=gete(BigInteger.valueOf(n));
				BigInteger ed=E.add(BigInteger.valueOf(1)).subtract(V);
				System.out.println(ed);
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/81046719