heritage的做題過程

題目
排名
狀態
統計
提問
17:Heritage
查看 提交 統計 提問
總時間限制: 5000ms 內存限制: 65536kB
描述
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.
輸入
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
剛開始以爲他比較簡單,結果發現這題的數的位數極多,
這道題的規律比較好找,f【i】=f【i-1】*(f【i-1】-1)+1;
但是,寫代碼一算就能看出來,用long long的話算到第八位就不準了,要用到大數的乘法,大數的乘法算法有不少,高級的分治法不太會使,但用普通的模擬列豎式的那種算法也能行,時間是5秒。觀察一下個位上的數字,會發現這樣的規律,2,3,7,3,7,3不會出現個位爲0或爲9的情況,因而可以在最後輸出的時候直接在表示個位的數組元素上加一就可以,由於用long long可以算到第七個數,因而可以先算出前七個作爲初始數據,最後還要解決表示大數的數組要開多大的問題,第七個數是14位,兩個十四位數相乘最多的位數爲28位,那麼算到第十八個數時最多的位數爲2^11乘以14,2028乘以14約爲三萬,所以數組元素應該有三萬個。

#include<iostream>
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n;
ll f[8]={0,2,3,7,43,1807,3263443,10650056950807};
int p[31000]={0,7,0,8,0,5,9,6,5,0,0,5,6,0,1};
int pp[31000]={0,6,0,8,0,5,9,6,5,0,0,5,6,0,1};
//int p[101]={0,5,1};
//int pp[101]={0,5,1};
void xc(int *a,int *b,int len,int t)
{
    if(t==0) return ;
    int c[31000]={0};
    int x=0;
   /* int lena=aa.length();
    int lenb=bb.length();*/
   /* for(int i=0;i<=lena-1;i++)
        a[lena-i]=a[i]-48;
    for(int i=0;i<=lenb-1;i++)
        b[lenb-i]=b[i]-48;*/
    for(int i=1;i<=len;i++)
    {
        x=0;
        for(int j=1;j<=len;j++)
        {
            c[i+j-1]=a[i]*b[j]+x+c[i+j-1];
            x=c[i+j-1]/10;
            c[i+j-1]=c[i+j-1]%10;
        }
        c[i+len]=x;
    }
    int lenc=len+len;
    while(c[lenc]==0&&lenc>1)
    {
        lenc--;
    }
    int cc[31000];
    for(int i=lenc;i>1;i--)
    {
        cout<<c[i];
        cc[i]=c[i];
    }
    cc[1]=c[1];
    cc[0]=0;
    cout<<c[1]+1;
    if(t!=1) cout<<endl;
    c[1]=c[1]+1;
    xc(cc,c,lenc,t-1);
}
int main()
{
    cin>>n;
    if(n<=7)
    {
       for(int i=1;i<=n;i++)
        cout<<f[i]<<endl;
    }
    else
    {
        for(int i=1;i<=7;i++)
            cout<<f[i]<<endl;
       int t=n-7;
       xc(p,pp,14,t);
    }
     return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章