Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <cmath>
- #include <math.h>
- #include <complex>
- #include <vector>
- using namespace std;
- typedef long long ll;
- typedef complex<double> cd;
- typedef vector<complex<double>> vcd;
- const double PI = acos(-1);
- const ll mod = 998244353;
- int rev(int ind, int bit) {
- int res = 0;
- for (int i = 0; i < bit; i++) {
- if (ind & (1 << i)) {
- res |= (1 << (bit - i - 1));
- }
- }
- return res;
- }
- void fft(vcd& a, int flag) {
- int n = (int)a.size();
- int bit = 0;
- while ((1 << bit) < n) bit++;
- for (int i = 0; i < n; i++) {
- if (i < rev(i, bit))
- swap(a[i], a[rev(i, bit)]);
- }
- for (int step = 2; step <= n; step *= 2) {
- cd wl(cos(2 * PI / step), flag * sin(2 * PI / step));
- for (int i = 0; i < n; i += step) {
- cd w(1, 0);
- for (int j = 0; j < step / 2; j++) {
- cd u = a[i + j], v = a[i + j + step / 2] * w;
- a[i + j] = u + v;
- a[i + j + step / 2] = u - v;
- w *= wl;
- }
- }
- }
- if (flag == -1) {
- for (cd& i : a) i /= n;
- }
- }
- vector<ll> operator*(vector<ll> a, vector<ll> b) {
- vcd a1(a.begin(), a.end());
- vcd b1(b.begin(), b.end());
- int n = 1;
- while (n <= a.size() + b.size()) n <<= 1;
- a1.resize(n);
- b1.resize(n);
- fft(a1, 1);
- fft(b1, 1);
- for (int i = 0; i < n; i++) {
- a1[i] *= b1[i];
- }
- fft(a1, -1);
- a.clear(); a.resize(n);
- for (int i = 0; i < n; i++) {
- a[i] = (ll)(a1[i].real() + 0.5) % mod;
- }
- return a;
- }
- const double eps = 1e-9;
- int main() {
- freopen("multiply.in", "r", stdin);
- freopen("multiply.out", "w", stdout);
- string st1, st2;
- cin >> st1 >> st2;
- int mm = (st1[0] == '-') + (st2[0] == '-');
- if (st1[0] == '-') st1 = st1.substr(1);
- if (st2[0] == '-') st2 = st2.substr(1);
- if (st1[0] == '0' || st2[0] == '0') {
- cout << 0 << endl;
- return 0;
- }
- vector<ll> a1(st1.size()), a2(st2.size());
- for (int i = 0; i < st1.size(); i++) {
- a1[i] = st1[i] - '0';
- }
- for (int i = 0; i < st2.size(); i++) {
- a2[i] = st2[i] - '0';
- }
- a1 = a1 * a2;
- ll carry = 0;
- for (int i = a1.size() - 1; i > 0; i--) {
- a1[i] += carry;
- carry = a1[i] / 10;
- a1[i] %= 10;
- }
- a1[0] += carry;
- string ans;
- for (ll x : a1) ans += to_string(x);
- int len = (int)(st1.size() + st2.size()) - 1;
- string s3 = st1.substr(0, min((int)st1.size(), 8));
- string s4 = st2.substr(0, min((int)st2.size(), 8));
- double x1 = stod(s3), x2 = stod(s4);
- while (x1 + eps > 10) x1 /= 10;
- while (x2 + eps > 10) x2 /= 10;
- double x = x1 * x2;
- if (x - eps > 10) len++;
- while (ans.size() > len) ans.pop_back();
- if (mm % 2) cout << '-';
- cout << ans << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement