結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー square1001square1001
提出日時 2018-07-28 09:57:48
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,911 bytes
コンパイル時間 548 ms
コンパイル使用メモリ 65,956 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-19 06:00:10
合計ジャッジ時間 1,650 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;
const unsigned mod = 1000000007;
class matrix {
private:
	unsigned a, b, c, d;
public:
	matrix() : a(0), b(0), c(0), d(0) {};
	matrix(unsigned a_, unsigned b_, unsigned c_, unsigned d_) : a(a_), b(b_), c(c_), d(d_) {};
	matrix& operator+=(const matrix& m) {
		a = (a + m.a < mod ? a + m.a : a + m.a - mod);
		b = (b + m.b < mod ? b + m.b : b + m.b - mod);
		c = (c + m.c < mod ? c + m.c : c + m.c - mod);
		d = (d + m.d < mod ? d + m.d : d + m.d - mod);
		return *this;
	}
	matrix& operator-=(const matrix& m) {
		a = (a < m.a ? a + mod - m.a : a - m.a);
		b = (b < m.b ? b + mod - m.b : b - m.b);
		c = (c < m.c ? c + mod - m.c : c - m.c);
		d = (d < m.d ? d + mod - m.d : d - m.d);
		return *this;
	}
	matrix& operator*=(const matrix& m) {
		unsigned ta = (1ULL * a * m.a + 1ULL * b * m.c) % mod;
		unsigned tb = (1ULL * a * m.b + 1ULL * b * m.d) % mod;
		unsigned tc = (1ULL * c * m.a + 1ULL * d * m.c) % mod;
		unsigned td = (1ULL * c * m.b + 1ULL * d * m.d) % mod;
		a = ta, b = tb, c = tc, d = td;
		return *this;
	}
	matrix operator+(const matrix& m) const { return matrix(*this) += m; }
	matrix operator-(const matrix& m) const { return matrix(*this) -= m; }
	matrix operator*(const matrix& m) const { return matrix(*this) *= m; }
	unsigned entry(unsigned x, unsigned y) {
		if (x == 0 && y == 0) return a;
		if (x == 0 && y == 1) return b;
		if (x == 1 && y == 0) return c;
		if (x == 1 && y == 1) return d;
		return -1;
	}
};
const matrix unit(1, 0, 0, 1);
matrix powersum(matrix a, long long b) {
	// a^0 + a^1 + a^2 + ... + a^(b-1)
	if (b == 1) return unit;
	if (b & 1) return a * powersum(a, b - 1) + unit;
	return (unit + a) * powersum(a * a, b >> 1);
}
int main() {
	long long n;
	cin >> n;
	matrix ret = powersum(matrix(5, 3, 3, 2), (n >> 1) + 1);
	int ans = (n & 1 ? ret.entry(0, 0) : ret.entry(1, 1) + mod - 1) % mod;
	cout << ans << '\n';
	return 0;
}
0