SPOJ VFMUL - Very Fast Multiplication (FFT)
题目链接: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)的更多相关文章
- SPOJ - VFMUL - Very Fast Multiplication FFT加速高精度乘法
SPOJ - VFMUL:https://vjudge.net/problem/SPOJ-VFMUL 这是一道FFT求高精度的模板题. 参考:https://www.cnblogs.com/Rabbi ...
- spoj VFMUL FFT快速傅立叶变换模板题
题意:求两个数相乘. 第一次写非递归的fft,因为一个数组开小了调了两天TAT. #include<iostream> #include<cstring> #include&l ...
- spoj Fast Multiplication
题意:乘法 要用nlogn的fft乘法. //#pragma comment(linker,"/STACK:1024000000,1024000000") #include< ...
- SPOJ TSUM Triple Sums(FFT + 容斥)
题目 Source http://www.spoj.com/problems/TSUM/ Description You're given a sequence s of N distinct int ...
- 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( ...
- Hamming Weight的算法分析(转载)
看代码时遇到一个求32bit二进制数中1的个数的问题,感觉算法很奇妙,特记录学习心得于此,备忘. 计算一个64bit二进制数中1的个数. 解决这个问题的算法不难,很自然就可以想到,但是要给出问题的最优 ...
- $\mathcal{FFT}$·$\mathcal{Fast \ \ Fourier \ \ Transformation}$快速傅立叶变换
\(2019.2.18upd:\) \(LINK\) 之前写的比较适合未接触FFT的人阅读--但是有几个地方出了错,大家可以找一下233 啊-本来觉得这是个比较良心的算法没想到这么抽搐这个算法真是将一 ...
- Algorithm: 多项式乘法 Polynomial Multiplication: 快速傅里叶变换 FFT / 快速数论变换 NTT
Intro: 本篇博客将会从朴素乘法讲起,经过分治乘法,到达FFT和NTT 旨在能够让读者(也让自己)充分理解其思想 模板题入口:洛谷 P3803 [模板]多项式乘法(FFT) 朴素乘法 约定:两个多 ...
- SPOJ 4206 Fast Maximum Matching (二分图最大匹配 Hopcroft-Carp 算法 模板)
题目大意: 有n1头公牛和n2头母牛,给出公母之间的m对配对关系,求最大匹配数.数据范围: 1 <= n1, n2 <= 50000, m <= 150000 算法讨论: 第一反应 ...
随机推荐
- 测试过程中bug分类
测试的核心任务是发现bug.在这之前是分析需求,之后是跟踪bug.跳出具体的项目来看,所有的bug无非是以下五大类. 软件没有实现应该实现的功能:如指定的登录功能. 软件出现了本应该避免的错误:如用户 ...
- 用 Flask 来写个轻博客 (10) — M(V)C_Jinja 常用过滤器与 Flask 特殊变量及方法
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 Jinja 中常用的过滤器 default float int len ...
- 模板引擎的简单原理template
var templateStr = "我的名字叫<%=name%>我是一只小狗,今年<%=age%>岁."; var data = { name:'旺财 ...
- markdown_TestOne
这个是我写的一个markdown尝试 1.2 dafsdfeasdfaefasdfase afsdfasdfefasdfeadfasdfe
- Python科学计算:用NumPy快速处理数据
创建数组 import numpy as np a=np.array([1,2,3]) b=np.array([[1,2,3],[4,5,6],[7,8,9]]) b[1,1]=10 print(a. ...
- CF322F
CF322F 拉格朗日插值 #include<iostream> #include<cstdio> #include<algorithm> #include< ...
- CF1239
然后ZUTTER_打的第一场div1以没敢交题 完!美!结!束!!! A 没有发现性质就找规律海星 我们可以算出一列的贡献:\(g[i][0]\)表示上两个不同,\(g[i][1]\)表示上两个相同就 ...
- OpenGL学习——绘制第一个三角形
终于把三角形绘制出来了,首先一些关键概念.操作. Vertex Data 顶点数据 VBO Vertex Buffer Objects 顶点缓冲对象 VA ...
- Samba服务的安装
Samba的安装 1.准备环境 Centos7 [root@localhost ~]# systemctl stop firewalld [root@localhost ~]# setenforce ...
- 解决Addin开发不能断点调试的问题
ArcMap或CAD在调试C#二次开发的插件的时候,在一些情况下不能正常进入到断点中,原因是debugger type不对应. eg. 右键单击Solution Explorer,选择Add-> ...