QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#765988#9479. And DNAguosounAC ✓0ms3684kbC++171.2kb2024-11-20 15:52:172024-11-20 15:52:17

Judging History

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

  • [2024-11-20 15:52:17]
  • 评测
  • 测评结果:AC
  • 用时:0ms
  • 内存:3684kb
  • [2024-11-20 15:52:17]
  • 提交

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) {
  static std::map<std::pair<int, int>, int> mp;
  if (mp.count({n, m})) return mp[{n, 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 mp[{n, m}] = ret;
  }
  if (m & 1)
    return mp[{n, m}] = (ll)f(n, (m - 1) / 2) * f(n, 1) % mod;
  else
    return mp[{n, m}] = (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: 3636kb

input:

3 2

output:

4

result:

ok "4"

Test #2:

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

input:

3 0

output:

1

result:

ok "1"

Test #3:

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

input:

100 100

output:

343406454

result:

ok "343406454"

Test #4:

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

input:

686579306 119540831

output:

260602195

result:

ok "260602195"

Test #5:

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

input:

26855095 796233790

output:

492736984

result:

ok "492736984"

Test #6:

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

input:

295310488 262950628

output:

503008241

result:

ok "503008241"

Test #7:

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

input:

239670714 149827706

output:

994702969

result:

ok "994702969"

Test #8:

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

input:

790779949 110053353

output:

898252188

result:

ok "898252188"

Test #9:

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

input:

726600542 795285932

output:

183726777

result:

ok "183726777"

Test #10:

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

input:

957970519 585582861

output:

298814018

result:

ok "298814018"

Test #11:

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

input:

93349859 634036506

output:

110693443

result:

ok "110693443"

Test #12:

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

input:

453035113 34126396

output:

306244220

result:

ok "306244220"

Test #13:

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

input:

31994526 100604502

output:

930620701

result:

ok "930620701"

Test #14:

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

input:

234760741 249817734

output:

440195858

result:

ok "440195858"

Test #15:

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

input:

542621111 646412689

output:

207377992

result:

ok "207377992"

Test #16:

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

input:

28492783 602632297

output:

65234506

result:

ok "65234506"

Test #17:

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

input:

213500301 768820204

output:

540205113

result:

ok "540205113"

Test #18:

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

input:

697808101 753041955

output:

93561295

result:

ok "93561295"

Test #19:

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

input:

585126464 450455977

output:

91109717

result:

ok "91109717"

Test #20:

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

input:

236696315 482334538

output:

925575199

result:

ok "925575199"

Test #21:

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

input:

632719214 298704996

output:

358926097

result:

ok "358926097"

Test #22:

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

input:

869119333 933404114

output:

318501108

result:

ok "318501108"

Test #23:

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

input:

6977994 814763202

output:

585332987

result:

ok "585332987"

Test #24:

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

input:

3 1

output:

3

result:

ok "3"

Test #25:

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

input:

3 1000000000

output:

279679226

result:

ok "279679226"

Test #26:

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

input:

4 0

output:

1

result:

ok "1"

Test #27:

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

input:

4 1

output:

2

result:

ok "2"

Test #28:

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

input:

4 1000000000

output:

1755648

result:

ok "1755648"

Test #29:

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

input:

1000000000 0

output:

1

result:

ok "1"

Test #30:

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

input:

1000000000 1

output:

696798313

result:

ok "696798313"

Test #31:

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

input:

1000000000 1000000000

output:

703786301

result:

ok "703786301"

Extra Test:

score: 0
Extra Test Passed