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 ...
随机推荐
- shop++源码反编译----随笔
一.applicationContext-mvc.xml配置 1.读取配置文件 <context:property-placeholder location="classpath*:/ ...
- Dart基础学习01--走近Dart
什么是Dart 在Dart的官网上是这样介绍Dart的: Dart is an open-source, scalable programming language, with robust libr ...
- Setting IE11 with Group Policy Preferences
一.Setting Home Page with Group Policy Preferences 1.Open the Group Policy Management Console and cre ...
- Storm的组件
摘自网上 当时写的很好,很详细的介绍了各个组件直接的关系 Storm集群和Hadoop集群表面上看很类似.但是Hadoop上运行的是MapReduce jobs,而在Storm上运行的是拓扑(topo ...
- RTSP客户端接收存储数据(live555库中的openRTSP实例)
一.openRTSP编译运行 a)windows下编译运行 还是以mediaServer作为服务端,openRTSP作为客户端 b)Linux下编译运行 转自http://kuafu80.blog.1 ...
- HDU2571:命运(简单dp)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2571 没什么好说的,不过要处理好边界. 代码如下: #include <iostream> # ...
- Ubuntu 13.04安装mysql workbench
1.如果你没有更换源,是主服务器的官网源,这时候你可以直接用sudo apt-get install mysql-workbench来安装(前提是已经装好mysql相关服务) 2.如果你第一条装不了, ...
- beego——URL构建
如果可以匹配URl,那么beego也可以生成URL吗?当然可以. UrlFor()函数就是用于构建执行函数的URL的.它把对应控制器和函数名结合的字符串作为第一个参数,其余参数对应URL中的变量.未知 ...
- matplotlib作图——plot() 线图
线图 #定义 matplotlib.pyplot.plot() plot([x], y, [fmt], data=None, **kwargs) plot([x], y, [fmt], [x2], y ...
- Django框架之单表操作
一.添加表记录 对于单表有两种方式 # 添加数据的两种方式 # 方式一:实例化对象就是一条表记录 Frank_obj = models.Student(name ="海东",cou ...