A * B Problem Plus(fft)
题目连接: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
end of file.
Note: the length of each integer will not exceed
50000.
2
1000
2
2000
#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)的更多相关文章
- hdu 1402 A * B Problem Plus FFT
/* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...
- 【CF954I】Yet Another String Matching Problem(FFT)
[CF954I]Yet Another String Matching Problem(FFT) 题面 给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)的子串与\(T\)的距离 两个 ...
- 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 ...
- 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 ...
- HDU1402 A * B Problem Plus FFT
分析:网上别家的代码都分析的很好,我只是给我自己贴个代码,我是kuangbin的搬运工 一点想法:其实FFT就是快速求卷积罢了,当小数据的时候我们完全可以用母函数来做,比如那种硬币问题 FFT只是用来 ...
- HDU-1402 A * B Problem Plus FFT(快速傅立叶变化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 一般的的大数乘法都是直接模拟乘法演算过程,复杂度O(n^2),对于这题来说会超时.乘法的过程基本 ...
- HDU 1402 A * B Problem Plus (FFT模板题)
FFT模板题,求A*B. 用次FFT模板需要注意的是,N应为2的幂次,不然二进制平摊反转置换会出现死循环. 取出结果值时注意精度,要加上eps才能A. #include <cstdio> ...
- HDU - 1402 A * B Problem Plus FFT裸题
http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意: 求$a*b$ 但是$a$和$b$的范围可以达到 $1e50000$ 题解: 显然...用字符串模拟 ...
- 洛谷.1919.[模板]A*B Problem升级版(FFT)
题目链接:洛谷.BZOJ2179 //将乘数拆成 a0*10^n + a1*10^(n-1) + ... + a_n-1的形式 //可以发现多项式乘法就模拟了竖式乘法 所以用FFT即可 注意处理进位 ...
随机推荐
- ABP .Net Core Entity Framework迁移使用MySql数据库
一.迁移说明 ABP模板项目Entity Framework Core默认使用的是Sql Server,也很容易将数据库迁移到MySQL,步骤如下. 二.迁移MySQL步骤 1. 下载项目 请到 ht ...
- ecsmart的开发经历
ecsmart是ecshop的产品之一,是2015年发布的一套系统.关于它的介绍请到官网去了解,也可以百度 “商之翼” “ecsmart” 1.ecsmart分别在pc.mobile.app三个方面都 ...
- rabbitmq 启动报错
=============================================== 2017/10/24_第1次修改 ccb_warlock = ...
- Linux下jira自启动设置
jira 的启动主要依靠的是bin目录下的catalina.sh脚本,提供了如init脚本的start,stop等参数----------------------------------------- ...
- Django--admin源码流程
admin.py from django.contrib import admin from . import models """ 通过原生的django admi ...
- Head First设计模式之观察者模式
一.定义 观察者设计模式定义了对象间的一种一对多的依赖关系,以便一个对象的状态发生变化时,所有依赖于它的对象都得到通知并自动刷新. 有时被称作发布/订阅模式,观察者模式定义了一种一对多的依赖关系,让多 ...
- angular4.0如何引入外部插件1:import方案
引入外部插件是项目中非常重要的环节.因为部分插件以js语法写的,而ng4用的是ts语法,所以在引入时需要配置. Step1:引入swiper插件的js文件[css在下面会讲到,先别急] 很重要的意见: ...
- word-break: break-word; 文本溢出
word-break: break-word; 中文汉字不会溢出,英文字母会溢出 这个时候添加属性 word-break: break-word; 即可 使得 不溢出 ======== ...
- Python 学习笔记大纲
Python Basic 第一章:Python基础の快速认识基本语法 (点击进入博客)我的第一个HelloPython程序.如何实现用户输入.Python的自带电池(模块).变量.格式化.条件判断.循 ...
- Java学习笔记8---类的静态成员变量与静态成员方法的访问与调用方式
(注:静态变量修改为静态成员变量,静态方法改为静态成员方法) 静态成员变量又称类变量,静态成员方法又称类方法,它们统称为静态成员或类成员.静态成员由static修饰,是属于整个类的,所有的对象共享这些 ...