Cod sursa(job #58819)

Utilizator crawlerPuni Andrei Paul crawler Data 7 mai 2007 16:01:53
Problema Patrate2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 6.79 kb
#include <stdio.h>
#include <vector>
#include <string>
#include <cmath>
#define base 1000000000
#define base2 ((ull) base * base)
#define KARATSUBA 111
#define fix(x) {x.resize(x[0]+3); x[x[0]+1]=x[x[0]+2]=0;}
#define ull unsigned long long

using namespace std;

class BigInt{
public:
      vector <unsigned> a;  
      int semn;       
      BigInt();
      BigInt(long long nr);
      BigInt& operator+=(const BigInt &b);   
      friend BigInt operator+(const BigInt &x, const BigInt &y);
      friend BigInt operator-(const BigInt &x, const BigInt &y);
      friend BigInt operator*(const BigInt &x, const BigInt &y);
      void shl(unsigned k);
      void write(FILE *f) const;
      void read(FILE *f);
};

BigInt::BigInt()
{
       a.resize(4);
       a[0]=1;         
}

BigInt::BigInt(long long nr)
{
       if (nr<0) semn=-1, nr=-nr;
       unsigned long long k=nr;
       a.resize(1);
       a[0]=0;
       while (k>=base) a.push_back(k%base), k/=base;
       a.push_back(k);
       a[0]=a.size()-1;
       fix(a);
}

void BigInt::shl(unsigned k)
{
        unsigned i;
        a[0]+=k;
        a.resize(a[0]+2);
        for (i=a[0]; i>k; i--)
            a[i]=a[i-k];
        memset(&a[1], 0, 4*k);
}

BigInt& BigInt::operator+=(const BigInt &b)
{
     if (this == &b){       //in cazul ca in care fac a+=a;
        unsigned i, n=a[0]+1;
        if (n<=a.size()) a.resize(n+1);
        for (i=1; i<n; i++) 
            a[i]+=a[i];
        for (i=1; i<n; i++)
            if (a[i]>=base) a[i]-=base, a[i+1]++;
        if (a[n]) a[0]++;
        return *this;
     }
     unsigned i, n=b.a[0]+1;
     if (n>=a.size()) a.resize(n+1);
     for (i=1; i<n; ++i)
         if (a[i]+b.a[i]>=base) a[i]+=(b.a[i]-base), a[i+1]++; else a[i]+=b.a[i];
     if (a[n]) n++;
     if (n-1>a[0]) a[0]=n-1;
     fix(a);
     return *this;
}

BigInt operator+(const BigInt &x, const BigInt &y)
{
       BigInt rez;
       const unsigned *a, *b;
       if (x.a[0]>y.a[0]) a=&x.a[0], b=&y.a[0]; else a=&y.a[0], b=&x.a[0];       
       unsigned n=a[0]+1, m=b[0]+1;
       rez.a.resize(a[0]+2);
       memcpy(&rez.a[0], a, sizeof(unsigned) * (n+1));
       for (unsigned i=1; i<m; i++){
           rez.a[i]+=b[i];
           if (rez.a[i]>=base) rez.a[i]-=base, ++rez.a[i+1];
       }
       unsigned k=m;
       while (rez.a[k]>=base) rez.a[k]-=base, ++k, ++rez.a[k];
       if (rez.a[a[0]+1]) rez.a[0]++;
       fix(rez.a);
       return rez;
}

BigInt operator-(const BigInt &x, const BigInt &y)
{
       BigInt rez;            
       const unsigned *a = &x.a[0], *b = &y.a[0];
       unsigned n=a[0]+1, m=b[0]+1;
       rez.a.resize(a[0]+3);
       memcpy(&rez.a[0], a, sizeof(unsigned) * (n+1));
       for (unsigned i=1; i<m; i++){
           if (rez.a[i]>base) rez.a[i]+=base, --rez.a[i+1];
           rez.a[i]-=b[i];     
           if (rez.a[i]>base) rez.a[i]+=base, --rez.a[i+1];
       }
       unsigned k=m;
       while (rez.a[k]>base) rez.a[k]+=base, ++k, --rez.a[k];
       while (!rez.a[rez.a[0]] && rez.a[0]>1) rez.a[0]--;
       fix(rez.a);
       return rez;
}

BigInt operator*(const BigInt &x, const BigInt &y)
{                          
       const unsigned *a, *b;
       if (x.a[0]>y.a[0]) a=&x.a[0], b=&y.a[0]; else a=&y.a[0], b=&x.a[0];       
       unsigned n=a[0]+1, m=b[0]+1, i, j, k;
       unsigned limit=75*n/100;
       if (m>KARATSUBA && m<limit){
          m--; n--;
          BigInt rez, A, B;
          A.a.resize(m+3);
          rez.a.resize(n+m+3);
          const BigInt *C;
          if (x.a[0]>y.a[0]) C=&y; else C=&x;
          A.a[0]=m;
          k=1;
          while (k<=n){
                if (k+m-1>n){
                   A.a[0]=n-k+1; fix(A.a);
                }
                memcpy(&A.a[1], a+k, 4*A.a[0]);
                B=A*(*C);
                for (i=1; i<=B.a[0]; i++){
                    rez.a[i+k-1]+=B.a[i];
                    if (rez.a[i+k-1]>=base) rez.a[i+k-1]-=base, ++rez.a[i+k];
                }
                k+=m;
          }
          n+=m;
          while (n>1 && !rez.a[n]) --n;
          rez.a[0]=n;
          rez.semn=x.semn*y.semn;
          fix(rez.a);
          return rez;
       }
       if (n>KARATSUBA && m>=limit){  
          BigInt x1, x2, y1, y2, A, B, C;
          unsigned p=(n+m)/4;
          x2.a.resize(p+4);
          x2.a[0]=p;
          memcpy(&x2.a[1], a+1, 4*p);
          x1.a.resize(n-p+4);
          x1.a[0]=n-p-1;
          memcpy(&x1.a[1], a+p+1, 4*(n-p-1));
          y2.a.resize(p+4);
          y2.a[0]=p;
          memcpy(&y2.a[1], b+1, 4*p);
          y1.a.resize(m-p+4);
          y1.a[0]=m-p-1;
          memcpy(&y1.a[1], b+p+1, 4*(m-p-1));
          A=x1*y1;
          B=x2*y2;
          C=(x1+x2)*(y1+y2)-A-B;
          A.shl(2*p);
          memcpy(&A.a[1], &B.a[1], sizeof(int)*2*p);
          C.shl(p);
          A+=C;
          A.semn=x.semn*y.semn;
          fix(A.a);
          return A;
       }
       vector <ull> sol(n+m+1); 
       sol[0]=n+m-3;
       for (i=1; i<m; i++)
           for (j=1; j<n; ){
               k = i+j-1;
               sol[k] += (ull) b[i] * a[j++];
               if (sol[k]>=base2) sol[k]-=base2, ++sol[k+2]; k++;
           }
       BigInt rez;
       rez.a.resize(sol[0]+3);
       for (i=0; i<n+m; i++)
           if (sol[i]>=base2) sol[i]-=base2, ++sol[i+2];
       for (i=0; i<n+m; i++){
           register unsigned cat=sol[i]/base;
           sol[i+1]+=cat;
           rez.a[i]=sol[i]-cat*(ull)base;
       }
       if (rez.a[rez.a[0]+1]) rez.a[0]++;
       fix(rez.a);
       rez.semn=x.semn * y.semn;
       return rez;
}

void BigInt::write(FILE *f) const
{
     int i,n=a[0];
     fprintf(f, "%u", a[n]);
     for (i=n-1; i>0; i--)
         fprintf(f, "%.9u", a[i]);
     printf("\n");
}

void BigInt::read(FILE *f)
{
     char c=0;
     while (c<'0' || c>'9') c=fgetc(f); //trebuie sa fie sigur ca e un numar de citit aici
     string s="000000000"; 
     while (c>='0' && c<='9') s+=c, c=fgetc(f);
     int i, j, n=s.length()/9, lung=s.length()%9;
     a.resize(n+2);
     memset(&a[0], 0, sizeof(unsigned)*(n+2));
     if (!lung) lung=9, n--;
     for (i=1; i<=n; i++)
         for (j=lung; j<lung+9; j++)
             a[i]=a[i]*10 + (s[(n-i)*9+j]-'0');        
     a[0]=n;
     while (!a[a[0]] && a[0]>0) a[0]--;
}


int main()
{
    freopen("patrate2.in", "r", stdin);
    freopen("patrate2.out", "w", stdout);

    int n,i;

    scanf("%d", &n);
    BigInt fact,put,doi;
    fact = 1;
    for(i=2;i<=n;++i)
     fact = fact * i;

    doi = 2;
    put = 1;

    n *= n;
    
    while(n)
     {
      if(n&1)
       put=put*doi;
      doi=doi*doi;
      n=n/2;
     }
     
    put=put*fact;

    put.write(stdout);
    
    return 0;
}