您的位置:首页 > 其它

nyoj 数论 541最强DE 战斗力

2015-05-02 23:36 686 查看
踏实做事,安心做人,这是最后的斗争,英特纳雄奈尔一定会实现。

题目分析:这题是一个数论题,第一开始我自己找规律的时候,发现任何一个正整数分解的字数都能转为2和3的乘积。。。

后来就baidu了一下,有这么一定数论定理!

定理:当把一个正整数n拆分加数因子的乘积时,尽可能的拆为3,若最后剩1,就拿回一个3凑成4,这样就是最大的乘积。

知道里定理,用java用的大数,就OK啦

代码:

import java.io.BufferedInputStream;
import java.io.PrintStream;
import java.math.BigInteger;

public class Main {
	static BufferedInputStream bis = new BufferedInputStream(System.in);
	static PrintStream out = System.out;
	static BigInteger bi3 = BigInteger.valueOf(3);

	public static void main(String[] args) throws Exception {
		int nCase;
		nCase = getInt();
		while (nCase-- != 0) {
			getAns(getInt());
		}
		out.close();
		bis.close();
	}

	static void getAns(int n) {
		int cntOf3 = 0, reminder;
		while (n - 3 >= 0) {//统计最多能拆分出多少个3
			cntOf3++;
			n -= 3;
		}
		reminder = n;
		if (reminder == 0) {
			reminder++;
		}
		if (n == 1 && cntOf3 > 0) {
			cntOf3--;
			reminder = 4;
		}
		out.println(bi3.pow(cntOf3).multiply(BigInteger.valueOf(reminder)));
	}

	static int getInt() throws Exception {
		int i, temp = 0, mark = 1;
		while ((i = bis.read()) < 45)
			;
		if (i == 45) {
			mark = -1;
			i = bis.read();
		}
		while (i > 47) {
			temp = temp * 10 + i - 48;
			i = bis.read();
		}
		return temp * mark;
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: