题目连接: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. mysql-5.7.17.msi安装

    mysql-5.7.17.msi安装,跟着截图摩擦,一步一步是爪牙,是魔鬼的步伐 开始: 可以创建其他用户 我自己改了日志名

  2. 公牛与状压dp

    T1 疾病管理 裸得不能再裸的状压dp 不过数据范围骗人 考试时k==0的点没过 我也很无奈呀qwq #include<iostream> #include<cstdio> # ...

  3. LAMP第三部分php,mysql配置

    php配置 1. 配置disable_functiondisable_functions = eval,assert,popen,passthru,escapeshellarg,escapeshell ...

  4. Jmeter+Ant+Jenkins接口自动化测试(二)_测试方案设计及jmeter脚本开发

    前言 根据之前部署好的测试环境,进行接口自动化测试的方案设计及Jmeter脚本开发.测试方案设计过程中采用了数据分离和对象分离等思路,因此直接通过特定的测试用例文档来驱动整个自动化接口测试的执行,相关 ...

  5. tomcat部署项目时省略项目名

    大家也许知道在eclipse上通过新建server来部署项目到tomcat,并且通过server来管理项目的启动配置.server会自动创建启动该项目的xml 如: <Context docBa ...

  6. ogg12-ERROR OGG-01031 file D:\OGG\dirdat\ed000000 is not in any allowed output directories

    配置ogg时出现这个错误: 2018-01-04 14:22:58 ERROR OGG-01031 Oracle GoldenGate Capture for Oracle, P147148.prm: ...

  7. Xamarin.Android 引导页

    http://blog.csdn.net/qq1326702940/article/details/78665588 https://www.cnblogs.com/catcher1994/p/555 ...

  8. Windows下搭建Redis服务器

    Redis服务器是当下比较流行的缓存服务器,Redis通常被人拿来和Memcached进行对比.在我看来,应当是各具优势吧,虽然应用场景基本类似,但总会根据项目的不同来进行不通的选用. 我们今天主要讲 ...

  9. AIO5凭证性质设置接收下/上差(%),但是订单操作不起效。

    问题: AIO5凭证性质设置接收下/上差(%),但是订单操作不起效. 例如: 现在采购订单下了200个,我想限制收货只能收两百以内. 在在线帮助上看到有接收下/上差(%)字段可以进行限制,但是在凭证性 ...

  10. python 学习源码练习(1)

    #编译方式,python3 文件名 #!/usr/bin/python3#print('hello world') mystring = 'hello world'print (mystring) # ...