QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#765980#9479. And DNAguosounTL 1013ms3720kbC++171.1kb2024-11-20 15:51:012024-11-20 15:51:02

Judging History

你现在查看的是最新测评结果

  • [2024-11-20 15:51:02]
  • 评测
  • 测评结果:TL
  • 用时:1013ms
  • 内存:3720kb
  • [2024-11-20 15:51:01]
  • 提交

answer

#include <bits/stdc++.h>
using ll = long long;
using matrix = std::array<std::array<int, 3>, 3>;

const int mod = 998244353;

matrix operator*(matrix a, matrix b) {
  matrix c{};
  for (int i : {0, 1, 2})
    for (int k : {0, 1, 2})
      for (int j : {0, 1, 2}) (c[i][j] += (ll)a[i][k] * b[k][j] % mod) %= mod;
  return c;
}

matrix qpow(matrix x, int y) {
  auto ret = (--y, x);
  for (; y; y >>= 1, x = x * x)
    if (y & 1) ret = ret * x;
  return ret;
}

int f(int n, int m) {
  if (m == 0) return 1;
  if (m == 1) {
    matrix a{};
    a[0][1] = 1;
    a[1][0] = 1;
    a[1][2] = 1;
    a[2][0] = 1;
    int ret = 0;
    auto b = qpow(a, n - 1);
    (ret += (b[0][1] + b[0][2]) % mod) %= mod;
    b = qpow(a, n - 2);
    (ret += ((b[0][1] + b[0][0]) % mod + b[2][0]) % mod) %= mod;
    return ret;
  }
  if (m & 1)
    return (ll)f(n, (m - 1) / 2) * f(n, 1) % mod;
  else
    return (f(n, m / 2) + f(n, m / 2 - 1)) % mod;
}

int main() {
  std::cin.tie(0)->sync_with_stdio(0);
  int n, m;
  std::cin >> n >> m;
  std::cout << f(n, m) << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3536kb

input:

3 2

output:

4

result:

ok "4"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3648kb

input:

3 0

output:

1

result:

ok "1"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3672kb

input:

100 100

output:

343406454

result:

ok "343406454"

Test #4:

score: 0
Accepted
time: 14ms
memory: 3640kb

input:

686579306 119540831

output:

260602195

result:

ok "260602195"

Test #5:

score: 0
Accepted
time: 405ms
memory: 3596kb

input:

26855095 796233790

output:

492736984

result:

ok "492736984"

Test #6:

score: 0
Accepted
time: 1013ms
memory: 3684kb

input:

295310488 262950628

output:

503008241

result:

ok "503008241"

Test #7:

score: 0
Accepted
time: 308ms
memory: 3720kb

input:

239670714 149827706

output:

994702969

result:

ok "994702969"

Test #8:

score: 0
Accepted
time: 150ms
memory: 3664kb

input:

790779949 110053353

output:

898252188

result:

ok "898252188"

Test #9:

score: -100
Time Limit Exceeded

input:

726600542 795285932

output:


result: