fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int mod = 998244353;
  4.  
  5. // ------------ Modulo Class ------------ //
  6. template<unsigned mod>
  7. class modulo {
  8. private:
  9. unsigned x;
  10. public:
  11. modulo() : x(0) {};
  12. modulo(unsigned x_) : x(x_) {};
  13. operator unsigned() { return x; }
  14. modulo operator==(const modulo& m) const { return x == m.x; }
  15. modulo operator!=(const modulo& m) const { return x != m.x; }
  16. modulo& operator+=(const modulo& m) { x = (x + m.x >= mod ? x + m.x - mod : x + m.x); return *this; }
  17. modulo& operator-=(const modulo& m) { x = (x < m.x ? x - m.x + mod : x - m.x); return *this; }
  18. modulo& operator*=(const modulo& m) { x = 1ULL * x * m.x % mod; return *this; }
  19. modulo operator+(const modulo& m) const { return modulo(*this) += m; }
  20. modulo operator-(const modulo& m) const { return modulo(*this) -= m; }
  21. modulo operator*(const modulo& m) const { return modulo(*this) *= m; }
  22. };
  23.  
  24. // ------------ Matrix Functions ------------ //
  25. typedef std::vector<modulo<mod> > matrix_base;
  26. typedef std::vector<matrix_base> matrix;
  27. matrix mul(const matrix& a, const matrix& b) {
  28. assert(a[0].size() == b.size());
  29. matrix ret(a.size(), matrix_base(b[0].size(), 0));
  30. for (int i = 0; i < a.size(); i++) {
  31. for (int j = 0; j < b[0].size(); j++) {
  32. for (int k = 0; k < b.size(); k++) ret[i][j] += a[i][k] * b[k][j];
  33. }
  34. }
  35. return ret;
  36. }
  37. matrix unit(int n) {
  38. matrix ret(n, matrix_base(n, 0));
  39. for (int i = 0; i < n; i++) ret[i][i] = 1;
  40. return ret;
  41. }
  42. matrix power(const matrix& a, long long b) {
  43. assert(a.size() == a[0].size());
  44. matrix f = a, ret = unit(a.size());
  45. while (b) {
  46. if (b & 1) ret = mul(ret, f);
  47. f = mul(f, f);
  48. b >>= 1;
  49. }
  50. return ret;
  51. }
  52. int nth_fibonacci(long long x) {
  53. matrix e(2, matrix_base(2));
  54. e[0][0] = e[0][1] = e[1][0] = 1;
  55. matrix p(2, matrix_base(1));
  56. p[0][0] = 1; p[1][0] = 0;
  57. return mul(power(e, x), p)[1][0];
  58. }
  59. int n; long long m;
  60. int main() {
  61. cin >> n >> m;
  62. if(n > 3) return 0;
  63. int ret = nth_fibonacci(m + (n - 1) * 2);
  64. if(n == 2) ret = (ret - 1 + mod) % mod;
  65. if(n == 3) ret = (ret - (m + 3) % mod + mod) % mod;
  66. cout << ret << endl;
  67. return 0;
  68. }
Success #stdin #stdout 0s 3476KB
stdin
3 20
stdout
46345