結果
| 問題 |
No.891 隣接3項間の漸化式
|
| コンテスト | |
| ユーザー |
Manuel1024
|
| 提出日時 | 2021-03-21 19:18:20 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 3,577 bytes |
| コンパイル時間 | 1,054 ms |
| コンパイル使用メモリ | 78,632 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-11-22 13:53:30 |
| 合計ジャッジ時間 | 2,486 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 39 |
ソースコード
/*
行列ライブラリ
できること
* 行列同士の加算,減算,乗算
* 行列累乗
行列の宣言
Matrix<int> hoge(行数, 列数);
Matrix<int> hoge({
{1, 2, 3}//行列の初期状態
});
行列累乗 matのx乗(MOD 1000000007)を計算する
Matrix<ll> res = modpow(mat, x, 1000000007);
*/
#include <iostream>
#include <vector>
using namespace std;
using ll = long long int;
template<typename T>
class Matrix{
private:
vector<vector<T>> mat;
int row;
int column;
public:
Matrix(int r = 1, int c = 1){
this->row = r;
this->column = c;
this->mat = vector<vector<T>>(this->row, vector<T>(this->column, 0));
}
Matrix(vector<vector<T>> initmat){
this->mat = initmat;
this->row = this->mat.size();
this->column = this->mat[0].size();
}
vector<T> &operator[](const int rowind){
return mat[rowind];
}
vector<T> operator[](const int rowind) const {
return mat[rowind];
}
const int getrow() const {
return this->row;
}
const int getcol() const {
return this->column;
}
Matrix operator+(const Matrix &other) const {
if(this->row != other.getrow() || this->column != other.getcol()){
cerr << "Matrix calc error(add)" << endl;
abort();
}
Matrix ans(this->row, this->column);
for(int i = 0; i < this->row; i++){
for(int j = 0; j < this->column; j++) ans[i][j] = this->mat[i][j] + other[i][j];
}
return ans;
}
Matrix operator-(const Matrix &other) const {
if(this->row != other.getrow() || this->column != other.getcol()){
cerr << "Matrix calc error(sub)" << endl;
abort();
}
Matrix ans(this->row, this->column);
for(int i = 0; i < this->row; i++){
for(int j = 0; j < this->column; j++) ans[i][j] = this->mat[i][j] - other[i][j];
}
return ans;
}
Matrix operator*(const Matrix &other) const {
if(this->column != other.getrow()){
cerr << "Matrix calc error(mult)" << endl;
abort();
}
Matrix ans(this->row, other.getcol());
for(int i = 0; i < this->row; i++){
for(int j = 0; j < other.getcol(); j++){
for(int k = 0; k < this->column; k++) ans[i][j] += this->mat[i][k] * other[k][j];
}
}
return ans;
}
};
template<typename T>
Matrix<T> modpow(Matrix<T> a, ll x, ll mod){
if(a.getrow() != a.getcol()){
cerr << "Matrix calc error(pow)" << endl;
abort();
}
Matrix<T> ans(a.getrow(), a.getcol());
for(int i = 0; i < ans.getrow(); i++) ans[i][i] = 1;
auto modmult = [](const Matrix<T> &a, const Matrix<T> &b, const ll mod){
Matrix<T> res(a.getrow(), b.getcol());
for(int i = 0; i < a.getrow(); i++){
for(int j = 0; j < b.getcol(); j++){
for(int k = 0; k < a.getcol(); k++){
res[i][j] += a[i][k] * b[k][j];
res[i][j] %= mod;
}
}
}
return res;
};
while(x > 0){
if(x & 1){
ans = modmult(ans, a, mod);
}
a = modmult(a, a, mod);
x >>= 1;
}
return ans;
}
int main(){
ll a, b, n;
cin >> a >> b >> n;
Matrix<ll> mat({
{a, b},
{1, 0},
});
mat = modpow(mat, n, 1000000007);
cout << mat[1][0] << endl;
return 0;
}
Manuel1024