指数型母函数的应用
求A B C D 在规定条件下n个元素的排列个数,先写出指数型母函数
G(X) = ( 1 + x + x^2/2! + x^3/3! +... )^2 * ( 1+ x^2/2! + x^4/! + .. )^2
前者表示:B, D出现方式不限制;后者表示:A, C 只能出现偶数或者不出现情况
又知: e^x=1+x/1!+x^2/2!+x^3/3!+...
e^(-x)=1-x/1!+x^2/2!-x^3/3!+...
化简得: G(x) = e^(2x) * ((e^x+e^(-x))/2)^2 = (1/4) * e^(2x) * (e^(2x) + 2 + e^(-2x))
= (1/4) * (e^(4x) + 2*e^(2x) +1)
= (1/4) * ( (1+4x/1!+(4x)^2/2!+(4x)^3/3!+...+(4x)^n/n!) + 2*(1+2x/1!+(2x)^2/2!+(2x)^3/3!+...+(2x)^n/n!) +1)
得: x^n 项系数
a(n) = (1/4) * ((4x)^n/n! + 2*(2x)^n/n!) = (1/4) * ( 4^n*x^n/n! + 2^(n+1)*x^n/n!) = (4^(n-1) + 2^(n-1)) * x^n/n!
即所求 F(n) = (4^(n-1) + 2^(n-1)) % 100
代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#include<stdlib.h>
#define ll __int64
using namespace std;
ll pows(ll a, ll b){
ll ans = 1;
while (b){
if (b&1) ans = (ans*a)%100;
b >>= 1;
a = (a*a)%100;
}
return ans;
}
int main(){
int t;
ll n;
while (cin>>t&&t){
for (int i=1; i<=t; i++){
cin>>n;
printf("Case %d: %I64d
",i,(pows(4,n-1)+pows(2,n-1))%100);
}
cout<<endl;
}
return 0;
}