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. iOS开发ReactiveCocoa学习笔记(一)

    学习 RAC 我们首先要了解 RAC 都有哪些类 RACSignal RACSubject RACSequence RACMulticastConnection RACCommand 在学习的时候写了 ...

  2. 使用JAVA读写Properties属性文件

     使用JAVA读写Properties属性文件 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数 ...

  3. 响应式及Bootstrap

    一丶CSS3的@media 查询 使用 @media 查询,你可以针对不同的屏幕大小定义不同的样式. @media 可以针对不同的屏幕尺寸设置不同的样式,特别是如果你需要设置设计响应式的页面,@med ...

  4. Qt安装教程

    一.Qt下载 官网下载链接http://download.qt.io/archive/qt/,下载最新版 5.10 官网的下载网站有的时候可能会抽风,也可以选择国内的镜像下载源http://mirro ...

  5. I-129表

    http://www.uscis.gov/i-129 移民局使用的非移民性质的工作身份申请表(I-129)表格将于下月底正式作废,4月30日之后,公民和移民服务局只接受新的I-129表格. 据了解,非 ...

  6. hihoCoder 挑战赛10 #1144 : 01串

    思路:这只是逻辑测试题吧,考虑周全就行了.考虑n=m的情况,n>m的情况,m>n的情况. (1)n>m的情况,0比1多几个都是行的,一共有m个“01”,后面补足够多个零即可. (2) ...

  7. hive 报错FAILED: Error in metadata: java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.metastore.HiveMetaStoreClient FAILED: Execu

    使用hive一段时间以后,今天在使用的时候突然报错,如下: hive> show databases;FAILED: Error in metadata: java.lang.RuntimeEx ...

  8. 《Ruby on Rails教程》学习笔记

    本文是我在阅读 Ruby on Rails 教程的简体中文版时所做的摘录,以及学习时寻找的补充知识.补充知识主要来自于 Ruby on Rails 實戰聖經. Asset Pipeline 在最新版 ...

  9. POJ-3080 Blue Jeans---字符串+暴力

    题目链接: https://vjudge.net/problem/POJ-3080 题目大意: 找最长的公共字串(长度>=3),长度相同就找字典序最小的 解题思路: 枚举第一个串的所以子串,处理 ...

  10. linux怎么进home目录下

    可以使用cd命令,cd命令的功能是切换到指定的目录: 命令格式:cd [目录名] 有几个符号作为目录名有特殊的含义: “/”代表根目录.“..”代表上一级目录.“~”代表HOME目录.“-”代表前一目 ...