跳到正文

算法·理论

乱谈数学

kevinZ99约 8 分钟
原文
文章信息与版本

文章信息

收录
2026年3月18日
更新
2026年3月18日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1

生成式函数

引入

求 fibnfib_{n}。

你看到这个多次出现的问题并感到不屑一顾。

你熟练的拿出矩阵快速幂点击提交,一片绿色出现在你的眼前。但是你已经厌倦了这种千篇一律的方式,你开始思考:“有没有别的方式呢?”。

“有点兄弟,有的。”

我们考虑令一个多项式:

f(x)=∑iaixif(x)=\sum_{i}a_{i}x^{i}

同时令其 ai=fibia_{i}=fib_{i} 也就是 aia_{i} 就是答案。

你将这个式子的前几项写了出来。

f(x)=0+x+x2+2x3+3x4+5x5f(x)=0+x+x^{2}+2x_{3}+3x^{4}+5x^{5}

你试图解出它。

你想起了,求等比数列的时候使用的隔项相消的原理与这个问题很相似。

于是你将这个多项乘以了 xx 和 x2x^{2}。

f(x)=0+x+x2+2x3+3x4+5x5f(x)=0+x+x^{2}+2x^{3}+3x^{4}+5x^{5} xf(x)=0+0+x2+x3+2x4+3x5x f(x)=0+0+x^{2}+x^{3}+2x^{4}+3x^{5} x2f(x)=0+0+0+x3+x4+2x5x^2 f(x)=0+0+0+x^3+x^4+2x^{5}

(这里对齐是为了好看。)

你惊喜的发现。

(1−x−x2)f(x)=x(1-x-x^2)f(x)=x

于是你自然的解出了。

f(x)=x/(1−x−x2)f(x)=x/(1-x-x^2)

于是 fibn=f(x)[xn]fib_{n}=f(x)[x^n] 你欣喜若狂。于是你自己发明了生成式函数。

基础

全 11 序列的生成式函数是 f(x)=11−xf(x)=\frac{1}{1-x}。

斐波那契数列的生成式函数是 f(x)=x1−x−x2f(x)=\frac{x}{1-x-x^{2}}。

还有一些可以在题目里面积累。

一些裸题

P2000

闲话

我说实话这题真没意思,评紫的唯一证据是 FFTFFT。

solution

你看到了题目,看到了一堆条件,你思考着,你发现多项式乘法好像刚好是这一题的形式。

也就是。

ans=∑∑i=110ai=n∏i=110fi[xai]ans=\sum_{\sum_{i=1}^{10}a_{i}=n}\prod_{i=1}^{10}f_{i}[x^{a_{i}}]

这不就是多项式乘法的定义吗!?!?(如果你不理解可以先从两个函数相乘开始理解)

于是你熟练地将所有条件单独的多项式都写了出来。

f1(x)=11−x6f_{1}(x)=\frac{1}{1-x^6} f2(x)=x10−1x−1f_{2}(x)=\frac{x^{10}-1}{x-1} f3(x)=x6−1x−1f_{3}(x)=\frac{x^6-1}{x-1} f4(x)=11−x4f_{4}(x)=\frac{1}{1-x^4} f5(x)=x8−1x−1f_{5}(x)=\frac{x^8-1}{x-1} f6(x)=11−x2f_{6}(x)=\frac{1}{1-x^2} f7(x)=x2−1x−1f_{7}(x)=\frac{x^2-1}{x-1} f8(x)=11−x8f_{8}(x)=\frac{1}{1-x^8} f9(x)=11−x10f_{9}(x)=\frac{1}{1-x^{10}} f10(x)=x4−1x−1f_{10}(x)=\frac{x^4-1}{x-1}

然后发现全部可以互相消掉。

你发现

ans(x)=1(1−x)5ans(x)=\frac{1}{(1-x)^5}

你发现一个全部为 11 的序列的生成式函数就是

f(x)=11−xf(x)=\frac{1}{1-x}

所以上面这个就是 55 个全部为 11 的序列成了起来。

也就是有 55 种物品,随便拿,共拿 nn 个的方式。

