poj3070 矩阵快速幂

Fibonacci

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13438   Accepted: 9562

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:

.

第一次写快速矩阵幂,按着Hint讲解的就好, 联想着快速幂,都有一个幂n,底数a,取余mod,还有一个ans = 1,作为初始;这里用

代替ans = 1, [1,1 ]作为底数a。。大概如此。。

[ 1,0]

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 const int mod = 10000;
 5 using namespace std;
 6 struct mat {
 7     int a[3][3];
 8     mat() {
 9         memset(a, 0, sizeof(a));
10     }
11     mat operator *(const mat &o) const {
12         mat t;
13         for(int i = 1; i <= 2; i++) {
14             for(int j = 1; j <= 2; j++)
15                 for(int k = 1; k <= 2; k++) {
16                     t.a[i][j] = (t.a[i][j] + a[i][k]*o.a[k][j])%mod;
17                 }
18         }
19         return t;
20     }
21 } a, b;
22
23 int main() {
24     int n;
25     while(scanf("%d", &n) !=EOF && n!= -1) {
26         a.a[1][1] = a.a[2][2] = 1, a.a[1][2] = a.a[2][1] = 0;
27         b.a[1][1] = b.a[1][2] = b.a[2][1] = 1, b.a[2][2] = 0;
28         if(n == 0) printf("0\n");
29         else {
30             n--;
31             while(n > 0) {
32                 if(n&1) {
33                     a = b*a;
34                     n--;
35                 }
36                 n >>= 1;
37                 b = b*b;
38             }
39             printf("%d\n", a.a[1][1]);
40         }
41     }
42     return 0;
43 }
时间: 2024-08-28 19:16:25

poj3070 矩阵快速幂的相关文章

POJ3070——矩阵快速幂——Fibonacci

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

POJ3070 Fibonacci【矩阵快速幂】

Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20098 Accepted: 13850 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 a

POJ3070 Fibonacci(矩阵快速幂)

用矩阵快速幂求fibonacci序列的第n项. /* *********************************************** Author :devil Created Time :2016/1/19 20:11:43 ************************************************ */ #include <iostream> #include <algorithm> #include <cstring> #in

一些特殊的矩阵快速幂 hdu5950 hdu3369 hdu 3483

思想启发来自, 罗博士的根据递推公式构造系数矩阵用于快速幂 对于矩阵乘法和矩阵快速幂就不多重复了,网上很多博客都有讲解.主要来学习一下系数矩阵的构造 一开始,最一般的矩阵快速幂,要斐波那契数列Fn=Fn-1+Fn-2的第n项,想必都知道可以构造矩阵来转移 其中,前面那个矩阵就叫做系数矩阵(我比较喜欢叫转移矩阵) POJ3070 Fibonacci 可以试一试 1 #include<cstdio> 2 typedef long long ll; 3 const ll md=10000; 4 st

矩阵快速幂刷题系列

来源自http://blog.csdn.net/chenguolinblog/article/details/10309423 hdu 1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5587    Accepted Submission(s): 4200 Problem Description A为一个方阵,则Tr

HDU 1757 A Simple Math Problem (矩阵快速幂)

[题目链接]:click here~~ [题目大意]: If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const

Codeforces Round #291 (Div. 2) E - Darth Vader and Tree (DP+矩阵快速幂)

这题想了好长时间,果断没思路..于是搜了一下题解.一看题解上的"快速幂"这俩字,不对..这仨字..犹如醍醐灌顶啊...因为x的范围是10^9,所以当时想的时候果断把dp递推这一方法抛弃了.我怎么就没想到矩阵快速幂呢.......还是太弱了..sad..100*100*100*log(10^9)的复杂度刚刚好. 于是,想到了矩阵快速幂后,一切就变得简单了.就可以把距离<=x的所有距离的点数都通过DP推出来,然后一个快速幂就解决了. 首先DP递推式很容易想到.递推代码如下: for(

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st

HDU 4990 Reading comprehension(找规律+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 Problem Description Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include