当前位置:网站首页>POJ 3070 Fibonacci
POJ 3070 Fibonacci
2022-06-26 13:11:00 【YJEthan】
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
The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.
Output
For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).
Sample Input
0 9 999999999 1000000000 -1
Sample Output
0 34 626 6875
Hint
As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by
.
Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:
.
analysis : This problem can be solved by fiboracci sequence and the nature of remainder Find the loop section m be f[n]%10000=f[n%m]%10000;
#include<stdio.h>
#include<string.h>
#define mod 10000
#include<vector>
using namespace std;
int main()
{
vector<int>fib;
fib.push_back(0);
fib.push_back(1);
int i;
for(i=2;;i++)
{
fib.push_back((fib[i-1]+fib[i-2])%10000);
if(fib[i]==1&&fib[i-1]==0)
break;
}
int cnt=i-1;
__int64 a;
while(scanf("%I64d",&a)&&a!=-1)
{
printf("%d\n",fib[a%cnt]);
}
}
边栏推荐
- Source code learning: atomicinteger class code internal logic
- 详细讲解C语言11(C语言系列)
- 外观模式(Facade)
- C - Common Subsequence
- 适配器模式(Adapter)
- 8、【STM32】定时器(TIM)——中断、PWM、输入捕获实验(一文精通定时器)
- 首批通过!百度智能云曦灵平台获信通院数字人能力评测权威认证
- Arcpy -- use of insertlayer() function: adding layers to map documents
- Common creation and usage of singletons
- H - Sumsets POJ 2229
猜你喜欢

Group counting practice experiment 9 -- using cmstudio to design microprogram instructions based on segment model machine (2)

倍福通过CTU和TON实现时间片大小和数量的控制

倍福将EtherCAT模块分到多个同步单元运行--Sync Units的使用

Beifu PLC passes MC_ Readparameter read configuration parameters of NC axis

Don't mess with full_ Case and parallel_ CASE

Enjoy element mode (flyweight)

倍福NC轴状态转移图解析

倍福PLC通过MC_ReadParameter读取NC轴的配置参数

Biff TwinCAT can quickly detect the physical connection and EtherCAT network through emergency scan

利用scrapy爬取句子迷网站优美句子存储到本地(喜欢摘抄的人有福了!)
随机推荐
Arcpy - - utilisation de la fonction insertlayer (): ajout de calques dans un document de carte
Electron official docs series: Best Practices
May product upgrade observation station
Do you know the limitations of automated testing?
倍福PLC基于NT_Shutdown实现控制器自动关机重启
详细讲解C语言10(C语言系列)
How does easygbs solve the abnormal use of intercom function?
Electron official docs series: Processes in Electron
Common creation and usage of singletons
C# 结构体:定义、示例
倍福TwinCAT3实现CSV、TXT文件读写操作
Processing polyhedron change
KVM video card transparent transmission -- the road of building a dream
倍福将EtherCAT模块分到多个同步单元运行--Sync Units的使用
5月产品升级观察站
组合模式(Composite )
中国剩余定理模板题 互质与非互质
Stream learning record
Arcpy——InsertLayer()函数的使用:掺入图层到地图文档里
Electron official docs series: Development