程序设计协会第一届九韶杯题解C: 斐波那契

斐波那契

这题的数据实在是太大了
使用__int 128

至于啥是__int 128,我想聪明的你一定知道。

AC代码(附保姆级题解):

#include <bits/stdc++.h>

using namespace std;

typedef __int128 lll; //这个功能很不错 

lll f[20], mother[20], son[20];

//f存斐波那契数列
//mother存分母
//son存分子 

//__int128不能直接输出
//这个是输出板子 
void print(lll x)
{
	if (x < 0)
	{
		putchar('-');
		x = -x;	
	}
	if (x > 9) print(x / 10);
	putchar (x % 10 + '0');
} 

int main()
{
	f[1] = f[2] = 1;
	for (int i = 3; i <= 15; i ++)
		f[i] = f[i - 1] + f[i - 2]; //算出斐波那契并存在里面 
		
	for (int i = 1; i <= 13; i ++)
		mother[i] = f[i] * f[i+1]; //递推算出分母 
	
	lll common = 1; //防止分母变成0 
	
	for (int i = 1; i <= 13; i ++)
	{
		common *= mother[i]; //分母乘分母  
		son[i] = 1;
	}
	//这时common已经变成了最大公倍分母 
	for (int i = 1; i <= 13; i ++)
	{
		lll cur = common / mother[i]; //通分操作 
		son[i] *= cur; //每个分子乘以除自己分母以外的分母 
	}
	
	lll ans = 0;
	for (int i = 1; i <= 13; i ++)
		ans += son[i]; //通分完的分子相加 
	
	lll g = __gcd(common, ans); //求最大公约数g 
	
	common /= g; //分母约分 
	ans /= g; //分子约分 
	
	print(ans);
	cout << "/";
	print(common);
	
	return 0;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旧岛江少

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值