POJ 1405 Heritage(模拟+高精度乘法)

本文详细探讨了POJ 1405题目,主要涉及如何通过模拟解决复杂问题,并重点讲解了在算法中实施高精度乘法的方法。通过实例分析,深入理解模拟和高精度计算在编程竞赛中的应用。

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

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

题解:

比较简单的一道高精度乘法题目,好久没做了,一开始做只是除10就TLE了,后来老刘改成了除10000就过了,原来还有这种操作,比赛的时候写了好一会,看来以后也要准备这个模板了

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<deque>
using namespace std;
int a[30005];
int c[18][30005];
int w[20];//储存位数
int main()
{
    int i,j,k,n,t;
    for(i=4;i<18;i++)
        memset(c[i],0,sizeof(c[i]));
    c[0][0]=2;
    c[1][0]=3;
    c[2][0]=7;
    c[3][0]=43;
    w[0]=1;
    w[1]=1;
    w[2]=1;
    w[3]=1;
    for(i=4;i<18;i++)
    {
        for(j=0;j<w[i-1];j++)
            a[j]=c[i-1][j];
        a[0]--;
        for(j=0;j<w[i-1];j++)
        {
            for(k=0;k<w[i-1];k++)
            {
                c[i][j+k]+=(a[k]*c[i-1][j]);
                c[i][j+k+1]+=(c[i][j+k]/10000);//改成了除10000就过了
                c[i][j+k]%=10000;
            }
        }
        c[i][0]++;
        t=2*w[i-1]-1;//乘法的出来的没有更新的位数为2*w[i-1]-1
        while(c[i][t]!=0)
        {
            if(c[i][t]>=10000)
            {
                c[i][t+1]+=(c[i][t]/10000);
                c[i][t]%=10000;
            }
            t++;
        }
        w[i]=t;
    }
    while(scanf("%d",&n)!=EOF)
    {
        for(i=0;i<n;i++)
        {
            printf("%d",c[i][w[i]-1]);//第一个数字特殊处理
            for(j=w[i]-2;j>=0;j--)
                printf("%04d",c[i][j]);
            printf("\n");
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值