$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. ACM ICPC 2016–2017, NEERC, Northern Subregional Contest Problem J. Java2016

    题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229510 时间限制:2s 空间限制:256MB 题目大意: 给定一个数字c 用 " ...

  2. Alpha冲刺——第十天

    Alpha第十天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...

  3. 博弈---尼姆博奕(Nimm Game)(重点)

    尼姆博奕(Nimm Game):有三堆各若干个物品,两个人轮流从某一堆取任意多的 物品,规定每次至少取一个,多者不限,最后取光者得胜. 这种情况最有意思,它与二进制有密切关系,我们用(a,b,c)表示 ...

  4. UVALive 6912 Prime Switch 暴力枚举+贪心

    题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  5. HDU 5656 CA Loves GCD 01背包+gcd

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5656 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  6. React-native APK打包

    安卓相关工具配置到环境变量,这样可以将安卓相关工具可以直接在cmd命令中调用 1 检查gradle版本 查看里面对应的编译工具版本号,如果提示版本不对你,那么直接去更新android sdk,相关的s ...

  7. 团队项目-BUG排查-ADT工程 To Android Studio 一整天的排查日记

    4-22 10:44至4-23 0:45 ①打开Eclipse从Github上Clone MathsApp到本机,报错'Unable to resolve target'android-19' ②尝试 ...

  8. 复利计算程序单元测试(C语言)

    对我们和复利计算程序,写单元测试. 有哪些场景? 期待的返回值 写测试程序. 运行测试. 我的复利计算程序是用C语言写的,不懂使用C语言的测试工具,所以用C语言的运行结果来反映测试结果. 测试模块(场 ...

  9. React.js + LiveReload配置详解

    一.介绍一下LiveReload: LiveReload monitors changes in the file system. As soon as you save a file, it is ...

  10. 转载-C++ vector 用法

    转自:http://www.cnblogs.com/wang7/archive/2012/04/27/2474138.html 在c++中,vector是一个十分有用的容器,下面对这个容器做一下总结. ...