Project Euler #624 Two heads are better than one【斐波那契】

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=12inf(in1),其中 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(215 )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=1105 ((41+5 )in1(415 )in1)=105 (i=1(41+5 )in1i=1(415 )in1)

就变成了两个等比数列的差。

因为 1 + 5 4 , 1 − 5 4 ∈ ( − 1 , 1 ) \frac{1+\sqrt{5}}{4},\frac{1-\sqrt{5}}{4} \in(-1,1) 41+5 ,415 (1,1)

所以无限项的通项公式就是 a 1 1 − q \frac{a_1}{1-q} 1qa1

代入就可以了。

【代码如下】

#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;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值