제출 #1341390

#제출 시각아이디문제언어결과실행 시간메모리
1341390trungcanStruktura (COCI26_struktura)C++20
110 / 110
1 ms344 KiB
#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 7;

int n, k;

struct Matrix{
    int m[3][3];
    Matrix(){
        for (int i = 1; i < 3; ++i)
            for (int j = 1; j < 3; ++j) m[i][j] = 0;
    }
} f, I;

Matrix operator* (Matrix A, Matrix B){
    Matrix C;
    for (int i = 1; i < 3; ++i)
        for (int k = 1; k < 3; ++k)
            for (int j = 1; j < 3; ++j)
                C.m[i][j] = (C.m[i][j] + A.m[i][k] * 1LL * B.m[k][j] % MOD) % MOD;
    return C;
}

Matrix binpow(Matrix A, int b){
    Matrix res = I;
    for (; b; b >>= 1, A = A * A)
        if (b & 1) res = res * A;
    return res;
}

int powint(int a, int b){
    int res = 1;
    for (; b; b >>= 1, a = a * 1LL * a % MOD)
        if (b & 1) res = res * 1LL * a % MOD;
    return res;
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

    cin >> n >> k;

    if (k < n) return cout << 0, 0;
    f.m[1][1] = f.m[1][2] = f.m[2][1] = I.m[1][1] = I.m[2][2] = 1;
    f = binpow(f, n - 1);

    cout << ((f.m[1][1] + f.m[1][2]) % MOD * 1LL * powint(powint(k, n), MOD - 2)) % MOD;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...