POJ---1405

Heritage
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6629 Accepted: 2442

Description

Your rich uncle died recently, and the heritage needs to be divided among your relatives and the church (your uncle insisted in his will that the church must get something). There are N relatives (N <= 18) that were mentioned in the will. They are sorted in descending order according to their importance (the first one is the most important). Since you are the computer scientist in the family, your relatives asked you to help them. They need help, because there are some blanks in the will left to be filled. Here is how the will looks:

Relative #1 will get 1 / ... of the whole heritage,
Relative #2 will get 1 / ... of the whole heritage,
---------------------- ...
Relative #n will get 1 / ... of the whole heritage.

The logical desire of the relatives is to fill the blanks in such way that the uncle's will is preserved (i.e the fractions are non-ascending and the church gets something) and the amount of heritage left for the church is minimized.

Input

The only line of input contains the single integer N (1 <= N <= 18).

Output

Output the numbers that the blanks need to be filled (on separate lines), so that the heritage left for the church is minimized.

Sample Input

2

Sample Output

2
3
import java.util.Scanner;
public class Main {
	public static final long limit = 100000000 ;
	public static final int maxLen = 4000;
	public static long[][] heritage = new long[19][maxLen];
	public static long[][] h = new long[19][maxLen];
	public static void main(String[] args){
		h[1][0]=1;
		h[1][1]=1;
		heritage[1][0]=1;
		heritage[1][1]=2;
		for(int i=2;i<19;i++){
			for(int j=1;j<=heritage[i-1][0];j++){
				for(int k=1;k<=h[i-1][0];k++){
					long ans = heritage[i-1][j]*h[i-1][k];
					heritage[i][j+k-1]+=ans;
					h[i][j+k-1]+=ans;
				}
			}
			heritage[i][1]++;
			int j;
			for(j=1;j<maxLen;j++){
				if(heritage[i][j]>=limit){
					heritage[i][j+1]+=heritage[i][j]/limit;
					heritage[i][j]%=limit;
				}
			}
			j=maxLen-1;
			while(j>0&&heritage[i][j]==0) j--;
			heritage[i][0]=j;
			
			for(j=1;j<maxLen;j++){
				if(h[i][j]>=limit){
					h[i][j+1]+=h[i][j]/limit;
					h[i][j]%=limit;
				}
			}
			j=maxLen-1;
			while(j>0&&h[i][j]==0) j--;
			h[i][0]=j;
			
		}
		Scanner cin = new Scanner(System.in);
		int N = cin.nextInt();
		for(int i=1;i<=N;i++){
			for(int j=(int)heritage[i][0];j>0;j--){
				if(j==(int)heritage[i][0]){
					System.out.print(heritage[i][j]);
				}else{
					long l = limit/10;
					while(l>1&&heritage[i][j]<l){
						System.out.print(0);
						l/=10;
					}
					System.out.print(heritage[i][j]);
				}
			}
			System.out.println();
		}
	}
}

Memory: 6444K Time: 250MS
Language: Java Result: Accepted

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值