結果

問題 No.2060 AND Sequence
ユーザー shauuebbit
提出日時 2022-09-05 23:10:43
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 3,546 bytes
コンパイル時間 1,747 ms
コンパイル使用メモリ 198,140 KB
最終ジャッジ日時 2025-02-07 02:58:06
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>


template <long long mod>
class ModInt {
   private:
    long long number;

   public:
    constexpr ModInt(const ModInt& x) : number(x.number) {}

    constexpr ModInt(const long long number = 0) : number(number) {
        if (number < 0 || number >= mod) this->number %= mod;
        if (this->number < 0) this->number += mod;
    }

    constexpr long long get() const { return number; }

    constexpr ModInt& operator=(const ModInt& rhs) & {
        number = rhs.number;
        return *this;
    }

    constexpr ModInt operator+() const { return ModInt(*this); }

    constexpr ModInt operator-() const { return ModInt(mod - number); }

    constexpr ModInt& operator+=(const ModInt& rhs) {
        number += rhs.number;
        if (number >= mod) number -= mod;
        return *this;
    }

    constexpr ModInt& operator-=(const ModInt& rhs) {
        number -= rhs.number;
        if (number < 0) number += mod;
        return *this;
    }

    constexpr ModInt& operator*=(const ModInt& rhs) {
        number *= rhs.number;
        if (number >= mod) number %= mod;
        return *this;
    }

    constexpr ModInt& operator/=(const ModInt& rhs) {
        (*this) *= rhs.inverse();
        return *this;
    }

    constexpr ModInt inverse() const {
        long long a = number, m = mod, u = 1, v = 0;

        while (m) {
            long long t = a / m;
            a -= t * m;
            u -= t * v;

            a ^= m;
            m ^= a;
            a ^= m;
            u ^= v;
            v ^= u;
            u ^= v;
        }

        return ModInt(u);
    }

    constexpr ModInt& operator++() { return *this += 1; }

    constexpr ModInt& operator--() { return *this -= 1; }

    constexpr ModInt operator++(int) {
        ModInt ret = *this;
        ++(*this);
        return ret;
    }

    constexpr ModInt operator--(int) {
        ModInt ret = *this;
        --(*this);
        return ret;
    }

    constexpr ModInt operator+(const ModInt& rhs) const { return ModInt(*this) += rhs; }

    constexpr ModInt operator-(const ModInt& rhs) const { return ModInt(*this) -= rhs; }

    constexpr ModInt operator*(const ModInt& rhs) const { return ModInt(*this) *= rhs; }

    constexpr ModInt operator/(const ModInt& rhs) const { return ModInt(*this) /= rhs; }

    constexpr ModInt operator^(const long long& rhs) const {
        long long b = number, e = rhs, y = 1, m = mod;
        bool inv = e < 0;
        if (inv) e = -e;

        while (e) {
            if (e & 1) (y *= b) %= m;

            (b *= b) %= m;
            e >>= 1;
        }

        return (inv ? ModInt(y).inverse() : ModInt(y));
    }
};

template <long long mod>
std::istream& operator>>(std::istream& is, ModInt<mod>& x) {
    long long in;
    is >> in;
    x = in;
    return is;
}

template <long long mod>
std::ostream& operator<<(std::ostream& os, const ModInt<mod>& x) {
    os << x.get();
    return os;
}

constexpr int MOD = 998244353;
constexpr int LEN = 30;

using namespace std;
using mint = ModInt<MOD>;

int main() {
    int N, M;
    cin >> N >> M;
    
    vector dp(LEN + 1, vector<mint>(2, 0));

    dp[0][0] = 1;

    for (int k = 0; k < LEN; k++) {
        if ((M >> (LEN - k - 1)) & 1) {
            dp[k + 1][1] += dp[k][1] * (N + 1);
            dp[k + 1][1] += dp[k][0];
            dp[k + 1][0] += dp[k][0] * N;
        } else {
            dp[k + 1][1] += dp[k][1] * (N + 1);
            dp[k + 1][0] += dp[k][0];
        }
    }

    cout << dp[LEN][0] + dp[LEN][1] << endl;

    return 0;
}
0