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. C# 实现实体类和Xml转换

    一.实体类转换成XML 将实体类转换成XML需要使用XmlSerializer类的Serialize方法,将实体类序列化 public static string XmlSerialize<T& ...

  2. 基于firebird的数据转存

    功能:使用于相同的表从一个数据库转存到另一数据库: 方式:直连fdb并加载django,引用django的model完成: 原因:1.select * from *** 返回的数有很多None,直接i ...

  3. cat 合并文件或查看文件内容

    1.命令功能 cat 合并文件或者查看文件内容. 2.语法格式 cat   option    file 参数说明 参数 参数说明 -n 打印文本,并显示每行行号并且空白行也同样包括 -b 与-n用法 ...

  4. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number (莫队)

    题目链接:http://codeforces.com/contest/617/problem/E 题目大意:有n个数和m次查询,每次查询区间[l, r]问满足ai ^ ai+1 ^ ... ^ aj ...

  5. vue子传父、父传子

    子传父 vue子传父使用$emit传值 子组件: <template> <div> <button @click="toParent">点击传到 ...

  6. python 日期封装

    import time import datetime import locale class TimeUtil: def __init__(self, curtime=None): self.cur ...

  7. Categorical Data

    This is an introduction to pandas categorical data type, including a short comparison with R's facto ...

  8. nyoj 119: 士兵杀敌(三) 【RMQ模板】

    题目链接 贴个板子.. #include<bits/stdc++.h> using namespace std; int n,q; ],d1[][],d2[][]; void RMQ_in ...

  9. 【leetcode】1048. Longest String Chain

    题目如下: Given a list of words, each word consists of English lowercase letters. Let's say word1 is a p ...

  10. Angular:OnPush变化检测策略介绍

    在OnPush策略下,Angular不会运行变化检测(Change Detection ),除非组件的input接收到了新值.接收到新值的意思是,input的值或者引用发生了变化.这样听起来不好理解, ...