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 <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
#define pr pair
#define mp make_pair
typedef long long LL;
const LL MOD = 1000000007;
class MAT2x2 {
public:
LL dat[2][2];
};
MAT2x2 mult(MAT2x2 a, MAT2x2 b) {
MAT2x2 r;
for (int i=0;i<2;i++) for (int j=0;j<2;j++) r.dat[i][j] = 0;
for (int i=0;i<2;i++) {
for (int j=0;j<2;j++) {
for (int k=0;k<2;k++) {
r.dat[i][j] = (r.dat[i][j] + a.dat[i][k] * b.dat[k][j]) % MOD;
}
}
}
return r;
}
MAT2x2 A, I;
void init() {
A.dat[0][0] = 1;
A.dat[0][1] = 1;
A.dat[1][0] = 1;
A.dat[1][1] = 0;
I.dat[0][0] = 1;
I.dat[0][1] = 0;
I.dat[1][0] = 0;
I.dat[1][1] = 1;
}
MAT2x2 Power(MAT2x2 X, LL p) {
MAT2x2 r = I;
while( p > 0 ) {
if (p&1) {
r = mult(r, X);
}
X = mult(X, X);
p >>= 1;
}
return r;
}
pr<LL, LL> getXY(LL a, LL b) { //aX + bY = gcd(a,b)
if (b == 0) return mp(1ll, 0ll);
pr<LL, LL> t = getXY(b, a%b);
LL r = a/b;
return mp(t.second, t.first - t.second * r);
}
LL getRev(LL x) { // get r s.t r*x = 1 (mod MOD)
pr<LL, LL> r = getXY(x, MOD);
return r.first;
}
LL modDiv(LL a, LL b) { // a/b
LL rev = getRev(b);
rev = (rev % MOD + MOD) % MOD;
return (a * rev) % MOD;
}
LL n, k;
int main() {
init();
scanf("%lld %lld", &n, &k);
MAT2x2 K = Power(A, k);
MAT2x2 NK = Power(K, n);
LL fk = K.dat[0][1];
LL fk_1 = K.dat[1][1];
LL fnk = NK.dat[0][1];
LL fnk_1 = NK.dat[1][1];
LL A = modDiv(fnk, fk);
LL B = ((fnk_1 - A*fk_1) % MOD + MOD) % MOD;
printf("%lld %lld\n", A, B);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |