无限手套 分治 + NTT + 生成函数

2023-10-27 14:30

本文主要是介绍无限手套 分治 + NTT + 生成函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

无限手套

在这里插入图片描述

solution

∏ k = 0 ∞ a i x i 2 + b i x i + 1 \prod\limits_{k=0}^{∞}a_ix_i^2+b_ix_i+1 k=0aixi2+bixi+1
考 虑 生 成 函 数 : 考虑生成函数:
= > ∑ k = 0 ∞ a i k 2 x k + b i x i x k + x k =>\sum\limits_{k=0}^{∞}a_ik^2x^k+b_ix_ix^k+x^k =>k=0aik2xk+bixixk+xk = ∑ k = 0 ∞ a i k 2 x k + ∑ k = 0 ∞ b i x i x k + ∑ k = 0 ∞ x k , ( 三 项 分 别 当 作 A , B , C ) =\sum\limits_{k=0}^{∞}a_ik^2x^k+\sum\limits_{k=0}^{∞}b_ix_ix^k+\sum\limits_{k=0}^{∞}x^k,(三项分别当作A,B,C) =k=0aik2xk+k=0bixixk+k=0xk,(A,B,C)
C = ∑ k = 0 ∞ x k = 1 1 − x C=\sum\limits_{k=0}^{∞}x^k=\frac{1}{1-x} C=k=0xk=1x1

B = ∑ k = 0 ∞ b i x i x k B=\sum\limits_{k=0}^{∞}b_ix_ix^k B=k=0bixixk
x B = ∑ k = 1 ∞ b i x i x k + 1 , 两 式 联 立 解 得 : xB=\sum\limits_{k=1}^{∞}b_ix_ix^{k+1},两式联立解得: xB=k=1bixixk+1
B = b i x ( 1 − x ) 2 B=\frac{b_ix}{(1-x)^2} B=(1x)2bix

