当前位置:网站首页>POJ 3070 Fibonacci
POJ 3070 Fibonacci
2022-06-26 12:39: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:
.
分析:此题可以用斐波拉契数列及取余的性质 找到循环节m则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]);
}
}
边栏推荐
- 源码学习:AtomicInteger类代码内部逻辑
- Basic principle and application routine of Beifu PLC rotary cutting
- A must for programmers, an artifact utools that can improve your work efficiency n times
- 软件测试测试常见分类有哪些?
- 程序员必备,一款让你提高工作效率N倍的神器uTools
- el-form-item 包含两个input, 校验这两个input
- 倍福PLC通过MC_ReadParameter读取NC轴的配置参数
- postgis 地理化函数
- OPLG: 新一代云原生可观测最佳实践
- Biff TwinCAT can quickly detect the physical connection and EtherCAT network through emergency scan
猜你喜欢

Redis learning - 02 common data types, operation commands and expiration time

Mongodb of NoSQL - 03 mongodb CRUD

软件测试 - 基础篇

Unit practice experiment 8 - using cmstudio to design microprogram instructions based on basic model machine (1)

Tiger DAO VC产品正式上线,Seektiger生态的有力补充

BigInt:处理大数字(任意长度的整数)

National standard gb28181 protocol easygbs video platform TCP active mode streaming exception repair

国标GB28181协议EasyGBS视频平台TCP主动模式拉流异常情况修复

详细讲解C语言11(C语言系列)

Openlayers drawing dynamic migration lines and curves
随机推荐
guacamole安装
使用SSH密钥对登陆服务器
Redis learning - 03 transaction
自定义封装下拉组件
[极客大挑战 2019]RCE ME 1
美学心得(第二百三十八集) 罗国正
Beifu PLC passes MC_ Readparameter read configuration parameters of NC axis
KVM video card transparent transmission -- the road of building a dream
opencv高速下载
RSS rendering of solo blog system failed
[geek challenge 2019] rce me 1
软件测试报告应该包含的内容?面试必问
EasyGBS如何解决对讲功能使用异常?
Research and development practice of Kwai real-time data warehouse support system
Detailed explanation of C const: definition and use of C constant
solo 博客系统的 rss 渲染失败
Electron official docs series: Distribution
不到40行代码手撸一个BlocProvider
710. random numbers in the blacklist
【网络是怎么连接的】第二章(下):一个网络包的接收