This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<cstdio>
#include<utility>
using namespace std;
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(Matrix base, ll exp) {
Matrix ret;
ret.mat[0][0] = ret.mat[1][1] = 1;
while(exp) {
if (exp&1) ret = ret * base;
base = base * base;
exp >>= 1;
}
return ret;
}
pair<long long, long long> extended_gcd(long long a, long long b) {
if (b == 0) return make_pair(1, 0);
pair<long long, long long> t = extended_gcd(b, a % b);
return make_pair(t.second, t.first - t.second * (a / b));
}
long long modinverse(long long a, long long m) {
return (extended_gcd(a, m).first % m + m) % m;
}
ll power(ll base, ll exp) {
if (exp < 0) return 0;
ll ret = 1;
while(exp) {
if (exp&1) ret = (ret*base)%MOD;
base = (base*base)%MOD;
exp >>= 1;
}
return ret;
}
int main() {
scanf("%lld%lld", &n, &k);
Matrix base;
base.mat[0][0] = base.mat[0][1] = base.mat[1][0] = 1;
Matrix a_k = power(base, k);
Matrix a_nk = power(a_k, n);
ll f_nk = a_nk.mat[0][1];
ll f_nk_1 = a_nk.mat[1][1];
ll f_k = a_k.mat[0][1];
ll f_k_1 = a_k.mat[1][1];
ll ans1, ans2;
if (f_k == 0) ans1 = n * power(f_k_1, n-1) % MOD;
else ans1 = f_nk * modinverse(f_k, MOD) % MOD;
ans2 = (f_nk_1 - f_k_1 * ans1 % MOD + MOD) % MOD;
printf("%lld %lld\n", ans1, ans2);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |