BZOJ 1263: [SCOI2006]整数划分

/*
* 分析:
* 贪心+高精度。。。尽量凑3,不足凑2
*
* */

http://www.lydsy.com/JudgeOnline/problem.php?id=1263 

import java.util.Scanner;
import java.math.*;

public class Main{
	public static void main(String [] args){
		BigInteger ans;
		BigInteger th = BigInteger.valueOf(3);
		int n;
		
		Scanner cin = new Scanner(System.in);

		while(cin.hasNext()){
			n = cin.nextInt();
			if(n==1){
				System.out.println(1);
				System.out.println(1);
				continue;
			}
			ans = BigInteger.ONE;
			int three = 0;
			while(n>4){
				three ++;
				n -= 3;
			}
			if(n==4||n==2)
				ans = ans.multiply(BigInteger.valueOf(n));
			else if(n==3)
				three ++;
			for(int i=0;i<three;i++)
				ans = ans.multiply(th);
			String s = ans.toString();
			System.out.println(s.length());
			if(s.length()>100){
				for(int i=0;i<100;i++)
					System.out.print(s.charAt(i));
				System.out.println();
			}
			else
				System.out.println(ans);
		}
	}
}

  

原文地址:https://www.cnblogs.com/yejinru/p/2823022.html