$A * B$

FFT模板题,找到了一个看起来很清爽的模板

/** @Date    : 2017-09-19 22:12:08
* @FileName: HDU 1402 FFT 大整数乘法.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 2e5+20;
const double eps = 1e-8;
const double pi = acos(-1.0); struct Complex
{
double a, b;
Complex(){}
Complex(double _a, double _b):a(_a),b(_b){}
Complex(double _a):a(_a),b(0.0){}
inline Complex operator +(const Complex &z)const{
return Complex(a + z.a, b + z.b);
}
inline Complex operator -(const Complex &z)const{
return Complex(a - z.a, b - z.b);
}
inline Complex operator *(const Complex &z)const{
return Complex(a * z.a - b * z.b, a * z.b + b * z.a);
}
inline Complex operator /(const Complex &z)const{
double m = z.a * z.a + z.b * z.b;
return Complex((a * z.a + b * z.b) / m, (z.a * b - z.b * a) / m);
}
}A[N], B[N];
int L;
int rev[N];
int ans[N];
char a[N], b[N]; void init()
{
MMF(A);
MMF(B);
MMF(ans);
L = 0;//?
} int reverse(int x,int r) //蝴蝶操作
{
int ans=0;
for(int i=0; i<r; i++){
if(x&(1<<i)){
ans+=1<<(r-i-1);
}
}
return ans;
} void rader(LL f[], int len)
{
for(int i = 1, j = len >> 1; i < len - 1; i++)
{
if(i < j) swap(f[i], f[j]);
int k = len >> 1;
while(j >= k)
{
j -= k;
k >>= 1;
}
if(j < k) j += k;
}
}
void FFT(Complex c[], int nlen, int on)
{
Complex wn, w, x, y;
for(int i = 0; i < nlen; i++)
if(i < rev[i]) swap(c[i], c[rev[i]]);
for(int i = 1; i < nlen; i <<= 1)
{
wn = Complex(cos(pi/i), sin(pi/i) * on);
for(int p = i << 1, j = 0; j < nlen; j+= p)
{
w = Complex(1, 0);
for(int k = 0; k < i; k++, w = w * wn)
{
x = c[j + k];
y = w * c[j + k + i];
c[j + k] = x + y;
c[j + k + i] = x - y;
}
}
}
if(on == -1)
for(int i = 0; i < nlen; i++)
c[i].a /= (double)nlen;
} void work(Complex a[], Complex b[], int len)
{
FFT(a, len, 1);
FFT(b, len, 1);
for(int i = 0; i < len; i++)
a[i] = a[i] * b[i];
FFT(a, len, -1);
}
int main()
{
while(~scanf("%s%s", a, b))
{
init();
int alen = strlen(a);
int blen = strlen(b);
int len = alen + blen;
//getlen
int n;
for(n = 1; n < len - 1; n <<= 1, L++);
//rev
for(int i = 0; i < n; i++)
rev[i] = (rev[i>>1] >> 1) | ((i & 1) << (L - 1));
//deal
for(int i = 0; i < alen; i++)
A[i] = a[alen - i - 1] - '0';
for(int i = 0; i < blen; i++)
B[i] = b[blen - i - 1] - '0';
work(A, B, n);
///
for(int i = 0; i <= len; i++)
ans[i] = (int)(A[i].a + 0.5);
for(int i = 0; i <= len; i++)
ans[i + 1] += ans[i] / 10, ans[i] %= 10;
while(ans[len] == 0 && len)
len--;
for(int i = len; ~i; i--)
printf("%c", ans[i] + '0');
printf("\n");
}
return 0;
}

HDU 1402 FFT 大数乘法的更多相关文章

  1. HDU 1402 fft 模板题

    题目就是求一个大数的乘法 这里数字的位数有50000的长度,按平时的乘法方式计算,每一位相乘是要n^2的复杂度的,这肯定不行 我们可以将每一位分解后作为系数,如153 = 1*x^2 + 5*x^1 ...

  2. hdu 1402 FFT(模板)

    A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. hdu 1402 A * B Problem Plus FFT

    /* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...

  4. 1028 大数乘法 V2(FFT or py)

    1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果.   Input 第1行:大数A 第2行:大数B ...

  5. ACM学习历程—51NOD1028 大数乘法V2(FFT)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028 题目大意就是求两个大数的乘法. 但是用普通的大数乘法,这 ...

  6. A * B Problem Plus HDU - 1402 (FFT)

    A * B Problem Plus HDU - 1402 (FFT) Calculate A * B.  InputEach line will contain two integers A and ...

  7. fft模板 HDU 1402

    // fft模板 HDU 1402 #include <iostream> #include <cstdio> #include <cstdlib> #includ ...

  8. [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 ...

  9. HDU 1402

    http://acm.hdu.edu.cn/showproblem.php?pid=1402 fft做O(nlog(n))大数乘法,kuangbin的模板 #include <stdio.h&g ...

随机推荐

  1. DB2 V9 默认帐户信息和服务启动信息

    1 dasusr1 DB2 管理服务器用户是管理DAS(Database Adminitrator Service).要完全适用db2 cc 必须启动DAS.DB2 管理服务器(DAS)响应来自 DB ...

  2. MySQL的并发访问控制(锁)

    前言:任何的数据集只要支持并发访问模型就必须基于锁机制进行访问控制 锁种类 读锁:共享锁,允许给其他人读,不允许他人写写锁:独占锁, 不允许其他人读和写 锁类型 显示锁:用户手动请求读锁或写锁隐式锁: ...

  3. beta阶段——项目复审

    beta阶段--项目复审 小组的名字和链接 优点 缺点 bug 排名顺序 颜罗王team http://www.cnblogs.com/LDLYMteam 界面清新,音乐能够选择是否播放,词汇按照四六 ...

  4. 补发9.26“天天向上”团队Scrum站立会议

    组长:王森 组员:张金生 张政 栾骄阳 时间:2016.09.26 地点:612寝 组员 已完成 未完成 王森 可行性分析 找出设计亮点 张金生 寻找UI素材 设计用户操作 张政 搭建环境 基础逻辑框 ...

  5. 设计模式php篇(一)————单例模式

    话不多说,直接上代码: <?php namespace DesignPattern; /** * php设计模式之单例模式 */ class SingleInstance { private s ...

  6. 【Linux 命令】- tar 命令

    语法 tar [-ABcdgGhiklmMoOpPrRsStuUvwWxzZ][-b <区块数目>][-C <目的目录>][-f <备份文件>][-F <Sc ...

  7. apache server-status配置

    引言 自己配置LAMP服务器时(xwamp),获取状态信息出现错误: You don't have permission to access /server-status on this server ...

  8. UVA11653_Buses

    这个题目很有意思,一不小心就会让人坑在里面. 题意是这样的,给你n,k,l.分别表示总共的长度,长度为5和10的车的不同颜色数量现在问你要把n的填满有多少种方案. 很多人一开始都会脑子一根筋地想用排列 ...

  9. CodeChef KnightMov

    码死了...考试的时候基本上是写一会儿思考一会儿人生....考完了调了调...最后400行+....不应该这么长的....以后重写一下再补题解..... 也许这就是蒟蒻吧.jpg 安利cstdio博客 ...

  10. 【hdu5306】Gorgeous Sequence 线段树区间最值操作

    题目描述 给你一个序列,支持三种操作: $0\ x\ y\ t$ :将 $[x,y]$ 内大于 $t$ 的数变为 $t$ :$1\ x\ y$ :求 $[x,y]$ 内所有数的最大值:$2\ x\ y ...