結果
| 問題 | No.891 隣接3項間の漸化式 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-09-21 14:48:34 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 5,305 bytes |
| 記録 | |
| コンパイル時間 | 1,562 ms |
| コンパイル使用メモリ | 175,556 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-18 22:34:13 |
| 合計ジャッジ時間 | 2,694 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 39 |
ソースコード
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cout;
using std::endl;
using std::cin;
template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}
using namespace std;
template< int mod >
struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
ModInt &operator+=(const ModInt &p) {
if((x += p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if((x += mod - p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int) (1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while(b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return ModInt(u);
}
ModInt pow(int64_t n) const {
ModInt ret(1), mul(x);
while(n > 0) {
if(n & 1) ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const ModInt &p) {
return os << p.x;
}
friend istream &operator>>(istream &is, ModInt &a) {
int64_t t;
is >> t;
a = ModInt< mod >(t);
return (is);
}
static int get_mod() { return mod; }
};
using modint = ModInt<(int)(1e9 + 7)>;
template<class T>
class square_matrix {
using value_type = T;
using i64 = int64_t;
std::vector<std::vector<value_type>> data;
public:
square_matrix() {}
square_matrix(const size_t & n) : data(n, std::vector<value_type>(n, T())) {}
static const square_matrix E(const size_t & n) {
square_matrix e(n);
for(size_t i = 0; i < n; i++) e[i][i] = 1;
return e;
}
static const square_matrix O(const size_t & n) {
return square_matrix(n);
}
const size_t height() const {
return data.size();
}
const size_t width() const {
return data.size();
}
const T determinant() const {
square_matrix B(*this);
T ret = 1;
for(int i = 0; i < width(); i++) {
int ind = -1;
for(int j = i; j < width(); j++) {
if(B[j][i] == 0) continue;
ind = j;
}
if(ind == -1) return 0;
if(i != ind) {
ret *= -1;
std::swap(B[i], B[ind]);
}
ret *= B[i][i];
for(int j = 0; j < width(); j++) B[i][j] /= B[i][i];
for(int j = i + 1; j < width(); j++)
for(int k = 0; k < width(); k++)
B[j][k] -= B[i][k] * B[j][i];
}
return ret;
}
const std::vector<value_type> & operator[](const size_t & k) const {
return data.at(k);
}
std::vector<value_type> & operator[](const size_t & k) {
return data.at(k);
}
square_matrix & operator+=(const square_matrix & B) {
assert(height() == B.height());
for(int i = 0; i < height(); i++)
for(int j = 0; j < width(); j++)
(*this)[i][j] += B[i][j];
return (*this);
}
square_matrix & operator-=(const square_matrix & B) {
assert(height() == B.height());
for(int i = 0; i < height(); i++)
for(int j = 0; j < width(); j++)
(*this)[i][j] -= B[i][j];
return (*this);
}
square_matrix & operator*=(const square_matrix & B) {
assert(height() == B.height());
auto C = square_matrix::O(height());
for(int i = 0; i < height(); i++)
for(int j = 0; j < width(); j++)
for(int k = 0; k < height(); k++)
C[i][j] = (C[i][j] + (*this)[i][k] * B[k][j]);
return (*this) = C;
}
square_matrix & operator^=(i64 k) {
auto B = square_matrix::E(height());
while(k) {
if(k & 1) B *= (*this);
(*this) *= (*this);
k >>= 1;
}
return (*this) = B;
}
square_matrix & operator=(const square_matrix & B) {
(*this).data = B.data;
return (*this);
}
const square_matrix operator+(const square_matrix & B) const {
return (square_matrix(*this) += B);
}
const square_matrix operator-(const square_matrix & B) const {
return (square_matrix(*this) -= B);
}
const square_matrix operator*(const square_matrix & B) const {
return (square_matrix(*this) *= B);
}
const square_matrix operator^(const i64 & k) const {
return (square_matrix(*this) ^= k);
}
const bool operator==(const square_matrix & B) const {
return (data == B.data);
}
friend std::ostream & operator<<(std::ostream & os, square_matrix & p) {
for(int i = 0; i < height(); i++) {
os << "[";
for(int j = 0; j < width(); j++) {
os << p[i][j] << (j + 1 == width() ? "]\n" : ", ");
}
}
return os;
}
};
int main() {
int a, b, n; scanf("%d%d%d", &a, &b, &n);
if(n == 0) {
printf("0\n");
return 0;
}
square_matrix<modint> A(2);
A[0][0] = a; A[0][1] = b; A[1][0] = 1;
A ^= n - 1;
printf("%d\n", A[0][0]);
return 0;
}