ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1108. Heritage

Romko [Lviv NU] Why this code can't got AC? [2] // Problem 1108. Heritage 12 Mar 2007 21:37
/....bigint class...////
....................
bigint a,b,c,temp;
    a = 2;
    b = 3;
    c = a*b;
    if(n == 1)
    {
        cout << '2';
        return 0;
    }
    cout << "2\n3\n";
    for(int i = 3; i <= n; i++)
    {
        temp = a*b;
        c = temp + 1;
        cout << c << endl;
        a = c;
        b = temp;
    }
................................
////////////////////////////////
Rumter (2) What is multiply wrong? I use Karatsub. [1] // Problem 1108. Heritage 1 Jul 2008 16:16
ubig ubig :: operator * (ubig p)
{
    if (min (n, p.n) < 100)
        return simple_mul (p);
    else
        return karatsuba_mul (p);
}

ubig ubig :: simple_mul (ubig p)
{
    int i, j;

    ubig s;
    for (i = 0; i < n; ++ i)
    {
        ubig row;
        for (j = 0; j < a[i]; ++ j)
            row = row + p;

        row = row << i;
        s = s + row;
    }

    while (s.a.size () > 1 && s.a.back () == 0) s.a.pop_back ();
    s.n = s.a.size ();
    return s;
}

ubig ubig :: karatsuba_mul (ubig p)
{
    int k = max (n, p.n) / 2;

    ubig a = (*this).last (k);
    ubig b = (*this) >> k;
    ubig c = p.last (k);
    ubig d = p >> k;

    ubig ac = a * c;
    ubig bd = b * d;
    ubig abcd = (a + b) * (c + d);

    ubig res = ((abcd - ac - bd) << k) + (bd << 2 * k) + ac;
    return res;
}

I get TL1. I dont understand :-(.
Rumter (2) Re: What is multiply wrong? I use Karatsub. // Problem 1108. Heritage 8 Jul 2008 15:37
My wrong is big constant in simple mul.
Now I get AC! :-)