POJ-3734(指数型生成函数)

题意:POJ3734

一段长度为 n n n的序列,你有红黄蓝绿四种颜色的砖块,一块砖长度为 1 1 1,问你铺砖的方案数,其中红黄颜色之和必须为偶数。

分析:

指数型生成函数。
F b l u e = F g r e e n = 1 + x 1 ! + x 2 2 ! + . . . + x n n ! = e x F_{blue}=F_{green}=1+\frac{x}{1!}+\frac{x^2}{2!}+...+\frac{x^n}{n!}=e^x Fblue=Fgreen=1+1!x+2!x2+...+n!xn=ex
F r e d = F y e l l o w = 1 + x 2 2 ! + x 4 4 ! + . . . + x n n ! = e x + e − x 2 F_{red}=F_{yellow}=1+\frac{x^2}{2!}+\frac{x^4}{4!}+...+\frac{x^n}{n!}=\frac{e^x+e^{-x}}{2} Fred=Fyellow=1+2!x2+4!x4+...+n!xn=2ex+ex
所以
G = F b l u e ∗ F g r e e n ∗ F b l u e ∗ F g r e e n = e 4 x + 2 e 2 x + 1 4 G=F_{blue}*F_{green}*F_{blue}*F_{green}=\frac{e^{4x}+2e^{2x}+1}{4} G=FblueFgreenFblueFgreen=4e4x+2e2x+1
因为
e k x = ∑ i = 0 ∞ k i x i i ! e^{kx}=\sum_{i=0}^{\infty}\frac{k^ix^i}{i!} ekx=i=0i!kixi
所以
G = 1 4 + ∑ i = 0 ∞ ( 4 i + 2 i + 1 ) ∗ x i i ! 4 G=\frac{1}{4}+\frac{\sum_{i=0}^{\infty}(4^i+2^{i+1})*\frac{x^i}{i!}}{4} G=41+4i=0(4i+2i+1)i!xi
1 4 \frac{1}{4} 41是常数项,在此可以忽略,又因为求 n n n的排列数所以 G ∗ n ! G*n! Gn!,所以答案就是 4 i + 2 i + 1 4 = 4 i − 1 + 2 i − 1 \frac{4^i+2^{i+1}}{4}=4^{i-1}+2^{i-1} 44i+2i+1=4i1+2i1
a n s = 4 n − 1 + 2 n − 1 ans=4^{n-1}+2^{n-1} ans=4n1+2n1

#include 
#include 
#include  
#include 
#include 
#include 
#include 
#include 
#include  
using namespace std;

#define fi first
#define se second

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int maxn  = 1000 + 5;
const int maxm  = 100 + 5;
const int inf   = 0x3f3f3f3f;
const LL  mod   = 10000 + 7;//19260817
const double pi = acos(-1.0);

LL n, res;

LL fpow(LL a, LL n){
	LL ans = 1;
	while(n){
		if(n & 1) ans = ans * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return ans;
}

int main(){
	int T;
	scanf("%d", &T);
	while(T--){
		scanf("%lld", &n);
		printf("%lld\n", (1LL * fpow(4, n - 1) % mod + fpow(2, n - 1) % mod) % mod);
	}
    return 0;
}

你可能感兴趣的:(生成函数)