提交记录 20874


用户 题目 状态 得分 用时 内存 语言 代码长度
GoldenFishX 1004a. 【模板题】高精度乘法2 Accepted 100 4.027 ms 1364 KB C++11 1.70 KB
提交时间 评测时间
2024-01-27 17:39:50 2024-01-27 17:39:53
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 3e6 + 5;

const double Pi = acos(-1);

struct cpl {
  double x, y;

  cpl operator + (const cpl& a) const {return {x + a.x, y + a.y};}
  cpl operator - (const cpl& a) const {return {x - a.x, y - a.y};}
  cpl operator * (const cpl& a) const {return {x * a.x - y * a.y, x * a.y + y * a.x};}
} a[MAXN], b[MAXN];

int r[MAXN];

void FFT(int n, cpl *a, int t) {
  for (int i = 0; i < n; ++i) {
    if (r[i] < i) swap(a[i], a[r[i]]); 
  }
  for (int k = 1; k < n; k <<= 1) {
    cpl G = {cos(Pi / k), t * sin(Pi / k)};
    for (int i = 0, o = k << 1; i < n; i += o) {
      cpl now = {1, 0};
      for (int j = 0; j < k; ++j, now = now * G) {
        cpl x = a[i + j], y = now * a[i + k + j];
        a[i + j] = x + y;
        a[i + k + j] = x - y;
      }
    }
  }
}

int ans[MAXN];

int main() {
  ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
  string s1, s2;
  cin >> s1 >> s2;
  int n = s1.size(), m = s2.size();
  for (int i = 0; i < n; ++i) {
    a[i].x = s1[n - i - 1] - '0';
    a[i].y = 0;
  }
  for (int i = 0; i < m; ++i) {
    b[i].x = s2[m - i - 1] - '0';
    b[i].y = 0;
  }
  int nm = 1, len = 0, nn = n + m - 1;
  while (nm < nn) nm <<= 1, ++len;
  for (int i = 0; i < nm; ++i) {
    r[i] = (r[i >> 1] >> 1) | ((i & 1) << (len - 1));
  }
  FFT(nm, a, 1);
  FFT(nm, b, 1);
  for (int i = 0; i <= nm; ++i) {
    a[i] = a[i] * b[i];
  }
  FFT(nm, a, -1);
  for (int i = 0; i < nn; ++i) {
    ans[i] = a[i].x / nm + 0.5;
  }
  int lst = 0;
  for (int i = 0; i < nn || ans[i] > 0; ++i) {
    if (ans[i] > 9) {
      ans[i + 1] += ans[i] / 10;
      ans[i] %= 10; 
    }
    lst = i;
  }
  for (int i = lst; i > -1; --i) {
    cout << ans[i];
  }
  return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #14.027 ms1 MB + 340 KBAcceptedScore: 100


Judge Duck Online | 评测鸭在线
Server Time: 2024-05-05 09:54:13 | Loaded in 2 ms | Server Status
个人娱乐项目,仅供学习交流使用