标签:
POJ - 3070
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: . |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; typedef long long LL; #define CLR(x,y) memset((x),(y),sizeof((x))) #define getint(x) int (x);scanf("%d",&(x)) #define get2int(x,y) int (x),(y);scanf("%d%d",&(x),&(y)) #define get3int(x,y,z) int (x),(y),(z);scanf("%d%d%d",&(x),&(y),&(z)) #define getll(x) LL (x);scanf("%I64d",&(x)) #define get2ll(x,y) LL (x),(y);scanf("%I64d%I64d",&(x),&(y)) #define get3ll(x,y,z) LL (x),(y),(z);scanf("%I64d%I64d%I64d",&(x),&(y),&(z)) #define getdb(x) double (x);scanf("%lf",&(x)) #define get2db(x,y) double (x),(y);scanf("%lf%lf",&(x),&(y)) #define get3db(x,y,z) double (x),(y),(z);scanf("%lf%lf%lf",&(x),&(y),&(z)) #define getint2(x) scanf("%d",&(x)) #define get2int2(x,y) scanf("%d%d",&(x),&(y)) #define get3int2(x,y,z) scanf("%d%d%d",&(x),&(y),&(z)) #define getll2(x) scanf("%I64d",&(x)) #define get2ll2(x,y) scanf("%I64d%I64d",&(x),&(y)) #define get3ll2(x,y,z) scanf("%I64d%I64d%I64d",&(x),&(y),&(z)) #define getdb2(x) scanf("%lf",&(x)) #define get2db2(x,y) scanf("%lf%lf",&(x),&(y)) #define get3db2(x,y,z) scanf("%lf%lf%lf",&(x),&(y),&(z)) #define getstr(str) scanf("%s",str) #define get2str(str1,str2) scanf("%s%s",str1,str2) #define FOR(x,y,z) for(int (x)=(y);(x)<(z);(x)++) #define FORD(x,y,z) for(int (x)=(y);(x)>=(z);(x)--) #define FOR2(x,y,z) for((x)=(y);(x)<(z);(x)++) #define FORD2(x,y,z) for((x)=(y);(x)>=(z);(x)--) const int maxn = 1000 + 100; #define MOD 10000 const int mat_size = 2; struct matrix{ long long a[mat_size][mat_size]; }; matrix matrixE; //单位矩阵 void inimatrixE(){ for ( int i = 0;i < mat_size;++i) for ( int j = 0;j < mat_size;++j) matrixE.a[i][j] = i == j ? 1 : 0; } void mat_mult(matrix & a,matrix & b,matrix & c, int m, int n, int s, long long mod){ //a == m*n b == n*s c == m*s memset (c.a,0, sizeof (c.a)); for ( int i = 0 ;i < m ; ++i) for ( int k = 0;k < n ; ++k) for ( int j = 0 ; j < s ; ++j){ c.a[i][j] = (c.a[i][j] + a.a[i][k]*b.a[k][j]) % mod; //如果结果不会超过longlong范围,那么取模运算可以放在第二个for内(第3个for 外面) } } void quick_matrix_pow(matrix & a, int p,matrix & res){ memset (res.a,0, sizeof (res.a)); matrix tmp = a,tmpres; res = matrixE; while (p >= 1){ if (p & 1){ mat_mult(tmp,res,tmpres,mat_size,mat_size,mat_size,MOD); res = tmpres; } p >>= 1; mat_mult(tmp,tmp,tmpres,mat_size,mat_size,mat_size,MOD); tmp = tmpres; } } int main() { int n; inimatrixE(); while (~ scanf ( "%d" ,&n) && ~n) { matrix P = {1,1,1,0},res; quick_matrix_pow(P,n,res); printf ( "%lld\n" ,res.a[0][1]); } return 0; } |
[2016-02-04][POJ][3070][Fibonacci]
标签:
原文地址:http://www.cnblogs.com/qhy285571052/p/5182558.html