Advertisement
Riz1Ahmed

BigInteger By Me (Riz1ahmed)

Nov 29th, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.93 KB | None | 0 0
  1. ///Full instruction at bottom
  2. ///AC sub: codeforces.com/problemsets/acmsguru/submission/99999/66034902
  3. ///Tested: codeforces.com/contest/1263/submission/66026737
  4. #include<bits/stdc++.h>
  5. using ll=long long int;
  6. using namespace std;
  7.  
  8. struct Bigint {
  9.     string a;
  10.     int sign;
  11.  
  12.     Bigint(){}
  13.     void operator = (string b) {
  14.         a= (b[0]=='-' ? b.substr(1) : b);
  15.         reverse(a.begin(), a.end());
  16.         (*this).Remove0(b[0]=='-' ? -1 : 1);
  17.     }
  18.     Bigint(string x) {(*this)=x;}
  19.     Bigint(ll x) {(*this)=to_string(x);}
  20.     void operator = (ll x){*this=to_string(x);}
  21.  
  22.     char operator[](int i){return a[i];}
  23.     int size() {return a.size();}
  24.     Bigint inverseSign() {sign*=-1; return (*this);}
  25.  
  26.     Bigint Remove0(int newSign) {
  27.         sign = newSign;
  28.         for(int i=a.size()-1; i>0 && a[i]=='0'; i--) a.pop_back();
  29.         if(a.size()==1 && a[0]=='0') sign=1;
  30.         return (*this);
  31.     }
  32.  
  33.     bool operator == (Bigint x) {return sign==x.sign && a==x.a;}
  34.     bool operator == (string x) {return *this==Bigint(x);}
  35.     bool operator == (ll x)     {return *this==Bigint(x);}
  36.     bool operator != (Bigint x) {return !(*this==x);}
  37.     bool operator != (string x) {return !(*this==x);}
  38.     bool operator != (ll x)     {return !(*this==x);}
  39.  
  40.     bool operator < (Bigint b) {
  41.         if (sign!=b.sign) return sign<b.sign;
  42.         if(a.size()!=b.size()) return a.size()*sign<b.size()*sign;
  43.         for(int i=a.size()-1; i>=0; i--)
  44.             if(a[i] != b[i]) return a[i]<b[i];
  45.         return false;
  46.     }
  47.     bool operator <  (string x) {return *this<Bigint(x);}
  48.     bool operator <  (ll x)     {return *this<Bigint(x);}
  49.     bool operator <= (Bigint b) {return *this==b || *this<b;}
  50.     bool operator <= (string b) {return *this==b || *this<b;}
  51.     bool operator <= (ll b)     {return *this==b || *this<b;}
  52.     bool operator >  (Bigint b) {return !(*this==b || *this<b);}
  53.     bool operator >  (string x) {return !(*this==x || *this<x);}
  54.     bool operator >  (ll b)     {return !(*this==b || *this<b);}
  55.     bool operator >= (Bigint b) {return *this==b || *this>b;}
  56.     bool operator >= (string b) {return *this==b || *this>b;}
  57.     bool operator >= (ll b)     {return *this==b || *this>b;}
  58.  
  59.     Bigint operator + (Bigint b) {
  60.         if(sign != b.sign) return (*this)-b.inverseSign();
  61.         Bigint sum;
  62.         for(int i=0, carry=0; i<a.size() || i<b.size() || carry; i++){
  63.             if (i<a.size()) carry+=a[i]-'0';
  64.             if (i<b.size()) carry+=b[i]-'0';
  65.             sum.a += (carry % 10 + 48);
  66.             carry /= 10;
  67.         }
  68.         return sum.Remove0(sign);
  69.     }
  70.     Bigint operator +  (string x) {return *this+Bigint(x);}
  71.     Bigint operator +  (ll x)     {return *this+Bigint(x);}
  72.     Bigint operator ++ (int) {*this+=1; return *this-1;}
  73.     Bigint operator ++ ()    {*this+=1; return *this;}
  74.       void operator += (Bigint x) {*this = *this+x;}
  75.       void operator += (string x) {*this = *this+x;}
  76.       void operator += (ll x)     {*this = *this+x;}
  77.  
  78.  
  79.     Bigint operator - ( Bigint b ) {
  80.         if(sign != b.sign) return (*this)+b.inverseSign();
  81.         if(*this < b) return (b - *this).inverseSign();
  82.         Bigint sub;
  83.         for(int i=0,borrow=0; i<a.size(); i++) {
  84.             borrow = a[i]-borrow-(i<b.size() ? b.a[i] : '0');
  85.             sub.a += borrow>=0 ? borrow+'0' : borrow + 58;
  86.             borrow = borrow>=0 ? 0:1;
  87.         }
  88.         return sub.Remove0(sign);
  89.     }
  90.     Bigint operator - (string x) {return *this-Bigint(x);}
  91.     Bigint operator - (ll x)     {return *this-Bigint(x);}
  92.     Bigint operator -- (int) {*this-=1; return *this+1;}
  93.     Bigint operator -- ()    {*this-=1; return *this;}
  94.       void operator -= (Bigint x) {*this = *this-x;}
  95.       void operator -= (string x) {*this = *this-x;}
  96.       void operator -= (ll x)     {*this = *this-x;}
  97.  
  98.     Bigint operator * (Bigint b) {
  99.         Bigint mult("0");
  100.         for(int i=0, k=a[i]; i<a.size(); i++, k=a[i]) {
  101.             while(k-- -'0') mult=mult+b;
  102.             b.a.insert(b.a.begin(),'0');
  103.         }
  104.         return mult.Remove0(sign * b.sign);
  105.     }
  106.     Bigint operator * (string x) {return *this*Bigint(x);}
  107.     Bigint operator * (ll x)     {return *this*Bigint(x);}
  108.       void operator *= (Bigint x) {*this = *this*x;}
  109.       void operator *= (string x) {*this = *this*x;}
  110.       void operator *= (ll x)     {*this = *this*x;}
  111.  
  112.     Bigint operator / (Bigint b) {
  113.         if(b.size()==1 && b[0]=='0') b.a[0]/=(b[0]-'0');
  114.         Bigint c("0"), d;
  115.         for(int j=0; j<a.size(); j++) d.a += "0";
  116.         int dSign = sign*b.sign; b.sign=1;
  117.         for(int i=a.size()-1; i>=0; i--) {
  118.             c.a.insert(c.a.begin(),'0');
  119.             c=c+a.substr(i,1);
  120.             while(!(c<b)) c=c-b, d.a[i]++;
  121.         }
  122.         return d.Remove0(dSign);
  123.     }
  124.     Bigint operator / (string x) {return *this/Bigint(x);}
  125.     Bigint operator / (ll x)     {return *this/Bigint(x);}
  126.       void operator /= (Bigint x) {*this = *this/x;}
  127.       void operator /= (string x) {*this = *this/x;}
  128.       void operator /= (ll x)     {*this = *this/x;}
  129.  
  130.     Bigint operator % (Bigint b) {
  131.         if( b.size()==1 && b[0]=='0') b.a[0]/=(b[0]-'0') ;
  132.         Bigint c("0");
  133.         int cSign = sign*b.sign; b.sign=1;
  134.         for( int i=a.size()-1; i>=0; i-- ) {
  135.             c.a.insert( c.a.begin(),'0');
  136.             c = c+a.substr(i,1);
  137.             while(!(c<b)) c=c-b;
  138.         }
  139.         return c.Remove0(cSign);
  140.     }
  141.     Bigint operator % (string x) {return *this%Bigint(x);}
  142.     Bigint operator % (ll x)     {return *this%Bigint(x);}
  143.       void operator %= (Bigint x) {*this = *this%x;}
  144.       void operator %= (string x) {*this = *this%x;}
  145.       void operator %= (ll x)     {*this = *this%x;}
  146.  
  147.     void print() {
  148.         if(sign==-1) putchar('-');
  149.         for(int i=a.size()-1; i>=0; i--) putchar(a[i]);
  150.     }
  151.     friend istream& operator >>(istream &in,Bigint &x){
  152.         string s; in>>s; x=s; return in;
  153.     }
  154.     friend ostream& operator <<(ostream &out,Bigint &x){
  155.         if(x.sign==-1) putchar('-');
  156.         for(int i=x.size()-1; i>=0; i--)
  157.             putchar(x[i]);
  158.         return out;
  159.     }
  160.  
  161.     friend Bigint pow(Bigint base,Bigint pw){
  162.         Bigint ans=1;
  163.         while(pw!=0){
  164.             if(pw%2 !=0) ans*=base;
  165.             base*=base, pw/=2;
  166.         }
  167.         return ans;
  168.     }
  169.     friend Bigint pow(Bigint a, Bigint b,Bigint mod) {
  170.         if (b==0) return Bigint(1);
  171.         Bigint tmp=pow(a,b/2,mod);
  172.         if ((b%2)==0) return (tmp*tmp)%mod;
  173.         else return (((tmp*tmp)%mod)*a)%mod;
  174.     }
  175.     friend Bigint sqrt(Bigint x) {
  176.         Bigint ans=x,tmp=(x+1)/2;
  177.         while (tmp<ans) ans=tmp, tmp=(tmp+x/tmp)/2;
  178.         return ans;
  179.     }
  180.     friend Bigint gcd(Bigint a,Bigint b){
  181.         return a%b==0 ? b : gcd(b, a%b);
  182.     }
  183.     friend Bigint lcm(Bigint a,Bigint b){
  184.         return a/gcd(a,b);
  185.     }
  186.  
  187. /**
  188. Here I used most of the arithmetic (+,-,/,*)
  189. logic from the LightOJ forum, written by Jane Alom Jan.
  190. Because whether I write the code and exist there some bugs.
  191.  
  192. This BigInteger as the same as the integer data type. But it
  193. has an unlimited range (actually limit of a string size).
  194. Because here used string as a number in BigInt. It doesn't
  195. support initializing value when declaring a variable. suppose,
  196. BigInt a=123, b="3435"; will not work.
  197. But you can use as,
  198. BitInt a("123"), b=BigInt("3435"), c=BigInt(3435);
  199.  
  200. Supported operations are:
  201. Arithmetic: +,+=,X++,++X, -,-=,X--,--X, *,*=, /,/=.
  202. Logical: <, <=, >. >=, ==, !=.
  203. Functions: pow(base,pw), pow(base,pw,mod), sqrt() ect.
  204.  
  205. 3 way to possible initialize value:
  206. i. Using BigInt. (n=b)
  207. ii. Using string. (n="1234")
  208. iii. Using Long Long int or Int. (n=12445)
  209. Also support cin>> to input from user. (cin>>a>>b;)
  210.  
  211. 2 way to possible output:
  212. i. using cout. (cout<<a<<b<<c<<endl;)
  213. ii. using built-in print function. (a.print())
  214.  
  215. */
  216. };
  217. int main(){
  218.     Bigint a,b,n,ans,M=1000000007;
  219.     cin>>a>>b>>n;
  220.     ans=pow(a,b,n);
  221.     cout<<ans<<endl;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement