結果

問題 No.1136 Four Points Tour
ユーザー tkmst201tkmst201
提出日時 2021-01-16 15:48:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 10,182 bytes
コンパイル時間 2,524 ms
コンパイル使用メモリ 212,952 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-05 18:39:58
合計ジャッジ時間 3,201 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
01_Sample03_evil.txt AC 1 ms
5,376 KB
04_Rnd_large_evil1.txt AC 2 ms
5,376 KB
04_Rnd_large_evil2.txt AC 2 ms
5,376 KB
04_Rnd_large_evil3.txt AC 1 ms
5,376 KB
04_Rnd_large_evil4.txt AC 2 ms
5,376 KB
04_Rnd_large_evil5.txt AC 1 ms
5,376 KB
04_Rnd_large_evil6.txt AC 1 ms
5,376 KB
04_Rnd_large_evil7.txt AC 2 ms
5,376 KB
04_Rnd_large_evil8.txt AC 1 ms
5,376 KB
04_Rnd_large_evil9.txt AC 2 ms
5,376 KB
04_Rnd_large_evil10.txt AC 2 ms
5,376 KB
05_Rnd_huge_evil1.txt AC 1 ms
5,376 KB
05_Rnd_huge_evil2.txt AC 1 ms
5,376 KB
05_Rnd_huge_evil3.txt AC 2 ms
5,376 KB
05_Rnd_huge_evil4.txt AC 2 ms
5,376 KB
05_Rnd_huge_evil5.txt AC 1 ms
5,376 KB
05_Rnd_huge_evil6.txt AC 1 ms
5,376 KB
05_Rnd_huge_evil7.txt AC 1 ms
5,376 KB
99_evil_01.txt AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = false;
//---------------------------------//

template<int M>
struct ModInt {
	static_assert(M > 0);
	
public:
	using value_type = int;
	using calc_type = std::int_fast64_t;
	
private:
	value_type val_;
	
public:
	constexpr ModInt(calc_type val = 0) : val_(val < 0 ? (val % M + M) % M : val % M) {}
	constexpr const value_type & val() const noexcept { return val_; }
	constexpr static decltype(M) mod() noexcept { return M; }
	
	explicit constexpr operator bool() const noexcept { return val_; }
	constexpr bool operator !() const noexcept { return !static_cast<bool>(*this); }
	constexpr ModInt operator +() const noexcept { return ModInt(*this); }
	constexpr ModInt operator -() const noexcept { return ModInt(-val_); }
	constexpr ModInt operator ++(int) noexcept { ModInt res = *this; ++*this; return res; }
	constexpr ModInt operator --(int) noexcept { ModInt res = *this; --*this; return res; }
	constexpr ModInt & operator ++() noexcept { val_ = val_ + 1 == M ? 0 : val_ + 1; return *this; }
	constexpr ModInt & operator --() noexcept { val_ = val_ == 0 ? M - 1 : val_ - 1; return *this; }
	constexpr ModInt & operator +=(const ModInt & rhs) noexcept { val_ += val_ < M - rhs.val_ ? rhs.val_ : rhs.val_ - M; return *this; }
	constexpr ModInt & operator -=(const ModInt & rhs) noexcept { val_ += val_ >= rhs.val_ ? -rhs.val_ : M - rhs.val_; return *this; }
	constexpr ModInt & operator *=(const ModInt & rhs) noexcept { val_ = static_cast<calc_type>(val_) * rhs.val_ % M; return *this; }
	constexpr ModInt & operator /=(const ModInt & rhs) noexcept { return *this *= rhs.inv(); }
	friend constexpr ModInt operator +(const ModInt & lhs, const ModInt & rhs) noexcept { return ModInt(lhs) += rhs; }
	friend constexpr ModInt operator -(const ModInt & lhs, const ModInt & rhs) noexcept { return ModInt(lhs) -= rhs; }
	friend constexpr ModInt operator *(const ModInt & lhs, const ModInt & rhs) noexcept { return ModInt(lhs) *= rhs; }
	friend constexpr ModInt operator /(const ModInt & lhs, const ModInt & rhs) noexcept { return ModInt(lhs) /= rhs; }
	friend constexpr bool operator ==(const ModInt & lhs, const ModInt & rhs) noexcept { return lhs.val_ == rhs.val_; }
	friend constexpr bool operator !=(const ModInt & lhs, const ModInt & rhs) noexcept { return !(lhs == rhs); }
	friend std::ostream & operator <<(std::ostream & os, const ModInt & rhs) { return os << rhs.val_; }
	friend std::istream & operator >>(std::istream & is, ModInt & rhs) { calc_type x; is >> x; rhs = ModInt(x); return is; }
	
	constexpr ModInt pow(calc_type n) const noexcept {
		ModInt res = 1, x = val_;
		if (n < 0) { x = x.inv(); n = -n; }
		while (n) { if (n & 1) res *= x; x *= x; n >>= 1; }
		return res;
	}
	
	constexpr ModInt inv() const noexcept {
		value_type a = val_, a1 = 1, a2 = 0, b = M, b1 = 0, b2 = 1;
		while (b > 0) {
			value_type q = a / b, r = a % b;
			value_type nb1 = a1 - q * b1, nb2 = a2 - q * b2;
			a = b; b = r;
			a1 = b1; b1 = nb1;
			a2 = b2; b2 = nb2;
		}
		assert(a == 1);
		return a1;
	}
};
using mint = ModInt<MOD>;

template<typename T>
struct Matrix {
public:
	using value_type = T;
	using size_type = std::size_t;
	
	Matrix() {}
	Matrix(size_type h, size_type w, const value_type & x = 0) : h(h), w(w), val(h, std::vector<value_type>(w, x)) {
		assert(h > 0 && w > 0);
	}
	Matrix(std::vector<std::vector<value_type>> val) : h(val.size()), w(val.size() ? val[0].size() : 0), val(val) {
		assert(h > 0 && w > 0);
		for (size_type i = 1; i < h; ++i) assert(val[i].size() == w);
	}
	Matrix(std::initializer_list<std::vector<value_type>> init) : val(init.begin(), init.end()) {
		h = val.size();
		w = val.size() ? val[0].size() : 0;
		assert(h > 0 && w > 0);
		for (size_type i = 1; i < h; ++i) assert(val[i].size() == w);
	}
	
	std::vector<value_type> & operator [](size_type i) noexcept { return val[i]; }
	const std::vector<value_type> & operator [](size_type i) const noexcept { return val[i]; };
	value_type & operator ()(size_type i, size_type j) noexcept { return val[i][j]; };
	const value_type & operator ()(size_type i, size_type j) const noexcept { return val[i][j]; }
	value_type & at(size_type i, size_type j) {
		assert(i < h && j < w);
		return val[i][j];
	}
	const value_type & at(size_type i, size_type j) const {
		assert(i < h & j < w);
		return val[i][j];
	}
	
	bool empty() const { return !(h || w); }
	std::pair<size_type, size_type> type() const { return std::make_pair(h, w); }
	bool match_type(const Matrix & rhs) const noexcept { return h == rhs.h && w == rhs.w; }
	bool is_square() const { return h == w; }
	const std::vector<std::vector<value_type>> & get() const noexcept { return val; }
	
	bool operator ==(const Matrix & rhs) const noexcept { return match_type(rhs) && val == rhs.val; }
	bool operator !=(const Matrix & rhs) const noexcept { return !(*this == rhs); }
	Matrix operator +() const { return Matrix(*this); }
	Matrix operator -() const { return Matrix(h, w) - Matrix(*this); }
	Matrix operator +(const Matrix & rhs) const { return Matrix(*this) += rhs; }
	Matrix operator -(const Matrix & rhs) const { return Matrix(*this) -= rhs; }
	Matrix operator *(const Matrix & rhs) const {
		assert(w == rhs.h);
		Matrix res(h, rhs.w);
		for (size_type i = 0; i < h; ++i) for (size_type j = 0; j < rhs.w; ++j) for (size_type k = 0; k < w; ++k)
			res.val[i][j] += val[i][k] * rhs.val[k][j];
		return res;
	}
	Matrix operator /(const Matrix & rhs) const { return Matrix(*this) /= rhs; }
	friend Matrix operator *(const value_type & lhs, const Matrix & rhs) {
		Matrix res(rhs.val);
		for (size_type i = 0; i < res.h; ++i) for (size_type j = 0; j < res.w; ++j)
			res.val[i][j] = lhs * res.val[i][j];
		return res;
	}
	Matrix operator *(const value_type & rhs) const {
		Matrix res(val);
		for (size_type i = 0; i < h; ++i) for (size_type j = 0; j < w; ++j)
			res.val[i][j] *= rhs;
		return res;
	}
	Matrix operator /(const value_type & rhs) const {
		Matrix res(val);
		for (size_type i = 0; i < h; ++i) for (size_type j = 0; j < w; ++j)
			res.val[i][j] /= rhs;
		return res;
	}
	Matrix & operator +=(const Matrix & rhs) {
		assert(match_type(rhs));
		for (size_type i = 0; i < h; ++i) for (size_type j = 0; j < w; ++j)
			val[i][j] += rhs.val[i][j];
		return *this;
	}
	Matrix & operator -=(const Matrix & rhs) {
		assert(match_type(rhs));
		for (size_type i = 0; i < h; ++i) for(size_type j = 0; j < w; ++j)
			val[i][j] -= rhs.val[i][j];
		return *this;
	}
	Matrix & operator *=(const Matrix & rhs) {
		*this = *this * rhs;
		return *this;
	}
	Matrix & operator /=(const Matrix & rhs) {
		*this *= rhs.inverse();
		return *this;
	}
	
	Matrix pow(long long n) const {
		Matrix res = identity(h), x(*this);
		if (n < 0) { x = x.inverse(); n = -n; }
		while (n) { if (n & 1) res *= x; x *= x; n >>= 1; }
		return res;
	}
	
	Matrix trans() const {
		Matrix res(w, h);
		for (size_type i = 0; i < h; ++i) for (size_type j = 0; j < w; ++j)
			res.val[j][i] = val[i][j];
		return res;
	}
	
	Matrix inverse() const {
		assert(is_square());
		Matrix aug_mat = this->hstack(identity(h));
		if (aug_mat.gauss_jordan().first != h) return Matrix();
		return aug_mat.submat(0, w, h, 2 * w);
	}
	
	Matrix vstack(const Matrix & A) const {
		assert(w == A.w);
		Matrix res(h + A.h, w);
		std::copy(val.begin(), val.end(), res.val.begin());
		std::copy(A.val.begin(), A.val.end(), res.val.begin() + h);
		return res;
	}
	
	Matrix hstack(const Matrix & A) const {
		assert(h == A.h);
		Matrix res(h, w + A.w);
		for (int i = 0; i < h; ++i) {
			std::copy(val[i].begin(), val[i].end(), res.val[i].begin());
			std::copy(A.val[i].begin(), A.val[i].end(), res.val[i].begin() + w);
		}
		return res;
	}
	
	Matrix submat(size_type i1, size_type j1, size_type i2, size_type j2) const {
		assert(i1 < i2 && j1 < j2 && i2 <= h && j2 <= w);
		Matrix res(i2 - i1, j2 - j1);
		for (size_type i = 0; i < i2 - i1; ++i)
			std::copy(val[i + i1].begin() + j1, val[i + i1].begin() + j2, res.val[i].begin());
		return res;
	}
	
	static Matrix identity(size_type n) {
		Matrix res(n, n);
		for (size_type i = 0; i < n; ++i) res(i, i) = 1;
		return res;
	}
	
	std::pair<size_type, value_type> gauss_jordan(size_type colnum = -1) {
		if (colnum == -1) colnum = w;
		size_type rank = 0;
		value_type det {};
		bool done = false, rflag = false;
		
		for (size_type k = 0; k < colnum; ++k) {
			size_type pivot = -1;
			for (size_type i = rank; i < h; ++i) {
				if (val[i][k] != 0) {
					pivot = i;
					break;
				}
			}
			if (pivot == -1) continue;
			if (pivot != rank) {
				rflag ^= 1;
				std::swap(val[rank], val[pivot]);
			}
			
			if (!done) { det = val[rank][k]; done = true; }
			else det *= val[rank][k];
			
			value_type div = static_cast<value_type>(1) / val[rank][k];
			for (size_type j = k; j < w; ++j) val[rank][j] *= div;
			
			for (size_type i = 0; i < h; ++i) if (i != rank) {
				for (size_type j = k + 1; j < w; ++j) val[i][j] -= val[rank][j] * val[i][k];
					val[i][k] = 0;
			}
			++rank;
		}
		
		if (!is_square() || rank != h) det = 0;
		else if (rflag) det *= -1;
		
		return {rank, det};
	}
	
	friend std::ostream & operator <<(std::ostream & os, const Matrix & rhs) {
		os << "type = (" << rhs.h << "," << rhs.w << ") [\n";
		for (size_type i = 0; i < rhs.h; ++i) for (size_type j = 0; j < rhs.w; ++j)
			os << (j == 0 ? " " : "") << rhs.val[i][j] << (j + 1 == rhs.w ? '\n' : ' ');
		return os << "]";
	}
	
private:
	size_type h, w;
	std::vector<std::vector<value_type>> val;
};

int main() {
	ll N;
	cin >> N;
	
	Matrix<mint> mat(4, 4);
	REP(i, 4) REP(j ,4) mat[i][j] = i != j;
	Matrix<mint> b(4, 1);
	b[0][0] = 1;
	
	mint ans = (mat.pow(N) * b)[0][0];
	cout << ans << endl;
}
0