题目链接:VFMUL - Very Fast Multiplication

Description

Multiply the given numbers.

Input

n [the number of multiplications <= 101]

l1 l2 [numbers to multiply (at most 300000 decimal digits each)]

Text grouped in [ ] does not appear in the input file.

Output

The results of multiplications.

Example

Input:
5
4 2
123 43
324 342
0 12
9999 12345 Output:
8
5289
110808
0
123437655

Warning: large Input/Output data, be careful with certain languages

Solution

题意

求两数的乘积

思路

FFT

FFT 求高精度乘法的模板题。

Code

#include <bits/stdc++.h>
using namespace std; const double PI = acos(-1);
typedef complex<double> Complex;
const int maxn = 3e6 + 10; Complex a[maxn], b[maxn];
int m, n;
int bit = 2, rev[maxn];
int ans[maxn]; void get_rev(){
memset(rev, 0, sizeof(rev));
while(bit <= n + m) bit <<= 1;
for(int i = 0; i < bit; ++i) {
rev[i] = (rev[i >> 1] >> 1) | (bit >> 1) * (i & 1);
}
} void FFT(Complex *a, int op) {
for(int i = 0; i < bit; ++i) {
if(i < rev[i]) swap(a[i], a[rev[i]]);
}
for(int mid = 1; mid < bit; mid <<= 1) {
Complex wn = Complex(cos(PI / mid), op * sin(PI / mid));
for(int j = 0; j < bit; j += mid<<1) {
Complex w(1, 0);
for(int k = 0; k < mid; ++k, w = w * wn) {
Complex x = a[j + k], y = w * a[j + k + mid];
a[j + k] = x + y, a[j + k + mid] = x - y;
}
}
}
} int main() {
int T;
scanf("%d", &T);
while(T--) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
string s1, s2;
cin >> s1 >> s2;
n = s1.size(), m = s2.size();
for(int i = 0; i < n; ++i) {
a[i] = s1[n - i - 1] - '0';
}
for(int i = 0; i < m; ++i) {
b[i] = s2[m - i - 1] - '0';
}
get_rev();
FFT(a, 1);
FFT(b, 1);
for(int i = 0; i <= bit; ++i) {
a[i] *= b[i];
}
FFT(a, -1);
for(int i = 0; i < n + m; ++i) {
ans[i] = (int)(a[i].real() / bit + 0.5);
}
for(int i = 1; i < n + m; ++i) {
ans[i] = ans[i] + ans[i - 1] / 10;
ans[i - 1] = ans[i - 1] % 10;
}
int s = n + m - 1;
for(; s >= 0; --s) {
if(ans[s]) break;
}
if(s < 0) printf("0\n");
else {
for(int i = s; i >= 0; --i) {
printf("%d", ans[i]);
}
printf("\n");
}
}
return 0;
}

最新文章

  1. shell实现ping命令查看哪些主机在线
  2. CSS高效开发实战:CSS 3、LESS、SASS、Bootstrap、Foundation --读书笔记(3)线性渐变
  3. putty-不输入密码直接登陆
  4. MyEclipse 2013优化配置【转】
  5. 你应该了解的jquery 验证框架
  6. 3A. Shortest path of the king
  7. yiiwheels.widgets.datetimepicker.WhDateTimePicker language
  8. mongodb创建副本集命令
  9. HDU 4970 Killing Monsters
  10. Windows Azure使用体验
  11. SenchaTouch2.3.1 正在使用listpaging以及pullrefresh插入 分页演示样品做
  12. 实现Windows程序的数据更新
  13. Fastjson 专题
  14. Java 字符流文件读写
  15. nginx springboot配置
  16. python版接口自动化测试框架源码完整版(requests + unittest)
  17. Vue中的~(静态资源处理)
  18. SQL Server profile使用技巧
  19. POI (Apache POI)
  20. VBS数组导出到Excel

热门文章

  1. Android 测试点归纳总结
  2. js对div取值与赋值
  3. 关于audio不能拖放
  4. msf generate exec payload
  5. Gradle教程
  6. sql server日志传送实践(基于server 2008 R2)
  7. 【小程序】获取到的e.target与e.currentTarget区别
  8. python基础之基础数据类型1
  9. 在并发Java应用程序中检测可见性错误
  10. 事件循环--eventloop