无限手套 分治 + 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

相关文章

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注

Python如何自动生成环境依赖包requirements

《Python如何自动生成环境依赖包requirements》:本文主要介绍Python如何自动生成环境依赖包requirements问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录生成当前 python 环境 安装的所有依赖包1、命令2、常见问题只生成当前 项目 的所有依赖包1、

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

MySQL高级查询之JOIN、子查询、窗口函数实际案例

《MySQL高级查询之JOIN、子查询、窗口函数实际案例》:本文主要介绍MySQL高级查询之JOIN、子查询、窗口函数实际案例的相关资料,JOIN用于多表关联查询,子查询用于数据筛选和过滤,窗口函... 目录前言1. JOIN(连接查询)1.1 内连接(INNER JOIN)1.2 左连接(LEFT JOI

MySQL中动态生成SQL语句去掉所有字段的空格的操作方法

《MySQL中动态生成SQL语句去掉所有字段的空格的操作方法》在数据库管理过程中,我们常常会遇到需要对表中字段进行清洗和整理的情况,本文将详细介绍如何在MySQL中动态生成SQL语句来去掉所有字段的空... 目录在mysql中动态生成SQL语句去掉所有字段的空格准备工作原理分析动态生成SQL语句在MySQL

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

C语言函数递归实际应用举例详解

《C语言函数递归实际应用举例详解》程序调用自身的编程技巧称为递归,递归做为一种算法在程序设计语言中广泛应用,:本文主要介绍C语言函数递归实际应用举例的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录前言一、递归的概念与思想二、递归的限制条件 三、递归的实际应用举例(一)求 n 的阶乘(二)顺序打印