Advertisement
Riz1Ahmed

Big Integer Code

Jan 27th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.75 KB | None | 0 0
  1. ///Big Integer (Only For Positive Numbers).
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. typedef int64_t ll;
  6. typedef long long ll;
  7. typedef pair<ll,ll> lll;
  8. typedef pair<ll,int> lli;
  9. typedef pair<int,int> ii;
  10. #define EL printf("\n")
  11. #define OK printf("OK")
  12. #define pb push_back
  13. #define mp make_pair
  14. #define ep emplace_back
  15. #define X  first
  16. #define Y  second
  17. #define fillchar(a,x) memset(a, x, sizeof(a))
  18. #define FOR(i,l,r) for (int i=l;i<=r;i++)
  19. #define FORD(i,r,l) for (int i=r;i>=l;i--)
  20.  
  21. const int base = 1e9;
  22. typedef vector<int> BigInt;
  23.  
  24. void Set(BigInt &a) {
  25.     while (a.size() > 1 && !a.back()) a.pop_back();
  26. }
  27.  
  28. void Print(BigInt a) {
  29.     Set(a);
  30.     printf("%d", !a.size() ? 0 : a.back());
  31.     for (int i=a.size()-2; i>=0; i--)
  32.         printf("%09d", a[i]); EL;
  33. }
  34.  
  35. BigInt Integer(string s) {
  36.     BigInt ans;
  37.     if (s[0] == '-') return ans;
  38.     if (!s.size()) {ans.pb(0); return ans;}
  39.     while (s.size()%9 != 0) s = '0'+s;
  40.     for (int i=0;i<s.size();i+=9) {
  41.         int v = 0;
  42.         for (int j=i;j<i+9;j++) v = v*10+(s[j]-'0');
  43.         ans.insert(ans.begin(),v);
  44.     }
  45.     Set(ans);
  46.     return ans;
  47. }
  48.  
  49. BigInt Integer(char c[]) {
  50.     string s = "";
  51.     FOR(i,0,strlen(c)-1) s = s + c[i];
  52.     return Integer(s);
  53. }
  54.  
  55. BigInt Integer(ll x) {
  56.     string s = "";
  57.     while (x > 0) s = char(x%10+'0') + s, x /= 10;
  58.     return Integer(s);
  59. }
  60.  
  61. BigInt Integer(int x) {
  62.     return Integer((ll) x);
  63. }
  64.  
  65.  
  66. void operator >> (istream &in, BigInt &a) {
  67.     string s;
  68.     getline(cin, s);
  69.     a = Integer(s);
  70. }
  71. void operator << (ostream &out, BigInt a) {Print(a);}
  72. bool operator < (BigInt a, BigInt b) {
  73.     Set(a), Set(b);
  74.     if (a.size() != b.size()) return (a.size() < b.size());
  75.     FORD(i,a.size()-1,0)
  76.         if (a[i] != b[i]) return (a[i] < b[i]);
  77.     return false;
  78. }
  79.  
  80. bool operator >  (BigInt a, BigInt b) {return (b < a);}
  81. bool operator == (BigInt a, BigInt b) {return (!(a < b) && !(b < a));}
  82. bool operator <= (BigInt a, BigInt b) {return (a < b || a == b);}
  83. bool operator >= (BigInt a, BigInt b) {return (b < a || b == a);}
  84. bool operator <  (BigInt a, int b) {return (a < Integer(b));}
  85. bool operator >  (BigInt a, int b) {return (a > Integer(b));}
  86. bool operator == (BigInt a, int b) {return (a == Integer(b));}
  87. bool operator >= (BigInt a, int b) {return (a >= Integer(b));}
  88. bool operator <= (BigInt a, int b) {return (a <= Integer(b));}
  89.  
  90. BigInt max(BigInt a, BigInt b) {return a>b?a:b;}
  91. BigInt min(BigInt a, BigInt b) {return a<b?a:b;}
  92.  
  93. ///Arithmetic Operations
  94. BigInt operator + (BigInt a, BigInt b) {
  95.     Set(a);
  96.     Set(b);
  97.     BigInt ans;
  98.     int carry = 0;
  99.     FOR(i,0,max(a.size(), b.size())-1) {
  100.         if (i < a.size()) carry += a[i];
  101.         if (i < b.size()) carry += b[i];
  102.         ans.pb(carry%base);
  103.         carry /= base;
  104.     }
  105.     if (carry) ans.pb(carry);
  106.     Set(ans);
  107.     return ans;
  108. }
  109. BigInt operator + (BigInt a, int b) {return a+Integer(b);}
  110. void operator += (BigInt &a, BigInt b){a=a+b;}
  111. void operator += (BigInt &a, int b) {a=a+b;}
  112. void operator ++ (BigInt &a) {a+=1;}
  113.  
  114. BigInt operator - (BigInt a, BigInt b) {///Here a>b
  115.     Set(a), Set(b);
  116.     BigInt ans;
  117.     int carry=0;
  118.     FOR(i,0,a.size()-1) {
  119.         carry+=a[i]-(i<b.size() ? b[i]:0);
  120.         if (carry<0) ans.pb(carry+base), carry=-1;
  121.         else ans.pb(carry), carry=0;
  122.     }
  123.     Set(ans);
  124.     return ans;
  125. }
  126. BigInt operator - (BigInt a, int b) {return a-Integer(b);}
  127. void operator -= (BigInt &a, BigInt b) {a=a+b;}
  128. void operator -= (BigInt &a, int b) {a=a-b;}
  129. void operator -- (BigInt &a) {a-=1;}
  130.  
  131. BigInt operator * (BigInt a, BigInt b) {
  132.     Set(a), Set(b);
  133.     BigInt ans;
  134.     ans.assign(a.size()+b.size(), 0);
  135.     FOR(i,0,a.size()-1) {
  136.         ll carry = 0ll;
  137.         for (int j=0;j<b.size() || carry > 0;j++) {
  138.             ll s = ans[i+j] + carry + (ll)a[i]*(j<b.size()?(ll)b[j]:0ll);
  139.             ans[i+j] = s%base;
  140.             carry = s/base;
  141.         }
  142.     }
  143.     Set(ans);
  144.     return ans;
  145. }
  146. BigInt operator *(BigInt a,int b) {return a*Integer(b);}
  147. void operator *= (BigInt &a,BigInt b) {a=a*b;}
  148. void operator *= (BigInt &a,int b) {a=a*b;}
  149.  
  150. BigInt operator / (BigInt a, BigInt b) {
  151.     Set(a), Set(b);
  152.     if (b==Integer(0)) return Integer("-1");
  153.     BigInt ans, cur;
  154.     FORD(i,a.size()-1,0) {
  155.         cur.insert(cur.begin(), a[i]);
  156.         int x = 0, L = 0, R = base;
  157.         while (L<=R) {
  158.             int mid = (L+R)>>1;
  159.             if (b*Integer(mid) > cur)
  160.                 x = mid, R = mid-1;
  161.             else L = mid+1;
  162.         }
  163.         cur = cur - Integer(x-1)*b;
  164.         ans.insert(ans.begin(),x-1);
  165.     }
  166.     Set(ans);
  167.     return ans;
  168. }
  169.  
  170. BigInt operator / (BigInt a, int b) {
  171.     Set(a);
  172.     BigInt ans;
  173.     ll cur = 0ll;
  174.     FORD(i,a.size()-1,0) {
  175.         cur = (cur*(ll)base + (ll)a[i]);
  176.         ans.insert(ans.begin(),cur/b);
  177.         cur %= b;
  178.     }
  179.     Set(ans);
  180.     return ans;
  181. }
  182.  
  183. void operator /= (BigInt &a,BigInt b) {a=a/b;}
  184. void operator /= (BigInt &a,   int b) {a=a/b;}
  185.  
  186.  
  187.  
  188. BigInt operator % (BigInt a, BigInt b) {
  189.     Set(a), Set(b);
  190.     if (b==Integer(0)) return Integer("-1");
  191.     BigInt ans;
  192.     FORD(i,a.size()-1,0) {
  193.         ans.insert(ans.begin(), a[i]);
  194.         int x = 0, L = 0, R = base;
  195.         while (L <= R) {
  196.             int mid = (L+R)>>1;
  197.             if (b*Integer(mid)>ans)
  198.                 x=mid, R=mid-1;
  199.             else L = mid+1;
  200.         }
  201.         ans = ans - Integer(x-1)*b;
  202.     }
  203.     Set(ans);
  204.     return ans;
  205. }
  206.  
  207. int operator % (BigInt a, int b) {
  208.     Set(a);
  209.     if (!b) return -1;
  210.     int ans=0;
  211.     FORD(i,a.size()-1,0)
  212.         ans = (ans*(base%b) + a[i]%b)%b;
  213.     return ans;
  214. }
  215. void operator %= (BigInt &a, BigInt b) {a=a%b;}
  216. void operator %= (BigInt &a, int b) {a=a%Integer(b);}
  217.  
  218. BigInt gcd(BigInt a, BigInt b) {
  219.     Set(a), Set(b);
  220.     while (b>Integer(0)) {
  221.         BigInt r = a%b;
  222.         a=b, b=r;
  223.     }
  224.     Set(a);
  225.     return a;
  226. }
  227. BigInt lcm(BigInt a, BigInt b) {return (a*b/gcd(a,b));}
  228.  
  229. BigInt sqrt(BigInt a) {
  230.     BigInt x=a, y=(a+1)/2;
  231.     while (y<x)
  232.         x=y, y=(y+a/y)/2;
  233.     return x;
  234. }
  235.  
  236. BigInt pow(BigInt a, BigInt b) {
  237.     if (b==Integer(0)) return Integer(1);
  238.     BigInt tmp=pow(a, b/2);
  239.     return (!(b%2) ? tmp*tmp:tmp*tmp*a);
  240. }
  241. BigInt pow(BigInt a, int b) {return pow(a,(Integer(b)));}
  242.  
  243. BigInt pow(BigInt a, BigInt b,BigInt mod) {
  244.     if (b==Integer(0)) return Integer(1);
  245.     BigInt tmp=pow(a, b/2, mod);
  246.     if (!(b%2)) return (tmp*tmp)%mod;
  247.     else return (((tmp*tmp)%mod)*a)%mod;
  248. }
  249.  
  250. int log(int n, BigInt a) { /// log_n(a)
  251.     Set(a);
  252.     int ans=0;
  253.     while (a>Integer(1)) ans++, a/=n;
  254.     return ans;
  255. }
  256. ll Pow(ll a,ll n,ll M){
  257.     if (n==1) return a;
  258.     ll x=Pow(a,n/2,M);
  259.     x=(x*x)%M;
  260.     if (n&1) x=(x*a)%M;
  261.     return (x*x)%M;
  262. }
  263. int main(){
  264.     ll t,x,y,z;
  265.     scanf("%lld",&t);
  266.     while(t--){
  267.         scanf("%lld %lld %lld",&x,&y,&z);
  268.         BigInt a=Integer(x),b=Integer(y),n=Integer(z);
  269.         M=Integer(1000000007), ab=a-b,ans;
  270.         if (ab==Integer(0))
  271.             ans=(pow(a,n,M)+pow(b,n,M))%M;
  272.         else
  273.             ans=(pow(a,n,ab)+pow(b,n,ab))%M,
  274.             ans=gcd(ans,ab)%M;
  275.         cout<<ans;puts("");
  276.     }
  277.     return 0;
  278. }
  279.  
  280. //int main(){
  281. //    BigInt B;  cin >> B;
  282. //    BigInt A = Integer("123456789");
  283. //    BigInt C = Integer(123456789ll);
  284. //    int x; x = 123456789;
  285. //
  286. //    if (B<=A)  cout << A - B;
  287. //    else cout << "-"<< B - A;
  288. //
  289. //    cout << A + B; Print(A + x);
  290. //    cout << A * B; Print(A * x);
  291. //    cout << A / B; Print(A / x);
  292. //    cout << A % B; printf("%d\n", A % x);
  293. //
  294. //    ++A, C=A, ++B, C+=B+x;
  295. //    Print(A); Print(B); Print(C);
  296. //
  297. //    cout<<max(A,B); cout<<min(A,B);
  298. //    cout<<gcd(A,B); cout<<lcm(A,B);
  299. //    cout<<sqrt(A);
  300. //    printf("%d %d %d\n", log(2,A), log(10,B), log(5,C));
  301. //
  302. //    A = Integer(16); x = 12;
  303. //    cout << pow(A,B);
  304. //    cout << pow(A,x);
  305. //    return 0;
  306. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement