Pisano Periods (找规律)

该博客介绍了斐波那契数列对给定模数取余后的周期长度问题,包括Donald Wall的发现和计算周期长度的思路,给出了程序设计的题目描述、输入输出格式,并提供了解决问题的思路和示例代码。

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

欢迎访问https://blog.youkuaiyun.com/lxt_Lucia~~

宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~~

 

----------------------------------------我只是一条可爱哒分界线-------------------------------------------

一、问题:

Description

In 1960, Donald Wall of IBM, in White Plains, NY, proved that the series obtained by taking each element of the Fibonacci series modulo m was periodic. For example, the first ten elements of the Fibonacci sequence, as well as their remainders modulo 11, are:

                   n |  1    2     3     4     5     6     7     8     9    10

             F (n) |  1    1     2     3  &nbs

要解决这个问题,我们需要分析斐波那契数列的特性以及如何高效地统计满足条件的项。 ### 背景知识 斐波那契数列是一个递归序列,定义为 \( F(1) = 1 \), \( F(2) = 1 \),对于 \( n \geq 3 \),有 \( F(n) = F(n-1) + F(n-2) \)[^1]。由于只关心个位数是否等于7,可以利用模运算简化计算过程。具体来说,只需要关注每项的结果对10取余即可。 #### 周期性观察 通过实验发现,斐波那契数列的最后一位具有周期性重复规律。这种现象被称为 **Pisano Period** (皮萨诺周期)。对于模数10的情况,该周期长度固定为60[^4]。这意味着如果知道某一段连续60项中的最后一项,则可以通过简单倍增法快速得出更远范围内的结果。 基于上述理论基础,我们可以设计算法如下: ### 解决方案 以下是完整的解决方案及其解释: ```c #include <stdio.h> // 计算单个周期内符合条件的数量 int countInPeriod() { int f1 = 1, f2 = 1; int cnt = 0; for(int i=1;i<=60;i++) { // Pisano period length is 60 when mod by 10. if(f1 % 10 == 7){ cnt++; } int next_f = (f1+f2)%10; f1=f2; f2=next_f; } return cnt; } void solve(long long total_items){ const int PERIOD_LENGTH = 60; // Calculate how many full periods fit into the range and remainder items after that. long long complete_periods = total_items / PERIOD_LENGTH; int remaining_items = total_items % PERIOD_LENGTH; // Get number of '7's per one pisano cycle. int sevens_per_period = countInPeriod(); // Total from all completed cycles plus those within incomplete part at end. long long result = complete_periods * sevens_per_period; // Now handle leftover elements separately since they may contain additional matches too. int current_first = 1,current_second=1; for(int j=1;j<=remaining_items && j!=0 ;j++){ if(current_first%10==7){ result++; } int temp=(current_first+current_second)%10; current_first=current_second; current_second=temp; } printf("Number of terms with last digit as 7 up to term %lld : %lld\n",total_items,result); } ``` 此程序首先确定整个区间能容纳多少完整周期,并分别处理这些周期内部匹配次数;接着单独考虑剩余不足构成新周期的部分是否有额外贡献。 ### 结果展示 运行以上代码并传入参数 `solve(202202011200)` 可得到最终答案。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值