POJ1405Heritage大树相乘

当一位富有的家族成员去世并留下遗嘱时,如何合理分配遗产给亲属和教会,确保分配方式既遵循遗嘱意愿又使得留给教会的遗产量最小?本文提供了一个解决此类问题的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

H - Heritage
Crawling in process... Crawling failed Time Limit:1000MS    Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

<pre name="code" class="cpp">#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
    //数组a储存an-1,数组sum存在an
    int i,j,k,p,m,N,a[50000]= {0},sum[50000]= {0};
    scanf("%d",&N);
    a[0]=2;
    printf("2\n");
    for(i=1,m=1; i<N; i++)
    {
        for(j=0; j<m; j++)
        {
            for(k=0; k<m; k++)
            {
                if(k==0)//k=0时即是个位数,要减1,代表(an-1 - 1)
                    sum[j+k]+=a[j]*(a[k]-1);
                else
                    sum[j+k]+=a[j]*a[k];
            }
        }
        m=j+k;//求和之后的和的最大位数
        sum[0]+=1;
        for(j=0; j<m; j++)//进位
        {
            if(sum[j]>9)
            {
                sum[j+1]+=sum[j]/10;
                sum[j]%=10;

            }
            a[j]=sum[j];//把结果赋给a[j],继续上述循环过程
        }
        for(k=m; sum[k]==0; k--);
        m=k+1;//缺失了不AC
        for(j=k; j>=0; j--)
        {
            printf("%d",sum[j]);
            sum[j]=0;//清除每次sum[j]带来的影响
        }
        printf("\n");
    }
    return 0;
}

这个数列的前几项如下:


2


3


7


43


1807


3263443


10650056950807


113423713055421844361000443


12864938683278671740537145998360961546653259485195807


165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443


通项:a[i]=2(i=0)

a[i]=a[i-1]*(a[i-1]-1)+1;

 
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值