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_JUDGEFIN
#endifint 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;
}

转载于:https://www.cnblogs.com/buerdepepeqi/p/11235390.html

P1919 FFT加速高精度乘法相关推荐

  1. FFT实现高精度乘法

    你应该知道$FFT$是用来处理多项式乘法的吧. 那么高精度乘法和多项式乘法有什么关系呢? 观察这样一个$20$位高精度整数$11111111111111111111$ 我们可以把它处理成这样的形式:$ ...

  2. 朴素高精度乘法的常数优化

    2015年辽宁省赛热身赛有一道高精度乘法 传送门:NEUOJ 1574 A*B 1574: A * B 时间限制: 10 Sec  内存限制: 128 MB 题目描述 Calculate $a \ti ...

  3. dp进阶之FFT加速+数据结构优化+不等式优化

    快速傅里叶变换 快速傅里叶变换(英语:Fast Fourier Transform, FFT),是快速计算序列的离散傅里叶变换(DFT)或其逆变换的方法.傅里叶分析将信号从原始域(通常是时间或空间)转 ...

  4. HDU5322 - cdq分治FFT加速dp

    5322 Hope [CDQ分治FFT加速计算dp] 题意 每一个每一个排列,排列中每个数向它后面第一个比它大的数连一条边. 每个排列对于答案的贡献是这个排列所生成的图中的每一个联通量中点的个数的平方 ...

  5. /* program p5_04_AC 《聪明人的游戏提高篇》 1307:【例1.3】高精度乘法

    /* program p5_04_AC <聪明人的游戏提高篇>  1307:[例1.3]高精度乘法 http://ybt.ssoier.cn:8088/problem_show.php?p ...

  6. 信息学奥赛一本通 1307:【例1.3】高精度乘法 | 1174:大整数乘法 | OpenJudge NOI 1.13 09:大整数乘法

    [题目链接] ybt 1307:[例1.3]高精度乘法 ybt 1174:大整数乘法 OpenJudge NOI 1.13 09:大整数乘法 [题目考点] 1. 高精度 考察:高精乘高精 高精度计算讲 ...

  7. 信息学奥赛一本通(1307:【例1.3】高精度乘法)

    1307:[例1.3]高精度乘法 时间限制: 1000 ms         内存限制: 65536 KB 提交数: 30399     通过数: 10666 [题目描述] 输入两个高精度正整数M和N ...

  8. 算法提高 高精度乘法(java)

    算法提高 高精度乘法 描述 在C/C++语言中,整型所能表示的范围一般为-231到231(大约21亿),即使long long型,一般也只能表示到-263到263.要想计算更加规模的数,就要用软件来扩 ...

  9. 国王游戏(贪心 + 高精度乘法 + 高精度除法 + 高精度比较大小)

    题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏. 首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数. 然后,让这 n 位大臣排成一排,国王站在队伍 ...

最新文章

  1. android 无埋点 简书,无埋点README
  2. BCH双花成功率极低——零确认交易安全性高达99.9%
  3. javascript写贪吃蛇
  4. java工程师linux命令,这篇文章就够了
  5. java整合html_springBoot整合mybatis、jsp 或 HTML
  6. display转块状化
  7. 【MPI学习3】MPI并行程序设计模式:不同通信模式MPI并行程序的设计
  8. C++ 重载强制类型转换运算符
  9. LINUX下载编译libfaac
  10. 【AR开发】ARCore官方示例(Android)
  11. 华为连接wifi显示wifi未连接服务器,华为路由器wifi连接上不能上网怎么办?
  12. 只需10行代码就能对Excel文件进行批量去重~
  13. 使用OpenKE预训练的freebase关系向量
  14. 高性能网站架构之缓存篇—Redis集群搭建
  15. 自动化部署工具OneinStack:从入坑到出坑
  16. 2022-2028全球及中国全耗尽绝缘体上硅(FD-SOI)行业研究调查分析报告
  17. 获取CPU每个核心的IDT信息
  18. [C++][线程池][完整实现] 转:线程池原理及创建(C++实现)
  19. Ruby学习-安装、升级Ruby菜鸟教程(Linux环境下)
  20. Python爬虫编程思想(103):项目实战--抓取QQ空间说说的内容

热门文章

  1. 成都拓嘉启远:拼多多评论置顶该怎样去弄
  2. 迅雷极速版修改边下边播的默认播放器为PotPlayer
  3. win10锁屏c语言,Win10秘笈:如何在锁屏打开任意应用程序?
  4. 刘强东,揭开京东未来盈利迷局
  5. matlab模拟硅中的点缺陷,硅中的杂质和缺陷.pdf
  6. python引流脚本开发工具_Python脚本 抖X自动关注粉丝引流脚本
  7. mds部署服务器系统,Windows服务器2003年对MDS/IPS-8配置示例的iSCSI主机
  8. eclipse 重命名文件、移动文件、删除等操作时出现.svntmp文件
  9. 那些油管上高质量的学习编程的频道 之二
  10. 【matlab】matlab相关系数计算公式(Pearson和Spearman,以及Kendall Rank)