답안 #8929

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
8929 2014-09-26T08:25:15 Z ekzm0204 Phibonacci (kriii2_P) C++
4 / 4
0 ms 1088 KB
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
#define pr pair
#define mp make_pair

typedef long long LL;
const LL MOD = 1000000007ll;
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 Power(LL a, LL b) {
	if (b == 0) return 1;
	LL half = Power(a, b/2);
	return half * half % MOD * ((b%2) ? (a) : (1)) % 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);
	if (fk == 0) {
		A = (n * Power(fk_1, n-1)) % MOD;
	}
	LL B = ((fnk_1 - A*fk_1) % MOD + MOD) % MOD;
	
	printf("%lld %lld\n", A, B);
	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1088 KB Output is correct
2 Correct 0 ms 1088 KB Output is correct
3 Correct 0 ms 1088 KB Output is correct
4 Correct 0 ms 1088 KB Output is correct
5 Correct 0 ms 1088 KB Output is correct
6 Correct 0 ms 1088 KB Output is correct
7 Correct 0 ms 1088 KB Output is correct
8 Correct 0 ms 1088 KB Output is correct
9 Correct 0 ms 1088 KB Output is correct
10 Correct 0 ms 1088 KB Output is correct
11 Correct 0 ms 1088 KB Output is correct
12 Correct 0 ms 1088 KB Output is correct
13 Correct 0 ms 1088 KB Output is correct
14 Correct 0 ms 1088 KB Output is correct
15 Correct 0 ms 1088 KB Output is correct
16 Correct 0 ms 1088 KB Output is correct
17 Correct 0 ms 1088 KB Output is correct
18 Correct 0 ms 1088 KB Output is correct
19 Correct 0 ms 1088 KB Output is correct
20 Correct 0 ms 1088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1088 KB Output is correct
2 Correct 0 ms 1088 KB Output is correct
3 Correct 0 ms 1088 KB Output is correct
4 Correct 0 ms 1088 KB Output is correct
5 Correct 0 ms 1088 KB Output is correct
6 Correct 0 ms 1088 KB Output is correct
7 Correct 0 ms 1088 KB Output is correct
8 Correct 0 ms 1088 KB Output is correct
9 Correct 0 ms 1088 KB Output is correct
10 Correct 0 ms 1088 KB Output is correct
11 Correct 0 ms 1088 KB Output is correct
12 Correct 0 ms 1088 KB Output is correct
13 Correct 0 ms 1088 KB Output is correct
14 Correct 0 ms 1088 KB Output is correct
15 Correct 0 ms 1088 KB Output is correct
16 Correct 0 ms 1088 KB Output is correct
17 Correct 0 ms 1088 KB Output is correct
18 Correct 0 ms 1088 KB Output is correct
19 Correct 0 ms 1088 KB Output is correct
20 Correct 0 ms 1088 KB Output is correct