P1919 FFT加速高精度乘法
P1919 FFT加速高精度乘法
传送门:https://www.luogu.org/problemnew/show/P1919
题意:
给出两个n位10进制整数x和y,你需要计算x*y。
题解:
对于十进制数我们可以将其转换成
\(a0*10^0+a1*10^1+a2*10^2...an*10^n\)
那么对于两个数,我们就可以求出两个的系数表示后得到a的点乘式和b的点乘式
最后得到的答案就是a和b的多项式的系数,这个问题O(n)扫一遍,
处理一下输出即可
代码:
#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 1e6 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1.0);
LL quick_pow(LL x, LL y) {
LL ans = 1;
while(y) {
if(y & 1) {
ans = ans * x % mod;
} x = x * x % mod;
y >>= 1;
} return ans;
}
struct complex {
double x, y;
complex(double xx = 0, double yy = 0) {
x = xx, y = yy;
}
} a[maxn], b[maxn];
complex operator + (complex a, complex b) {
return complex(a.x + b.x, a.y + b.y);
}
complex operator - (complex a, complex b) {
return complex(a.x - b.x, a.y - b.y);
}
complex operator * (complex a, complex b) {
return complex(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
int n, m;
int l, r[maxn];
int limit = 1;
void fft(complex *A, int type) {
for(int i = 0; i < limit; i++) {
if(i < r[i]) swap(A[i], A[r[i]]);
}
for(int mid = 1; mid < limit; mid <<= 1) {
complex Wn(cos(Pi / mid), type * sin(Pi / mid));
for(int R = mid << 1, j = 0; j < limit; j += R) {
complex w(1, 0);
for(int k = 0; k < mid; k++, w = w * Wn) {
complex x = A[j + k], y = w * A[j + mid + k];
A[j + k] = x + y;
A[j + k + mid] = x - y;
}
}
}
}
int ans[maxn];
char numA[maxn], numB[maxn];
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
int n;
while(scanf("%d", &n) != EOF) {
scanf("%s %s", numA, numB);
// debug3(n,numA,numB);
int lena = 0;
int lenb = 0;
for(int i = n - 1; i >= 0; i--) {
a[lena++].x = numA[i] - '0';
}
for(int i = n - 1; i >= 0; i--) {
b[lenb++].x = numB[i] - '0';
}
while(limit < n + n) limit <<= 1, l++;
for(int i = 0; i <= limit; i++) {
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));
}
fft(a, 1);
fft(b, 1);
for(int i = 0; i <= limit; i++) {
a[i] = a[i] * b[i];
}
fft(a, -1);
int tot = 0;
for(int i = 0; i <= limit; i++) {
ans[i] += (int)(a[i].x / limit + 0.5);
if(ans[i] >= 10) {
ans[i + 1] += ans[i] / 10;
ans[i] %= 10;
limit += (i == limit);
}
}
while(!ans[limit] && limit >= 1) limit--;
limit++;
while(--limit >= 0) cout << ans[limit];
}
return 0;
}
P1919 FFT加速高精度乘法的更多相关文章
- SPOJ - VFMUL - Very Fast Multiplication FFT加速高精度乘法
SPOJ - VFMUL:https://vjudge.net/problem/SPOJ-VFMUL 这是一道FFT求高精度的模板题. 参考:https://www.cnblogs.com/Rabbi ...
- FFT实现高精度乘法
你应该知道$FFT$是用来处理多项式乘法的吧. 那么高精度乘法和多项式乘法有什么关系呢? 观察这样一个$20$位高精度整数$11111111111111111111$ 我们可以把它处理成这样的形式:$ ...
- BZOJ2179: FFT快速傅立叶 FFT实现高精度乘法
Code: #include <cstdio> #include <algorithm> #include <cmath> #include <cstring ...
- HDU 1402 A * B Problem Plus (FFT求高精度乘法)
A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU - 1402 A * B Problem Plus (FFT实现高精度乘法)
题意:计算A*B,A,B均为长度小于50000的整数. 这是FFT在大整数相乘中的一个应用,我本来想用NTT做的,但NTT由于取模很可能取炸,所以base必须设得很小,而且效率也比不上FFT. A和B ...
- 高精度乘法(FFT)
学会了FFT之后感觉自己征服了世界! 当然是幻觉... 不过FFT还是很有用的,在优化大规模的动规问题的时候有极大效果. 一般比较凶残的计数动规题都需要FFT(n<=1e9). 下面是高精度乘法 ...
- [vijos P1040] 高精度乘法
如果这次noip没考好,完全是因为从7月29日之后就没有再写过程序了.说起来,真是一个泪流满面的事实… 那这样一个弱智题练手恢复代码能力,竟然还花了我两个晚上(当然不是两整个晚上…) 第一天TLE了, ...
- 【PKU1001】Exponentiation(高精度乘法)
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 145642 Accepted: 35529 ...
- hdu 1042 N!(高精度乘法 + 缩进)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 题目大意:求n!, n 的上限是10000. 解题思路:高精度乘法 , 因为数据量比较大, 所以 ...
随机推荐
- Ubuntu里node命令无效解决方法
在ubuntu里用sudo apt-get install nodejs安装Node.js后, 会发现terminals里运行node命令(比如node –-version)时候会有No such f ...
- 【JZOJ4859】【NOIP2016提高A组集训第7场11.4】连锁店
题目描述 Dpstr开了个饮料连锁店,连锁店共有n家,出售的饮料种类相同.为了促销,Dpstr决定让每家连锁店开展赠送活动.具体来说,在第i家店,顾客可以用ai个饮料瓶兑换到bi瓶饮料和1个纪念币(注 ...
- 对The Curse of Dimensionality(维度灾难)的理解
一个特性:低维(特征少)转向高维的过程中,样本会变的稀疏(可以有两种理解方式:1.样本数目不变,样本彼此之间距离增大.2.样本密度不变,所需的样本数目指数倍增长). 高维度带来的影响: 1.变得可分. ...
- IDEA-创建WEB项目并部署Tomcat
一.创建简单web项目 1.创建一个web project File -> new Project ->选择project sdk 为1.6(如果没有sdk的同学请先配置)-> Ne ...
- 【Leetcode链表】两两交换链表中的节点(24)
题目 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表.你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2->3->4, 你应该返回 2- ...
- C# —— 枚举
一.使用枚举的优点 枚举能够使代码更加的清晰,它允许使用描述性的名称表示整数值. 枚举使代码更易于维护,有助于确保给变量指定合法的.期望的值. 枚举使代码更易输入. 二.枚举说明 1.简单枚举 枚举使 ...
- 2018-7-21-win10-uwp-调用-Microsoft.Windows.Photos_8wekyb3d8bbwe-应用
title author date CreateTime categories win10 uwp 调用 Microsoft.Windows.Photos_8wekyb3d8bbwe 应用 linde ...
- keep-alive vue组件缓存避免多次加载相应的组件
keep-alive vue组件缓存避免多次加载相应的组件
- Android Http实现文件的上传和下载
最近做一个项目,其中涉及到文件的上传和下载功能,大家都知道,这个功能实现其实已经烂大街了,遂.直接从网上荡了一堆代码用,结果,发现网上的代码真是良莠不齐,不是写的不全面,就是有问题,于是自己重新整理了 ...
- Python 基础03 序列
sequence 序列 sequence(序列) 是一组有顺序的元素的集合 (严格的说,是对象的集合,但鉴于我们还没有引入"对象" 概念,暂时说元素) 序列可以包含一个或多个元素, ...