A * B Problem Plus
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的更多相关文章
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- 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 ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- [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 ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案
$s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...
随机推荐
- Oracle EBS中分类账和法人实体 的关系(有sql语句实例)
Oracle EBS中分类账和法人实体 的关系(有sql语句实例) 2012-12-06 16:05 2822人阅读 评论(0) 收藏 举报 分类: Oracle EBS(12) Oracle数据 ...
- VSFTP服务——实验
一.VSFTP 作用:提供文件共享服务,可以应用在互联网中,实现外地登录服务器下载公司文件的功能,不区分客户端,在windows和linux中都是可以使用的 1.安装vsftpd [root@Serv ...
- angular1.x 脏检测
写在前面 双向绑定是angular的大亮点,然后支撑它的就是脏检测.一直对脏检测都有一些理解,却没有比较系统的概念. 以下是我阅读网上博文以及angular高级程序设计的理解与总结. 接收指导与批评. ...
- curl命令PostJson
curl -H "Content-Type: application/json" -X POST --data '{"data":"1"} ...
- IOS数据库操作SQLite3使用详解(转)
iPhone中支持通过sqlite3来访问iPhone本地的数据库.具体使用方法如下1:添加开发包libsqlite3.0.dylib首先是设置项目文件,在项目中添加iPhone版的sqlite3的数 ...
- 论JSON的重要性☞异步上传过程中data取多组值的方法
异步上传取多组值一起post改变属性 如图: 看图说话
- chap3 数组 #C
4.1 数组的基本概念 4.1.1 要点归纳 一维数组 定义: int a[10]; 数组名是一个地址常量,不允许修改. 引用: 初始化: 静态数组 static int a[10];的初值? 全部赋 ...
- NYOJ 299
(前言:这是一道关于矩阵快速幂的问题,介绍矩阵快速幂之前,首先看"快速幂"问题. 在前面的博客里有记录到快速幂取模算法,不过总体的思想总是和取模运算混淆在一起,而忽略了" ...
- 利用PyCharm进行Python远程调试
背景描述 有时候Python应用的代码在本地开发环境运行十分正常,但是放到线上以后却出现了莫名其妙的异常,经过再三排查以后还是找不到问题原因,于是就在想,要是可以在服务器环境中进行单步跟踪调试就好了. ...
- MFC多线程各种线程用法 .
http://blog.csdn.net/qq61394323/article/details/9328301 一.问题的提出 编写一个耗时的单线程程序: 新建一个基于对话框的应用程序SingleTh ...