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. a标签常用跳转

    1.a标签跳转qq <a href="http://wpa.qq.com/msgrd?v=3&uin=123456789&site=qq&menu=yes&qu ...

  2. Retrofit 2.0:有史以来最大的改进

    因为其简单与出色的性能,Retrofit 是安卓上最流行的HTTP Client库之一. 不过它的缺点是在Retrofit 1.x中没有直接取消正在进行中任务的方法.如果你想做这件事必须手动杀死,而这 ...

  3. Android学习笔记1——Android开发环境配置

    一.JDK配置 Android是基于Java进行开发的,首先需要在电脑上配置JDK(Java Development Kit).在http://www.androiddevtools.cn/下载对应系 ...

  4. Java有关List的stream基本操作

    参考博客: https://www.jianshu.com/p/9fe8632d0bc2 Stream简介 Java 8引入了全新的Stream API.这里的Stream和I/O流不同,它更像具有I ...

  5. DB2数据库常用语句

    1.快速清空大量数据表数据,但是还原不了 alter table rm_customer activate not logged initially with empty table2.大量导出表语句 ...

  6. log4j-初识

    1.配置文件介绍: 1.1. 控制台输出:log4j.rootLogger=DEBUG, Console ,File #Console log4j.appender.Console=org.apach ...

  7. HDU 3351 Seinfeld 宋飞正传(水)

    题意: 给出一个串,串内只有大括号,问经过几次改变可使全部括号合法?改变指的是可以将某一方向的括号变成另一方向. 思路: 利用栈的特点,若出现成对的合法括号,直接删掉,留下那些不合法的成为一串.既然不 ...

  8. 页面中插入视频兼容ie8以上的浏览器

    有时候页面中需要插入视频,如果不考虑ie8的话:就是直接用h5标签<video></video>就可以了: 但是有的时候需求是需要考虑ie8,这样我们就可以用插件,这种vide ...

  9. 联动选择通过ajax获取选择对应的数据

    网站有时候需要这种联动然后获取到想对应的数据 思路: 这种的话就是你每次选择哪一个就将这个设置一个标注 表示你现在选择的是哪一个 然后每选择一次就进行一次ajax查询,ajax里面有一个data里面添 ...

  10. 【洛谷3759】[TJOI2017] 不勤劳的图书管理员(树套树)

    点此看题面 大致题意: 给定一个序列,每个元素有两个属性\(a_i\)和\(v_i\),每次操作改变两个元素的位置,求每次操作后\(\sum{v_i+v_j}[i<j,a_i>a_j]\) ...