A = ∑ k = 0 ∞ a i k 2 x k A=\sum\limits_{k=0}^{∞}a_ik^2x^k A=k=0aik2xk
C = ∑ k = 0 ∞ x k = 1 1 − x C=\sum\limits_{k=0}^{∞}x^k=\frac{1}{1-x} C=k=0xk=1x1
C ′ = ∑ k = 0 ∞ k x k − 1 = 1 ( 1 − x ) 2 C'=\sum\limits_{k=0}^{∞}kx^{k-1}=\frac{1}{(1-x)^2} C=k=0kxk1=(1x)21
x C ′ = ∑ k = 0 ∞ k x k = x ( 1 − x ) 2 xC'=\sum\limits_{k=0}^{∞}kx^{k}=\frac{x}{(1-x)^2} xC=k=0kxk=(1x)2x
( x C ′ ) ′ = ∑ k = 0 ∞ k 2 x k − 1 = ( x + 1 ) ( 1 − x ) 3 (xC')'=\sum\limits_{k=0}^{∞}k^2x^{k-1}=\frac{(x+1)}{(1-x)^3} (xC)=k=0k2xk1=(1x)3(x+1)
x ( x C ′ ) ′ = ∑ k = 0 ∞ k 2 x k = x ( x + 1 ) ( 1 − x ) 3 x(xC')'=\sum\limits_{k=0}^{∞}k^2x^{k}=\frac{x(x+1)}{(1-x)^3} x(xC)=k=0k2xk=(1x)3x(x+1)
A = a i ∗ x ( x C ′ ) ′ = a i x ( x + 1 ) ( 1 − x ) 3 A=a_i*x(xC')'=\frac{a_ix(x+1)}{(1-x)^3} A=aix(xC)=(1x)3aix(x+1)

因 此 , 因此,

∑ k = 0 ∞ a i k 2 x k + b i x i x k + x k \sum\limits_{k=0}^{∞}a_ik^2x^k+b_ix_ix^k+x^k k=0aik2xk+bixixk+xk = a i x ( x + 1 ) ( 1 − x ) 3 + b i x ( 1 − x ) 2 + 1 1 − x =\frac{a_ix(x+1)}{(1-x)^3}+\frac{b_ix}{(1-x)^2}+\frac{1}{1-x} =(1x)3aix(x+1)+(1x)2bix+1x1 = a i x ( x + 1 ) + ( 1 − x ) b i x + ( 1 − x ) 2 ( 1 − x ) 3 =\frac{a_ix(x+1)+(1-x)b_ix+(1-x)^2}{(1-x)^3} =(1x)3aix(x+1)+(1x)bix+(1x)2 = 1 + ( a i + b i − 2 ) x + ( a i − b i + 1 ) x 2 ( 1 − x ) 3 =\frac{1+(a_i+b_i-2)x+(a_i-b_i+1)x^2}{(1-x)^3} =(1x)31+(ai+bi2)x+(aibi+1)x2

A n s w e r = ∏ i = 1 m 1 + ( a i + b i − 2 ) x + ( a i − b i + 1 ) x 2 ( 1 − x ) 3 Answer=\prod\limits_{i=1}^{m} \frac{1+(a_i+b_i-2)x+(a_i-b_i+1)x^2}{(1-x)^3} Answer=i=1m(1x)31+(ai+bi2)x+(aibi+1)x2 = ∏ i = 1 m 1 + ( a i + b i − 2 ) x + ( a i − b i + 1 ) x 2 ( 1 − x ) 3 m = \frac{\prod\limits_{i=1}^{m}1+(a_i+b_i-2)x+(a_i-b_i+1)x^2}{(1-x)^{3m}} =(1x)3mi=1m1+(ai+bi2)x+(aibi+1)x2

1 ( 1 − x ) n = ∑ i = 0 ∞ C n + i − 1 i x i \frac1{(1-x)^{n}}=\sum\limits_{i=0}^{∞}C_{n+i-1}^{i}{x^{i}} (1x)n1=i=0Cn+i1ixi

A n s w e r = ∏ i = 1 m ( 1 + ( a i + b i − 2 ) x + ( a i − b i + 1 ) x 2 ) ∗ ∑ i = 0 ∞ C 3 m + i − 1 i x i Answer=\prod\limits_{i=1}^{m}(1+(a_i+b_i-2)x+(a_i-b_i+1)x^2)*\sum\limits_{i=0}^{∞}C_{3m+i-1}^{i}{x^{i}} Answer=i=1m(1+(ai+bi2)x+(aibi+1)x2)i=0C3m+i1ixi

由于这个式子的复杂度会达到 O ( m n l o g n < 2 e 8 ) , m < 1 e 3 , n < 1 e 4 , l o g n < 20 O(mnlogn<2e8),m<1e3,n<1e4,logn<20 O(mnlogn<2e8),m<1e3,n<1e4,logn<20,因此需要运用分治。

code

/*SiberianSquirrel*//*CuteKiloFish*/
#include <bits/stdc++.h>
using namespace std;
#define gcd(a,b) __gcd(a,b)
#define Polynomial vector<ll>
#define Inv(x) quick_pow(x, mod - 2)
#define DEBUG(x, y) cout << x << ": " << y << '\n';
using ld = long double;
using ll = long long;
using ull = unsigned long long;
const ll mod = 998244353, mod_g = 3, img = 86583718;
//const ll mod = 1004535809, mod_g = 3;
const int N = int(1e5 + 10);
template<typename T> inline T read() {T x = 0, f = 1;char ch = getchar();while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); }while(ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }return x * f;
}
template<typename T> inline T print(T x) {if(x < 0) { putchar('-'); x =- x; }if(x > 9) print(x / 10);putchar(x % 10 + '0');
}void print(Polynomial &a, int len){for(int i = 0; i < len; ++ i)cout << a[i] << ' ';cout << '\n';
}
void exgcd(int a,int b,int &x,int &y){if(!b) {x = 1; y = 0;return;}exgcd(b, a % b, y, x);y -= x * (a / b);
}
int quick_pow(int ans, int p, int res = 1) {for(; p; p >>= 1, ans = 1ll * ans * ans % mod)if(p & 1) res = 1ll * res * ans % mod;return res % mod;
}Polynomial R;
//二进制向上取整,为方便NTT变换准备。
inline int Binary_Rounding(const int &n) {int len = 1; while(len < n) len <<= 1;return len;
}
//预处理R数组,准备变换,在每次NTT之前理论都要调用此函数。
inline int Prepare_Transformation(int n){int L = 0, len;for(len = 1; len < n; len <<= 1) L++;R.clear(); R.resize(len);for(int i = 0; i < len; ++ i)R[i] = (R[i>>1]>>1)|((i&1)<<(L-1));return len;
}
void NTT(Polynomial &a, int f){int n = a.size();for(int i = 0; i < n; ++ i)if(i < R[i])swap(a[i], a[R[i]]);for(int i = 1; i < n; i <<= 1)for(int j = 0, gn = quick_pow(mod_g,(mod - 1) / (i<<1)); j < n; j += (i<<1))for(int k = 0, g = 1, x, y; k < i; ++ k, g = 1ll * g * gn % mod)x = a[j + k], y = 1ll * g * a[i + j + k] % mod,a[j + k] = (x + y) % mod, a[i + j + k] = (x - y + mod) % mod;if(f == -1){reverse(a.begin() + 1, a.end());int inv = Inv(n);for(int i = 0; i < n; ++ i) a[i] = 1ll * a[i] * inv % mod;}
}inline Polynomial operator +(const Polynomial &a, const int &b){int sizea = a.size(); Polynomial ret = a; ret.resize(sizea);for(int i = 0; i < sizea; ++ i)ret[i] = (1ll * a[i] + b + mod) % mod;return ret;
}
inline Polynomial operator -(const Polynomial &a, const int &b){int sizea = a.size(); Polynomial ret = a; ret.resize(sizea);for(int i = 0; i < sizea; ++ i)ret[i] = (1ll * a[i] - b + mod) % mod;return ret;
}
inline Polynomial operator *(const Polynomial &a, const int &b){int sizea = a.size(); Polynomial ret = a; ret.resize(sizea);for(int i = 0; i < sizea; ++ i) ret[i] = (1ll * a[i] * b % mod + mod) % mod;return ret;
}
inline Polynomial operator +(const Polynomial &a, const Polynomial &b){int sizea = a.size(), sizeb = b.size(), size = max(sizea, sizeb);Polynomial ret = a; ret.resize(size);for(int i = 0; i < sizeb; ++ i) ret[i] = (1ll * ret[i] + b[i]) % mod;return ret;
}
inline Polynomial operator -(const Polynomial &a, const Polynomial &b){int sizea = a.size(), sizeb = b.size(), size = max(sizea, sizeb);Polynomial ret = a; ret.resize(size);for(int i = 0; i < sizeb; ++ i) ret[i] = (1ll * ret[i] - b[i] + mod) % mod;return ret;
}
inline Polynomial Inverse(const Polynomial &a){Polynomial ret, inv_a;ret.resize(1);ret[0] = Inv(a[0]); int ed = a.size();for(int len = 2; len <= ed; len <<= 1){int n = Prepare_Transformation(len << 1);inv_a = a; inv_a.resize(n); ret.resize(n);for(int i = len; i < n; ++ i) inv_a[i] = 0;NTT(inv_a, 1); NTT(ret, 1);for(int i = 0; i < n; ++ i)ret[i] = 1ll * (2ll - 1ll * inv_a[i] * ret[i] % mod + mod) % mod * ret[i] % mod;NTT(ret, -1);for(int i = len; i < n; ++ i) ret[i] = 0;}ret.resize(ed);return ret;
}
inline Polynomial operator *(const Polynomial &a, const Polynomial &b){Polynomial lsa = a, lsb = b, ret;int n = lsa.size(), m = lsb.size();n = Prepare_Transformation(n + m);lsa.resize(n); lsb.resize(n); ret.resize(n);NTT(lsa,1); NTT(lsb,1);for(int i = 0; i < n; ++ i) ret[i] = 1ll * lsa[i] * lsb[i] % mod;NTT(ret,-1);return ret;
}
inline Polynomial operator /(const Polynomial &a, const Polynomial &b){Polynomial ret = a, ls = b;reverse(ret.begin(), ret.end());reverse(ls.begin(), ls.end());ls.resize(Binary_Rounding(a.size() + b.size()));ls = Inverse(ls);ls.resize(a.size() + b.size());ret = ret * ls; ret.resize(a.size() - b.size() + 1);reverse(ret.begin(), ret.end());return ret;
}
inline Polynomial operator %(const Polynomial &a, const Polynomial &b){Polynomial ret = a / b;ret = ret * b; ret.resize(a.size() + b.size());ret = a - ret; ret.resize(a.size() + b.size());return ret;
}
inline Polynomial Derivation(const Polynomial &a){int size = a.size(); Polynomial ret; ret.resize(size);for(int i = 1; i < size; ++ i) ret[i - 1] = 1ll * i * a[i] % mod;ret[size - 1] = 0;return ret;
}
inline Polynomial Integral(const Polynomial &a){int size = a.size(); Polynomial ret; ret.resize(size);for(int i = 1; i < size; ++ i) ret[i] = 1ll * Inv(i) * a[i - 1] % mod;ret[0] = 0;return ret;
}
inline Polynomial Composition_Inverse(const Polynomial &a){int n = a.size();Polynomial ret, Cinv = a, Pow;Cinv.resize(n); ret.resize(n); Pow.resize(n); Pow[0] = 1;for(int i = 0; i < n - 1; ++ i) Cinv[i] = Cinv[i + 1]; Cinv[n - 1] = 0;Cinv = Inverse(Cinv);for(int i = 1; i < n; ++ i){Pow = Pow * Cinv; Pow.resize(n);ret[i] = 1ll * Pow[i - 1] * Inv(i) % mod;}return ret;
}
inline Polynomial Logarithmic(const Polynomial &a){Polynomial ln_a = Derivation(a) * Inverse(a);ln_a.resize(a.size());return Integral(ln_a);
}
inline Polynomial Exponential(const Polynomial &a, int Constant = 1){Polynomial ret, D; int ed = a.size();ret.resize(1); ret[0] = Constant;for(int len = 2; len <= ed; len <<= 1){D = Logarithmic(ret); D.resize(len);D[0] = (1ll * a[0] + 1ll - D[0] + mod) % mod;for(int i = 1; i < len; ++i) D[i] = (1ll * a[i] - D[i] + mod) % mod;int n = Prepare_Transformation(len<<1);ret.resize(n); D.resize(n);NTT(ret, 1); NTT(D,1);for(int i = 0; i < n; ++ i) ret[i] = 1ll * ret[i] * D[i] % mod;NTT(ret, -1);for(int i = len; i < (len<<1); ++ i) ret[i] = D[i] = 0;}ret.resize(ed);return ret;
}ll fac[int(6e5 + 10)] = {1}, ifac[int(6e5 + 10)] = {1};
ll C(int n, int m) {if(m == 0 || n == m) return 1;if(n < m) return 0;return 1ll * fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}//Polynomial ans(3);
Polynomial a(1010), b(1010);Polynomial DAC_NTT(int l, int r) {if (l == r) {Polynomial res;ll A = 1;ll B = (a[l] + b[l] - 2) % mod; while(B < 0) B += mod;ll C = (a[l] - b[l] + 1) % mod; while(C < 0) C += mod;res.push_back(A); res.push_back(B); res.push_back(C);return res;}int mid = l + r >> 1;return DAC_NTT(l, mid) * DAC_NTT(mid + 1, r);
}inline void solve() {for(int i = 1; i <= int(6e5); ++ i) fac[i] = 1ll * fac[i - 1] * i % mod, ifac[i] = Inv(fac[i]);int m; cin >> m;Polynomial res(50010);for(int i = 0; i <= 50000; ++ i) {res[i] = C(3 * m + i - 1, i);}
//     Polynomial ans;for(int i = 1; i <= m; ++ i) {cin >> a[i] >> b[i];
//         ll A = 1;
//         ll B = (a[i] + b[i] - 2) % mod; while(B < 0) B += mod;
//         ll C = (a[i] - b[i] + 1) % mod; while(C < 0) C += mod;
//         if(i == 1) {
//             ans.push_back(A), ans.push_back(B), ans.push_back(C);
//         } else {
//             Polynomial temp;
//             temp.push_back(A), temp.push_back(B), temp.push_back(C);
//             ans = ans * temp;
//         }}Polynomial ans = DAC_NTT(1, m);ans = ans * res;int q; cin >> q; while(q --) {int n; cin >> n;cout << ans[n] << '\n';}
}signed main() {ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#ifdef ACM_LOCALfreopen("input", "r", stdin);
//    freopen("output", "w", stdout);signed test_index_for_debug = 1;char acm_local_for_debug = 0;do {if (acm_local_for_debug == '$') exit(0);if (test_index_for_debug > 20)throw runtime_error("Check the stdin!!!");auto start_clock_for_debug = clock();solve();auto end_clock_for_debug = clock();cout << "Test " << test_index_for_debug << " successful" << endl;cerr << "Test " << test_index_for_debug++ << " Run Time: "<< double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;cout << "--------------------------------------------------" << endl;} while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#elsesolve();
#endifreturn 0;
}

这篇关于无限手套 分治 + NTT + 生成函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/286255

相关文章

MybatisGenerator文件生成不出对应文件的问题

《MybatisGenerator文件生成不出对应文件的问题》本文介绍了使用MybatisGenerator生成文件时遇到的问题及解决方法,主要步骤包括检查目标表是否存在、是否能连接到数据库、配置生成... 目录MyBATisGenerator 文件生成不出对应文件先在项目结构里引入“targetProje

Python使用qrcode库实现生成二维码的操作指南

《Python使用qrcode库实现生成二维码的操作指南》二维码是一种广泛使用的二维条码,因其高效的数据存储能力和易于扫描的特点,广泛应用于支付、身份验证、营销推广等领域,Pythonqrcode库是... 目录一、安装 python qrcode 库二、基本使用方法1. 生成简单二维码2. 生成带 Log

Python使用Pandas库将Excel数据叠加生成新DataFrame的操作指南

《Python使用Pandas库将Excel数据叠加生成新DataFrame的操作指南》在日常数据处理工作中,我们经常需要将不同Excel文档中的数据整合到一个新的DataFrame中,以便进行进一步... 目录一、准备工作二、读取Excel文件三、数据叠加四、处理重复数据(可选)五、保存新DataFram

SpringBoot生成和操作PDF的代码详解

《SpringBoot生成和操作PDF的代码详解》本文主要介绍了在SpringBoot项目下,通过代码和操作步骤,详细的介绍了如何操作PDF,希望可以帮助到准备通过JAVA操作PDF的你,项目框架用的... 目录本文简介PDF文件简介代码实现PDF操作基于PDF模板生成,并下载完全基于代码生成,并保存合并P

Oracle的to_date()函数详解

《Oracle的to_date()函数详解》Oracle的to_date()函数用于日期格式转换,需要注意Oracle中不区分大小写的MM和mm格式代码,应使用mi代替分钟,此外,Oracle还支持毫... 目录oracle的to_date()函数一.在使用Oracle的to_date函数来做日期转换二.日

详解Java中如何使用JFreeChart生成甘特图

《详解Java中如何使用JFreeChart生成甘特图》甘特图是一种流行的项目管理工具,用于显示项目的进度和任务分配,在Java开发中,JFreeChart是一个强大的开源图表库,能够生成各种类型的图... 目录引言一、JFreeChart简介二、准备工作三、创建甘特图1. 定义数据集2. 创建甘特图3.

C++11的函数包装器std::function使用示例

《C++11的函数包装器std::function使用示例》C++11引入的std::function是最常用的函数包装器,它可以存储任何可调用对象并提供统一的调用接口,以下是关于函数包装器的详细讲解... 目录一、std::function 的基本用法1. 基本语法二、如何使用 std::function

AI一键生成 PPT

AI一键生成 PPT 操作步骤 作为一名打工人,是不是经常需要制作各种PPT来分享我的生活和想法。但是,你们知道,有时候灵感来了,时间却不够用了!😩直到我发现了Kimi AI——一个能够自动生成PPT的神奇助手!🌟 什么是Kimi? 一款月之暗面科技有限公司开发的AI办公工具,帮助用户快速生成高质量的演示文稿。 无论你是职场人士、学生还是教师,Kimi都能够为你的办公文

hdu1171(母函数或多重背包)

题意:把物品分成两份,使得价值最接近 可以用背包,或者是母函数来解,母函数(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v) 其中指数为价值,每一项的数目为(该物品数+1)个 代码如下: #include<iostream>#include<algorithm>

pdfmake生成pdf的使用

实际项目中有时会有根据填写的表单数据或者其他格式的数据,将数据自动填充到pdf文件中根据固定模板生成pdf文件的需求 文章目录 利用pdfmake生成pdf文件1.下载安装pdfmake第三方包2.封装生成pdf文件的共用配置3.生成pdf文件的文件模板内容4.调用方法生成pdf 利用pdfmake生成pdf文件 1.下载安装pdfmake第三方包 npm i pdfma