結果
| 問題 |
No.2156 ぞい文字列
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-02-05 13:06:07 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 3,248 bytes |
| コンパイル時間 | 4,589 ms |
| コンパイル使用メモリ | 243,852 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-04 03:03:18 |
| 合計ジャッジ時間 | 4,982 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
#include <bits/stdc++.h>
#include "atcoder/all"
#define debug cout << "OK" << endl;
template<typename T>
inline bool chmax(T& a, const T b) { if (a < b) { a = b; return true; } return false; }
template<typename T>
inline bool chmin(T& a, const T b) { if (a > b) { a = b; return true; } return false; }
inline int Code(char c) {
if ('A' <= c and c <= 'Z')return (int)(c - 'A');
if ('a' <= c and c <= 'z')return (int)(c - 'a');
if ('0' <= c and c <= '9')return (int)(c - '0');
assert(false);
}
using namespace std;
using namespace atcoder;
#define int long long
constexpr int MOD = 998244353;
constexpr int INF = (1LL << 62);
using minit = modint998244353;
template<typename T>
ostream& operator << (ostream& st, const vector<T> &v) {
for (const T value : v) {
st << value << " ";
}
return st;
}
template<typename T>
istream& operator >> (istream& st, vector<T>& v) {
for (T &value : v) {
st >> value;
}
return st;
}
class Matrix {
public:
int mod = MOD;
Matrix() {}
Matrix(int x) { mat.resize(x); }
Matrix(int x, int y) { mat.resize(x, vector<int>(y)); }
vector<vector<int>> mat;
void Set(int x, int y, int value) {
mat[x][y] = value;
return;
}
void operator = (const Matrix& m) {
mat = m.mat;
return;
}
void operator = (const vector<vector<int>>& v) {
mat = v;
return;
}
bool operator == (const Matrix& m) {
return mat == m.mat;
}
Matrix operator + (Matrix& m) {
assert(mat.size() == m.mat.size());
assert(mat[0].size() == m.mat[0].size());
Matrix m2(mat.size(), mat[0].size());
for (int i = 0; i < mat.size(); i++) {
for (int j = 0; j < mat[0].size(); j++) {
m2.mat[i][j] = mat[i][j] + m.mat[i][j];
m2.mat[i][j] %= mod;
}
}
return m2;
}
Matrix operator - (Matrix& m) {
assert(mat.size() == m.mat.size());
assert(mat[0].size() == m.mat[0].size());
Matrix m2(mat.size(), mat[0].size());
for (int i = 0; i < mat.size(); i++) {
for (int j = 0; j < mat[0].size(); j++) {
m2.mat[i][j] = mat[i][j] - m.mat[i][j];
m2.mat[i][j] += mod;
m2.mat[i][j] %= mod;
}
}
return m2;
}
Matrix operator * (Matrix& m) {
assert(mat[0].size() == m.mat.size());
Matrix m2(mat.size(), m.mat[0].size());
for (int i = 0; i < mat.size(); i++) {
for (int j = 0; j < m.mat[0].size(); j++) {
for (int k = 0; k < m.mat.size(); k++) {
m2.mat[i][j] += (mat[i][k] * m.mat[k][j]) % mod;
m2.mat[i][j] %= mod;
}
}
}
return m2;
}
Matrix pow(int a) {
bool inited = false;
Matrix ans;
Matrix base = (*this);
for (int i = 0; i < 63; i++) {
if (a & (1ll << i)) {
if (!inited) {
ans = base;
inited = true;
}
else ans = ans * base;
}
base = base * base;
}
return ans;
}
};
ostream& operator << (ostream& st, const Matrix& m) {
for (const vector<int> v : m.mat) {
for (const int a : v) {
cout << a << " ";
}
cout << endl;
}
return st;
}
void solve() {
Matrix base(2, 2);
base = { {1,1},{1,0} };
Matrix ot(2, 1);
vector<vector<int>> p = { {1},{1} };
ot = p;
int N;
cin >> N;
if (N == 1) {
cout << 0 << endl;
return;
}
Matrix ans = base.pow(N) * ot;
cout << ans.mat[1][0] - 1 << endl;
}
signed main() {
int T = 1;
// cin >> T;
for (int i = 0; i < T; i++)
solve();
return 0;
}