QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#656533#9479. And DNAucup-team008#AC ✓1ms3864kbC++178.5kb2024-10-19 13:16:442024-10-19 13:16:45

Judging History

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

  • [2024-10-19 13:16:45]
  • 评测
  • 测评结果:AC
  • 用时:1ms
  • 内存:3864kb
  • [2024-10-19 13:16:44]
  • 提交

answer

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
#define derr if(0) cerr
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << flush;
// END NO SAD

template<class Fun>
class y_combinator_result {
  Fun fun_;
public:
  template<class T>
  explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}

  template<class ...Args>
  decltype(auto) operator()(Args &&...args) {
    return fun_(std::ref(*this), std::forward<Args>(args)...);
  }
};

template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
  return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

template<class T>
bool updmin(T& a, T b) {
  if(b < a) {
    a = b;
    return true;
  }
  return false;
}
template<class T>
bool updmax(T& a, T b) {
  if(b > a) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

struct barrett_reduction {
    unsigned mod;
    uint64_t div;
 
    barrett_reduction(unsigned m) : mod(m), div(-1LLU / m) {}
 
    unsigned operator()(uint64_t a) const {
#ifdef __SIZEOF_INT128__
        uint64_t q = uint64_t(__uint128_t(div) * a >> 64);
        uint64_t r = a - q * mod;
        return unsigned(r < mod ? r : r - mod);
#endif
        return unsigned(a % mod);
    }
};
 
template<const int &MOD, const barrett_reduction &barrett>
struct _b_int {
    int val;
 
    _b_int(int64_t v = 0) {
        if (v < 0) v = v % MOD + MOD;
        if (v >= MOD) v %= MOD;
        val = int(v);
    }
 
    _b_int(uint64_t v) {
        if (v >= uint64_t(MOD)) v %= MOD;
        val = int(v);
    }
 
    _b_int(int v) : _b_int(int64_t(v)) {}
    _b_int(unsigned v) : _b_int(uint64_t(v)) {}
 
    static int inv_mod(int a, int m = MOD) {
        // https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Example
        int g = m, r = a, x = 0, y = 1;
 
        while (r != 0) {
            int q = g / r;
            g %= r; swap(g, r);
            x -= q * y; swap(x, y);
        }
 
        return x < 0 ? x + m : x;
    }
 
    explicit operator int() const { return val; }
    explicit operator unsigned() const { return val; }
    explicit operator int64_t() const { return val; }
    explicit operator uint64_t() const { return val; }
    explicit operator double() const { return val; }
    explicit operator long double() const { return val; }
 
    _b_int& operator+=(const _b_int &other) {
        val -= MOD - other.val;
        if (val < 0) val += MOD;
        return *this;
    }
 
    _b_int& operator-=(const _b_int &other) {
        val -= other.val;
        if (val < 0) val += MOD;
        return *this;
    }
 
    static unsigned fast_mod(uint64_t x) {
#if !defined(_WIN32) || defined(_WIN64)
        return barrett(x);
#endif
        // Optimized mod for Codeforces 32-bit machines.
        // x must be less than 2^32 * MOD for this to work, so that x / MOD fits in an unsigned 32-bit int.
        unsigned x_high = unsigned(x >> 32), x_low = unsigned(x);
        unsigned quot, rem;
        asm("divl %4\n"
            : "=a" (quot), "=d" (rem)
            : "d" (x_high), "a" (x_low), "r" (MOD));
        return rem;
    }
 
    _b_int& operator*=(const _b_int &other) {
        val = fast_mod(uint64_t(val) * other.val);
        return *this;
    }
 
    _b_int& operator/=(const _b_int &other) {
        return *this *= other.inv();
    }
 
    friend _b_int operator+(const _b_int &a, const _b_int &b) { return _b_int(a) += b; }
    friend _b_int operator-(const _b_int &a, const _b_int &b) { return _b_int(a) -= b; }
    friend _b_int operator*(const _b_int &a, const _b_int &b) { return _b_int(a) *= b; }
    friend _b_int operator/(const _b_int &a, const _b_int &b) { return _b_int(a) /= b; }
 
    _b_int& operator++() {
        val = val == MOD - 1 ? 0 : val + 1;
        return *this;
    }
 
    _b_int& operator--() {
        val = val == 0 ? MOD - 1 : val - 1;
        return *this;
    }
 
    _b_int operator++(int) { _b_int before = *this; ++*this; return before; }
    _b_int operator--(int) { _b_int before = *this; --*this; return before; }
 
    _b_int operator-() const {
        return val == 0 ? 0 : MOD - val;
    }
 
    friend bool operator==(const _b_int &a, const _b_int &b) { return a.val == b.val; }
    friend bool operator!=(const _b_int &a, const _b_int &b) { return a.val != b.val; }
    friend bool operator<(const _b_int &a, const _b_int &b) { return a.val < b.val; }
    friend bool operator>(const _b_int &a, const _b_int &b) { return a.val > b.val; }
    friend bool operator<=(const _b_int &a, const _b_int &b) { return a.val <= b.val; }
    friend bool operator>=(const _b_int &a, const _b_int &b) { return a.val >= b.val; }
 
    _b_int inv() const {
        return inv_mod(val);
    }
 
    _b_int pow(int64_t p) const {
        if (p < 0)
            return inv().pow(-p);
 
        _b_int a = *this, result = 1;
 
        while (p > 0) {
            if (p & 1)
                result *= a;
 
            p >>= 1;
 
            if (p > 0)
                a *= a;
        }
 
        return result;
    }
 
    friend ostream& operator<<(ostream &os, const _b_int &m) {
        return os << m.val;
    }
 
    friend istream& operator>>(istream &is, _b_int &m) {
        int64_t x;
        is >> x;
        m = x;
        return is;
    }
};
 
int MOD = 998244353;
barrett_reduction barrett(MOD);
using mnum = _b_int<MOD, barrett>;
typedef vector<vector<mnum>> matrix;

matrix mul(const matrix& a, const matrix& b) {
  matrix c(sz(a));
  for (int i = 0; i < sz(a); i++) {
    c[i].resize(sz(b[0]));
    for (int j = 0; j < sz(b[0]); j++) {
      for (int k = 0; k < sz(a[0]); k++) {
        c[i][j] += a[i][k] * b[k][j];
      }
    }
  }
  return c;
}
matrix fpow(matrix b, ll e) {
  matrix res(sz(b));
  for (int i = 0; i < sz(b); i++) {
    res[i].resize(sz(b));
    res[i][i] = 1;
  }
  while (e) {
    if (e & 1) {
      res = mul(res, b);
    }
    b = mul(b, b);
    e >>= 1;
  }
  return res;
}
void solve() {
  int n, m;
  cin >> n >> m;
  matrix b(3);
  for(auto& x: b) x.resize(3);
  b[0][1] = 1; b[1][2] = 1; b[2][0] = 1; b[2][1] = 1;
  matrix r = fpow(b, n-2);
  mnum scale = 3*r[2][0] + 2*r[2][2];
  map<int, mnum> dp;
  auto dfs = y_combinator([&](auto self, int n) -> mnum {
    if(dp.count(n)) return dp[n];
    if(n <= 1) return n;
    if(n%2==0) dp[n] = self(n/2)*scale;
    else dp[n] = self(n/2) + self(n/2+1);
    return dp[n];
  });
  cout << dfs(m+1) << "\n";
}

// what would chika do
// are there edge cases?
// did you actually sort the thing instead of just thinking it?
// integer overflow?

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3580kb

input:

3 2

output:

4

result:

ok "4"

Test #2:

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

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: 3856kb

input:

686579306 119540831

output:

260602195

result:

ok "260602195"

Test #5:

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

input:

26855095 796233790

output:

492736984

result:

ok "492736984"

Test #6:

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

input:

295310488 262950628

output:

503008241

result:

ok "503008241"

Test #7:

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

input:

239670714 149827706

output:

994702969

result:

ok "994702969"

Test #8:

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

input:

790779949 110053353

output:

898252188

result:

ok "898252188"

Test #9:

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

input:

726600542 795285932

output:

183726777

result:

ok "183726777"

Test #10:

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

input:

957970519 585582861

output:

298814018

result:

ok "298814018"

Test #11:

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

input:

93349859 634036506

output:

110693443

result:

ok "110693443"

Test #12:

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

input:

453035113 34126396

output:

306244220

result:

ok "306244220"

Test #13:

score: 0
Accepted
time: 1ms
memory: 3520kb

input:

31994526 100604502

output:

930620701

result:

ok "930620701"

Test #14:

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

input:

234760741 249817734

output:

440195858

result:

ok "440195858"

Test #15:

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

input:

542621111 646412689

output:

207377992

result:

ok "207377992"

Test #16:

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

input:

28492783 602632297

output:

65234506

result:

ok "65234506"

Test #17:

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

input:

213500301 768820204

output:

540205113

result:

ok "540205113"

Test #18:

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

input:

697808101 753041955

output:

93561295

result:

ok "93561295"

Test #19:

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

input:

585126464 450455977

output:

91109717

result:

ok "91109717"

Test #20:

score: 0
Accepted
time: 1ms
memory: 3576kb

input:

236696315 482334538

output:

925575199

result:

ok "925575199"

Test #21:

score: 0
Accepted
time: 1ms
memory: 3584kb

input:

632719214 298704996

output:

358926097

result:

ok "358926097"

Test #22:

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

input:

869119333 933404114

output:

318501108

result:

ok "318501108"

Test #23:

score: 0
Accepted
time: 1ms
memory: 3864kb

input:

6977994 814763202

output:

585332987

result:

ok "585332987"

Test #24:

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

input:

3 1

output:

3

result:

ok "3"

Test #25:

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

input:

3 1000000000

output:

279679226

result:

ok "279679226"

Test #26:

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

input:

4 0

output:

1

result:

ok "1"

Test #27:

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

input:

4 1

output:

2

result:

ok "2"

Test #28:

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

input:

4 1000000000

output:

1755648

result:

ok "1755648"

Test #29:

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

input:

1000000000 0

output:

1

result:

ok "1"

Test #30:

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

input:

1000000000 1

output:

696798313

result:

ok "696798313"

Test #31:

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

input:

1000000000 1000000000

output:

703786301

result:

ok "703786301"

Extra Test:

score: 0
Extra Test Passed