HDU1042 A * B Problem Plus
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
Input
Note: the length of each integer will not exceed 50000.
Output
Sample Input
Sample Output
分析
一直不过,最后发现数组开小了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的更多相关文章
- 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 ...
随机推荐
- 自动布局库--Masonry使用
参考资料(戳这里): > Masonry官网 > Masonry介绍与使用实践(快速上手Autolayout) > iOS 开发实践之 Auto Layout > Ma ...
- springboot mybatis 自动生成代码(maven+IntelliJ IDEA)
1.在pom文件中加入需要的依赖(mybatis-generator-core) 和 插件(mybatis-generator-maven-plugin) <dependency> < ...
- Spring cloud Eureka 服务治理(注册服务提供者)
搭建完成服务注册中心,下一步可以创建服务提供者并向注册中心注册服务. 接下来我们创建Spring Boot 应用将其加入Eureka服务治理体系中去. 直接使用签名章节创建hello服务项目改造: 1 ...
- concurrent.futures模块与协程
concurrent.futures —Launching parallel tasks concurrent.futures模块同时提供了进程池和线程池,它是将来的使用趋势,同样我们之前学习 ...
- FusionCharts可使用JavaScript渲染iPhone/iPod/iPad图表
FusionCharts使用JavaScript: FusionCharts允许用户创建建立JavaScript图表(也就是web上的HTML5 /Canvas图表).这个特性允许用户在不支持Flas ...
- Android-->RxJava2更新体验
截止日前最新版2017-3-15: RxJava compile ‘io.reactivex:rxjava:’ compile ‘io.reactivex:rxandroid:’ RxJava2 co ...
- JAVA中的多态概念
多态性是指同一操作作用于某一类对象,可以有不同的解释,产生不同的执行结果. 多态存在的三个必要条件 需要存在继承和实现关系 同样的方法调用而执行不同操作,运行不同代码. 在运行时父类或者接口的引用变量 ...
- 【C++】【MFC】创建新的线程函数
DWORD WINAPI MyThreadProc (LPVOID lpParam){ somestruct* pN = (somestruct*)lpParam; // 将参数转为你的类型 ... ...
- genlist -s 192.168.21.\*
显示网段192.168.21中可用的主机.
- mustache.js 数组循环的索引
在使用mustache作为模板引擎时,想要利用数组中的对象的索引排序,却发现mustache中无法获得数组索引,在一番搜索之后,发现在数组的对象中加入索引,就可以了,示例如下 /html {{#dat ...