๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 11444๋ฒˆ - ํ”ผ๋ณด๋‚˜์น˜์ˆ˜ 6

by dkswnkk 2021. 10. 28.
 

11444๋ฒˆ: ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜ 6

์ฒซ์งธ ์ค„์— n์ด ์ฃผ์–ด์ง„๋‹ค. n์€ 1,000,000,000,000,000,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

www.acmicpc.net

#include <iostream>
#include <vector>
#define MOD 1000000007
using namespace std;

using ll = long long;
using mat = vector<vector<ll>>;
ll n;

mat mulMatrix(mat &a, mat & b){
    mat tmp(2,vector<ll>(2));
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 2; j++){
            for(int k = 0; k < 2; k++){
                tmp[i][j] += a[i][k] * b[k][j];
            }
            tmp[i][j] %= MOD;
        }
    }
    return tmp;
}

ll fibonacci(ll num){
    mat ans = {{{0,1},{1,0}}};
    mat a = {{1,1},{1,0}};
    while(num > 0){
        if(num & 1) ans = mulMatrix(a,ans);
        a = mulMatrix(a,a);
        num /= 2;
    }
    return ans[0][0];
}



int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);


    ll num; cin>>num;
    cout<<fibonacci(num);

}

๋Œ“๊ธ€