题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1402

hdu_1402:A * B Problem Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15419    Accepted Submission(s):
3047

Problem Description
Calculate A * B.
 
Input
Each line will contain two integers A and B. Process to
end of file.

Note: the length of each integer will not exceed
50000.

 
Output
For each case, output A * B in one line.
 
Sample Input
1
2
1000
2
 
Sample Output
2
2000
 
Author
DOOM III
 
题解: 练习用fft实现大数的乘法达到O(nlog(n))的算法,两个数相乘看成是两个多项式的乘法,这样多项式中的x=10,fft套用模板即可
给出代码:
 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAX=;
const double PI=acos(-1.0),eps=1e-;;
double cof1[MAX], cof2[MAX];
int n, k, permutation[MAX];
char s1[MAX],s2[MAX];
int ans[MAX];
struct complex {//复数
double r, v;
complex operator + (complex& obj) {
complex temp;
temp.r = r + obj.r;
temp.v = v + obj.v;
return temp;
}
complex operator - (complex& obj) {
complex temp;
temp.r = r - obj.r;
temp.v= v - obj.v;
return temp;
}
complex operator * ( complex& obj) {
complex temp;
temp.r = r*obj.r - v*obj.v;
temp.v = r*obj.v + v*obj.r;
return temp;
}
} p1[MAX], p2[MAX], omiga[MAX], result1[MAX], result2[MAX];
void caculate_permutation(int s, int interval, int w, int next) {
if(interval==n) {
permutation[w] = s;
return ;
}
caculate_permutation(s,interval*, w, next/);
caculate_permutation(s+interval, interval*, w+next, next/);
}
void fft(complex transform[], complex p[]) {
int i, j, l, num, m;
complex temp1, temp2;
for(i=; i<n; i++)transform[i] = p[ permutation[i] ] ;
num = , m = n;
for(i=; i<=k; i++) {
for(j=; j<n; j+=num*)
for(l=; l<num; l++)
temp2 = omiga[m*l]*transform[j+l+num],
temp1 = transform[j+l],
transform[j+l] = temp1 + temp2,
transform[j+l+num] = temp1 - temp2;
num*=,m/=;
}
}
void polynomial_by(int n1,int n2) {//多项式乘法,cof1、cof2保存的是a[0],a[1]..a[n-1]的值(a[i]*x^i)
int i;
double angle;
k = , n = ;
while(n<n1+n2-)k++,n*=;
for(i=; i<n1; i++)p1[i].r = cof1[i], p1[i].v = ;
while(i<n)p1[i].r = p1[i].v = , i++;
for(i=; i<n2; i++)p2[i].r = cof2[i], p2[i].v = ;
while(i<n)p2[i].r = p2[i].v = , i++;
caculate_permutation(,,,n/);
angle = PI/n;
for(i=; i<n; i++)omiga[i].r = cos(angle*i), omiga[i].v = sin(angle*i);
fft(result1,p1);
fft(result2,p2);
for(i=; i<n; i++)result1[i]= result1[i]*result2[i];
for(i=; i<n; i++)omiga[i].v = -omiga[i].v;
fft(result2, result1);
for(i=; i<n; i++)result2[i].r/=n;
i = n -;
while(i&&fabs(result2[i].r)<eps)i--;
n = i+;
while(i>=) ans[i]=(int)(result2[i].r+0.5), i--;
}
int main() {
while(scanf("%s",s1)!=EOF){
scanf("%s",s2);
int n1=strlen(s1),n2=strlen(s2);
for(int i=;i<n1;i++){
cof1[i]=s1[n1--i]-'';
}
for(int i=;i<n2;i++){
cof2[i]=s2[n2--i]-'';
}
memset(ans,,sizeof(ans));
polynomial_by(n1,n2);
for(int i=;i<n;i++){
if(ans[i]>=){
ans[i+]+=ans[i]/;
ans[i]%=;
}
}
while(ans[n]>){
if(ans[n]>=){
ans[n+]+=ans[n]/;
ans[n]%=;
}
n++;
}
for(int i=n-;i>=;i--){
putchar(''+ans[i]);
}
puts("");
}
return ;
}

