$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. KNN算法之图像处理一

    KNN: 1.数据挖掘分类技术中最简单的方法之一. 2.也称为邻近算法,K最近邻分类算法 3.每个样本都可以用它最接近的k个邻居来代表 4.一般,距离使用欧式距离或曼哈顿距离(通常,k≤20) pyt ...

  2. SGU 199 Beautiful People 二维最长递增子序列

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20885 题意: 求二维最长严格递增子序列. 题解: O(n^2) ...

  3. DataTable转List<T>集合

    #region DataTable转List集合 +static IList<T> DataTableToList<T>(DataTable dt) where T : cla ...

  4. [图算法] 1030. Travel Plan (30)

    1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...

  5. PAT 甲级 1008 Elevator

    https://pintia.cn/problem-sets/994805342720868352/problems/994805511923286016 The highest building i ...

  6. (转)Linux 命令--查看物理CPU个数、核数、逻辑CPU个数

    # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数 cat /proc/cpuinfo| ...

  7. PHP中普通方法和静态方法

    普通方法(实例方法) 一个类中定义的方法,可以为这个类的所有对象调用的方法.也可以理解为,这个类的所有对象,都各自有自己的一个该方法: 定义形式: class  类名{ function  方法名(形 ...

  8. Mysql 学习之 SQL的执行顺序

    mysql的json查询:                                                                       1.一条普通的SQL SELEC ...

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

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

  10. C++解析(15):二阶构造模式

    0.目录 1.构造函数与半成品对象 2.二阶构造 3.小结 1.构造函数与半成品对象 关于构造函数: 类的构造函数用于对象的初始化 构造函数与类同名并且没有返回值 构造函数在对象定义时自动被调用 问题 ...