Advertisement
najim

BigInteger Library (update 1)

Feb 24th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.12 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <cassert>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <iostream>
  9. #include <complex>
  10. using namespace std;
  11.  
  12. #define BIT 10009
  13. #define GRP     5
  14. #define RADIX   100000  // 10^GRP
  15.  
  16. typedef complex<long double> Complex;
  17. long double PI = 2 * acos(0.0L);
  18.  
  19. unsigned revtab[65536];
  20. char fin[4000000];
  21. void init()
  22. {
  23.     for (int x = 0; x < 65536; x++)
  24.     {
  25.         int y = 0;
  26.         for (int i = 0; i < 16; i++)y |= ((x >> i) & 1) << (15 - i);
  27.         revtab[x] = y;
  28.     }
  29. }
  30.  
  31. void FFT(Complex *a, int N, int dir)
  32. {
  33.     int lgN;
  34.     for (lgN = 1; (1 << lgN) < N; lgN++);
  35.     assert((1 << lgN) == N);
  36.     for (unsigned i = 0; i < N; i++)
  37.     {
  38.         unsigned j = ((revtab[i & 65535] << 16) | revtab[i >> 16]) >> (32 - lgN);
  39.         if (i < j) swap(a[i], a[j]);
  40.     }
  41.     for (int s = 1; s <= lgN; s++)
  42.     {
  43.         int m = 1 << s, h = m/2;
  44.         Complex w, w_m = exp(Complex(0, dir*2*PI/m));
  45.         for (int k = 0; k < N; k += m)
  46.         {
  47.             // even=a[k..k+n), odd=a[k+n..k+m)
  48.             w = 1;
  49.             for (int j = 0; j < h; j++)
  50.             {
  51.                 Complex t = w * a[k+h+j];
  52.                 a[k+h+j] = a[k+j] - t;
  53.                 a[k+j] += t;
  54.                 w *= w_m;
  55.             }
  56.         }
  57.     }
  58. }
  59. void parse(vector<Complex> &v, const char *s)
  60. {
  61.     int n = strlen(s);
  62.     int m = (n + GRP-1) / GRP;
  63.     v.resize(m);
  64.     for (int i = 0; i < m; i++)
  65.     {
  66.         int b = n - GRP * i, x = 0;
  67.         for (int a = max(0, b - GRP); a < b; a++)x = x * 10 + s[a] - '0';
  68.         v[i] = x;
  69.     }
  70. }
  71. void Extract(const vector<Complex> &v,int signa,int signb)
  72. {
  73.     int i, N = v.size();
  74.     vector<long long> digits(N + 1, 0);
  75.     long double err = 0;
  76.     for (i = 0; i < N; i++)
  77.     {
  78.         long long x = (long long)(v[i].real() + 0.5);
  79.         err = max(err, abs(x - v[i].real()));
  80.         err = max(err, abs(v[i].imag()));
  81.         digits[i] = x;
  82.     }
  83.     long long c = 0;
  84.     for (i = 0; i < N; i++)
  85.     {
  86.         c += digits[i];
  87.         digits[i] = c % RADIX;
  88.         c /= RADIX;
  89.     }
  90.     //cout << "_______________" << c << endl;
  91.     assert(c == 0);
  92.     for (i = N-1; i > 0 && digits[i] == 0; i--);
  93.     int idx=0,blen=0;
  94.     char buff[30];
  95.     sprintf(buff,"%lld", digits[i]);
  96.     if(signa!=signb)fin[idx++]='-';
  97.     while(buff[blen]!='\0')fin[idx++]=buff[blen++];
  98.     for (i--; i >= 0; i--)
  99.     {
  100.         sprintf(buff,"%.*lld", GRP, digits[i]);
  101.         blen=0;
  102.         while(buff[blen]!='\0')fin[idx++]=buff[blen++];
  103.     }
  104.     fin[idx]='\0';
  105. }
  106. void mul(string &xx,string &yy,int signa,int signb)
  107. {
  108.     vector<Complex> A, B;
  109.     parse(A, xx.c_str());
  110.     parse(B, yy.c_str());
  111.     int N = 1;
  112.     while (N < max(A.size(), B.size())) N *= 2;
  113.     N *= 2;
  114.     A.resize(N);
  115.     B.resize(N);
  116.     FFT(&A[0], N, +1);
  117.     FFT(&B[0], N, +1);
  118.     for (int i = 0; i < N; i++) A[i] *= B[i];
  119.     FFT(&A[0], N, -1);
  120.     for (int i = 0; i < N; i++) A[i] /= N;
  121.     Extract(A,signa,signb);
  122. }
  123. template<class T> string toString(T n)
  124. {
  125.     ostringstream ost;
  126.     ost << n;
  127.     ost.flush();
  128.     return ost.str();
  129. }
  130.  
  131. int iniflag;
  132. struct Bigint
  133. {
  134.     // representations and structures
  135.     string a; // to store the digits
  136.     int sign; // sign = -1 for negative numbers, sign = 1 otherwise
  137.     void getInt()
  138.     {
  139.         a.clear();
  140.         char ch;
  141.         while(true)
  142.         {
  143.             ch=getchar();
  144.             if(ch=='\n' || ch==' ')break;
  145.             a.push_back(ch);
  146.         }
  147.         (*this) = a;
  148.     }
  149.     // constructors
  150.     Bigint() {if(!iniflag){init();iniflag=1;}} // default constructor
  151.     Bigint( string b )
  152.     {
  153.         if(!iniflag){init();iniflag=1;}
  154.         (*this) = b;   // constructor for string
  155.     }
  156.     // some helpful methods
  157.     int size()
  158.     {
  159.         return a.size();   // returns number of digits
  160.     }
  161.     Bigint inverseSign()
  162.     {
  163.         sign *= -1;
  164.         return (*this);
  165.     }
  166.     // changes the sign
  167.     Bigint normalize( int newSign )   // removes leading 0, fixes sign
  168.     {
  169.         for( int i = a.size() - 1; i > 0 && a[i] == '0'; i-- )a.erase(a.begin() + i);
  170.         sign = ( a.size() == 1 && a[0] == '0' ) ? 1 : newSign;
  171.         return (*this);
  172.     }
  173.     void operator = ( int b )
  174.     {
  175.         (*this)=toString(b);
  176.     }
  177.     // assignment operator
  178.     void operator = ( string b )
  179.     {
  180.         a = b[0] == '-' ? b.substr(1) : b;
  181.         reverse( a.begin(), a.end() );
  182.         this->normalize( b[0] == '-' ? -1 : 1 );
  183.         //a.
  184.     }
  185.     // conditional operators
  186.     bool operator < ( const Bigint &b ) const   // less than operator
  187.     {
  188.  
  189.         if( sign != b.sign ) return sign < b.sign;
  190.         if( a.size() != b.a.size() )return sign == 1 ? a.size() < b.a.size() : a.size() > b.a.size();
  191.         for( int i = a.size() - 1; i >= 0; i-- ) if( a[i] != b.a[i] )return sign == 1 ? a[i] < b.a[i] : a[i] > b.a[i];
  192.         return false;
  193.     }
  194.     bool operator < ( const int &b ) const
  195.     {
  196.         return (*this) < toString(b);
  197.     }
  198.     bool operator == ( const Bigint &b ) const   // operator for equality
  199.     {
  200.         return a == b.a && sign == b.sign;
  201.     }
  202.     bool operator == ( const long long &b ) const
  203.     {
  204.         return (*this) == toString(b);
  205.     }
  206. // mathematical operators
  207.     Bigint operator + ( Bigint b )   // addition operator overloading
  208.     {
  209.         if( sign != b.sign ) return (*this) - b.inverseSign();
  210.         Bigint c;
  211.         for(int i = 0, carry = 0; i<a.size() || i<b.size() || carry; i++ )
  212.         {
  213.             carry+=(i<a.size() ? a[i]-48 : 0)+(i<b.a.size() ? b.a[i]-48 : 0);
  214.             c.a += (carry % 10 + 48);
  215.             carry /= 10;
  216.         }
  217.         return c.normalize(sign);
  218.     }
  219.     Bigint operator + ( long long b )
  220.     {
  221.         return (*this) + toString(b);
  222.     }
  223.     Bigint operator - ( Bigint b )   // subtraction operator overloading
  224.     {
  225.         if( sign != b.sign ) return (*this) + b.inverseSign();
  226.         int s = sign;
  227.         sign = b.sign = 1;
  228.         if( (*this) < b ) return ((b - (*this)).inverseSign()).normalize(-s);
  229.         Bigint c;
  230.         for( int i = 0, borrow = 0; i < a.size(); i++ )
  231.         {
  232.             borrow = a[i] - borrow - (i < b.size() ? b.a[i] : 48);
  233.             c.a += borrow >= 0 ? borrow + 48 : borrow + 58;
  234.             borrow = borrow >= 0 ? 0 : 1;
  235.         }
  236.         return c.normalize(s);
  237.     }
  238.     Bigint operator - ( long long  b )
  239.     {
  240.         return (*this) - toString(b);
  241.     }
  242.     Bigint operator * ( Bigint b )   // multiplication operator overloading
  243.     {
  244.         reverse( a.begin(), a.end() );
  245.         reverse( b.a.begin(), b.a.end() );
  246.         mul(a,b.a,this->sign,b.sign);
  247.         reverse( a.begin(), a.end() );
  248.         reverse( b.a.begin(), b.a.end() );
  249.         return Bigint((string)fin);
  250.     }
  251.     Bigint operator * ( long long b )
  252.     {
  253.         return (*this) * toString(b);
  254.     }
  255.  
  256.     Bigint operator / ( Bigint b )   // division operator overloading BI/BI
  257.     {
  258.         if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 );
  259.         Bigint c("0"), d;
  260.         for( int j = 0; j < a.size(); j++ ) d.a += "0";
  261.         int dSign = sign * b.sign;
  262.         b.sign = 1;
  263.         for( int i = a.size() - 1; i >= 0; i-- )
  264.         {
  265.             c.a.insert( c.a.begin(), '0');
  266.             c = c + a.substr( i, 1 );
  267.             while( !( c < b ) ) c = c - b, d.a[i]++;
  268.         }
  269.         return d.normalize(dSign);
  270.     }
  271.     Bigint operator / ( long long n )   // division operator overloading BI/BI
  272.     {
  273.         reverse( a.begin(), a.end() );
  274.         //reverse( b.begin(), b.end() );
  275.         long long int rem=0,i,j=0;
  276.         int f=1;
  277.         int idx=0;
  278.         int len=a.length();
  279.         for(i=0; i<len; i++)
  280.         {
  281.             rem=a[i]-'0'+rem*10;
  282.             if(rem/n!=0)f=0;
  283.             if(f==0)fin[idx++]=((char)((rem/n)+'0'));
  284.             rem=rem%n;
  285.         }
  286.         if(f==1)fin[idx++]='0';
  287.         fin[idx]='\0';
  288.         reverse( a.begin(), a.end() );
  289.         //reverse( b.begin(), b.end() );
  290.         return Bigint((string)fin);
  291.     }
  292.     Bigint operator % ( Bigint b )   // modulo operator overloading
  293.     {
  294.         if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 );
  295.         Bigint c("0");
  296.         b.sign = 1;
  297.         for( int i = a.size() - 1; i >= 0; i-- )
  298.         {
  299.             c.a.insert( c.a.begin(), '0');
  300.             c = c + a.substr( i, 1 );
  301.             while( !( c < b ) ) c = c - b;
  302.         }
  303.         return c.normalize(sign);
  304.     }
  305.     Bigint operator % ( long long n )   // modulo operator overloading
  306.     {
  307.         reverse( a.begin(), a.end() );
  308.         long long int i,rem=0,j;
  309.         int idx=0;
  310.         int len=a.length();
  311.         for(i=0; i<len; i++)
  312.         {
  313.             rem=a[i]-'0'+rem*10;
  314.             rem=rem%n;
  315.         }
  316.         reverse( a.begin(), a.end() );
  317.         return Bigint(toString(rem));
  318.     }
  319.     void print()
  320.     {
  321.         if( sign == -1 ) putchar('-');
  322.         for( int i = a.size() - 1; i >= 0; i-- ) putchar(a[i]);
  323.     }
  324.     Bigint sqrt()
  325.     {
  326.         reverse( this->a.begin(), this->a.end() );
  327.         string a=this->a;
  328.         int idx=0;
  329.         int n = a.size();
  330.         bool f = n%2;
  331.         n = (f)?n+1:n;
  332.         vector <int> num (n, 0);
  333.         int i;
  334.         if (f)for (i = 0; i < n-1; i++)num[i+1] = a[i] - 48;
  335.         else for (i = 0; i < n; i++)num[i] = a[i] - 48;
  336.         n /= 2;
  337.         vector <int> root(n, 0);
  338.         vector <int> recieve;
  339.         for (i = 0; i < n; i++)
  340.         {
  341.             recieve.push_back (num[i*2]);
  342.             recieve.push_back(num[i*2+1]);
  343.             vector <int> cand(i+1, 0);
  344.             int tmp, ac = 0;
  345.             for (int j = i; j > 0; j--)
  346.             {
  347.                 tmp = root[j-1]*2 + ac;
  348.                 cand[j] = tmp%10;
  349.                 ac = tmp/10;
  350.             }
  351.             if (ac) cand[0] += ac;
  352.             int j;
  353.             for (j = 9; j >= 0; j--)
  354.             {
  355.                 int l = 2*(i+1);
  356.                 vector <int>  m (l, 0);
  357.                 m[l-1] = j*j;
  358.                 m[l-2] = m[l-1]/10;
  359.                 m[l-1] %= 10;
  360.                 int k, o;
  361.                 int ac = 0;
  362.                 for (k = l-2, o = i; o >= 0 && k > 0; k--, o--)
  363.                 {
  364.                     m[k] += cand[o]*j;
  365.                     m[k-1] = m[k]/10;
  366.                     m[k] %= 10;
  367.                 }
  368.                 bool flag = 1;
  369.                 for (k = 0; k < l; k++)
  370.                 {
  371.                     if (recieve[k] > m[k]) break;
  372.                     if (recieve[k] < m[k])
  373.                     {
  374.                         flag = 0;
  375.                         break;
  376.                     }
  377.                 }
  378.                 if (flag)
  379.                 {
  380.                     ac = 0;
  381.                     for (k = l-1; k >= 0; k--)
  382.                     {
  383.                         tmp = recieve[k] - m[k] - ac;
  384.                         if (tmp < 0)recieve[k] = tmp+10,ac = 1;
  385.                         else recieve[k] = tmp;
  386.                         ac = 0;
  387.  
  388.                     }
  389.                     break;
  390.                 }
  391.             }
  392.             root[i] = j;
  393.             fin[idx++]=(j+'0');
  394.         }
  395.         fin[idx]='\0';
  396.         reverse( this->a.begin(),this->a.end() );
  397.         //reverse( b.begin(), b.end() );
  398.         return Bigint(string(fin));
  399.     }
  400. };
  401. istream& operator >> (istream &in, Bigint &x)
  402. {
  403.     in >> x.a;
  404.     x=Bigint(x.a);
  405.     return in;
  406. }
  407. ostream& operator << (ostream &out, const Bigint &x)
  408. {
  409.     string p=x.a;
  410.     reverse(p.begin(),p.end());
  411.     if(x.sign==-1)p="-"+p;
  412.     out << p;
  413.     return out;
  414. }
  415. char inp[BIT];
  416. int main()
  417. {
  418. //    Bigint A,B,C,D;
  419. //    A.getInt();//character by character read.needs to care about extra spaces/newlines
  420. //    cin >> B;//string
  421. //    cout << A/B << endl;
  422. //
  423. //    C="-100";
  424. //    D=-400LL;
  425. //    cout << A << " " << B << " " << C << " " << D << endl;
  426. //    cout << A.sign << " " << B.sign << " " << C.sign << " " << D.sign << endl;
  427. //    if(A==B)cout << "A equals to B\n";
  428. //    else if(A<B)cout << "A is less than B\n";
  429. //    else cout << "A is greater than B\n";
  430. //
  431. //    cout << "A+B" << " " << A+B << endl;
  432. //    cout << "A-B" << " " << A-B << endl;
  433. //    cout << "A*B" << " " << A*B << endl;
  434. //    cout << "A/B" << " " << A/B << endl;
  435. //
  436. //
  437. //    cout << A.sqrt() << endl;
  438. //    cout << A << " " << B << " " << A+B << " " << C << " " << (A+B)/C << endl;
  439. //    cout << A%B << endl;
  440. //
  441. //    cout << A+B/C << endl;
  442. //    cout << A%50 << endl;
  443. //
  444. //    int INT=50;
  445. //    cout << B%INT << endl;
  446. //
  447. //    if(A<10)cout << "A is less than 10\n";
  448. //    if(A+15+A<25)cout << A << " + 15 is less than 25" << endl;
  449. //
  450. //    cout << A+17 << " " << A-19 << endl;
  451.     Bigint A = Bigint("99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999");
  452.     Bigint B = Bigint("19999999999999999999999999999999999999999999999999999999999999999999999999999999999999998");
  453.  
  454.     Bigint C = Bigint("0");
  455.     for(int i=0;i<10000;i++)
  456.     {
  457.         C = B * A;
  458.     }
  459.  
  460.     cout << C << "\n\n" << C/B  << endl;
  461.  
  462.     return 0;
  463. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement