博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(矩阵快速幂) Fibonacci -- poj -- 3070
阅读量:5905 次
发布时间:2019-06-19

本文共 1886 字,大约阅读时间需要 6 分钟。

链接:
 
Fibonacci
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11236   Accepted: 7991

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

099999999991000000000-1

Sample Output

0346266875

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
#define MOD 10000struct node{ int m[2][2];}a, b;node cheng(node x, node y){ int i, j, k; node c; for(i=0; i<2; i++) for(j=0; j<2; j++) { c.m[i][j] = 0; for(k=0; k<2; k++) c.m[i][j] = (c.m[i][j] + x.m[i][k]*y.m[k][j])%MOD; } return c;}int Fast_MOD(int n) { a.m[0][0] = a.m[0][1] = a.m[1][0] = 1; a.m[1][1] = 0; b.m[0][0] = b.m[1][1] = 1; /// b 初始化为单位矩阵 b.m[0][1] = b.m[1][0] = 0; while(n) { if(n&1) /// n是奇数 b = cheng(b, a); a = cheng(a, a); n >>= 1; } return b.m[0][1];}int main(){ int n; while(scanf("%d", &n), n!=-1) { printf("%d\n", Fast_MOD(n)); } return 0;}
View Code

 

 

转载于:https://www.cnblogs.com/YY56/p/4830136.html

你可能感兴趣的文章
机器学习 - pycharm, tensorflow集成篇
查看>>
Java基础-位运算符Bitwise Operators
查看>>
Linux常用基本命令( rmdir, rm, mv )
查看>>
python 排列组合
查看>>
python 取整的两种方法
查看>>
POJ2406 Power Strings(KMP)
查看>>
java B2B2C Springcloud电子商城系统-Feign基本使用
查看>>
Qtum量子链x2018区块链新经济论坛:区块链基础设施建设发展方向
查看>>
Java反射与hook混用反射某支付的方法
查看>>
前端性能优化 - Resource Hints 资源预加载
查看>>
JavaScript-console的使用_016
查看>>
两种方式设置iframe的高度区别
查看>>
应用后台省电秘籍——低功耗状态下应用如何正常运行?
查看>>
Iterator 和 for...of 循环
查看>>
关于iOS 11.x微信连wifi流程中,在Portal页无法拉起微信问题的简单记录
查看>>
Python GUI库wxPython官网Hello World示例的逐行解释
查看>>
RE·WORK 巅峰对话:深度学习将彻底改变医疗健康领域
查看>>
计算机网络物理层
查看>>
Mysql如何使自增字段重新计算?
查看>>
使用Telnet测试基本POP3服务
查看>>