HDU 3070 Fibonacci

本文介绍了一种计算斐波那契数列第n项最后四位数字的方法,使用矩阵快速幂来提高效率,适用于n值非常大的情况。

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

Fibonacci

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 15   Accepted Submission(s) : 6
Problem Description

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

An alternative formula for the Fibonacci sequence is

.

Given an integer n, your goal is to compute the last 4 digits of Fn.

 

Input
<span lang="en-us"><p>The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ <i>n</i> ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.</p></span>
 

Output
<span lang="en-us"><p>For each test case, print the last four digits of <i>F<sub>n</sub></i>. If the last four digits of <i>F<sub>n</sub></i> are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print <i>F<sub>n</sub></i> mod 10000).</p></span>
 

Sample Input
0 9 999999999 1000000000 -1
 

Sample Output
0 34 626 6875
 

Source
PKU
 
快速幂模板:
long long modexp(long long a, long long b, int mod)  
{  
    long long res=1;  
    while(b>0)  
    {  
        //a=a%mod;(有时候n的值太大了会超出long long的储存,所以要先取余)  
        if(b&1)//&位运算:判断二进制最后一位是0还是1,&的运算规则为前后都是1的时候才是1,奇数偶数判断;  
            res=res*a%mod;  
        b=b>>1;//相当于除以2;  
        a=a*a%mod;  
    }  
    return res;  
}  
代码:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
struct fast_mod
{
    int t[2][2];
};
fast_mod matrixmul(fast_mod a,fast_mod b)
{
    int i,j,k;
    fast_mod c;
    for(i=0; i<2; i++)
    {
        for(j=0; j<2; j++)
        {
            c.t[i][j]=0;
            for(k=0; k<2; k++)
            {
                c.t[i][j]+=(a.t[i][k]*b.t[k][j])%10000;
            }
        }
    }
    return c;
}
int main()
{
    int n,i;
    while(scanf("%d",&n),n!=-1)
    {
        if(!n)
        {
            printf("0\n");
            continue;
        }
        fast_mod s;
        s.t[0][0]=s.t[0][1]=s.t[1][0]=1;
        s.t[1][1]=0;
        fast_mod ans;
        memset(ans.t,0,sizeof(ans.t));
        for(i=0; i<2; i++)
        {
            ans.t[i][i]=1;
        }
        while(n)
        {
            if(n&1)
                ans=matrixmul(ans,s);
            n=n>>1;
            s=matrixmul(s,s);
        }
        cout<<ans.t[0][1]%10000<<endl;
    }

}




内容概要:本文档是关于基于Tecnomatix的废旧智能手机拆解产线建模与虚拟调试的毕业设计任务书。研究内容主要包括:分析废旧智能手机拆解工艺流程;学习并使用Tecnomatix软件搭建拆解产线的三维模型,包括设备、输送装置等;进行虚拟调试以模拟各种故障情况,并对结果进行分析提出优化建议。研究周期为16周,涵盖了文献调研、拆解实验、软件学习、建模、调试和论文撰写等阶段。文中还提供了Python代码来模拟部分关键流程,如拆解顺序分析、产线布局设计、虚拟调试过程、故障模拟与分析等,并实现了结果的可视化展示。 适合人群:本任务书适用于机械工程、工业自动化及相关专业的本科毕业生,尤其是那些对智能制造、生产线优化及虚拟调试感兴趣的学生。 使用场景及目标:①帮助学生掌握Tecnomatix软件的应用技能;②通过实际项目锻炼学生的系统建模和虚拟调试能力;③培养学生解决复杂工程问题的能力,提高其对废旧电子产品回收再利用的认识和技术水平;④为后续的研究或工作打下坚实的基础,比如从事智能工厂规划、生产线设计与优化等工作。 其他说明:虽然文中提供了部分Python代码用于模拟关键流程,但完整的产线建模仍需借助Tecnomatix商业软件完成。此外,为了更好地理解和应用这些内容,建议学生具备一定的编程基础(如Python),并熟悉相关领域的基础知识。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值