Last active
June 3, 2018 12:48
-
-
Save maple3142/1140d78464d9f8dd6c964cdb2797d1e1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <sstream> | |
| #include <cstring> | |
| #include <iomanip> | |
| #include <algorithm> | |
| using namespace std; | |
| typedef unsigned long long int ll; | |
| const int basecnt = 7; | |
| const int base = 10000000; | |
| const int MX = 30000; | |
| template <typename T> | |
| class bigint { | |
| public: | |
| T ar[MX]; | |
| int len = 0; | |
| bigint() | |
| { | |
| ar[len++] = 0; | |
| } | |
| bigint(T n) | |
| { | |
| while (n > 0) | |
| { | |
| ar[len++] = n % base; | |
| n /= base; | |
| } | |
| } | |
| bigint(string& s) | |
| { | |
| //reverse(s.begin(), s.end()); | |
| int slen = s.length(); | |
| int l = slen / basecnt + 1; | |
| int off = l * basecnt - slen; | |
| for (int i = 0; i < l; i++) | |
| { | |
| int x = 0; | |
| //for (int j = basecnt-1; j>=0&&(i * basecnt + j-off)>=0 ; j--) | |
| for (int j = 0; j < basecnt; j++) | |
| { | |
| if ((i * basecnt + j - off) < 0) | |
| continue; | |
| x *= 10; | |
| x += s[i * basecnt + j - off] - '0'; | |
| } | |
| ar[l - 1 - (len++)] = x; | |
| } | |
| } | |
| inline void stripzero() | |
| { | |
| while (ar[len - 1] == 0 && len > 0) | |
| len--; | |
| } | |
| bigint operator*(const bigint& b) | |
| { | |
| bigint<T> c; | |
| memset(c.ar, 0, sizeof(c.ar)); | |
| c.len = this->len + b.len; | |
| for (int i = 0; i < this->len; i++) | |
| { | |
| for (int j = 0; j < b.len; j++) | |
| { | |
| c.ar[i + j] += this->ar[i] * b.ar[j]; | |
| } | |
| } | |
| for (int i = 0; i < c.len; i++) | |
| { | |
| c.ar[i + 1] += c.ar[i] / base; | |
| c.ar[i] %= base; | |
| } | |
| c.stripzero(); | |
| return c; | |
| } | |
| friend ostream& operator<<(ostream& stream, const bigint& v) | |
| { | |
| stream << v.ar[v.len - 1]; | |
| for (int i = v.len - 2; i >= 0; --i) | |
| { | |
| if (v.ar[i] < 10) | |
| stream << "000000"; | |
| else if (v.ar[i] < 100) | |
| stream << "00000"; | |
| else if (v.ar[i] < 1000) | |
| stream << "0000"; | |
| else if (v.ar[i] < 10000) | |
| stream << "000"; | |
| else if (v.ar[i] < 100000) | |
| stream << "00"; | |
| else if (v.ar[i] < 1000000) | |
| stream << "0"; | |
| stream << v.ar[i]; | |
| } | |
| return stream; | |
| } | |
| }; | |
| int main() | |
| { | |
| ios::sync_with_stdio(false); | |
| cin.tie(0); | |
| char k; | |
| cin >> k >> k >> k; | |
| if (k == 'l') | |
| { | |
| string ka, kb; | |
| cin >> ka >> kb; | |
| bigint<ll> a(ka), b(kb); | |
| cout << a * b << endl; | |
| } | |
| else | |
| { | |
| string ka; | |
| int b; | |
| cin >> ka >> b; | |
| bigint<ll> a(ka), ans(1); | |
| while (b > 0) { | |
| if (b % 2 == 1) | |
| ans = ans * a; | |
| a = a * a; | |
| b /= 2; | |
| } | |
| cout << ans << endl; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment