結果

問題 No.2156 ぞい文字列
ユーザー tnakao0123tnakao0123
提出日時 2022-12-10 18:19:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,148 bytes
コンパイル時間 714 ms
コンパイル使用メモリ 46,420 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 00:40:51
合計ジャッジ時間 1,512 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2156.cc:  No.2156 ぞい文字列 - yukicoder
 */

#include<cstdio>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 100;
const int M = 2;
const int MOD = 998244353;

/* typedef */

typedef long long ll;

template<const int MOD>
struct MI {
  int v;
  MI(): v() {}
  MI(int _v): v(_v % MOD) {}
  MI(long long _v): v(_v % MOD) {}

  MI operator+(const MI m) const { return MI(v + m.v); }
  MI operator-(const MI m) const { return MI(v + MOD - m.v); }
  MI operator*(const MI m) const { return MI((long long)v * m.v); }

  MI &operator+=(const MI m) { return (*this = *this + m); }
  MI &operator-=(const MI m) { return (*this = *this - m); }
  MI &operator*=(const MI m) { return (*this = *this * m); }

  bool operator==(const MI m) const { return v == m.v; }
  bool operator!=(const MI m) const { return v != m.v; }

  MI pow(int n) const {  // a^n % MOD
    MI pm = 1, a = *this;
    while (n > 0) {
      if (n & 1) pm *= a;
      a *= a;
      n >>= 1;
    }
    return pm;
  }

  MI inv() const { return pow(MOD - 2); }
  MI operator/(const MI m) const { return *this * m.inv(); }
  MI &operator/=(const MI m) { return (*this = *this / m); }
};

typedef MI<MOD> mi;

template <typename T, const int M>
struct Mat {
  T v[M][M];
  Mat(): v() {}

  void clear() { for (int i = 0; i < M; i++) fill(v[i], v[i] + M, 0); }
  void unit() {
    clear();
    for (int i = 0; i < M; i++) v[i][i] = 1;
  }
  
  Mat operator*(const Mat &m0) const {
    Mat r;
    for (int i = 0; i < M; i++)
      for (int j = 0; j < M; j++) {
        r.v[i][j] = 0;
        for (int k = 0; k < M; k++) r.v[i][j] += v[i][k] * m0.v[k][j];
    }
    return r;
  }

  Mat pow(ll e) const {
    Mat r, m0 = *this;
    r.unit();

    while (e > 0) {
      if (e & 1) r = r * m0;
      m0 = m0 * m0;
      e >>= 1;
    }
    return r;
  }
};

/* global variables */

Mat<mi,M> ma, mb;

/* subroutines */

/* main */

int main() {
  ll n;
  scanf("%lld", &n);

  ma.v[0][0] = 1, ma.v[0][1] = 1;
  ma.v[1][0] = 1, ma.v[1][1] = 0;

  mb = ma.pow(n - 1);

  printf("%d\n", (mb.v[0][0] + mb.v[1][0] - 1).v);
  return 0;
}
0