Cod sursa(job #1394741)

Utilizator valiro21Valentin Rosca valiro21 Data 20 martie 2015 16:58:09
Problema Patrate2 Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <fstream>
#include <vector>

using namespace std;

class nrm {
public:
    vector<int> v;

    nrm () {
        v.push_back (0);
    }

    nrm (int x) {
        while (x)
            v.push_back (x%10),
            x /= 10;
    }

    void operator = (int x) {
        v.clear ();
        while (x)
            v.push_back (x%10),
            x /= 10;
    }

    void operator *= (nrm b) {
        vector<int> c;
        for (int i = 0; i < v.size (); i++)
            for (int j = 0, t = 0, val; j < b.v.size () || t; j++, t /= 10) {
                val = (t += v[i] * b.v[j]) % 10;
                if (i + j >= c.size ())
                    c.push_back (val);
                else
                    c[i+j] += val;
            }
        v.clear ();
        v = c;
    }

    friend ostream& operator<< (ostream& fout, const nrm x) {
        for (int i = x.v.size ()-1; i >= 0; i--)
            fout << x.v[i];
        return fout;
    }
};

nrm lgpow (nrm base, int pw) {
    if (pw == 1)
        return base;

    nrm x = lgpow (base, pw/2);
    x *= x;
    if (pw&1)
        x *= base;
    return x;
}

int main()
{
    ifstream fin ("patrate2.in");
    ofstream fout ("patrate2.out");
    int n;
    fin >> n;
    nrm n2 (1), fact (1), two(2);
    for (int i = 1; i <= n; i++) {
        nrm x (i);
        fact *= x;
    }
    n2 = lgpow (two, n * n);
    fact *= n2;

    fout << fact;

    return 0;
}