Submission #9812

#TimeUsernameProblemLanguageResultExecution timeMemory
9812veckalPhibonacci (kriii2_P)C++14
1 / 4
0 ms1088 KiB
#include<stdio.h> typedef long long ll; const int MOD = 1000000000 + 7; ll n, k; struct Matrix { ll mat[2][2]; Matrix() { mat[0][0]=mat[0][1]=mat[1][0]=mat[1][1]=0; } }; Matrix operator *(Matrix& A, Matrix& B) { Matrix ret; for (int i=0; i<2; ++i) for (int j=0; j<2; ++j) for (int k=0; k<2; ++k) ret.mat[i][j] = (ret.mat[i][j] + A.mat[i][k] * B.mat[k][j])%MOD; return ret; } Matrix power(ll exp) { Matrix ret, base; base.mat[0][0] = base.mat[0][1] = base.mat[1][0] = 1; ret.mat[0][0] = ret.mat[1][1] = 1; while(exp) { if (exp&1) ret = ret * base; base = base * base; exp >>= 1; } return ret; } int main() { scanf("%lld%lld", &n, &k); if (n == 1) { puts("1 0"); } else if (n == 0) { puts("0 1"); } else if (k == 1) { Matrix fib = power(n); printf("%lld %lld\n", fib.mat[0][1], fib.mat[1][1]); } else if (k == 2) { Matrix fib = power(n+n-1); ll ans2 = (MOD - fib.mat[1][1])%MOD; Matrix base; base.mat[0][0] = base.mat[0][1] = base.mat[1][0] = 1; fib = fib * base; ll ans1 = fib.mat[0][1]; printf("%lld %lld\n", ans1, ans2); } else puts("-1"); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...