Submission #9335

# Submission time Handle Problem Language Result Execution time Memory
9335 2014-09-28T05:38:09 Z corea Phibonacci (kriii2_P) C++14
1 / 4
0 ms 1676 KB
#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 time Memory Grader output
1 Correct 0 ms 1676 KB Output is correct
2 Correct 0 ms 1676 KB Output is correct
3 Correct 0 ms 1676 KB Output is correct
4 Correct 0 ms 1676 KB Output is correct
5 Correct 0 ms 1676 KB Output is correct
6 Correct 0 ms 1676 KB Output is correct
7 Correct 0 ms 1676 KB Output is correct
8 Correct 0 ms 1676 KB Output is correct
9 Correct 0 ms 1676 KB Output is correct
10 Correct 0 ms 1676 KB Output is correct
11 Correct 0 ms 1676 KB Output is correct
12 Correct 0 ms 1676 KB Output is correct
13 Correct 0 ms 1676 KB Output is correct
14 Correct 0 ms 1676 KB Output is correct
15 Correct 0 ms 1676 KB Output is correct
16 Correct 0 ms 1676 KB Output is correct
17 Correct 0 ms 1676 KB Output is correct
18 Correct 0 ms 1676 KB Output is correct
19 Correct 0 ms 1676 KB Output is correct
20 Correct 0 ms 1676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 1676 KB Output isn't correct
2 Halted 0 ms 0 KB -