結果

問題 No.3799 1 + 6 = 7 (Another version)
ユーザー hos.lyric
提出日時 2026-11-13 23:20:37
言語 C++14
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
RE  
実行時間 -
コード長 4,814 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 830 ms
コンパイル使用メモリ 139,284 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-11-13 23:20:52
合計ジャッジ時間 4,833 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16 RE * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <chrono>
#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 T> ostream &operator<<(ostream &os, const vector<T> &as);
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")

////////////////////////////////////////////////////////////////////////////////
struct ModLong {
  static unsigned long long M;
  static long double INV_M;
  static void setM(unsigned long long m) { M = m; INV_M = 1.0L / M; }
  unsigned long long x;
  ModLong() : x(0ULL) {}
  ModLong(unsigned x_) : x(x_ % M) {}
  ModLong(unsigned long long x_) : x(x_ % M) {}
  ModLong(int x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModLong(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModLong &operator+=(const ModLong &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModLong &operator-=(const ModLong &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModLong &operator*=(const ModLong &a) {
    // https://github.com/kth-competitive-programming/kactl/blob/main/doc/modmul-proof.pdf
    // This works at least for M < 2^62, assuming the usual 80-bit long double.
    const long long y = x * a.x - M * static_cast<unsigned long long>(INV_M * x * a.x);
    x = (y < 0LL) ? (y + M) : (y >= static_cast<long long>(M)) ? (y - M) : y;
    return *this;
  }
  ModLong &operator/=(const ModLong &a) { return (*this *= a.inv()); }
  ModLong pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModLong a = *this, b = 1ULL; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModLong inv() const {
    unsigned long long a = M, b = x; long long y = 0, z = 1;
    for (; b; ) { const unsigned long long q = a / b; const unsigned long long c = a - q * b; a = b; b = c; const long long w = y - static_cast<long long>(q) * z; y = z; z = w; }
    assert(a == 1ULL); return ModLong(y);
  }
  ModLong operator+() const { return *this; }
  ModLong operator-() const { ModLong a; a.x = x ? (M - x) : 0ULL; return a; }
  ModLong operator+(const ModLong &a) const { return (ModLong(*this) += a); }
  ModLong operator-(const ModLong &a) const { return (ModLong(*this) -= a); }
  ModLong operator*(const ModLong &a) const { return (ModLong(*this) *= a); }
  ModLong operator/(const ModLong &a) const { return (ModLong(*this) /= a); }
  template <class T> friend ModLong operator+(T a, const ModLong &b) { return (ModLong(a) += b); }
  template <class T> friend ModLong operator-(T a, const ModLong &b) { return (ModLong(a) -= b); }
  template <class T> friend ModLong operator*(T a, const ModLong &b) { return (ModLong(a) *= b); }
  template <class T> friend ModLong operator/(T a, const ModLong &b) { return (ModLong(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModLong &a) const { return (x == a.x); }
  bool operator!=(const ModLong &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModLong &a) { return os << a.x; }
};
unsigned long long ModLong::M;
long double ModLong::INV_M;
// !!!Use ModLong::setM!!!
////////////////////////////////////////////////////////////////////////////////

/*
  state -(M-1), ..., M
  -(M-1) <= u <= 0  ~~>  u+1, ..., u+M
  1 <= u <= M       ~~>  u-M, ..., u-1
  identify states by mod (M+1)
  ~~> typical counting: seqs in [1, M]^N s.t. (M+1) | sum
*/

constexpr Int MO = 998244353;

int main() {
  Int N, M;
  for (; ~scanf("%lld%lld", &N, &M); ) {
    ModLong::setM(MO * (M+1));
    ModLong numer = ModLong(M).pow(N) + M * (N&1?-1:+1);
    assert(numer.x % (M+1) == 0);
    const Int ans = numer.x / (M+1);
    printf("%lld\n", ans);
  }
  return 0;
}
0