╰( ̄▽ ̄)╭

给出2个大整数A,B,计算A*B的结果。

(A,B的长度 <= 100000,A,B >= 0)

(⊙ ▽ ⊙)

把大整数A看做一个次数界为lenA的多项式A(x),其中x=10,

同样,把B看做一个次数界为lenB的多项式B(x),其中x=10。

然后套上快速傅里叶变换

( ̄~ ̄)

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
const char* fin="ftt.in";
const char* fout="fttx.out";
const int inf=0x7fffffff;
const double pi=acos(-1),eps=10e-6;
const int maxn=400007;
struct Z{
double x,y;
Z(double _x=0,double _y=0){x=_x;y=_y;}
Z operator +(const Z &b){return Z(x+b.x,y+b.y);}
Z operator -(const Z &b){return Z(x-b.x,y-b.y);}
Z operator *(const Z &b){return Z(x*b.x-y*b.y,x*b.y+y*b.x);}
}a[maxn],b[maxn],t[maxn];
int ans[maxn],lena,lenb,n,N,i,j,k;
void read(Z *a,int &len){
int i,j,k;
char ch=getchar();
while (ch<'0' || ch>'9') ch=getchar();
while (ch>='0' && ch<='9') a[len++]=ch-'0',ch=getchar();
for (i=0;i<(len+1)/2;i++) swap(a[i],a[len-i-1]);
}
void getn(){
int i,j,k=max(lena,lenb);
for (n=1;n<k;n<<=1) N++;
n<<=1;N++;
}
int fan(int v){
int i=N,k=0;
while (i--){
k=(k<<1)+(v&1);
v>>=1;
}
return k;
}
void fft(Z *a,int sig){
int i,j,k,m;
for (i=0;i<n;i++) t[fan(i)]=a[i];
for (m=2;m<=n;m<<=1){
int ha=m/2;
for (i=0;i<ha;i++){
Z w(cos(i*sig*pi/ha),sin(i*sig*pi/ha));
for (j=i;j<n;j+=m){
Z u=t[j],v=w*t[j+ha];
t[j]=u+v;
t[j+ha]=u-v;
}
}
}
for (i=0;i<n;i++) a[i]=t[i];
}
int main(){
freopen(fin,"r",stdin);
freopen(fout,"w",stdout);
read(a,lena);read(b,lenb);
getn(); fft(a,1);
fft(b,1);
for (i=0;i<n;i++) a[i]=a[i]*b[i];
fft(a,-1); for (i=0;i<=n;i++){
ans[i]+=int(a[i].x/n+eps);
ans[i+1]+=ans[i]/10;
ans[i]%=10;
}
while (!ans[n]) n--;
for (;n>=0;n--) printf("%d",ans[n]);
return 0;
}

(⊙v⊙)

Pay Attention

1.read()中,不要把len打成lena;
翻转大整数时,是从0枚举到(len+1)/2; 2.getn()中,n最后要再乘一次2,因为:
A*B的次数界是lena*lenb。 3.maxn要开到4倍; 4.pi=acos(-1); 5.当对一个小数x用int()取整时,需要打成int(x+eps),其中,eps=10e-6。

【51NOD1028】大数乘法 V2的更多相关文章

  1. ACM学习历程—51NOD1028 大数乘法V2(FFT)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028 题目大意就是求两个大数的乘法. 但是用普通的大数乘法,这 ...

  2. 51 Nod 1028 大数乘法 V2【Java大数乱搞】

    1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A ...

  3. 1028 大数乘法 V2(FFT or py)

    1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果.   Input 第1行:大数A 第2行:大数B ...

  4. 51Nod 1028 大数乘法 V2

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028 分析: FFT/NTT板子题... 代码: NTT板子: #inc ...

  5. FFT/NTT [51Nod 1028] 大数乘法 V2

    题目链接:51Nod 传送门 没压位,效率会低一点 1.FFT #include <cstdio> #include <cstring> #include <algori ...

  6. 51nod 1028 大数乘法 V2 【FFT模板题】

    题目链接 模板题.. #include<bits/stdc++.h> using namespace std; typedef int LL; typedef double db; nam ...

  7. 51nod 1027大数乘法

    题目链接:51nod 1027大数乘法 直接模板了. #include<cstdio> #include<cstring> using namespace std; ; ; ; ...

  8. [POJ] #1001# Exponentiation : 大数乘法

    一. 题目 Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 156373   Accepted: ...

  9. 用分治法实现大数乘法,加法,减法(java实现)

    大数乘法即多项式乘法问题,求A(x)与B(x)的乘积C(x),朴素解法的复杂度O(n^2),基本思想是把多项式A(x)与B(x)写成 A(x)=a*x^m+b B(x)=c*x^m+d 其中a,b,c ...

随机推荐

  1. SQL的特点

    1.综合统一 2.高度非过程化 3.面向集合的操作方式 4.一同一种语法结构提供两种使用5.语言简洁易学易用

  2. PHP的垃圾回收机制(开启垃圾回收机制后的优缺点是什么)

    PHP的垃圾回收机制(开启垃圾回收机制后的优缺点是什么) 一.总结 一句话总结: 拿时间换空间:针对内存泄露的情况,可以节省大量的内存空间,但是由于垃圾回收算法运行耗费时间,开启垃圾回收算法会增加脚本 ...

  3. 【DM642学习笔记三】flash的烧写

    ICETEK-DM642-PCI板上的29L008B芯片提供了8M位的Flash空间(访问地址空间是CE1,90000000h~90080000h).主要用于自启动功能和存储FPGA的配置数据. 一. ...

  4. 粗浅看 Tomcat系统架构分析

    原文出处: 吴士龙 http://www.importnew.com/21112.html Tomcat的结构很复杂,但是Tomcat也非常的模块化,找到了Tomcat最核心的模块,就抓住了Tomca ...

  5. qq邮箱问卷,测试不支持form表单

    想做个类似苹果调查问卷的: 找到qq邮箱的代码编辑器: 写好我们的网页(h5) <!DOCTYPE html> <html lang="en"> <h ...

  6. 彭亮—Python学习

    1.1 Python简单介绍 1.2 安装Python和配置环境 1.配置Python      1.1 下载Python(直接去官网下载就可以)      1.2 安装Python(点解默认安装即可 ...

  7. BZOJ2120&&2453 数颜色&&维护队列

    2453: 维护队列 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1442 Solved: 678 [Submit][Status][Discuss ...

  8. Leetcode429.N-ary Tree Level Order TraversalN叉树的层序遍历

    给定一个 N 叉树,返回其节点值的层序遍历. (即从左到右,逐层遍历). 例如,给定一个 3叉树 : 返回其层序遍历: [ [1], [3,2,4], [5,6] ] 说明: 树的深度不会超过 100 ...

  9. Leetcode400Nth Digit第N个数字

    在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 个数字. 注意: n 是正数且在32为整形范围内 ( n < 231). 示例 1: ...

  10. js 高亮显示关键字

    示例: var defaultEmphasisHandler = function(keyword, data){ var regex = RegExp("("+keyword.r ...