P3803 FFT求多项式系数

传送门:https://www.luogu.org/problemnew/show/P3803

题意:

这是一道FFT模板题,求多项式系数

题解:

对a和b的系数求一个fft,转换为点乘式后

O(n)扫一遍直接算系数即可

对于多项式相加

\(\begin{array}{l}{A(x)=\left(x_{0}, y_{0}\right),\left(x_{1}, y_{1}\right) \ldots\left(x_{n}, y_{n}\right)} \\ {B(x)=\left(x_{0}, y_{0}^{\prime}\right),\left(x_{1}, y_{1}^{\prime}\right) \ldots .\left(x_{n}, y_{n}\right)}\end{array}\)

\(A(x)+B(x)=\left(x_{0}, y_{0}+y_{0}^{\prime}\right),\left(x_{1}, y_{1}+y_{1}^{\prime}\right)\left(x_{n}, y_{n}+y_{n}^{\prime}\right)\)

对于多项式相乘,我们需要补上一些项使得最后乘的的系数个数为2n+1

\(\begin{array}{l}{A(x)=\left(x_{0}, y_{0}\right),\left(x_{1}, y_{1}\right) \ldots\left(x_{2 n}, y_{2 n}\right)} \\ {B(x)=\left(x_{0}, y_{0}^{\prime}\right),\left(x_{1}, y_{1}^{\prime}\right) \ldots .\left(x_{2 n}, y_{2 n}\right)}\end{array}\)

\(A(x) B(x)=\left(x_{0}, y_{0} y_{0}^{\prime}\right),\left(x_{1}, y_{1} y_{1}^{\prime}\right)\left(x_{2 n}, y_{2 n} y_{2 n}^{\prime}\right)\)

代码:

#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 1e7 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1.0);
LL quick_pow(LL x, LL y) {
LL ans = 1;
while(y) {
if(y & 1) {
ans = ans * x % mod;
} x = x * x % mod;
y >>= 1;
} return ans;
}
struct complex {
double x, y;
complex(double xx = 0, double yy = 0) {
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);
} int n, m;
int l, r[maxn];
int limit = 1;
void fft(complex *A, int type) {
for(int i = 0; i < limit; i++) {
if(i < r[i]) swap(A[i], A[r[i]]);
}
for(int mid = 1; mid < limit; mid <<= 1) {
complex Wn(cos(Pi / mid), type * sin(Pi / mid));
for(int R = mid << 1, j = 0; j < limit; j += R) {
complex w(1, 0);
for(int k = 0; k < mid; k++, w = w * Wn) {
complex x = A[j + k], y = w * A[j + mid + k];
A[j + k] = x + y;
A[j + mid + k] = x - y;
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
scanf("%d%d", &n, &m);
for(int i = 0; i <= n; i++) scanf("%lf", &a[i].x);
for(int i = 0; i <= m; i++) scanf("%lf", &b[i].x);
while(limit <= n + m) limit <<= 1, l++;
for(int i = 0; i < limit; i++) {
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));
}
fft(a, 1);
fft(b, 1);
for(int i = 0; i <= limit; i++) {
a[i] = a[i] * b[i];
}
fft(a, -1);
for(int i = 0; i <= n + m; i++) {
printf("%d ", (int)(a[i].x / limit + 0.5));
}
printf("\n");
return 0;
}

P3803 FFT求多项式系数的更多相关文章

  1. 洛谷p3803 FFT入门

    洛谷p3803 FFT入门 ps:花了我一天的时间弄懂fft的原理,感觉fft的折半很神奇! 大致谈一谈FFT的基本原理: 对于两个多项式的卷积,可以O(n^2)求出来(妥妥的暴力) 显然一个多项式可 ...

  2. [笔记]ACM笔记 - 利用FFT求卷积(求多项式乘法)

    卷积 给定向量:, 向量和: 数量积(内积.点积): 卷积:,其中 例如: 卷积的最典型的应用就是多项式乘法(多项式乘法就是求卷积).以下就用多项式乘法来描述.举例卷积与DFT. 关于多项式 对于多项 ...

  3. CodeForces - 528D Fuzzy Search (FFT求子串匹配)

    题意:求母串中可以匹配模式串的子串的个数,但是每一位i的字符可以左右偏移k个位置. 分析:类似于 UVALive -4671. 用FFT求出每个字符成功匹配的个数.因为字符可以偏移k个单位,先用尺取法 ...

  4. FFT求卷积(多项式乘法)

    FFT求卷积(多项式乘法) 卷积 如果有两个无限序列a和b,那么它们卷积的结果是:\(y_n=\sum_{i=-\infty}^\infty a_ib_{n-i}\).如果a和b是有限序列,a最低的项 ...

  5. BZOJ3527 推出卷积公式FFT求值

    BZOJ3527 推出卷积公式FFT求值 传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3527 题意: \(F_{j}=\sum_{i&l ...

  6. HDU 1402 A * B Problem Plus (FFT求高精度乘法)

    A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. Gym - 101667H - Rock Paper Scissors FFT 求区间相同个数

    Gym - 101667H:https://vjudge.net/problem/Gym-101667H 参考:https://blog.csdn.net/weixin_37517391/articl ...

  8. MATLAB使用fft求取给定音频信号的频率

    一段10s立体声音频,采样率位8000Hz,已知频率为1000Hz clc; clear; [data, Fs] = audioread('1khz_stereo_8000.wav'); fs=Fs; ...

  9. 【FFT求卷积】Problem D. Duel

    [AC] #include <stdio.h> #include <iostream> #include <string.h> #include <algor ...

随机推荐

  1. 小爬爬1:开篇&&简单介绍启动

    1.第一阶段的内容 2.学习的方法? 思考,总结,重复 3.长大了意味着什么?家庭的责任,真的很重 4.数据分析&&数据清洗 numpy&&pandas&&am ...

  2. spoj Distinct Substrings 后缀数组

    给定一个字符串,求不相同的子串的个数. 假如给字符串“ABA";排列的子串可能: A B A AB  BA ABA 共3*(3+1)/2=6种; 后缀数组表示时: A ABA BA 对于A和 ...

  3. 通过 FastAdmin 理解开源软件

    通过 FastAdmin 理解开源软件 开源软件 ≠ 免费软件,免费是遵循其开源协议下的一个特性. 开源软件虽然免费,但服务是可以收费的,因为房子要钱. 开源的目的是为了用户更自由. 做开源每天会遇到 ...

  4. jq 操作CSS

    方式有两种,一种是操作元素className间接控制样式,一种是设置css属性值直接控制样式. jQuery 属性操作方法.jQuery CSS 操作函数 1.addClass() $(selecto ...

  5. oracle连接命令

    (1)conn[ect] 用法:conn  用户名/密码@网络服务器名  [as sysdba/sysoper] 当用特权用户身份连接时,必须带上as sysdba或是as sysoper 该命令常用 ...

  6. Java练习 SDUT-3849_分数四则运算

    分数四则运算 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 编写程序,实现两个分数的加减法 Input 输入包含多行数 ...

  7. 从入侵到变现——“黑洞”下的黑帽SEO分析

    概述 由于互联网入口流量主要被搜索引擎占据,网站在搜索引擎中的排名直接影响到市场营销效果,因此SEO服务应运而生.SEO(Search Engine Optimization)全称为搜索引擎优化,是指 ...

  8. 微博第三方登录时,域名使用错误报错, Laravel \ Socialite \ Two \ InvalidStateException No message

    使用微博第三方登录时,报错 Laravel \ Socialite \ Two \ InvalidStateException No message Laravel \Socialite \Two \ ...

  9. ElementUI分页Pagination自动到第一页

    当数据量过多时,使用分页请求数据. 设置分页的页数自动回到第一页. 例: <div class="pagination"> <el-pagination back ...

  10. poj 1263 Reflections (Simple Geometry)

    1263 -- Reflections 简单计算几何.题目给出射线以及若干个不相交的圆,求出射线会在哪些圆上反弹,依次写出反弹球的编号. 代码如下: #include <cstdio> # ...