A * B Problem Plus

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

FFT

(FFT的详细证明参见算法导论第三十章)

一个多项式有两种表达方式:

1.系数表示法,系数表示的多项式相乘,时间复杂度为O(n^2);

2.点值表示法,点值表示的多项式相乘,时间复杂度为O(n).

简单的说,FFT能办到的就是将系数表示的多项式转化为点值表示,其时间复杂度为O(nlgn),而将点值表示的多项式转化为系数表示需要IFFT(FFT的逆运算),其形式与FFT相似,时间复杂度也为O(nlgn).

这道题需要用FFT将两个大数转化为点值表示,相乘后再用IFFT将点值表示转化回系数表示,总时间复杂度为O(nlgn).

代码如下:

 #include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
#define N 200005
using namespace std;
const double pi=acos(-1.0);
struct Complex{
double r,i;
Complex(double r=,double i=):r(r),i(i){};
Complex operator + (const Complex &rhs){
return Complex(r+rhs.r,i+rhs.i);
}
Complex operator - (const Complex &rhs){
return Complex(r-rhs.r,i-rhs.i);
}
Complex operator * (const Complex &rhs){
return Complex(r*rhs.r-i*rhs.i,i*rhs.r+r*rhs.i);
}
}a[N],b[N],c[N];
char s1[N],s2[N];
int ans[N],n1,n2,len;
inline void sincos(double theta,double &p0,double &p1){
p0=sin(theta);
p1=cos(theta);
}
void FFT(Complex P[], int n, int oper){
for(int i=,j=;i<n-;i++){
for(int s=n;j^=s>>=,~j&s;);
if(i<j)swap(P[i],P[j]);
}
Complex unit_p0;
for(int d=;(<<d)<n;d++){
int m=<<d,m2=m*;
double p0=pi/m*oper;
sincos(p0,unit_p0.i,unit_p0.r);
for(int i=;i<n;i+=m2){
Complex unit=;
for(int j=;j<m;j++){
Complex &P1=P[i+j+m],&P2=P[i+j];
Complex t=unit*P1;
P1=P2-t;
P2=P2+t;
unit=unit*unit_p0;
}
}
}
if(oper==-)for(int i=;i<len;i++)P[i].r/=len;
}
void Conv(Complex a[],Complex b[],int len){//求卷积
FFT(a,len,);//FFT
FFT(b,len,);//FFT
for(int i=;i<len;++i)c[i]=a[i]*b[i];
FFT(c,len,-);//IFFT
}
void init(char *s1,char *s2){
len=;
n1=strlen(s1),n2=strlen(s2);
while(len<*n1||len<*n2)len<<=;
int idx;
for(idx=;idx<n1;++idx){
a[idx].r=s1[n1--idx]-'';
a[idx].i=;
}
while(idx<len){
a[idx].r=a[idx].i=;
idx++;
}
for(idx=;idx<n2;++idx){
b[idx].r=s2[n2--idx]-'';
b[idx].i=;
}
while(idx<len){
b[idx].r=b[idx].i=;
idx++;
}
}
int main(void){
while(scanf("%s%s",s1,s2)==){
init(s1,s2);
Conv(a,b,len);
for(int i=;i<len+len;++i)ans[i]=;//93ms
//memset(ans,0,sizeof(ans));//140ms
int index;
for(index=;index<len||ans[index];++index){
ans[index]+=(c[index].r+0.5);
ans[index+]+=(ans[index]/);
ans[index]%=;
}
while(index>&&!ans[index])index--;
for(;index>=;--index)printf("%d",ans[index]);
printf("\n");
}
}

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

  1. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  2. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...

  3. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  4. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  5. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  6. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  7. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  8. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  9. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  10. PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案

    $s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...

随机推荐

  1. 从Chrome源码看浏览器如何构建DOM树

    .aligncenter { clear: both; display: block; margin-left: auto; margin-right: auto } p { font-size: 1 ...

  2. Java重写与重载之间的区别

    重写(Override) 重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变.即外壳不变,核心重写! 重写的好处在于子类可以根据需要,定义特定于自己的行为. 也就是说子类 ...

  3. Python学习笔记---形式参数(parameter)和实际参数(argument)

    def mydemo(name): '函数定义过程中的name是叫形参' #因为它只是一个形式,表示占据一个参数位置 print('传递进来的' + name + '叫做实参,因为它是具体的参数值!' ...

  4. JQuery动态操作表格

    新人,小白一枚,刚刚参加工作,所以会在这里记录一些遇到的问题. 最近要做的东西,是对一个表格动态的添加行,删除行,并且对表格中内容进行非空验证. <!DOCTYPE html> <h ...

  5. appium+robotframework的简单实例

    在上篇文章中,我们搭建好了appium+robotframework的环境,这篇文章中主要是一个简单实例. 一.测试用例编写前提 1.模拟器(或手机)连接电脑 adb devices         ...

  6. flexbox备忘

    伸缩项目的父元素: display:flex || display:inline-flex fiex-direction: row(默认) | row-reverse | column | colum ...

  7. c#计算datatable中某一列值的和

    double sumPercentage = dt.AsEnumerable().Where(dr => { return dt.Rows.IndexOf(dr) > 0; }).Sum( ...

  8. action中list传到JSP中取不到值的问题

    今天遇到了这个问题 action中list传到JSP中取不到值 搞了半天是因为我在JSP中取值的的时候 <s:iterator  value="shlist" var=&qu ...

  9. 多微博账号同时发微博的插件--fawave

    我们每个人应该都不止一个博客或者微博,尤其是明星.为了考虑与新闻社区媒体的关系,必须在每个平台都要入住一下才好,还有一方面也是对粉丝的一种关爱吧.反正里面关系很复杂.有没有一个工具可以讲微博的内容同步 ...

  10. Substring (nyoj 308)

    练习使用字符串函数了. 1.字串的反转也是它的字串,2.最长,3.最先出现 string: #include<iostream> #include<cstdio> #inclu ...