Submission #9805

# Submission time Handle Problem Language Result Execution time Memory
9805 2014-09-28T09:22:10 Z veckal Phibonacci (kriii2_P) C++14
0 / 4
1000 ms 1084 KB
#include<stdio.h>
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 identity() {
	Matrix ret;
	ret.mat[0][0] = ret.mat[1][1] = 1;
	ret.mat[0][1] = ret.mat[1][0] = 0;
	return ret;
}

Matrix power(int exp) {
	Matrix ret, base;
	base.mat[0][0] = base.mat[0][1] = base.mat[1][0] = 1;
	ret.mat[0][0] = ret.mat[1][1] = 1;
	while(exp) {
		if (exp&1) ret = ret * base;
		base = base * base;
		exp >>= 1;
	}
	return ret;
}

int main() {
	scanf("%lld%lld", &n, &k);
	if (n == 1) {
		puts("1 0");
	} else if (n == 0) {
		puts("0 1");
	} else if (k == 1) {
		Matrix fib = power(n);
		printf("%lld %lld\n", fib.mat[0][1], fib.mat[1][1]);
	} else if (k == 2) {
		Matrix fib = power(n+n-1);
		ll ans2 = (MOD - fib.mat[1][1])%MOD;
		Matrix base;
		base.mat[0][0] = base.mat[0][1] = base.mat[1][0] = 1;
		fib = fib * base;
		ll ans1 = fib.mat[0][1];
		printf("%lld %lld\n", ans1, ans2);
	} else
		puts("-1");
	return 0;
}
# Verdict Execution time Memory Grader output
1 Execution timed out 1000 ms 1084 KB Program timed out
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Halted 0 ms 0 KB -