POJ 3070 Fibonacci
创始人
2024-05-08 08:08:17
0

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 30932Accepted: 20284

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:

知识点矩阵快速幂加速递推

#include 
#include 
#include 
#include 
#include 
#include using namespace std;
typedef long long ll;const ll m = 10000;
// 矩阵的快速幂
struct matrix{ ll m[3][3]; };
matrix operator *(const matrix& a, const matrix& b) { // 重载*为矩阵乘法,注意constmatrix c;memset(c.m,0, sizeof(c.m));for (int i = 0; i < 2; i++) {for (int j = 0; j < 2; j++) {for (int k = 0; k < 2; k++) {c.m[i][j] = (c.m[i][j] + a.m[i][k] * b.m[k][j]) % m;}}}return c;
}matrix pow_matrix(matrix a, ll n) {matrix ans;memset(ans.m, 0, sizeof(ans.m));for (int i = 0; i < 2; i++) ans.m[i][i] = 1;while (n) {if (n & 1) ans = ans * a;a = a * a;n >>= 1;}return ans;
}
int main() {ll n;while (cin >> n ,n != EOF){if (n == -1)break;if (n == 0){printf("0\n");continue;}matrix a;a.m[0][0] = 1; a.m[0][1] = 1;a.m[1][0] = 1; a.m[1][1] = 0;a = pow_matrix(a, n);printf("%lld\n", a.m[0][1]);}return 0;
}

相关内容

热门资讯

安卓系统相册软件下载,下载与使... 手机里的相册是不是已经满满当当,想要给它们找个新家?别急,今天就来给你安利几款超好用的安卓系统相册软...
安卓9系统优化软件,解锁流畅体... 你有没有发现,自从你的安卓手机升级到了安卓9系统,运行速度好像变得更快了?是不是觉得手机变得更加流畅...
各厂商安卓系统对比,性能、特色... 你有没有发现,现在手机市场上安卓系统的竞争可是相当激烈呢!各大厂商纷纷推出自己的特色系统,让人眼花缭...
车机进入安卓系统,智能驾驶体验... 你有没有发现,最近你的车机系统好像变得不一样了?没错,车机系统正在悄悄地进入安卓的大家庭!这可不是什...
安卓系统自带壁纸高清,自带高清... 亲爱的手机控们,你是否曾为安卓系统自带的那些高清壁纸而驻足欣赏?那些色彩斑斓、风格迥异的画面,是不是...
安卓机换成钟表系统,探索智能穿... 你有没有想过,你的安卓手机其实也可以换上钟表系统呢?是的,你没听错,就是那种优雅、简洁、充满艺术感的...
安卓lcs操作系统,轻量级、安... 你知道吗?在智能手机的世界里,有一个操作系统可是相当出名的,那就是安卓LCS操作系统。它就像一位魔法...
安卓系统微信包月,畅享无限制沟... 你知道吗?在咱们这个手机不离手的年代,微信可是咱们日常生活中不可或缺的好帮手。不过,你知道吗?安卓系...
我想换安卓系统,系统升级换新体... 亲爱的读者,你是否也有过这样的冲动?看着身边的朋友纷纷换上了安卓系统,心里痒痒的,也想尝试一下?没错...
用了苹果换安卓系统,系统更迭背... 你知道吗?最近我可是经历了一场大变身呢!是的,你没听错,我用苹果手机换成了安卓系统。这可不是一个小决...
手机刷机系统安卓,解锁手机潜能... 你有没有想过,你的手机是不是已经有点儿“老态龙钟”了呢?别急,别急,今天就来给你揭秘如何给手机来个焕...
安卓pe10系统,功能与特色深... 你有没有听说最近安卓PE10系统火得一塌糊涂?没错,就是那个让无数手机用户为之疯狂的系统。今天,我就...
安卓系统程序安装目录,安卓系统... 你有没有想过,当你手机里安装了一个又一个应用程序时,它们都藏在哪里呢?没错,就是那个神秘的安卓系统程...
ios系统能定位安卓系统吗,i... 你有没有想过,你的iPhone和安卓手机之间竟然能玩出这么一出“追踪大戏”?没错,我要说的就是那个让...
安卓系统时间放到桌面,桌面概览... 你有没有发现,手机上的时间有时候会偷偷跑得飞快,让你不知不觉就错过了重要的事情?别急,今天就来教你怎...
安卓系统怎么刷win,体验全新... 你有没有想过,把你的安卓手机变成一台Windows电脑呢?听起来是不是有点不可思议?但别急,今天我就...
安卓仿苹果系统设置,打造极致用... 你有没有发现,现在越来越多的安卓手机开始模仿苹果的操作系统了?没错,就是那个简洁又好用的设置界面!今...
emui 安卓系统对应关系,E... 你有没有发现,每次打开你的华为手机,那个界面看起来是不是特别顺眼?那是因为华为的EMUI系统,它就像...
永诺安卓系统相机,功能解析与使... 你有没有发现,手机拍照已经成为我们生活中不可或缺的一部分?而在这其中,永诺安卓系统的相机功能可是相当...
tinder安卓版系统错误,揭... 最近在使用Tinder安卓版的时候,你是不是也遇到了一些让人头疼的系统错误呢?别急,今天就来和你聊聊...