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.
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.
The only line of input contains the single integer N (1 <= N <= 18).
Output the numbers that the blanks need to be filled (on separate lines), so that the heritage left for the church is minimized.
2
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;
}