'commit'
This commit is contained in:
46
TangDou/AcWingSuanFaJinJieZhiNan/P1962.cpp
Normal file
46
TangDou/AcWingSuanFaJinJieZhiNan/P1962.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
typedef long long LL;
|
||||
const int N = 10;
|
||||
const int MOD = 1e9 + 7;
|
||||
|
||||
LL base[N][N], res[N][N];
|
||||
|
||||
inline void mul(LL C[][N], LL A[][N], LL B[][N]) {
|
||||
static LL tmp[N][N];
|
||||
memset(tmp, 0, sizeof(tmp));
|
||||
for (int i = 1; i <= 2; i++)
|
||||
for (int j = 1; j <= 2; j++)
|
||||
for (int k = 1; k <= 2; k++)
|
||||
tmp[i][j] = (tmp[i][j] + (A[i][k] * B[k][j]) % MOD) % MOD;
|
||||
memcpy(C, tmp, sizeof tmp);
|
||||
}
|
||||
|
||||
//快速幂
|
||||
inline void qmi(LL k) {
|
||||
//初始化结果数组的(i,i)=1
|
||||
memset(res, 0, sizeof res);
|
||||
res[1][1] = res[1][2] = 1;
|
||||
while (k) {
|
||||
if (k & 1) mul(res, res, base);
|
||||
mul(base, base, base);
|
||||
k >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
LL n;
|
||||
cin >> n;
|
||||
if (n <= 2) {
|
||||
puts("1");
|
||||
return 0;
|
||||
}
|
||||
//结果矩阵初始化
|
||||
memset(base, 0, sizeof base);
|
||||
base[1][1] = base[1][2] = base[2][1] = 1;
|
||||
//快速幂
|
||||
qmi(n - 2);
|
||||
//结果
|
||||
cout << (res[1][1]) % MOD << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user