题目链接: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;
}

SPOJ VFMUL - Very Fast Multiplication (FFT)的更多相关文章

  1. SPOJ - VFMUL - Very Fast Multiplication FFT加速高精度乘法

    SPOJ - VFMUL:https://vjudge.net/problem/SPOJ-VFMUL 这是一道FFT求高精度的模板题. 参考:https://www.cnblogs.com/Rabbi ...

  2. spoj VFMUL FFT快速傅立叶变换模板题

    题意:求两个数相乘. 第一次写非递归的fft,因为一个数组开小了调了两天TAT. #include<iostream> #include<cstring> #include&l ...

  3. spoj Fast Multiplication

    题意:乘法 要用nlogn的fft乘法. //#pragma comment(linker,"/STACK:1024000000,1024000000") #include< ...

  4. SPOJ TSUM Triple Sums(FFT + 容斥)

    题目 Source http://www.spoj.com/problems/TSUM/ Description You're given a sequence s of N distinct int ...

  5. 2018.11.18 spoj Triple Sums(容斥原理+fft)

    传送门 这次fftfftfft乱搞居然没有被卡常? 题目简述:给你nnn个数,每三个数ai,aj,ak(i<j<k)a_i,a_j,a_k(i<j<k)ai​,aj​,ak​( ...

  6. Hamming Weight的算法分析(转载)

    看代码时遇到一个求32bit二进制数中1的个数的问题,感觉算法很奇妙,特记录学习心得于此,备忘. 计算一个64bit二进制数中1的个数. 解决这个问题的算法不难,很自然就可以想到,但是要给出问题的最优 ...

  7. $\mathcal{FFT}$·$\mathcal{Fast \ \ Fourier \ \ Transformation}$快速傅立叶变换

    \(2019.2.18upd:\) \(LINK\) 之前写的比较适合未接触FFT的人阅读--但是有几个地方出了错,大家可以找一下233 啊-本来觉得这是个比较良心的算法没想到这么抽搐这个算法真是将一 ...

  8. Algorithm: 多项式乘法 Polynomial Multiplication: 快速傅里叶变换 FFT / 快速数论变换 NTT

    Intro: 本篇博客将会从朴素乘法讲起,经过分治乘法,到达FFT和NTT 旨在能够让读者(也让自己)充分理解其思想 模板题入口:洛谷 P3803 [模板]多项式乘法(FFT) 朴素乘法 约定:两个多 ...

  9. SPOJ 4206 Fast Maximum Matching (二分图最大匹配 Hopcroft-Carp 算法 模板)

    题目大意: 有n1头公牛和n2头母牛,给出公母之间的m对配对关系,求最大匹配数.数据范围:  1 <= n1, n2 <= 50000, m <= 150000 算法讨论: 第一反应 ...

随机推荐

  1. 6、基于highcharts实现的线性拟合,计算部分在java中实现,画的是正态概率图

    1.坐标点类 package cn.test.domain; public class Point { double x; double y; public Point(){ } public Poi ...

  2. echarts数据包坐标拾取工具

    http://geojson.io/#map=4/37.20/103.45

  3. Linux NIO 系列(04-3) epoll

    目录 一.why epoll 1.1 select 模型的缺点 1.2 epoll 模型优点 二.epoll API 2.1 epoll_create 2.2 epoll_ctl 2.3 epoll_ ...

  4. [已解决]报错:xlrd.compdoc.CompDocError: Workbook: size exceeds expected 17920 bytes; corrupt?

    报错代码如下: filePath='test.xls' data=pd.read_excel(filePath) print(data.head()) 报错内容如下: Traceback (most ...

  5. 给Laravel4添加中文语系(转)

    Laravel 4 官方不附带英文以外的 validataion 错误信息翻译. 今天发现GitHub 上有一个 repository 收集不同的翻译,大家可以下载需要的翻译. GitHub项目地址: ...

  6. 微服务-技术专区-链路追踪(pinpoint)-部署使用

    https://naver.github.io/pinpoint/ https://github.com/naver/pinpoint 背景 随着项目微服务的进行,微服务数量逐渐增加,服务间的调用也越 ...

  7. svnlook - Subversion 仓库检索工具

    SYNOPSIS 总览 svnlook command /path/to/repos [options] [args] OVERVIEW 概述 Subversion 是一个版本控制系统,允许保存旧版本 ...

  8. Android Studio在Ubuntu下离线安装Gradle

    更新android studio3.0后又要升级gradle了,估计又要很长时间,晚上临走前跟开始更新下载,第二天一早发现又卡了,吐血. 在某CSDN下载gradle-4.1-all.zip,直接手动 ...

  9. Codeforces 1149C 线段树 LCA

    题意:给你一个括号序列,这个括号序列将确定一颗二叉树.有q次询问,每次询问输出这颗树的直径. 思路:https://blog.csdn.net/Huah_2018/article/details/89 ...

  10. rabbitMQ 问题

    1.有时候在学习或者测试的时候,发现我在一个EXCHANGE  上面绑定了多个通道,这些通道的ROUTING_KEY 各不相同.但是从发送端 发到EXCHANGE 时,却在别的通道上面也收到了该消息, ...