Project Euler #624 Two heads are better than one
【题目描述】
【题解】
最后我们可以通过打表或推理得到这个式子 G ( n ) = ∑ i = 1 ∞ f ( i ∗ n − 1 ) 2 i ∗ n G(n)=\large \sum_{i=1}^{\infty} \frac{f(i * n-1)}{2^{i * n}} G(n)=∑i=1∞2i∗nf(i∗n−1),其中 f ( i ) f(i) f(i)为斐波那契数列。
我们知道 f ( i ) = 5 5 ( ( 1 + 5 2 ) i − ( 1 − 5 2 ) i ) \large f(i)=\frac{\sqrt{5}}{5}((\frac{1+\sqrt{5}}{2})^i-(\frac{1-\sqrt{5}}{2})^i) f(i)=55((21+5)i−(21−5)i)
将 f ( i ) f(i) f(i)代入 G ( n ) = ∑ i = 1 ∞ 5 10 ( ( 1 + 5 4 ) i ∗ n − 1 − ( 1 − 5 4 ) i ∗ n − 1 ) = 5 10 ( ∑ i = 1 ∞ ( 1 + 5 4 ) i ∗ n − 1 − ∑ i = 1 ∞ ( 1 − 5 4 ) i ∗ n − 1 ) G(n)=\large \sum_{i=1}^{\infty} \frac{\sqrt{5}}{10}((\frac{1+\sqrt{5}}{4})^{i * n-1}-(\frac{1-\sqrt{5}}{4})^{i * n-1})\\=\large \frac{\sqrt{5}}{10}(\sum_{i=1}^{\infty}(\frac{1+\sqrt{5}}{4})^{i * n-1}-\sum_{i=1}^{\infty}(\frac{1-\sqrt{5}}{4})^{i * n-1}) G(n)=∑i=1∞105((41+5)i∗n−1−(41−5)i∗n−1)=105(∑i=1∞(41+5)i∗n−1−∑i=1∞(41−5)i∗n−1)
就变成了两个等比数列的差。
因为 1 + 5 4 , 1 − 5 4 ∈ ( − 1 , 1 ) \frac{1+\sqrt{5}}{4},\frac{1-\sqrt{5}}{4} \in(-1,1) 41+5,41−5∈(−1,1)
所以无限项的通项公式就是 a 1 1 − q \frac{a_1}{1-q} 1−qa1
代入就可以了。
【代码如下】
#include<cstdio>
using namespace std;
typedef long long LL;
const LL MOD=1e9+9,NI=383008016;
LL n;
LL qsm(LL x,LL b){
LL Mul=1;x=(x%MOD+MOD)%MOD;
for(;b;b>>=1,x=1ll*x*x%MOD) if(b&1) Mul=1ll*Mul*x%MOD;
return Mul;
}
int main(){
freopen("coin.in","r",stdin);
freopen("coin.out","w",stdout);
scanf("%lld",&n);
LL A=qsm(1+NI ,n-1)*4%MOD*qsm(qsm(4,n)-qsm(1+NI,n),MOD-2)%MOD;
LL B=qsm(1-NI+MOD,n-1)*4%MOD*qsm(qsm(4,n)-qsm(1-NI,n),MOD-2)%MOD;
printf("%lld\n",NI*qsm(10,MOD-2)%MOD*(((A-B)%MOD+MOD)%MOD)%MOD);
return 0;
}