csu 1104 Fibonacci Numbers

本文介绍了一种计算Fibonacci数列方法,对于超过8位数的数,仅输出首尾四位,并使用矩阵幂法优化计算过程,显著减少时间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

超过8位只输出高低四位,中间用...隔开:

Fibonacci numbers get large pretty quickly, so whenever the answer has more than 8 digits, output only the first and last 4 digits of the answer, separating the two parts with an ellipsis (“...”). 

高四位的输出:利用通项公式,先求出取对数的小数部分,然后取前四位即可。

第四位:直接递推会严重超时!!所以只能用矩阵幂的方法(其实题目提示了用线性代数知识:Use your linear algebra knowledge)

/* csu 1104 */
# include <stdio.h>
# include <math.h>
# include <time.h>
# define MODN 10000

typedef struct Matrix
{
int a,b,c,d;
} mat;


int f[] = {0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,
2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,
317811,514229,832040,1346269,2178309,3524578,5702887,9227465,
14930352,24157817,39088169,63245986
};


mat cur, mpow, tmp;

int last_4_dig(int n)
{
int i, k, m;

m = (n-1)>>1;
cur.a = cur.d = 1, cur.b = cur.c = 0;
mpow.a = mpow.b = mpow.c = 1, mpow.d = 2;

for (k = 31; k >= 0; --k)
if ((m>>k) & 0x1) break;
for (i = 0; i <= k; ++i)
{
if ((m>>i) & 0x1)
{
tmp = cur;
cur.a = (tmp.a*mpow.a+tmp.b*mpow.c) % MODN;
cur.b = (tmp.a*mpow.b+tmp.b*mpow.d) % MODN;
cur.c = (tmp.c*mpow.a+tmp.d*mpow.c) % MODN;
cur.d = (tmp.c*mpow.b+tmp.d*mpow.d) % MODN;
}
tmp = mpow;
mpow.a = (tmp.a*tmp.a + tmp.b*tmp.c) % MODN;
mpow.b = (tmp.b * (tmp.a+tmp.d)) % MODN;
mpow.c = (tmp.c * (tmp.a+tmp.d)) % MODN;
mpow.d = (tmp.c*tmp.b + tmp.d*tmp.d) % MODN;
}
if (n & 0x1) return (cur.a+cur.b)%MODN;
else return (cur.c+cur.d)%MODN;
}

//int last_4_dig(int n)  // 严重 TLE !!
//{
// int a, b, i;
//
// i = 38;
// a = f[38] % 10000;
// b = f[39] % 10000;
//
// while (i+1 < n)
// {
// a = (a + b) % 10000;
// b = (a + b) % 10000;
// i += 2;
// }
// return (n&0x1) ? b:a;
//}

int main()
{
int n;
double first_4_dig;

freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);

while (~scanf("%d", &n))
{
if (n < 40)
{
printf("%d\n", f[n]);
continue;
}
else
{
first_4_dig = n*log10(0.5*(1+sqrt(5)))-0.5*log10(5.0);
first_4_dig -= (int)first_4_dig;
first_4_dig = pow(10, first_4_dig);
while (first_4_dig < 1000) first_4_dig *= 10;
printf("%d", (int)first_4_dig);
printf("...%04d\n", last_4_dig(n));
}
}

printf("time cost %.3lf\n", (double)clock()/CLOCKS_PER_SEC);

return 0;
}

其实原来弄错了,以为是从第1项开始依次是0、1、1,中间还怀疑示例数据错了。。。o(╯□╰)o

所以在求后四位数那里,可以稍微改一下,返回时直接返回矩阵的某一个元素即可(f[0]=0,f[1]=1)

转载于:https://www.cnblogs.com/JMDWQ/archive/2012/03/09/2386040.html

基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。 智能教学辅助系统 这是一个智能教学辅助系统的前端项目,基于 Vue3+TypeScript 开发,使用 Ant Design Vue 作为 UI 组件库。 功能模块 用户模块 登录/注册功能,支持学生和教师角色 毛玻璃效果的登录界面 教师模块 备课与设计:根据课程大纲自动设计教学内容 考核内容生成:自动生成多样化考核题目及参考答案 学情数据分析:自动化检测学生答案,提供数据分析 学生模块 在线学习助手:结合教学内容解答问题 实时练习评测助手:生成随练题目并纠错 管理模块 用户管理:管理员/教师/学生等用户基本管理 课件资源管理:按学科列表管理教师备课资源 大屏概览:使用统计、效率指数、学习效果等 技术栈 Vue3 TypeScript Pinia 状态管理 Ant Design Vue 组件库 Axios 请求库 ByteMD 编辑器 ECharts 图表库 Monaco 编辑器 双主题支持(专业科技风/暗黑风) 开发指南 # 安装依赖 npm install # 启动开发服务器 npm run dev # 构建生产版本 npm run build 简介 本项目旨在开发一个基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值