HDU1402 A * B Problem Plus
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
题目链接:HDU1402
正解:FFT
解题报告:
FFT模板题,注意一下进位处理。
重要的话说三遍:去掉多余的0!!!WA了几遍…
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
using namespace std;
typedef long long LL;
typedef complex<double> C;
const int MAXN = 300011;
const double pi = acos(-1);
char ch[MAXN],s[MAXN];
int n,m,ans[MAXN];
C a[MAXN],b[MAXN]; inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void fft(C *a,int n,int f){
if(n==1) return ;
C a0[n>>1],a1[n>>1],wn(cos(2*pi/n),sin(2*pi*f/n)),w(1,0),t;
for(int i=0;i<n>>1;i++) a0[i]=a[i<<1],a1[i]=a[i<<1|1];
fft(a0,n>>1,f); fft(a1,n>>1,f);
for(int i=0;i<n>>1;i++,w*=wn) {
t=a1[i]*w;
a[i]=a0[i]+t;
a[i+(n>>1)]=a0[i]-t;
}
} inline void work(){
while(scanf("%s",ch)!=EOF) {
scanf("%s",s); memset(ans,0,sizeof(ans));
memset(a,0,sizeof(a)); memset(b,0,sizeof(b));//记得清空!
n=strlen(ch); m=strlen(s); n--; m--;
for(int i=n;i>=0;i--) a[n-i]=(int)ch[i]-'0';
for(int i=m;i>=0;i--) b[m-i]=(int)s[i]-'0';
m+=n; for(n=1;n<=m;n<<=1);
fft(a,n,1); fft(b,n,1);
for(int i=0;i<=n;i++) a[i]*=b[i];
fft(a,n,-1);
for(int i=0;i<=m;i++) ans[i]=(int)(a[i].real()/n+0.5);
for(int i=0;i<=m;i++) ans[i+1]+=ans[i]/10,ans[i]%=10;
while(ans[m+1]>0) m++,ans[m+1]+=ans[m]/10,ans[m]%=10;
while(ans[m]==0) m--;//注意去掉多余的0!!!
if(m>=0) { for(int i=m;i>=0;i--) printf("%d",ans[i]); puts(""); }
else puts("0");
}
} int main()
{
work();
return 0;
}
HDU1402 A * B Problem Plus的更多相关文章
- FFT/NTT模板 既 HDU1402 A * B Problem Plus
@(学习笔记)[FFT, NTT] Problem Description Calculate A * B. Input Each line will contain two integers A a ...
- hdu----(1402)A * B Problem Plus(FFT模板)
A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU1402 A * B Problem Plus(FFT)
http://acm.hdu.edu.cn/showproblem.php?pid=1402 初学FFT. http://www.cnblogs.com/WABoss/p/FFT_Note.html ...
- HDU1402 A * B Problem Plus FFT
分析:网上别家的代码都分析的很好,我只是给我自己贴个代码,我是kuangbin的搬运工 一点想法:其实FFT就是快速求卷积罢了,当小数据的时候我们完全可以用母函数来做,比如那种硬币问题 FFT只是用来 ...
- HDU-1402 A * B Problem Plus FFT(快速傅立叶变化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 一般的的大数乘法都是直接模拟乘法演算过程,复杂度O(n^2),对于这题来说会超时.乘法的过程基本 ...
- 【NTT】hdu1402 A * B Problem Plus
r·2^k+1 r k g 3 1 1 2 5 1 2 2 17 1 4 3 97 3 5 5 193 3 6 5 257 1 8 3 7681 15 9 17 12289 3 12 11 40961 ...
- 【FFT】hdu1402 A * B Problem Plus
FFT板子. 将大整数看作多项式,它们的乘积即多项式的乘积在x=10处的取值. #include<cstdio> #include<cmath> #include<cst ...
- [hdu1402]A * B Problem Plus(NTT)
解题关键:快速数论变换NTT模板. 注意$ans$数组的$ans[n]$一定要注意置$0$,或者结果从$n-1$开始遍历,这里很容易出错. 代码1:ACdreamer 的板子. 为什么要reverse ...
- [hdu1402]A * B Problem Plus(FFT模板题)
解题关键:快速傅里叶变换fft练习. 关于结果多项式长度的确定,首先将短多项式扩展为长多项式,然后扩展为两倍. #include<cstdio> #include<cstring&g ...
随机推荐
- vue+node+mongoDB 火车票H5(一)---准备工作,基本配置
前端菜鸟一枚,由于公司项目用到了vue,我虽然参与了,但是很多环境配置和流程还不是特别清楚,就想自己个人业余做个webapp看看, 对于完全新手而言,很多坑会纠结很久,所以想借此机会自己做的同时记录各 ...
- 3 CActiveXUI的一个Bug
如果主窗口直接用变量生成,则关闭窗口时会产生崩溃 如果用new的方式生成,则不会崩溃,所以给出一个临时的快速解决方案,即主窗口都用new生成,_tWinMain改为下面这样: i ...
- windows下使用IIS创建git服务
Bonobo Git Server 下载地址: https://bonobogitserver.com/ 安装方法:https://bonobogitserver.com/install/ 配置简单, ...
- pip安装lxml报错 Fatal error in launcher: Unable to create process using '"c:\users\administrator\appdata\local\programs\python\python36\python.exe" "C:\Users\Administrator\AppData\L
pip install lxml 安装报错 E:\apollo\spider_code>Fatal error in launcher: Unable to create process usi ...
- Python 模块之 time & datetime
Python 中提供了对时间日期的多种多样的处理方式,主要是在有 time 和 datetime 两个模块. time 在 Python 文档里,time 是归类在 Generic Operating ...
- 10款最佳SQL Server服务器监控工具
转自:http://server.51cto.com/sSecurity-587355.htm 推荐 | 10款最佳SQL Server服务器监控工具 服务器是网络中最重要的资源之一,SQL Serv ...
- ps 和 grep 查找消除 grep自身查找(转载)
用ps -def | grep查找进程很方便,最后一行总是会grep自己. $ ps -def | grep dragonfly-framework dean 5273 5272 0 15:23 pt ...
- Hazelcast 内存数据网格
Hazelcast ( www.hazelcast.com)是一种内存数据网格 in-memory data grid,提供Java程序员关键任务交易和万亿级内存应用. Hazelcast的集群属于“ ...
- CKEditor的下载、配置与使用
CKEditor简介: CKEditor 是一款功能强大的开源在线文本编辑器.它所见即所得的特点,使你在编辑时所看到的内容和格式,能够与发布后看到的效果完全一致.CKEditor 完全是基于 Java ...
- PAT 1095 Cars on Campus
1095 Cars on Campus (30 分) Zhejiang University has 8 campuses and a lot of gates. From each gate we ...