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 ...
随机推荐
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第八天(非原创)
文章大纲 一.课程介绍二.Solr基本介绍三.ssm整合Solr四.项目源码与资料下载五.参考文章 一.课程介绍 一共14天课程(1)第一天:电商行业的背景.淘淘商城的介绍.搭建项目工程.Svn的 ...
- yum 安装Tomcat7(centos)
yum 安装Tomcat7 其实最重要的就是yum源吗.初始源的里面既没有nginx也没有tomcat7. 1,搞定nginx,她家自己有源的: rpm -ivh http://nginx.org ...
- 学习《CSS选择器Level-4》不完全版
1 概述 1.1 前言 选择器是CSS的核心组件.本文依据W3C的Selectors Level 4规范,概括总结了Level1-Level4中绝大多数的选择器,并做了简单的语法说明及示例演示.希望对 ...
- python起源,变量,用户交互,流程语句
1.Python的起源 Python是一门解释型弱类型编程语言. 特点:简单.明确.优雅 2.Python解释器 CPython官方提供的, 内部使用C语言来实现 PyPy,一次性把我们的代码解释成字 ...
- isset或array_key_exists,检查数组键是否存在
今天在导出报表的时候遇到了一个问题,undefined index:pid,然后就纳闷了,我的数组里面根本就没有pid,为什么会出现这个错误呢,我遍历了一下数组,发现果然有pid这个键,奇怪呀,我有做 ...
- PHP报错configure error Cannot find libmysqlclient under usr
编译PHP报错configure error Cannot find libmysqlclient under usr的解决方法 (问题产生,mysql是yum安装的,libmysqlclient* ...
- shell脚本调试技巧
shell脚本调试之工具——bashdb http://www.cnblogs.com/itcomputer/p/5011845.html
- springMvc-对servletApi的支持以及把后台对象以json方式传到前台
1.对servletApi的支持:request.response以及session.cookie的支持 2.把后台代码以json格式向前台输出: 代码: package com.java.contr ...
- 在vue-cli中引入外部插件
一.可以用npm下载的 现在以jquery为例子: 1 先在package.json中的dependencies中写入“jquery”:“^3.2.1”(jquery版本) 2 在npm中搜索jque ...
- VERITAS NETBACKUP运维手册(自制)
ps:本文为目录.详情请点如下目录超链接 1 VERITAS NETBACKUP介绍 1.1 NBU基本概念 1.2 配置存储单元 1.3 配置备份策略(Policy) 1.4 配置NetBackup ...