https://www.luogu.org/problemnew/show/P3803

用反向学习的FFT通过这个东西。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; const int MAXN = 3e5;
const double PI = acos(-1.0); struct Complex {
double x, y;
Complex() {}
Complex(double x, double y): x(x), y(y) {}
friend Complex operator+(const Complex &a, const Complex &b) {
return Complex(a.x + b.x, a.y + b.y);
}
friend Complex operator-(const Complex &a, const Complex &b) {
return Complex(a.x - b.x, a.y - b.y);
}
friend Complex operator*(const Complex &a, const Complex &b) {
return Complex(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
} A[MAXN + 5], B[MAXN + 5]; void FFT(Complex a[], int n, int op) {
for(int i = 1, j = n >> 1; i < n - 1; ++i) {
if(i < j)
swap(a[i], a[j]);
int k = n >> 1;
while(k <= j) {
j -= k;
k >>= 1;
}
j += k;
}
for(int len = 2; len <= n; len <<= 1) {
Complex wn(cos(2.0 * PI / len), sin(2.0 * PI / len)*op);
for(int i = 0; i < n; i += len) {
Complex w(1.0, 0.0);
for(int j = i; j < i + (len >> 1); ++j) {
Complex u = a[j], t = a[j + (len >> 1)] * w ;
a[j] = u + t, a[j + (len >> 1)] = u - t;
w = w * wn;
}
}
}
if(op == -1) {
for(int i = 0; i < n; ++i)
a[i].x = (int)(a[i].x / n + 0.5);
}
} int pow2(int x) {
int res = 1;
while(res < x)
res <<= 1;
return res;
} void convolution(Complex A[], Complex B[], int Asize, int Bsize) {
int n = pow2(Asize + Bsize - 1);
for(int i = 0; i < n; ++i) {
A[i].y = 0.0;
B[i].y = 0.0;
}
for(int i = Asize; i < n; ++i)
A[i].x = 0;
for(int i = Bsize; i < n; ++i)
B[i].x = 0;
FFT(A, n, 1);
FFT(B, n, 1);
for(int i = 0; i < n; ++i)
A[i] = A[i] * B[i];
FFT(A, n, -1);
return;
} int C[MAXN + 5]; int carry(int n) {
for(int i = 0; i < n; i++) {
C[i] = A[i].x;
}
for(int i = 0; i < n; i++) {
C[i + 1] += C[i] / 10;
C[i] %= 10;
}
while(C[n]) {
C[n + 1] += C[n] / 10;
C[n] %= 10;
n++;
}
while(n && !C[n - 1]) {
n--;
}
return n;
} int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
int n;
scanf("%d", &n);
for(int i = n-1; i >=0; --i) {
int tmp;
scanf("%1d", &tmp);
A[i].x = tmp;
}
for(int i = n-1; i >=0; --i) {
int tmp;
scanf("%1d", &tmp);
B[i].x = tmp;
}
convolution(A, B, n, n);
n = carry(pow2(n + n - 1));
if(n == 0) {
printf("0\n");
} else {
for(int i = n - 1; i >= 0; --i) {
printf("%d", C[i]);
}
printf("\n");
}
return 0;
}

洛谷 - P3803 - 【模板】多项式乘法(FFT) - FFT的更多相关文章

  1. 洛谷.3803.[模板]多项式乘法(FFT)

    题目链接:洛谷.LOJ. FFT相关:快速傅里叶变换(FFT)详解.FFT总结.从多项式乘法到快速傅里叶变换. 5.4 又看了一遍,这个也不错. 2019.3.7 叕看了一遍,推荐这个. #inclu ...

  2. 洛谷.3803.[模板]多项式乘法(NTT)

    题目链接:洛谷.LOJ. 为什么和那些差那么多啊.. 在这里记一下原根 Definition 阶 若\(a,p\)互质,且\(p>1\),我们称使\(a^n\equiv 1\ (mod\ p)\ ...

  3. P3803 [模板] 多项式乘法 (FFT)

    Rt 注意len要为2的幂 #include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); inli ...

  4. 洛谷.4512.[模板]多项式除法(NTT)

    题目链接 多项式除法 & 取模 很神奇,记录一下. 只是主要部分,更详细的和其它内容看这吧. 给定一个\(n\)次多项式\(A(x)\)和\(m\)次多项式\(D(x)\),求\(deg(Q) ...

  5. 洛谷.4238.[模板]多项式求逆(NTT)

    题目链接 设多项式\(f(x)\)在模\(x^n\)下的逆元为\(g(x)\) \[f(x)g(x)\equiv 1\ (mod\ x^n)\] \[f(x)g(x)-1\equiv 0\ (mod\ ...

  6. 洛谷 P4512 [模板] 多项式除法

    题目:https://www.luogu.org/problemnew/show/P4512 看博客:https://www.cnblogs.com/owenyu/p/6724611.html htt ...

  7. 洛谷 P4238 [模板] 多项式求逆

    题目:https://www.luogu.org/problemnew/show/P4238 看博客:https://www.cnblogs.com/xiefengze1/p/9107752.html ...

  8. FFT/NTT总结+洛谷P3803 【模板】多项式乘法(FFT)(FFT/NTT)

    前言 众所周知,这两个东西都是用来算多项式乘法的. 对于这种常人思维难以理解的东西,就少些理解,多背板子吧! 因此只总结一下思路和代码,什么概念和推式子就靠巨佬们吧 推荐自为风月马前卒巨佬的概念和定理 ...

  9. 洛谷P3803 【模板】多项式乘法(FFT)

    P3803 [模板]多项式乘法(FFT) 题目背景 这是一道FFT模板题 题目描述 给定一个n次多项式F(x),和一个m次多项式G(x). 请求出F(x)和G(x)的卷积. 输入输出格式 输入格式: ...

  10. 洛谷 P3803 【模板】多项式乘法(FFT)

    题目链接:P3803 [模板]多项式乘法(FFT) 题意 给定一个 \(n\) 次多项式 \(F(x)\) 和一个 \(m\) 次多项式 \(G(x)\),求 \(F(x)\) 和 \(G(x)\) ...

随机推荐

  1. linux下挂载U盘方法

    1.使用 cat /proc/partitions 查看系统现在有哪些分区:[root@localhost ~]# cat /proc/partitions major minor  #blocks  ...

  2. 2014-03-01 春季PAT 1073-1076解题报告

    今天下午的PAT考试状态不理想,回来怒刷了一遍,解题报告如下: 1073. Scientific Notation (20) 基本模拟题,将一长串的科学计数转换为普通的数字表示方式.思路是是数组存储输 ...

  3. django之配置文件setting.py

    一:配置文件setting.py中的简单配置更改 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 简单解释 ...

  4. dede标签大全

    想必很多人对后台不熟悉,并且觉得很难.其实不难,只是你们没有找到合适的方法学习而已!只有找到一个合适的学习方法,不管做什么事情,我想都很容易.学习讲究的是效率,而效率又是由思路决定的.就拿网页制作来说 ...

  5. Android多线程方案

    为主线程减轻负的多线程方案有哪些呢?这些方案分别适合在什么场景下使用? Android系统为我们提供了若干组工具类来帮助解决这个问题. AsyncTask: 为UI线程与工作线程之间进行快速的切换提供 ...

  6. 笨办法学Python(learn python the hard way)--练习程序1-10

    下面是当初看这本书时按照书中的代码做的练习,一行一行敲下来的,都已经试运行过,没有错误(基于python3),练习1-练习10 #ex1.py 1 #print("Hello world!& ...

  7. ["1", "2", "3"].map(parseInt) 答案是多少?

    让我们先看看最直接最粗暴的方式 没错,答案就是:[1, NaN, NaN],那为什么答案是[1, NaN, NaN]呢? 1.让我们先了解一下map函数的定义 JavaScript Array map ...

  8. vue子组件获取父组件的数据

  9. html+js(swiper.js)+css左右滑动切换页面效果,适配移动端

    demo: 截图: 结构:1.swiper-progress.html2.css文件夹 -swiper.css -swiper.min.css 3.js文件夹 -swiper.min.js -swip ...

  10. 2017华南理工华为杯D bx回文

    比赛的时候队友过了,补补题XD. 题目链接:https://scut.online/p/125(赛后补题) 125. 笔芯回文     题目描述 bx有一个长度一个字符串S,bx可以对其进行若干次操作 ...