POJ3734
题目描述
题解
EGF.
显然红色和绿色的方案数为:
F
(
x
)
=
∑
i
=
0
∞
[
2
∣
n
]
x
i
i
!
=
e
x
+
e
−
x
2
F(x)=\sum\limits_{i=0}^{\infty}[2|n]\frac{x^i}{i!}=\frac{e^x+e^{-x}}{2}\\
F(x)=i=0∑∞[2∣n]i!xi=2ex+e−x
而黄色和蓝色的方案数为:
G
(
x
)
=
∑
i
=
0
∞
x
i
i
!
=
e
x
G(x)=\sum\limits_{i=0}^{\infty}\frac{x^i}{i!}=e^x\\
G(x)=i=0∑∞i!xi=ex
将它们全部乘起来,即可得到答案的生成函数:
A
n
s
(
x
)
=
e
4
x
+
2
e
2
x
+
1
4
Ans(x)=\frac{e^{4x}+2e^{2x}+1}{4}
Ans(x)=4e4x+2e2x+1
考虑转换成通项公式:
A
n
s
(
x
)
=
1
4
+
1
4
∑
i
=
0
∞
4
i
x
i
i
!
+
1
2
∑
i
=
0
∞
2
i
x
i
i
!
Ans(x)=\frac{1}{4}+\frac{1}{4}\sum\limits_{i=0}^{\infty}4^i\frac{x^i}{i!}+\frac{1}{2}\sum\limits_{i=0}^{\infty}2^i\frac{x^i}{i!}
Ans(x)=41+41i=0∑∞4ii!xi+21i=0∑∞2ii!xi
常数项可忽略,由于是排列,还要乘上
n
!
n!
n!,那么
[
x
n
]
[x^n]
[xn]即为:
n
!
4
n
−
1
+
2
n
−
1
n
!
=
4
n
−
1
+
2
n
−
1
n!\frac{4^{n-1}+2^{n-1}}{n!}=4^{n-1}+2^{n-1}\\
n!n!4n−1+2n−1=4n−1+2n−1
代码
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define int long long
using namespace std;
const int mod=1e4+7;
int ksm(int a,int b){
int ans=1;
while(b){
if(b&1) ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}return ans;
}
int n,t;
signed main(){
cin>>t;
while(t--){
cin>>n;
cout<<(ksm(4,n-1)%mod+ksm(2,n-1)%mod)%mod<<endl;
}return 0;
}