51Nod 1242 斐波那契數列的第N項(矩陣快速冪)
裡面提到
最後一個式子展開
所有經過n-1次冪之後,直接輸出第一個元素即可
矩陣快速冪和乘法快速冪大同小異
#include<iostream> #include<cstdio> #include<math.h> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<vector> using namespace std; const int mod=1000000009; typedef struct node{ long long m[2][2]; }node; node cheng(node n1,node n2){ node sum; for(int i=0;i<=1;i++){ for(int j=0;j<=1;j++){ sum.m[i][j]=0; for(int k=0;k<=1;k++){ sum.m[i][j]+=n1.m[i][k]*n2.m[k][j]; sum.m[i][j]%=mod; } } } return sum; } node f(node a,long long n){ node sum=a; while(n){ if(n&1) sum=cheng(sum,a); a=cheng(a,a); n>>=1; } return sum; } int main(){ long long n; cin>>n; if(n==1) { cout<<1; return 0; } if(n==2){ cout<<1; return 0; } node a={1,1,1,0}; node no=f(a,n-2); cout<<no.m[0][0]%mod; return 0; }
相關推薦
斐波那契數列第N項f(N)[矩陣快速冪]
string sin int code char const mat ret truct 矩陣快速冪 定義矩陣A(m*n),B(p*q),A*B有意義當且僅當n=p。即A的列數等於B的行數。 且C=A*B,C(m*q)。 例如: 進入正題,由於現
使用線性代數求解斐波那契數列第n項
一:問題 已知斐波那契數列,f1=1,f2=1,f3=3,f4=5...求第n項的數值 二:問題轉化為線性代數問題 可以得到方程 f(n+2)=f(n+1)+f(n) 為了解決問題,新增方程 f(n+1)=f(n+1) 記向量,則上兩式可以寫作 要求,只需知道,
斐波那契數列第n項的三種求法
方法1: 利用遞迴方法,但是遞迴看似簡單但是無法處理較大的項數,時間複雜度為o(2^n)。 public class fib_遞迴 { /** * @param args */ public static
斐波那契數列第N項(C++)
求斐波那契數的第N項,N可以很大但結果不能超過1000位; #include <iostream> #include <memory.h> #include <strin
java遞迴求斐波那契數列第n項
public class Fibonacci { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int f
遞迴用python求解斐波那契數列第n項
波那契數列(Fibonacci sequence),又稱黃金分割數列、因數學家列昂納多·斐波那契(Leonardoda Fibonacci)以兔子繁殖為例子而引入,故又稱為“兔子數列”,指的
求解斐波那契數列第n項(JavaBigInteger之自底向上迭代)
import java.math.BigInteger; import java.util.*; public class Fab //用迭代法自底向上求解斐波那契數列第n項 { publi
求斐波那契數列第N項的最後一位
RT,該怎麼求呢? 首先,你可能會想到,順序遍歷求解。 利用通項公式,可以得到斐波那契序列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34... 那每一項的最後一位就是: 0, 1, 1, 2, 3, 5, 8, 3, 1, 4... 這樣做的效
Java練習題-求斐波那契數列第n項
/** * 求斐波那契數列第n項,n<30,斐波那契數列前10項為 1,1,2,3,5,8,13,21,34,55 * @author Tang * */ public clas
斐波那契數列第N項
#include<stdio.h> int FibonacciSequence(int n) { int f1=1; int f2=1; int f3=1; for(int i=3;i<=n;i++) { f3=f1+f2; f1=f2;
斐波那契數列第n項的高效解法
參考書籍《劍指Offer》 常見解法 談及斐波那契數列,我們直接就可以想到f(n)=f(n-1)+f(n-2)。於是做出如下解: long long Fibonacci(unsigned
矩陣快速模冪 + 求斐波那契數列第n項(Fibonacci)
兩矩陣相乘,樸素演算法的複雜度是O(N^3)。如果求一次矩陣的M次冪,按樸素的寫法就是O(N^3*M)。既然是求冪,不免想到快速冪取模的演算法,前面有快速冪取模的介紹,a^b %m 的複雜度可以降
51Nod 1242 斐波那契數列的第N項 矩陣快速冪
斐波那契數列的定義如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,
51nod 1242 斐波那契數列的第N項
斐波那契數列的定義如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 2
51Nod 1242 斐波那契數列的第N項 ——————杜教篩,BM板子
1242 斐波那契數列的第N項 基準時間限制:1 秒 空間限制:131072 KB 分值: 0 難度:基礎題 收藏 關注 斐波那契數列的定義如下: F(0)=0F(1)=1F(n)=F(n−1)+F(n−2)(n>=2)F(0) = 0 \\ F
51Nod 1242 斐波那契數列的第N項——————矩陣快速冪
1242 斐波那契數列的第N項 基準時間限制:1 秒 空間限制:131072 KB 分值: 0 難度:基礎題 收藏 關注 斐波那契數列的定義如下: F(0)=0F(1)=1F(n)=F(n−1)+F(n−2)(n>=2)F(0) = 0 \\ F
51Nod 1242 斐波那契數列的第N項 矩陣快速冪
斐波那契數列的定義如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ...)
51Nod 1242 斐波那契數列的第N項(矩陣快速冪)
題目連結 大神部落格 裡面提到 最後一個式子展開 所有經過n-1次冪之後,直接輸出第一個元素即可 矩陣快速冪和乘法快速冪大同小異 #include<iostream> #inclu
51nod 1242 斐波那契數列的第N項 (矩陣快速冪)
斐波那契數列的定義如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, 13, 2
51Nod 1242:斐波那契數列的第N項
N比較大,用矩陣快速冪解決。 與POJ 3070題目一樣。 AC程式碼: #include <iostream> #include <stdio.h> #include &