結果

問題 No.891 隣接3項間の漸化式
ユーザー tkmst201
提出日時 2020-02-26 23:14:25
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 9,081 bytes
コンパイル時間 2,025 ms
コンパイル使用メモリ 180,088 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-10-13 15:34:15
合計ジャッジ時間 3,129 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31 WA * 8
権限があれば一括ダウンロードができます

ソースコード

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)
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
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 = 0;
//---------------------------------//
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() != 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;
}
size_type gauss_jordan(size_type colnum = -1) {
if (colnum == -1) colnum = w;
size_type rank = 0;
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) std::swap(val[rank], val[pivot]);
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;
}
return rank;
}
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;
};
template<int M>
struct ModInt {
public:
using value_type = long long;
ModInt(value_type val = 0) : val(val < 0 ? (M - (-val % M)) % M : val % M) {}
explicit operator bool() const noexcept { return val; }
bool operator ==(const ModInt & rhs) const noexcept { return val == rhs.val; }
bool operator !=(const ModInt & rhs) const noexcept { return !(*this == rhs); }
ModInt operator +() const noexcept { return ModInt(*this); }
ModInt operator -() const noexcept { return ModInt(0) -= *this; }
ModInt operator +(const ModInt & rhs) const noexcept { return ModInt(*this) += rhs; }
ModInt operator -(const ModInt & rhs) const noexcept { return ModInt(*this) -= rhs; }
ModInt operator *(const ModInt & rhs) const noexcept { return ModInt(*this) *= rhs; }
ModInt operator /(const ModInt & rhs) const noexcept { return ModInt(*this) /= rhs; }
ModInt & operator +=(const ModInt & rhs) noexcept {
val += rhs.val;
if (val >= M) val -= M;
return *this;
}
ModInt & operator -=(const ModInt & rhs) noexcept {
if (val < rhs.val) val += M;
val -= rhs.val;
return *this;
}
ModInt & operator *=(const ModInt & rhs) noexcept {
val = val * rhs.val % M;
return *this;
}
ModInt & operator /=(const ModInt & rhs) noexcept {
*this *= rhs.inverse();
return *this;
}
ModInt pow(value_type n) const {
ModInt res = 1, x = val;
if (n < 0) { x = x.inverse(); n = -n; }
while (n) { if (n & 1) res *= x; x *= x; n >>= 1; }
return res;
}
ModInt inverse() const {
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;
}
const value_type & get() const noexcept { return val; }
static decltype(M) get_mod() noexcept { return M; }
friend std::ostream & operator <<(std::ostream & os, const ModInt & rhs) { return os << rhs.val; }
friend std::istream & operator >>(std::istream & is, ModInt & rhs) {
value_type x;
is >> x;
rhs = ModInt(x);
return is;
}
private:
value_type val;
};
using mint = ModInt<MOD>;
int main() {
ll a, b, n;
cin >> a >> b >> n;
mint ans;
if (n <= 1) ans = n;
else {
Matrix<mint> A{{a, b}, {1, 0}};
Matrix<mint> b{{1}, {0}};
cout << (A.pow(n - 1) * b)[0][0] << endl;
}
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0