poj 3070 斐波拉切快速幂公式
来源:互联网 发布:淘宝网黄金首饰 编辑:程序博客网 时间:2024/06/01 22:50
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
涨姿势了不是,斐波拉切的快速幂公式就在这里,解决了n很大是递归公式爆栈的问题吧
注意n=0特判即可
#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef long long ll;const int maxn = 4;const int mod = 10000;struct mat { int s[maxn][maxn]; mat(){ memset(s,0,sizeof(s)); }; mat operator * (const mat& c) { mat ans; for (int i = 0; i < maxn; i++) //矩阵乘法 for (int j = 0; j < maxn; j++) for (int k = 0; k < maxn; k++) ans.s[i][j] = (ans.s[i][j] + s[i][k] * c.s[k][j]) % mod; return ans; }}str;mat pow_mod(ll k) { if (k == 1) return str; mat a = pow_mod(k/2);//不能改 mat ans = a * a; if (k & 1) ans = ans * str; return ans;}int main() { //freopen("in.txt","r",stdin); int n; str.s[0][0] = 1; str.s[0][1] = 1; str.s[1][0] = 1; str.s[1][1] = 0; while(~scanf("%d",&n)&&n!=-1) { if(n==0) puts("0"); else { mat sub = pow_mod(n); ll res = 0; res = sub.s[0][1]%mod; cout<<res<<endl; } } return 0;}
- poj 3070 斐波拉切快速幂公式
- POJ 3012 : A Number from Yanghui Triangle - 快速幂;推公式
- POJ 1845 Sumdiv (快速幂+质因数+约数和公式+同余模)
- 【欧拉降幂公式+快速幂】HDU_4704_Sum
- 【快速幂 && 公式推导】LightOJ
- POJ 3070 矩阵快速幂
- poj 3070 矩阵快速幂
- poj 3070(矩阵快速幂)
- poj 3070 矩阵快速幂
- 矩阵快速幂 poj 3070
- POJ 3070 矩阵快速幂
- POJ 3070 矩阵快速幂
- POJ 3070 矩阵快速幂
- poj 3070 矩阵快速幂
- POJ 3070 矩阵快速幂
- poj 3070 矩阵快速幂
- 矩阵快速幂POJ-3070
- poj 3070 矩阵快速幂
- oracle 全文检索--多列(二)
- <操作系统:精髓与设计原理> 计算机安全
- LeetCode113 Path Sum II
- LeetCode112 Path Sum
- Java 自定义ClassLoad
- poj 3070 斐波拉切快速幂公式
- LeetCode114 Flatten Binary Tree to Linked List
- Android 开发 Tip 7 -- volatile & synchronized
- java中字符流一次到底读几个字节?
- this关键字
- Caffe依赖包解析
- Linux(2)-目录,文件,链接处理命令
- Redis主从复制 问题
- C++函数之类的构造函数与析构函数