nyoj 数论 541最强DE 战斗力

踏实做事,安心做人,这是最后的斗争,英特纳雄奈尔一定会实现。


题目分析:这题是一个数论题,第一开始我自己找规律的时候,发现任何一个正整数分解的字数都能转为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;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值