A * B Problem Plus(fft)的更多相关文章

  1. hdu 1402 A * B Problem Plus FFT

    /* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...

  2. 【CF954I】Yet Another String Matching Problem(FFT)

    [CF954I]Yet Another String Matching Problem(FFT) 题面 给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)的子串与\(T\)的距离 两个 ...

  3. 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 ...

  4. CODEVS3123 a*b problem plus (FFT)

    type xh=record x,y:double; end; arr=..] of xh; var n,m:longint; s1,s2:ansistring; a,b,g,w:arr; ch:ch ...

  5. HDU1402 A * B Problem Plus FFT

    分析:网上别家的代码都分析的很好,我只是给我自己贴个代码,我是kuangbin的搬运工 一点想法:其实FFT就是快速求卷积罢了,当小数据的时候我们完全可以用母函数来做,比如那种硬币问题 FFT只是用来 ...

  6. HDU-1402 A * B Problem Plus FFT(快速傅立叶变化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 一般的的大数乘法都是直接模拟乘法演算过程,复杂度O(n^2),对于这题来说会超时.乘法的过程基本 ...

  7. HDU 1402 A * B Problem Plus (FFT模板题)

    FFT模板题,求A*B. 用次FFT模板需要注意的是,N应为2的幂次,不然二进制平摊反转置换会出现死循环. 取出结果值时注意精度,要加上eps才能A. #include <cstdio> ...

  8. HDU - 1402 A * B Problem Plus FFT裸题

    http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意: 求$a*b$ 但是$a$和$b$的范围可以达到 $1e50000$ 题解: 显然...用字符串模拟 ...

  9. 洛谷.1919.[模板]A*B Problem升级版(FFT)

    题目链接:洛谷.BZOJ2179 //将乘数拆成 a0*10^n + a1*10^(n-1) + ... + a_n-1的形式 //可以发现多项式乘法就模拟了竖式乘法 所以用FFT即可 注意处理进位 ...

随机推荐

  1. three.js实现3D模型展示

    由于项目需要展示3d模型,所以对three做了点研究,分享出来 希望能帮到大家 先看看效果: three.js整体来说 不是很难 只要你静下心来研究研究 很快就会上手的 首先我们在页面上需要创建一个能 ...

  2. 如何给动态添加的form表单控件添加表单验证

    最近使用jQuery Validate做表单验证很方便,api地址为http://www.runoob.com/jquery/jquery-plugin-validate.html 但是在使用的时候也 ...

  3. Sum of AP series——AP系列之和

    A series with same common difference is known as arithmetic series. The first term of series is 'a' ...

  4. UVALive 4850 Installations

    题目大意:有若干个任务,每个任务耗时si,期限为di,同一时间只能做一个任务.对于一个任务,惩罚值为max(0,完成时间-期限).问怎么安排,使(最大惩罚值+次大惩罚值)最小,O(n^2). 如果没有 ...

  5. [C#]使用Quartz.NET来创建定时工作任务

    本文为原创文章.源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称.作者及网址,谢谢! 开发工具:VS2017 语言:C# DotNet版本:.Net FrameWork 4.0及以 ...

  6. 重写JS的鼠标右键点击菜单

    重写JS的鼠标右键点击菜单 该效果主要有三点,一是对重写的下拉菜单的隐藏和显示:二是屏蔽默认的鼠标右键事件:三是鼠标左键点击页面下拉菜单隐藏. 不多说,上html代码: 1 <ul id=&qu ...

  7. lesson - 7 课程笔记 vim

    vim :修改文件 模式: 默认进来是一般模式.i 编辑模式.esc 退出编辑 .shift+: 底行模式 参数: w: write/q:quit/! force 编辑模式:  /a:光标之后插入内容 ...

  8. lesson - 6 Linux下磁盘管理

    1. 查看磁盘或者目录的容量df  查看磁盘各分区使用情况   不加参数以k为单位   df -i inode数,df -h  以G或者T或者M   df -m  以M单位显示  du 查看目录或者文 ...

  9. Qt个人研究进展

    1:纯socket通信实现多线程邮件发送,支持多个收件人和附件,通用任何平台,包括ARM.2:纯串口通信AT命令实现多线程短信收发,支持多个收件人和长短信,通用任何平台,包括ARM.3:纯串口通信PO ...

  10. C# DataGridView 列的显示顺序

    this.dataGridView1.Columns["列名"].DisplayIndex=Convert.ToInt32("你要放置的位置")