POJ 1405 Heritage

Source: Internet
Author: User

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 be 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
Divide a property into n parts, 1/x1, 1/x2, 1/x3 ,..., 1/xn. make x1> x2> x3>...> xn: minimum assets allocated to the church. xn = x1 * x2 *... * xn-1 + 1.
If the first k points are left 1/c, because 1/c> 1/(c + 1)> 1/(c + 2)>... so the next one takes 1/(c + 1), and the remaining 1/(c * (c + 1 )).

 

# Include <stdio. h>
# Include <stdlib. h>
# Define inf 1000000000
# Define maxn100000
Int len1, len2, len3, len [18];
Long a [maxn], B [maxn], ans [maxn], res [18] [maxn];
Void setAns ()
{
For (int I = 0; I <len3; I ++)
{
Ans [I] = 0;
}
}
Void copy ()
{
For (int I = 0; I <len3; I ++)
{
A [I] = ans [I];
}
Len1 = len3;
}
Void multiply ()
{
Int I, j;
SetAns ();
For (I = 0; I <len2; I ++)
{
For (j = 0; j <len1; j ++)
{
Ans [I + j] + = a [j] * B [I];
Ans [I + j + 1] + = ans [I + j]/inf;
If (ans [I + j + 1] & len3 <= I + j + 1)
{
Len3 = I + j + 2;
}
Else if (len3 <= I + j)
{
Len3 = I + j + 1;
}
Ans [I + j] % = inf;
}
}
}
Void plus ()
{
Int r;
For (len2 = 0, r = 1; len2 <len1; len2 ++)
{
R + = a [len2];
If (r> = inf)
{
B [len2] = r-inf;
R = 1;
}
Else
{
B [len2] = r;
R = 0;
}
}
If (r)
{
B [len2 ++] = r;
}
}
Int main ()
{
Int I, j, n;
Scanf ("% d", & n );
A [0] = 2;
Len1 = 1;
Printf ("2 \ n ");
For (I = 2; I <= n; I ++)
{
Plus ();
Printf ("% lld", B [len2-1]);
For (j = len2-2; j> = 0; j --)
{
Printf ("%. 9lld", B [j]);
} Printf ("\ n ");
If (I <n)
{
Multiply ();
Copy ();
}
}
Return 0;
}


Author: ultimater

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.