首页 > 其他分享 >我的 poly 模板

我的 poly 模板

时间:2023-03-07 16:13:00浏览次数:25  
标签:int NTT poly len b1 clr 模板 define

#include <bits/stdc++.h>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef double db;
#define ep emplace_back
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define fout freopen("out.out","w",stdout);
#define fin freopen("in.in","r",stdin);
#define DE {cerr << "QAQ" << endl;}
#define dd(x) {cerr << (x) << endl}
inline int read() {
	int x=0, v=1,ch=getchar();
	while('0'>ch||ch>'9') {
		if(ch=='-') v=0;
		ch=getchar();
	}while('0'<=ch&&ch<='9') {
		x=(x*10)+(ch^'0');
		ch=getchar();
	}return v?x:-x;
}
const int MAX = 1e6 + 5, P = 998244353, G = 3, iG = 332748118;
int qpow(int x,int p){
	int r=1;
	for(;p;p>>=1,x=1ll*x*x%P)if(p&1)r=1ll*r*x%P;
	return r;
}
namespace poly {
	// N ?a 4 ±??¥é??? 
	#define N (524300)
	#define clr(f, n) memset(f, 0, sizeof(int) * n)
	#define cpy(f, g, n) memcpy(f, g, sizeof(int) * n)
	int tr[N], now = 0;
	void tpre(int n) {
		if(n == now) return ; now = n;
		for(int i = 0; i < n; ++ i) tr[i] = (tr[i >> 1] >> 1) | ((i & 1) ? (n >> 1) : 0);
	}
	int inc(int a, int b) { a += b; return (a >= P) ? a - P : a; }
	void NTT(int *f, int n, int op) {
		tpre(n);
		for(int i = 0; i < n; ++ i) if(i < tr[i]) swap(f[i], f[tr[i]]);
		static int w[N] = {1}; 
		for(int l = 1; l < n; l <<= 1) {
			int wG = qpow(op ? G : iG, (P - 1) / (l << 1));
			for(int i = 1; i < l; ++ i) w[i] = 1ll * w[i - 1] * wG % P;
			for(int i = 0; i < n; i += (l << 1)) {
				for(int j = 0; j < l; ++ j) {
					int a = f[i | j], b = 1ll * f[i | j | l] * w[j] % P;
					f[i | j] = inc(a, b);
					f[i | j | l] = inc(a, P - b);
				}
			}
		}
		if(op == 0) {
			int invn = qpow(n, P - 2);
			for(int i = 0; i < n; ++ i) f[i] = 1ll * f[i] * invn % P;
		}
	}
	void px(int *f, int *g, int n) {
		for(int i = 0; i < n; ++ i) f[i] = 1ll * f[i] * g[i] % P; 
	}
	void times(int *f, int *g, int len, int lim) {
		static int tmp[N];
		int n = 1; while(n < len + len) n <<= 1;
		clr(tmp, n), cpy(tmp, g, n);
		NTT(f, n, 1), NTT(tmp, n, 1);
		px(f, tmp, n), NTT(f, n, 0);
		clr(f + lim, n - lim), clr(tmp, n); 
	}
	void BF(int *f, int *g, int n) {
		static int h[N];
		memset(h, 0, sizeof(h));
		for(int i = 0; i < n; ++ i) 
			for(int j = 0; i + j < n; ++ j) 
				h[i + j] = inc(h[i + j], 1ll * f[i] * g[j] % P);
		for(int i = 0; i < n; ++ i) f[i] = h[i]; 
	}
	void myinv(int *f, int m) {
		int n = 1; for(n = 1; n < m; n <<= 1);
		static int w[N], r[N], sav[N];
		w[0] = qpow(f[0], P - 2);
		for(int len = 2; len <= n; len <<= 1) {
			for(int i = 0; i < (len >> 1); ++ i) r[i] = w[i];
			cpy(sav, f, len), NTT(sav, len, 1);
			NTT(r, len, 1), px(r, sav, len);
			NTT(r, len, 0), clr(r, (len >> 1));
			cpy(sav, w, len), NTT(sav, len, 1);
			NTT(r, len, 1), px(r, sav, len);
			NTT(r, len, 0);
			for(int i = len >> 1; i < len; ++ i) w[i] = inc(2ll * w[i] % P, P - r[i]) % P;
		} 
		cpy(f, w, m), clr(sav, n), clr(w, n), clr(r, n);
	}
	void mysqrt(int *f, int m) {
		int n = 1; while(n < m) n <<= 1;
		static int b1[N], b2[N]; b1[0] = 1;
		for(int len = 2; len <= n; len <<= 1) {
			for(int i = 0; i < (len >> 1); ++ i) b2[i] = inc(b1[i], b1[i]);
			myinv(b2, len);
			NTT(b1, len, 1), px(b1, b1, len), NTT(b1, len, 0);
			for(int i = 0; i < len; ++ i) b1[i] = inc(f[i], b1[i]);
			times(b1, b2, len, len); 
		} cpy(f, b1, m), clr(b1, n + n), clr(b2, n + n); 
	}
	
