A * B Problem Plus

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

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
 

分析

一直不过,最后发现数组开小了qwq。

思路:把每个数分解成多项式

$n = a_0*10^0+a_1*10^1+...a_k*10^k$

然后多项式乘法,FFT模板题。

code

 #include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<iostream> using namespace std; const int N = ;
const double pi = acos(-1.0);
char a[N],b[N];
int ans[N]; //数组大小! struct Complex{
double x,y;
Complex() {x=,y=;}
Complex(double _x,double _y) {x = _x,y = _y;}
}A[N],B[N];
Complex operator + (Complex a,Complex b) {
return Complex(a.x+b.x,a.y+b.y);
}
Complex operator - (Complex a,Complex b) {
return Complex(a.x-b.x,a.y-b.y);
}
Complex operator * (Complex a,Complex b) {
return Complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
}
void FFT(Complex *a,int n,int ty) {
for (int i=,j=; i<n; ++i) {
if (i < j) swap(a[i],a[j]);
for (int k=n>>; (j^=k)<k; k>>=); //妙啊!!!
}
for (int m=; m<=n; m<<=) {
Complex w1 = Complex(cos(*pi/m),ty*sin(*pi/m));
for (int i=; i<n; i+=m) {
Complex w = Complex(,);
for (int k=; k<(m>>); ++k) {
Complex t = w * a[i+k+(m>>)];
a[i+k+(m>>)] = a[i+k] - t;
a[i+k] = a[i+k] + t;
w = w * w1;
}
}
}
}
int main () {
while (scanf("%s%s",a,b)!=EOF) {
int len1 = strlen(a),len2 = strlen(b);
int n = ;
while (n < (len1+len2)) n <<= ;
for (int i=; i<n; ++i) {
if (i < len1) A[i] = Complex(a[len1-i-]-'',);
else A[i] = Complex(,);
if (i < len2) B[i] = Complex(b[len2-i-]-'',);
else B[i] = Complex(,);
}
FFT(A,n,);
FFT(B,n,);
for (int i=; i<n; ++i) A[i] = A[i] * B[i];
FFT(A,n,-);
for (int i=; i<n; ++i)
ans[i] = (int)(A[i].x/n+0.5);
for (int i=; i<n-; ++i) {
ans[i+] += (ans[i]/);
ans[i] %= ;
}
bool fir = false;
for (int i=n-; i>=; --i) {
if (ans[i]) printf("%d",ans[i]),fir = true;
else if (fir || i==) printf("");
}
puts("");
}
return ;
}

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

随机推荐

  1. 学习笔记:SVG和Canvas

    SVG SVG 与 Flash 类似,都是用于二维矢量图形,二者的区别在于,SVG 是一个 W3C 标准,基于 XML,是开放的.因为是 W3C 标准,SVG 与其他的 W3C 标准,比如 CSS.D ...

  2. H5如何做手机app(移动Web App)?图片轮播?ionic、MUI

    移动Web App 跨平台开发 用户不需要去卖场来下载安装App 任何时候都可以发布App只需要一个开发项目 可以使用HTML5,CSS3以及JavaScript以及服务器端语言来完成(PHP,Rub ...

  3. 浏览器兼容圆角Border-radius的问题

    圆角css代码:border-radius只有在以下版本的浏览器:Firefox4.0+.Google Chrome 10.0+.Opera 10.5+.IE9+支持border-radius标准语法 ...

  4. Entity Framework --Entity SQL注意事项

    Entity SQL 是 ADO.NET 实体框架 提供的 SQL 类语言,用于支持 实体数据模型 (EDM).Entity SQL 可用于对象查询和使用 EntityClient 提供程序执行的查询 ...

  5. PHP的模板引擎smarty原理浅谈

    mvc是开发中的一个伟大的思想,使得开发代码有了更加清晰的层次,让代码分为了三层各施其职.无论是对代码的编写以及后期的阅读和维护,都提供了很大的便利. 我们在php开发中,视图层view是不允许有ph ...

  6. jQuery-安装方法(2类)

    一.下载到本地,调用本地jQuery库 下载地址:http://jquery.com/download/ 共有两个版本的 jQuery 可供下载: 1.精简版:用于实际的网站中,已被精简和压缩. 2. ...

  7. 小图示优化 - ASP.NET Sprite and Image Optimization (Web Form)

    小图示优化 - ASP.NET Sprite and Image Optimization (Web Form) 透过 NuGet安装下面的套件,可以将您的小图示(icon)合并成一张图 透过 CSS ...

  8. http请求头和相应头的作用

    请求头(Request Headers) Accept:application/json, text/plain, */* Accept-Encoding:gzip, deflate Accept-L ...

  9. 日常入新坑,py一下

    首先是IDE,因为我经常在Ubuntu 18和win 10两个系统换来换去,所以IDE必须要能跨平台,所以这里就选了PyCharm.Py划重点—— 从Jet Brains的网站下载安装包,直接跟着默认 ...

  10. IOS截取部分图片

    截取部分图片这么简单: - (void)loadView {     [[UIApplication sharedApplication] setStatusBarHidden:YES withAni ...