简体   繁体   English

长数字。 师

[英]Long numbers. Division

world! 世界! I have a problem. 我有个问题。 Today I tried to create a code, which finds Catalan number. 今天,我试图创建一个代码,该代码可以找到加泰罗尼亚语数字。 But in my program can be long numbers. 但是在我的程序中可以是长数字。 I found numerator and denominator. 我发现了分子和分母。 But i can't div long numbers! 但是我不能div长数字! Also, only standard libraries was must use in this program. 另外,在此程序中只能使用标准库。 Help me please. 请帮帮我。 This is my code 这是我的代码

#include <vector>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    const int base = 1000*1000*1000;
    vector <int> a, b;
    int n, carry = 0;
    cin>>n;
    a.push_back(n);                     
    for (int ii=n+2; ii!=(2*n)+1;++ii) { 
        carry = 0;                      
        for (size_t i=0; i<a.size() || carry; ++i) {
            if (i == a.size())
                a.push_back (0);
            long long cur = carry + a[i] * 1ll * ii;
            a[i] = int (cur % base);
            carry = int (cur / base);
        }
    }
    while (a.size() > 1 && a.back() == 0) a.pop_back();

    b.push_back(n);                
    for (int ii=1; ii!=n+1;++ii) {
        carry = 0;
        for (size_t i=0; i<b.size() || carry; ++i) {
            if (i == b.size())
                b.push_back (0);
            long long cur = carry + b[i] * 1ll * ii;
            b[i] = int (cur % base);
            carry = int (cur / base);
        }
    }
    while (b.size() > 1 && b.back() == 0) b.pop_back();

    cout<<(a.empty() ? 0 : a.back());
    for (int i=(int)a.size()-2; i>=0; --i) cout<<(a[i]);

    cout<<"   ";

    cout<<(b.empty() ? 0 : b.back());
    for (int i=(int)b.size()-2; i>=0; --i) cout<<(b[i]);
    //system("PAUSE");
    cout<<endl;
    return 0;
}

PS Sorry for my bad english =) PS对不起,我的英语不好=)

You don't have to calculate (2n)! 您不必计算(2n)! in order to calculate (2n)!/(n!(n+1)!)), because you can use the recurrence relation given in this link : 为了计算(2n)!/(n!(n + 1)!)),因为您可以使用此链接中给出的递归关系:

C(0)=1 C(0)= 1
C(n)=(4n-2)C(n-1)/(n+1) C(n)=(4n-2)C(n-1)/(n + 1)

This gives you the first 15 terms using just 32-bit arithmetic. 这使您仅使用32位算术即可获得前15个项。 And you can generate up to 16384 terms just by using multiplication and division of an arbitrary-length integer by a 16-bit integer. 仅通过将任意长度的整数除以16位整数就可以生成多达16384个项。 This is much easier than general arbitrary-precision arithmetic, and might well be set as a homework task. 这比一般的任意精度算术容易得多,并且很可能被设置为一项家庭作业。

It will be very difficult implement division in your representation of long numbers, but it is real. 用长数表示很难实现除法,但这是真实的。 In my opinion, the simplest method is Long_division . 我认为,最简单的方法是Long_division

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM