P4238 【模板】多项式乘法逆
#include <cstdio>
#include <iostream>
#define re register
using namespace std;
typedef long long LL;
const int N = 3e5 + 5, P = 998244353, g = 3;
int rev[N], n;
LL a[N], b[N], c[N];
inline int fpow(LL x, int y)
{
LL res = 1;
for(; y; y >>= 1, x = x * x % P) if (y & 1) res = res * x % P;
return res;
}
inline void NTT(LL *a, int len, int inv)
{
if (len == 1) return;
for(re int i = 1; i < len; i++) if (i < rev[i]) swap(a[i], a[rev[i]]);
for(re int mid = 1, I; mid < len; mid <<= 1)
{
I = fpow(g, (P - 1) / (mid << 1));
if (inv == -1) I = fpow(I, P - 2);
for(re int i = 0, W; i < len; i += mid << 1)
{
W = 1;
for(re int j = 0, x, y; j < mid; j++, W = 1LL * W * I % P)
x = a[i + j], y = a[i + j + mid] * W % P,
a[i + j] = (x + y) % P, a[i + j + mid] = (x - y + P) % P;
}
}
}
void solve(int n, LL *a, LL *b)
{
if (n == 1) return void(b[0] = fpow(a[0], P - 2));
solve((n + 1) >> 1, a, b);
int len = 1, bit = 0;
while (len < (n << 1)) len <<= 1, bit++;
for(re int i = 0; i < len; i++) rev[i] = ((rev[i >> 1] >> 1) | (i & 1) << bit - 1);
for(re int i = 0; i < n; i++) c[i] = a[i];
for(re int i = n; i < len; i++) c[i] = 0;
NTT(c, len, 1), NTT(b, len, 1);
for(re int i = 0; i < len; i++) b[i] = b[i] * (2LL - b[i] * c[i] % P + P) % P;
NTT(b, len, -1);
int inv = fpow(len, P - 2);
for(re int i = 0; i < n; i++) b[i] = b[i] * inv % P;
for(re int i = n; i < len; i++) b[i] = 0;
}
int main()
{
scanf("%d", &n);
for(re int i = 0; i < n; i++) scanf("%lld", &a[i]);
solve(n, a, b);
for(re int i = 0; i < n; i++) printf("%lld ", b[i]);
}
P4238 【模板】多项式乘法逆的更多相关文章
- 洛谷 P4238 [模板] 多项式求逆
题目:https://www.luogu.org/problemnew/show/P4238 看博客:https://www.cnblogs.com/xiefengze1/p/9107752.html ...
- [模板] 多项式: 乘法/求逆/分治fft/微积分/ln/exp/幂
多项式 代码 const int nsz=(int)4e5+50; const ll nmod=998244353,g=3,ginv=332748118ll; //basic math ll qp(l ...
- 洛谷.3803.[模板]多项式乘法(FFT)
题目链接:洛谷.LOJ. FFT相关:快速傅里叶变换(FFT)详解.FFT总结.从多项式乘法到快速傅里叶变换. 5.4 又看了一遍,这个也不错. 2019.3.7 叕看了一遍,推荐这个. #inclu ...
- 多项式求逆元详解+模板 【洛谷P4238】多项式求逆
概述 多项式求逆元是一个非常重要的知识点,许多多项式操作都需要用到该算法,包括多项式取模,除法,开跟,求ln,求exp,快速幂.用快速傅里叶变换和倍增法可以在$O(n log n)$的时间复杂度下求出 ...
- 洛谷.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\ ...
- 洛谷.3803.[模板]多项式乘法(NTT)
题目链接:洛谷.LOJ. 为什么和那些差那么多啊.. 在这里记一下原根 Definition 阶 若\(a,p\)互质,且\(p>1\),我们称使\(a^n\equiv 1\ (mod\ p)\ ...
- P3803 [模板] 多项式乘法 (FFT)
Rt 注意len要为2的幂 #include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); inli ...
- 2018.12.30 洛谷P4238 【模板】多项式求逆
传送门 多项式求逆模板题. 简单讲讲? 多项式求逆 定义: 对于一个多项式A(x)A(x)A(x),如果存在一个多项式B(x)B(x)B(x),满足B(x)B(x)B(x)的次数小于等于A(x)A(x ...
- [模板][P4238]多项式求逆
NTT多项式求逆模板,详见代码 #include <map> #include <set> #include <stack> #include <cmath& ...
- 洛谷P4238【模板】多项式求逆
洛谷P4238 多项式求逆:http://blog.miskcoo.com/2015/05/polynomial-inverse 注意:直接在点值表达下做$B(x) \equiv 2B'(x) - A ...
随机推荐
- npm 依赖下载报错:主机名/IP与证书的altname不匹配
npm 依赖下载报错:主机名/IP与证书的altname不匹配: //取消ssl验证 npm set strict-ssl false npm config set registry http://r ...
- Ubuntu20.04创建快捷方式(CLion)
打开命令行,创建在桌面上xxx.desktop文件 touch ~/Desktop/Clion.desktop 编辑desktop文件 [Desktop Entry] Encoding=UTF-8 N ...
- 4.1:简单python爬虫
简单python爬虫 在创建的python文件中输入下列代码: # coding:utf-8 import requests from bs4 import BeautifulSoup def spi ...
- JAVA里Map的一些常用方法
Map的常用方法 案例1 场景:一张建行用户体验金信息大表(百万级别),里面存在一个字段对多条数据,需要统计某个字段的多条数据累加值以供于别的服务调用. 优化前解决:直接查出来一个大list给到另一个 ...
- Mysql安装失败-GPG验证不通过或Failed to start mariadb.service: Unit not fou
1.报错原因 报错原文 Key imported successfully Import of key(s) didn't help, wrong key(s)? Public key for mys ...
- SQL语句查询关键字:where筛选、group by分组、distinc去重、order by排序、limit分页、操作表的SQL语句布补充
目录 SQL语句查询关键字 前期数据准备 编写SQL语句的小技巧 查询关键字之where筛选 查询关键字之group by分组 查询关键字之having过滤 查询关键字值distinct去重 查询关键 ...
- 使用Springboot+redis+Vue实现秒杀的一个Demo
目录 1.Redis简介 2.实现代码 3.启动步骤 4.使用ab进行并发测试 5.线程安全 6.总结 7.参考资料 1.Redis简介 Redis是一个开源的key-value存储系统. Redis ...
- 2021强网杯青少赛(qwtac)楼上大佬ddw WriteUp
楼上大佬ddw战队WRITEUP 战队信息 战队名称:楼上大佬ddw 战队排名:24 解题情况 解题过程 01 签到 操作内容: 下载附件,打开运行拿到flag 如该题使用自己编写的脚本代码请详细写出 ...
- [图像处理] YUV图像处理入门5
12 yuv420转换为rgb(opencv mat) yuv格式具有亮度信息和色彩信息分离的特点,但大多数图像处理操作都是基于RGB格式,而且自己造轮子工作量太大.因此通常都会将yuv转换为rgb, ...
- Linux利用crontab执行定时任务
Linux利用crontab执行定时任务 crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认 ...