結果

問題 No.2349 Power!! (Hard)
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-06-09 22:02:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 7,075 bytes
コンパイル時間 4,160 ms
コンパイル使用メモリ 180,336 KB
実行使用メモリ 12,020 KB
最終ジャッジ日時 2024-06-10 13:19:31
合計ジャッジ時間 16,290 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")

#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 <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; }

////////////////////////////////////////////////////////////////////////////////
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>;


#include "atcoder/convolution"
void fft(vector<Mint> &as) {
  using mint = atcoder::modint998244353;
  vector<mint> as_(as.size());
  for (int i = 0; i < (int)as.size(); ++i) as_[i] = mint::raw(as[i].x);
  atcoder::internal::butterfly(as_);
  for (int i = 0; i < (int)as.size(); ++i) as[i].x = as_[i].val();
}
void invFft(vector<Mint> &as) {
  using mint = atcoder::modint998244353;
  vector<mint> as_(as.size());
  for (int i = 0; i < (int)as.size(); ++i) as_[i] = mint::raw(as[i].x);
  atcoder::internal::butterfly_inv(as_);
  for (int i = 0; i < (int)as.size(); ++i) as[i].x = as_[i].val();
}


/*
  [0, n] * [0, n - m + 1]
  
  [ a[0]                     ]
  [ ...    a[0]              ]
  [ a[m-1] ...               ]
  [        a[m-1]            ]
  [               ...        ]
  [                   a[0]   ]
  [                   ...    ]
  [                   a[m-1] ]
  
  [x^j] (rev(a) b)  m - 1 <= j <= n - 1
*/
vector<Mint> middle(vector<Mint> as, vector<Mint> bs) {
  const int m = as.size();
  const int n = bs.size();
  assert(m <= n);
  int nn = 1;
  for (; nn < n; nn <<= 1) {}
  reverse(as.begin(), as.end());
  as.resize(nn, 0);
  fft(as);
  bs.resize(nn, 0);
  fft(bs);
  for (int i = 0; i < nn; ++i) {
    bs[i] *= as[i];
  }
  invFft(bs);
  bs.resize(n);
  bs.erase(bs.begin(), bs.begin() + (m - 1));
  return bs;
}


// a^e,  0 <= e < 2^32
struct Power {
  static constexpr int E = 11;
  vector<Mint> baby, mid, giant;
  Power() {}
  Power(Mint a) : baby((1 << E) + 1), mid((1 << E) + 1), giant(1 << E) {
    baby[0] = 1;
    for (int i = 1; i <= 1 << E; ++i) baby[i] = baby[i - 1] * a;
    mid[0] = 1;
    for (int i = 1; i <= 1 << E; ++i) mid[i] = mid[i - 1] * baby[1 << E];
    giant[0] = 1;
    for (int i = 1; i < 1 << E; ++i) giant[i] = giant[i - 1] * mid[1 << E];
  }
  Mint operator()(unsigned e) const {
    return baby[e & ((1<<E)-1)] * mid[(e >> E) & ((1<<E)-1)] * giant[e >> (2*E)];
  }
};

int main() {
  for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    Mint A;
    int N;
    scanf("%u%d", &A.x, &N);
    const int K = sqrt(N);
    
    const Power aa(A);
    const Power aaKK(aa(K * K));
    
    vector<Mint> as(K);
    for (int j = 0; j < K; ++j) {
      as[j] = aa(j * j);
    }
// cerr<<"as = "<<as<<endl;
    
    const Mint B = aa(2 * K);
    const Mint invB = B.inv();
    vector<Mint> bb2(2 * K);
    {
      Mint bb = 1;
      bb2[0] = 1;
      for (int i = 1; i < 2 * K; ++i) {
        bb2[i] = bb2[i - 1] * bb;
        bb *= B;
      }
    }
    vector<Mint> invbb2(K);
    {
      Mint invbb = 1;
      invbb2[0] = 1;
      for (int i = 1; i < K; ++i) {
        invbb2[i] = invbb2[i - 1] * invbb;
        invbb *= invB;
      }
    }
    
    auto gs = as;
    for (int i = 0; i < K; ++i) {
      gs[i] *= invbb2[i];
    }
    auto fs = middle(gs, bb2);
    for (int i = 0; i < K; ++i) {
      fs[i] *= invbb2[i];
    }
// cerr<<"fs = "<<fs<<endl;
    
    Mint ans = 0;
    for (int i = 0; i < K; ++i) {
      ans += aaKK(i * i) * fs[i];
    }
    {
      Mint pw = aaKK(K * K);
      for (int i = K * K; i < N; ++i) {
        ans += pw;
        pw *= aa(2 * i + 1);
      }
    }
    printf("%u\n", ans.x);
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}
0