【模板】A*B Problem(FFT快速傅里叶)
题目:给出两个n位10进制整数x和y,你需要计算x*y。($n \leq 60000$)
分析:
两个正整数的相乘可以视为两个多项式的相乘,
例如 $15 \times 16 = 240$,
可写成 $(5+x)*(6+x) = 30 + 11x + x^2$,$x=10$
这样得到多项式 $A(x)$ 和 $B(x)$,并且能用FFT求出 $C(x)=A(x)B(x)$,
怎么得到最终结果,我们要将 $x=10$ 代入吗?
$n$ 这么大,遍历一遍也没有这么大的数据类型能存下,其次,这也不是必要的。
$x=10$ 是 $C(x)$ 已经相当于十进制,我们模拟一下进位就可以了。
// luogu-judger-enable-o2
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int MAXN = * + ;
inline int read() {
char c = getchar(); int x = , f = ;
while (c < '' || c > '') {if (c == '-')f = -; c = getchar();}
while (c >= '' && c <= '') {x = x * + c - ''; c = getchar();}
return x * f;
}
const double Pi = acos(-1.0);
const double Eps = 1e-;
double ccos[MAXN], ssin[MAXN];
struct complex {
double x, y;
complex (double xx = , double yy = ) {x = xx, y = yy;}
} a[MAXN], b[MAXN];
complex operator + (complex a, complex b) { return complex(a.x + b.x , a.y + b.y);}
complex operator - (complex a, complex b) { return complex(a.x - b.x , a.y - b.y);}
complex operator * (complex a, complex b) { return complex(a.x * b.x - a.y * b.y , a.x * b.y + a.y * b.x);} //不懂的看复数的运算那部分
void fast_fast_tle(int limit, complex *a, int type) {
if (limit == ) return ; //只有一个常数项
complex a1[limit >> ], a2[limit >> ];
for (int i = ; i < limit; i += ) //根据下标的奇偶性分类
a1[i >> ] = a[i], a2[i >> ] = a[i + ];
fast_fast_tle(limit >> , a1, type);
fast_fast_tle(limit >> , a2, type);
complex Wn = complex(ccos[limit] , type * ssin[limit]), w = complex(, );
//complex Wn = complex(cos(2.0 * Pi / limit) , type * sin(2.0 * Pi / limit)), w = complex(1, 0);
//Wn为单位根,w表示幂
for (int i = ; i < (limit >> ); i++, w = w * Wn) //这里的w相当于公式中的k
{
complex tmp = w * a2[i];
a[i] = a1[i] + tmp;
a[i + (limit >> )] = a1[i] - tmp; //利用单位根的性质,O(1)得到另一部分
}
} char s[MAXN];
int res[MAXN]; int main() {
int N = read();
scanf("%s", s);
for (int i = ; i < N; i++) a[i].x = s[N--i]-'';
scanf("%s", s);
for (int i = ; i < N; i++) b[i].x = s[N--i]-''; //for(int i = 0;i < N;i++) printf("%f ", a[i]); int limit = ; while (limit <= *N) limit <<= ; for(int i = ;i <= limit;i++)
{
ccos[i] = cos(2.0 * Pi / i);
ssin[i] = sin(2.0 * Pi / i);
} fast_fast_tle(limit, a, );
fast_fast_tle(limit, b, );
//后面的1表示要进行的变换是什么类型
//1表示从系数变为点值
//-1表示从点值变为系数
//至于为什么这样是对的,可以参考一下c向量的推导过程,
for (int i = ; i <= limit; i++)
a[i] = a[i] * b[i];
fast_fast_tle(limit, a, -); for(int i = ;i <= *N;i++) res[i] = int(a[i].x/limit+0.5); int tmp = ; //进位
for(int i = ;i <= *N;i++)
{
res[i] += tmp;
tmp = res[i] / ;
res[i] = res[i] % ;
} bool flag = false;
for (int i = *N; i >= ; i--)
{
if(res[i]) flag = true; //注意处理前导0,题干有说
if(flag) printf("%d", res[i]); //按照我们推倒的公式,这里还要除以n
}
return ;
}
【模板】A*B Problem(FFT快速傅里叶)的更多相关文章
- FFT快速傅里叶模板
FFT快速傅里叶模板…… /* use way: assign : h(x) = f(x) * g(x) f(x):len1 g(x):len2 1. len = 1; while(len < ...
- [Luogu 1919]【模板】A*B Problem升级版(FFT快速傅里叶)
Description 给出两个n位10进制整数x和y,你需要计算x*y. Input 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数为n的正整数y. Output 输出 ...
- luogu P1919 【模板】A*B Problem升级版(FFT快速傅里叶)
模板 嗯 做多项式乘法,进位 没了 #include<cmath> #include<cstdio> #include<cstring> #include<a ...
- Luogu P1919 【模板】A*B Problem升级版(FFT快速傅里叶_FFT
这其实就是一道裸的FFT 核心思想:把两个数拆成两个多项式用FFT相乘,再反序输出 py解法如下: input() print(int(input())*int(input())) 皮一下hihi f ...
- 洛谷 P1919 【模板】A*B Problem升级版(FFT快速傅里叶)
题目来源 吐槽下P3803都是紫题... 真心好写,本想一遍过的...但是 我真是太菜了... #include<bits/stdc++.h> using namespace std; ; ...
- P1919 【模板】A*B Problem升级版(FFT快速傅里叶)
题目描述 给出两个n位10进制整数x和y,你需要计算x*y. 输入输出格式 输入格式: 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数为n的正整数y. 输出格式: 输出一 ...
- 洛谷P1919 【模板】A*B Problem升级版(FFT快速傅里叶)
题目描述 给出两个n位10进制整数x和y,你需要计算x*y. 输入输出格式 输入格式: 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数为n的正整数y. 输出格式: 输出一 ...
- 【模板】A*B Problem升级版(FFT快速傅里叶)
题目描述 给出两个 $n$ 位10进制数x和y,求x*y(详见 洛谷P1919) 分析 假设已经学会了FFT/NTT. 高精度乘法只是多项式乘法的特殊情况,相当于$x=10$ 时. 例如n=3,求12 ...
- 洛谷.1919.[模板]A*B Problem升级版(FFT)
题目链接:洛谷.BZOJ2179 //将乘数拆成 a0*10^n + a1*10^(n-1) + ... + a_n-1的形式 //可以发现多项式乘法就模拟了竖式乘法 所以用FFT即可 注意处理进位 ...
随机推荐
- CSP(noip)中的简单对拍写法
以a+b为例 这是随机数据 #include<iostream> #include<cstdio> #include<ctime> using namespace ...
- 类的练习2——python编程从入门到实践
9-7 管理员: 管理员是一种特殊的用户.编写一个名为Admin的类,并让它继承练习9-3或者9-5的User类.添加一个名为privileges的属性,用于存储一个由字符串(如"can a ...
- RT1052 BootLoader总结
RT1052 BootLoader总结 概述 Bootloader涉及到的RT1052单片机资源有:Cache,ram,外部SDRAM,ARM7汇编指令,外部dataFlash. 升级功能涉及到的其 ...
- Android Studio代码错误提示无效(not available in Power Save mode)
针对一位博友提的问题,我这边写出来,估计还是很多人会碰到这个问题,但是不知道如何解决的. 就是在设置了代码自动提示功能后,发现不生效的,如何设置代码自动提示请戳这:Android Studio如何设置 ...
- 阿里巴巴 Java 开发手册 (七) 控制语句
1. [强制]在一个 switch 块内,每个 case 要么通过 break/return 等来终止,要么注释说明程 序将继续执行到哪一个 case 为止:在一个 switch 块内,都必须包含一个 ...
- django.db.utils.InternalError: (1060, "Duplicate column name 'user_id'")迁移报错解决方法
django.db.utils.InternalError: (1060, "Duplicate column name 'user_id'")迁移报错解决方法 django.db ...
- 自学Python编程的第五天(希望有IT大牛帮我看最下面的代码)----------来自苦逼的转行人
2019-09-15-15:40:24 今天没有学知识,是一个一周总结,把这一周学的知识总结一遍,然后把做过的练习题再做一遍 看是否还会有再出现同样的错误,而且还可以知道有哪些知识点没有掌握好,可以把 ...
- python小作业
目录 1.简述变量命名规范 2.name = input(">>>") name变量是什么数据类型通过代码检测 3.if条件语句的基本结构? 4.用print打印 ...
- spring中bean的作用域属性singleton与prototype的区别
1.singleton 当一个bean的作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会 ...
- react 实现评分组件
写了个评分组件,效果如下 组件Rate.js import React, { Component } from 'react' import './Rate.less' export default ...