	int inv[N];
	void init(int lim) {
		inv[1] = 1;
		for(int i = 2; i <= lim; ++ i) {
			inv[i] = 1ll * inv[P % i] * (P - P / i) % P; 
		}
	}
	void dao(int *f ,int m) {
		for(int i=1;i<m;++i) {
			f[i-1] = 1ll * f[i] * i % P; 
		} f[m - 1] = 0;
	}
	void jf(int *f,int m) {
		for(int i=m;i;--i) 
			f[i] = 1ll * f[i-1] * inv[i] % P;
		f[0] = 0;
	}
	void myln(int *f, int m) {
		static int g[N];
		cpy(g, f, m);
		myinv(g, m), dao(f, m);
		times(f, g, m, m), jf(f, m - 1);
		clr(g, m);
	}
	void myexp(int *f,int m) {
		static int s[N], s2[N];
		int n = 1; while(n < m) n <<= 1;
		s2[0] = 1;
		for(int len = 2; len <= n; len <<= 1) {
			cpy(s, s2, len >> 1), myln(s, len);
			for(int i = 0; i < len; ++ i) s[i] = inc(f[i], P - s[i]);
			s[0] = inc(s[0], 1);
			times(s2, s, len, len);
		}  cpy(f, s2, m), clr(s, n), clr(s2, n);
	}
	void myqpow(int *f, int m, int k) {
		myln(f, m);
		for(int i = 0; i < m; ++ i) f[i] = 1ll * f[i] * k % P;
		myexp(f, m);
	}
} 
using namespace poly;
int n;
signed main() {
	n = read();
	
	return 0;
}

标签:int,NTT,poly,len,b1,clr,模板,define
From: https://www.cnblogs.com/Lates/p/17188407.html

相关文章

  • 欧拉函数模板
    //求n的欧拉函数intcalPhi(intn){intret=n;intbd=std::sqrt(n);for(inti=2;i<=n/i;++i){if(n%i==0){......
  • 织梦显示模板的PHP代码
    require_once("../include/common.inc.php");require(dirname(__FILE__)."/config.php");require_onceDEDEINC."/arc.partview.class.php";$pv=newPartView();......
  • Python Flask 之 路由和渲染模板讲解与示例演示
    目录一、概述二、路由三、渲染模板四、重定向和错误五、日志六、集成WSGI中间件一、概述Flask是一款使用Python编写的Web应用框架,其设计理念是轻量级和简单易学。......
  • c++ 模板的简单使用
    c++函数模板的两种用法,第二种是可变参数个数的使用方法,其中sizeof...()函数可以获取输入可变参数的数量#include<iostream>template<typenameT>TAddMyNum(const......
  • 树链剖分模板(为什么我的这么长???)
    #include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>#definelidid<<1#defineridid<<1|1usingnamespacestd;l......
  • 使用注解开发SpringMVC,也是以后开发的模板(重点)
    注解版配置SpringMVC(重点)第一步:新建一个moudel,添加web支持!建立包结构top.lostyou.controller第二步:由于maven可能存在资源过滤问题,我们将配置完善<!--在build中......
  • PAT 甲级 1009 Product of Polynomials
    Thistime,youaresupposedtofind A×B where A and B aretwopolynomials.InputSpecification:Eachinputfilecontainsonetestcase.Eachcaseoccupi......
  • 浅谈二分标准模板以及边界变形
    1.5、搜索算法1.5.1、折半搜索(二分)二分的基础的用法是在单调序列或单调函数中进行查找。因此当问题的答案具有单调性时,就可以通过二分把求解转化为判定。1.5.1.1、标准......
  • 整数二分模板
    文章目录​​Question​​​​Ideas​​​​Code​​​​C++​​​​Python​​Question给定一个按照升序排列的长度为n的整数数组,以及q个查询。对于每个查询,返回一个元......
  • vscode快捷生成vue模板
    1,文件-->首选项-->用户代码片段-->点击新建代码片段--取名vue.json确定2,粘贴模板:{"Printtoconsole":{"prefix":"vue","body":[......