Submission #9335

#TimeUsernameProblemLanguageResultExecution timeMemory
9335coreaPhibonacci (kriii2_P)C++14
1 / 4
0 ms1676 KiB
#include <stdio.h>
#include <iostream>
#include <array>
#include <cstring>
using namespace std;

struct matrix {
	array< array<long, 2>, 2> v;
	matrix() { }
	matrix(array< array<long, 2>, 2> &s) {
		v = s;
	}
};

#define REP(i, n) for(int i=0; i < n; ++i)

const int MOD = 1000000007;

matrix operator * (const matrix &a, const matrix &b) {
	matrix c;
	c.v[0][0] = c.v[0][1] = c.v[1][0] = c.v[1][1] = 0;

	REP(k, 2) REP(i, 2) REP(j, 2) {
		c.v[i][j] += a.v[i][k] * b.v[k][j];
		c.v[i][j] %= MOD;
	}
	return c;
}

matrix I;

matrix power(const matrix &A, long long n) {
	if(n == 0) return I;
	if(n == 1) return A;

	if(n % 2 == 1) {
		return power(A, n - 1) * A;
	}
	else {
		matrix t = power(A, n / 2);
		return t * t;
	}
}

long long fibonacci(long long n) {

	matrix A;
	A.v[0][0] = 0;
	A.v[0][1] = A.v[1][0] = A.v[1][1] =1 ;

	matrix An = power(A, n);
	return An.v[0][1];
}

int main() {
	I.v[0][0] = I.v[1][1] = 1;
	I.v[0][1] = I.v[1][0] = 0;

	long long n, k;
	cin >> n >> k;

	long long x, y;
	x = fibonacci(n);
	y = fibonacci(n - 1);
	if(n == 0) y = 1;
	cout << x << ' ' << y << endl;

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...