HDU 1402 A * B Problem Plus ——(大数乘法,FFT)
因为刚学fft,想拿这题练练手,结果WA了个爽= =。
总结几点犯的错误:
1.要注意处理前导零的问题。
2.一定要注意数组大小的问题。(前一个fft的题因为没用到b数组,所以b就没管,这里使用了b数组,结果忘记给其大小乘以4倍了)
代码如下:
#include<bits/stdc++.h>
using namespace std;
const double pi = atan(1.0)*;
const int N = 5e4 + ;
typedef long long ll; struct Complex {
double x,y;
Complex(double _x=,double _y=)
:x(_x),y(_y) {}
Complex operator + (Complex &tt) { return Complex(x+tt.x,y+tt.y); }
Complex operator - (Complex &tt) { return Complex(x-tt.x,y-tt.y); }
Complex operator * (Complex &tt) { return Complex(x*tt.x-y*tt.y,x*tt.y+y*tt.x); }
};
Complex a[N*],b[N*];
void fft(Complex *a, int n, int rev) {
// n是(大于等于相乘的两个数组长度)2的幂次 ; 比如长度是5 ,那么 n = 8 2^2 < 5 2^3 > 5
// 从0开始表示长度,对a进行操作
// rev==1进行DFT,==-1进行IDFT
for (int i = ,j = ; i < n; ++ i) {
for (int k = n>>; k > (j^=k); k >>= );
if (i<j) std::swap(a[i],a[j]);
}
for (int m = ; m <= n; m <<= ) {
Complex wm(cos(*pi*rev/m),sin(*pi*rev/m));
for (int i = ; i < n; i += m) {
Complex w(1.0,0.0);
for (int j = i; j < i+m/; ++ j) {
Complex t = w*a[j+m/];
a[j+m/] = a[j] - t;
a[j] = a[j] + t;
w = w * wm;
}
}
}
if (rev==-) {
for (int i = ; i < n; ++ i) a[i].x /= n,a[i].y /= n;
}
} char s[N], t[N];
int c[N*]; int main(){
/*a[0] = Complex(0,0); // a[0]: x的0次项。
a[1] = Complex(1,0);
a[2] = Complex(2,0);
a[3] = Complex(3,0); b[0] = Complex(3,0);
b[1] = Complex(2,0);
b[2] = Complex(1,0);
b[3] = Complex(0,0);
fft(a,8,1);
fft(b,8,1);
for(int i = 0 ; i < 8 ; i ++){
a[i] = a[i] * b[i];
}
fft(a,8,-1);
for(int i = 0 ; i < 8 ; i ++){
cout << i << " " << a[i].x << endl;;
}*/
while(scanf("%s%s",s,t) == )
{
int n = strlen(s), m = strlen(t);
for(int i=;i<n;i++) a[i] = Complex(s[n-i-]-'', );
for(int i=;i<m;i++) b[i] = Complex(t[m-i-]-'', );
int len = n+m-;
int LIM = ;
while(LIM < len) LIM <<= ;
for(int i=n;i<LIM;i++) a[i] = Complex(, );
for(int i=m;i<LIM;i++) b[i] = Complex(, );
fft(a, LIM, );
fft(b, LIM, );
for(int i=;i<LIM;i++) a[i] = a[i] * b[i];
fft(a, LIM, -);
memset(c, , sizeof c);
for(int i=;i<len-;i++)
{
c[i] += (int)(a[i].x + 0.1);
c[i+] += c[i] / ;
c[i] %= ;
}
c[len-] += (int)(a[len-].x + 0.1);
if(c[len-] > )
{
c[len] = c[len-] / ;
c[len-] %= ;
len++;
}
for(int i=len-;i>;i--) if(c[i] == ) len--; else break; // 0 * 345 = 0 instead of 000
for(int i=;i<len/;i++) swap(c[i], c[len-i-]);
for(int i=;i<len;i++) printf("%d",c[i]);
puts("");
}
return ;
}
HDU 1402 A * B Problem Plus ——(大数乘法,FFT)的更多相关文章
- HDU 1402 A * B Problem Plus 快速傅里叶变换 FFT 多项式
http://acm.hdu.edu.cn/showproblem.php?pid=1402 快速傅里叶变换优化的高精度乘法. https://blog.csdn.net/ggn_2015/artic ...
- hdu 1402 A * B Problem Plus FFT
/* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...
- [hdu1402]大数乘法(FFT模板)
题意:大数乘法 思路:FFT模板 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ...
- 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 ...
- HDU 1402 大数乘法 FFT、NTT
A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU - 1402 A * B Problem Plus (FFT实现高精度乘法)
题意:计算A*B,A,B均为长度小于50000的整数. 这是FFT在大整数相乘中的一个应用,我本来想用NTT做的,但NTT由于取模很可能取炸,所以base必须设得很小,而且效率也比不上FFT. A和B ...
- HDU - 1402 A * B Problem Plus FFT裸题
http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意: 求$a*b$ 但是$a$和$b$的范围可以达到 $1e50000$ 题解: 显然...用字符串模拟 ...
- HDU 1402 A * B Problem Plus(FFT)
Problem Description Calculate A * B. Input Each line will contain two integers A and B. Process to ...
- HDU 1402:A * B Problem Plus
A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- 使用Android Studio遇到的问题
学校这课程安排没明白...又要写安卓了. 这里把使用Android Studio3.1时遇到的问题记录下. Android Studio无法启动模拟器 解决: 控制面板->程序->关闭Hy ...
- sql 语句实现一串数字位数不足在左侧补0的技巧
https://www.cnblogs.com/mylydg/p/5725189.html 在日常使用sql做查询插入操作时,我们通常会用到用sql查询一串编号,这串编号由数字组成.为了统一美观,我们 ...
- 定时任务cron表达式详解
参考自:https://blog.csdn.net/fanrenxiang/article/details/80361582 一 cron表达式 顺序 秒 分 时 日期 月份 星期 年(可选) 取值 ...
- CocoaPods - 发布自己的模块(公有库、私有库)
CocoaPods发布框架到远程公有库 1.编写代码~上传远程仓库 git init git add . git commit -m '提交到本地分支' //关联远程仓库 git remote add ...
- JVM锁优化以及区别
偏向所锁,轻量级锁都是乐观锁,重量级锁是悲观锁. 首先简单说下先偏向锁.轻量级锁.重量级锁三者各自的应用场景: 偏向锁:只有一个线程进入临界区: 轻量级锁:多个线程交替进入临界区: 重量级锁:多个线程 ...
- Linux查找文件之Find命令
Linux系统文件中常用属性包括以下内容:名称,大小,权限,属主,属组,修改时间,访问时间等.在庞大的Linux系统中查询文件,需要借助查找工具来实现,依此可以查询相同或指定属性的文件,本文所讲的查询 ...
- sql server统计总成绩和排名
这里的图片可以拖拽到一个新页面查看原图!!!! 这里有两个表,需要查询总成绩和排名 Sql语句: select ST.name,SE.Chinese,SE.Math,SE.English, ( SE. ...
- OpenStack kilo版(7) 部署dashboard
安装dashboard root@controller:~# apt-get install openstack-dashboard 配置 /etc/openstack-dashboard/loc ...
- apache ftp server 设置
<server xmlns="http://mina.apache.org/ftpserver/spring/v1" xmlns:xsi="http://www.w ...
- pringBoot2.0启用https协议
SpringBoot2.0之后,启用https协议的方式与1.*时有点儿不同,贴一下代码. 我的代码能够根据配置参数中的condition.http2https,确定是否启用https协议,如果启用h ...