你不屑的笑到,这不就是隔板法吗?

于是答案就是 (n+44)\binom {n+4}{4} 你使用 pypy3 写出了如下代码。并成功 AC 了此题。

Python
import sys
sys.set_int_max_str_digits(10000000)
n=int(input())
print((n+1)*(n+2)*(n+3)*(n+4)//24)
后话

这一题 python3 交会 TLE 请输入文本。

P10780

你看到题目,非常不屑。你花了 ϵ\epsilon 就想出了答案 (n+23)\binom{n+2}{3} 然后你花了 10−114514191981010^{-1145141919810} 秒写出了一下 c++ 代码。并成功 AC 了此题。

C++
//Author:Kevin Z K Y
#include <bits/stdc++.h>
#define up(a,b,c) for(int (a)=(b);(a)<=(c);(a)++)
#define dn(a,b,c) for(int (a)=(b);(a)>=(c);(a)--)
#define fst first
#define sed second
#define Max(x,y) (((x)>(y))?(x):(y))
#define Min(x,y) (((x)<(y))?(x):(y))
#define Abs(x) (((x)<0)?(-(x)):(x))
using namespace std;
using us = unsigned short ;using ldb = long double ;
using ull = unsigned long long ;using ui = unsigned int ;
using ll = long long ;using hint = __int128 ;
using pii = pair<int,int> ;using pll = pair<ll,ll> ;
using pil = pair<int,ll> ;using vpil = vector<pil> ;
using pli = pair<ll,int> ;using vpli = vector<pli> ;
using vi = vector<int> ;using vl = vector<ll> ;
using vpi = vector<pii> ;using vpl = vector<pll> ;
using db = double ;namespace mystl{
	#define gc() getchar()
	inline ll qpow(ll a , ll b , ll p) { if (a==0ll) return 0ll; ll c=1ll;
		   	  while(b) { if(b & 1) c=a*c%p; a=a*a%p; b>>=1; } return c ; }
	void exgcd ( ll a , ll b , ll & cx , ll & cy){if(a % b==0)cx = 0,cy=1;
		   		else { exgcd( b , a % b , cy , cx) ; cy -= a / b * cx ; }}
	inline ll lcm ( ll x , ll y ){return x / std :: __gcd( x , y ) * y ; }
	template<typename T>inline void read(T&x) {x=0; bool f=false; char ch;
				ch = gc(); while(ch<'0'||ch>'9') f |=( ch=='-') , ch=gc();
				while(ch>='0'&&ch<='9') x=x*10+ch-'0', ch=gc(); x=f?-x:x;}
	template<typename T>inline void write(T x){char s[40];short d=0;T y=x;
				if(x<0) putchar('-'),y=-y;if(x==0){ putchar('0'); return;}
				while(y){s[++d]=y%10+48;y/=10;}while(d>0)putchar(s[d--]);}
	template<typename T>inline void wris(T x,char c){write(x);putchar(c);}
}using namespace mystl;
namespace my{
	const int N=(int)(0),P=(int)(10007);
	const int inf=(int)(1e9);
	inline void madd(int&x,int y){x=(x+y>=P)?(x+y-P):(x+y);}
	inline int fmadd(int x,int y){return (x+y>=P)?(x+y-P):(x+y);}
	inline void msub(int&x,int y){x=(x<y)?(x-y+P):(x-y);}
	inline int fmsub(int x,int y){return (x<y)?(x-y+P):(x-y);}
	inline void mmul(int&x,int y){x=1ll*x*y%P;}
	inline int fmmul(int x,int y){return 1ll*x*y%P;}
	inline void SOLVE(){
		char c=gc();
		int n=0;
		while(c<'0'||c>'9')c=gc();
		while(c>='0'&&c<='9')n=(n*10+c-'0')%P,c=gc();
		cout<<(1ll*(n)*(n+1)*(n+2)/6)%P<<'\n';
	}
}
int main(){
//  freopen("","r",stdin);
//  freopen("","w",stdout);
	ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
	int _=1;while(_--)my::SOLVE();return 0;
}
/*

*/
原文