結果
| 問題 |
No.2156 ぞい文字列
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-12-09 21:53:53 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 5,886 bytes |
| コンパイル時間 | 1,263 ms |
| コンパイル使用メモリ | 109,016 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-10-14 21:44:02 |
| 合計ジャッジ時間 | 2,042 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <climits>
#include <functional>
#include <cassert>
#include <numeric>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define per(i, n) for(int i = (n) - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
constexpr int mod = 998244353;
using namespace std;
template<class T, class U>
bool chmax(T &a, const U &b){ return a < b ? (a = b, 1) : 0; }
template<class T, class U>
bool chmin(T &a, const U &b){ return a > b ? (a = b, 1) : 0; }
template <int mod>
struct Mint {
ll x;
constexpr Mint(ll x = 0) : x((x + mod) % mod){}
static constexpr int get_mod(){ return mod; }
constexpr Mint operator-() const{ return Mint(-x); }
constexpr Mint operator+=(const Mint &a){
if((x += a.x) >= mod) x -= mod;
return *this;
}
constexpr Mint &operator++(){
if(++x == mod) x = 0;
return *this;
}
constexpr Mint operator++(int){
Mint temp = *this;
if(++x == mod) x = 0;
return temp;
}
constexpr Mint &operator-=(const Mint &a){
if((x -= a.x) < 0) x += mod;
return *this;
}
constexpr Mint &operator--(){
if(--x < 0) x += mod;
return *this;
}
constexpr Mint operator--(int){
Mint temp = *this;
if(--x < 0) x += mod;
return temp;
}
constexpr Mint &operator*=(const Mint &a){
(x *= a.x) %= mod;
return *this;
}
constexpr Mint operator+(const Mint &a) const{ return Mint(*this) += a; }
constexpr Mint operator-(const Mint &a) const{ return Mint(*this) -= a; }
constexpr Mint operator*(const Mint &a) const{ return Mint(*this) *= a; }
constexpr Mint pow(ll t) const{
if(!t) return 1;
Mint res = 1, v = *this;
while(t){
if(t & 1) res *= v;
v *= v;
t >>= 1;
}
return res;
}
constexpr Mint inv() const{ return pow(mod - 2); }
constexpr Mint &operator/=(const Mint &a){ return (*this) *= a.inv(); }
constexpr Mint operator/(const Mint &a) const{ return Mint(*this) /= a; }
constexpr bool operator==(const Mint &a) const{ return x == a.x; }
constexpr bool operator!=(const Mint &a) const{ return x != a.x; }
constexpr bool operator<(const Mint &a) const{ return x < a.x; }
constexpr bool operator>(const Mint &a) const{ return x > a.x; }
friend istream &operator>>(istream &is, Mint &a){ return is >> a.x; }
friend ostream &operator<<(ostream &os, const Mint &a){ return os << a.x; }
};
using mint = Mint<mod>;
template <class T>
struct matrix {
int n,m;
private:
vector<vector<T>> a;
public:
matrix(int n) : n(n), m(n), a(n, vector<T>(n)){}
matrix(int n, int m) : n(n), m(m), a(n, vector<T>(m)){}
matrix(const vector<vector<T>> &d) : a(d), n(d.size()), m(d[0].size()){}
vector<T> &operator[](const int &i){ return a[i]; }
matrix &operator*=(const matrix &b){
assert(m == b.n);
vector<vector<T>> c(n, vector<T>(b.m));
for(int i = 0; i < n; i++) for(int j = 0; j < m; j++)
for(int k = 0; k < b.m; k++){
c[i][k] += a[i][j] * b.a[j][k];
}
a = c;
return *this;
}
matrix &operator+=(const matrix &b){
assert(n == b.n && m == b.m);
for(int i = 0; i < n; i++) for(int j = 0; j < m; j++)
a[i][j] += b.a[i][j];
return *this;
}
matrix &operator-=(const matrix &b){
assert(n == b.n && m == b.m);
for(int i = 0; i < n; i++) for(int j = 0; j < m; j++)
a[i][j] -= b.a[i][j];
return *this;
}
matrix operator*(const matrix &b) const{ return matrix(*this) *= b; }
matrix operator+(const matrix &b) const{ return matrix(*this) += b; }
matrix operator-(const matrix &b) const{ return matrix(*this) -= b; }
matrix pow(ll t) const{
assert(n == m);
matrix<T> b(n), c = *this;
for(int i = 0; i < n; i++) b[i][i] = 1;
while(t > 0){
if(t & 1) b *= c;
c *= c;
t >>= 1;
}
return b;
}
T det() const{
assert(n == m);
matrix b = *this;
T res(1);
bool flip = false;
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
while(b[j][i] > 0){
swap(b[i], b[j]);
flip ^= 1;
const T d = b[j][i] / b[i][i];
for(int k = i; k < n; k++){
b[j][k] -= b[i][k] * d;
}
}
}
if(b[i][i] == 0) return 0;
res *= b[i][i];
}
if(flip) res = -res;
return res;
}
matrix inv(){
assert(n == m);
matrix b(n), c = *this;
for(int i = 0; i < n; i++) b[i][i] = 1;
int r = 0;
for(int i = 0; i < n && r < n; i++){
if(c[r][i] == 0){
T max_val = 0; int mx_pos;
for(int j = r+1; j < n; j++){
if(max_val < c[j][i]) max_val = c[j][i], mx_pos = j;
}
if(max_val == 0) return false;
swap(c[r], c[mx_pos]); swap(b[r], b[mx_pos]);
}
T d = T(1) / c[r][i];
for(int j = 0; j < n; j++) c[r][j] *= d, b[r][j] *= d;
for(int j = 0; j < n; j++){
T v = c[j][i];
if(j == r || c[j][i] == 0) continue;
for(int k = 0; k < n; k++){
c[j][k] -= c[r][k] * v;
b[j][k] -= b[r][k] * v;
}
}
r++;
}
return b;
}
int size() const{ return n; }
void debug(){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++) cerr << a[i][j] << " ";
cerr << "\n";
}
}
};
int main(){
ll n;
cin >> n;
matrix<mint> mat({ { 1, 1 }, { 1, 0 }, });
mat = mat.pow(n-1);
mat.debug();
cout << mat[0][0] + mat[1][0] - 1 << "\n";
}