QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#673652#9479. And DNAhos_lyricAC ✓0ms4048kbC++145.2kb2024-10-25 04:41:552024-10-25 04:41:55

Judging History

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

  • [2024-10-25 04:41:55]
  • 评测
  • 测评结果:AC
  • 用时:0ms
  • 内存:4048kb
  • [2024-10-25 04:41:55]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;


Mint divAt1(vector<Mint> ps, vector<Mint> qs, Int n) {
  assert(qs.size() >= 1);
  assert(qs[0] == 1);
  for (; n; n >>= 1) {
    auto negQs = qs;
    for (int i = 1; i < (int)qs.size(); i += 2) negQs[i] = -negQs[i];
    vector<Mint> pps((int)ps.size() + (int)qs.size() - 1, 0);
    vector<Mint> qqs((int)qs.size() + (int)qs.size() - 1, 0);
    for (int i = 0; i < (int)ps.size(); ++i) for (int j = 0; j < (int)qs.size(); ++j) pps[i + j] += ps[i] * negQs[j];
    for (int i = 0; i < (int)qs.size(); ++i) for (int j = 0; j < (int)qs.size(); ++j) qqs[i + j] += qs[i] * negQs[j];
    ps.clear();
    qs.clear();
    for (int i = n & 1; i < (int)pps.size(); i += 2) ps.push_back(pps[i]);
    for (int i = 0    ; i < (int)qqs.size(); i += 2) qs.push_back(qqs[i]);
  }
  return ps[0];
}


Mint way;

map<Int, Mint> cache;
Mint calc(Int m) {
  auto it = cache.find(m);
  if (it != cache.end()) return it->second;
  Mint ret = 0;
  
  if (m == 0) {
    ret += 1;
  } else if (m & 1) {
    ret += way * calc(m / 2);
  } else {
    ret += calc(m / 2);
    ret += calc(m / 2 - 1);
  }
  
  return cache[m] = ret;
}

int main() {
  Int N, M;
  for (; ~scanf("%lld%lld", &N, &M); ) {
    way = divAt1({0, 0, 0, 3, 0, -1}, {1, 0, -2, -1, 1, 1}, N);
    if (N % 2 == 0) {
      way += 2;
    }
cerr<<"way = "<<way<<endl;
    
    cache.clear();
    const Mint ans = calc(M);
    printf("%u\n", ans.x);
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3 2

output:

4

result:

ok "4"

Test #2:

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

input:

3 0

output:

1

result:

ok "1"

Test #3:

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

input:

100 100

output:

343406454

result:

ok "343406454"

Test #4:

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

input:

686579306 119540831

output:

260602195

result:

ok "260602195"

Test #5:

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

input:

26855095 796233790

output:

492736984

result:

ok "492736984"

Test #6:

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

input:

295310488 262950628

output:

503008241

result:

ok "503008241"

Test #7:

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

input:

239670714 149827706

output:

994702969

result:

ok "994702969"

Test #8:

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

input:

790779949 110053353

output:

898252188

result:

ok "898252188"

Test #9:

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

input:

726600542 795285932

output:

183726777

result:

ok "183726777"

Test #10:

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

input:

957970519 585582861

output:

298814018

result:

ok "298814018"

Test #11:

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

input:

93349859 634036506

output:

110693443

result:

ok "110693443"

Test #12:

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

input:

453035113 34126396

output:

306244220

result:

ok "306244220"

Test #13:

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

input:

31994526 100604502

output:

930620701

result:

ok "930620701"

Test #14:

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

input:

234760741 249817734

output:

440195858

result:

ok "440195858"

Test #15:

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

input:

542621111 646412689

output:

207377992

result:

ok "207377992"

Test #16:

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

input:

28492783 602632297

output:

65234506

result:

ok "65234506"

Test #17:

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

input:

213500301 768820204

output:

540205113

result:

ok "540205113"

Test #18:

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

input:

697808101 753041955

output:

93561295

result:

ok "93561295"

Test #19:

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

input:

585126464 450455977

output:

91109717

result:

ok "91109717"

Test #20:

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

input:

236696315 482334538

output:

925575199

result:

ok "925575199"

Test #21:

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

input:

632719214 298704996

output:

358926097

result:

ok "358926097"

Test #22:

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

input:

869119333 933404114

output:

318501108

result:

ok "318501108"

Test #23:

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

input:

6977994 814763202

output:

585332987

result:

ok "585332987"

Test #24:

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

input:

3 1

output:

3

result:

ok "3"

Test #25:

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

input:

3 1000000000

output:

279679226

result:

ok "279679226"

Test #26:

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

input:

4 0

output:

1

result:

ok "1"

Test #27:

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

input:

4 1

output:

2

result:

ok "2"

Test #28:

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

input:

4 1000000000

output:

1755648

result:

ok "1755648"

Test #29:

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

input:

1000000000 0

output:

1

result:

ok "1"

Test #30:

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

input:

1000000000 1

output:

696798313

result:

ok "696798313"

Test #31:

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

input:

1000000000 1000000000

output:

703786301

result:

ok "703786301"

Extra Test:

score: 0
Extra Test Passed