给定数列 1,1,1,3,5,9,17,…,从第 4 项开始,每项都是前 3 项的
和。求第 20230601 项的最后 4 位数字
a,b,c,d=1,1,1,1
for _ in range(20230601):
a,b,c=b,c,d
d=(a+b+c)%10000
print(a,b,c,d)
参考博文:https://blog.csdn.net/qq_45281807/article/details/108918842
问题描述
给定数列 1, 1, 1, 3, 5, 9, 17, …,从第 4 项开始,每项都是前 3 项的和。求第 20190324 项的最后 4 位数字。
答案提交
这是一道结果填空的题,你只需要算出结果后提交输出即可。本题的结果为一个 4 位整数(提示:答案的千位不为 0),在提交答案时只输出这个整数,输出多余的内容将无法得分。
输入
没有输入。
输出
输出一个整数。
提示
把答案放在输出语句中输出,例如C/C++语言可以用printf或cout。
注意:需要输出的是一个整数,不要输出任何多余内容。
思路:
水题,斐波那契数列的变式。斐波那契数列相信大家都不陌生。这里面需要注意的还是超出数据范围的问题,最后需要保留四位小数,只需要每次求出的和对10000取余即可。
#include <stdio.h>
int main ()
{
int a,b,c,d;
int i;
a=1;
b=1;
c=3;
int temp=0;
for(i=4;i<2020230601;i++)
{
d=(a+b+c)%10000;
a=b;
b=c;
c=d;
}
printf("%d",d);
return 0;
}
chatgpt 很快给出答案
def find_nth_term(n):
sequence = [1, 1, 1]
for i in range(3, n):
next_term = sequence[i-1] + sequence[i-2] + sequence[i-3]
sequence.append(next_term)
return sequence[n-1] % 10000
n = 20230601
result = find_nth_term(n)
print(f"The last 4 digits of the {n}th term are: {result}")