QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#584052#9247. An Easy Counting Problemhos_lyricAC ✓1076ms101296kbC++1410.3kb2024-09-23 05:03:072024-09-23 05:03:07

Judging History

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

  • [2024-09-23 05:03:07]
  • 评测
  • 测评结果:AC
  • 用时:1076ms
  • 内存:101296kb
  • [2024-09-23 05:03:07]
  • 提交

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 = 998244353U;
constexpr unsigned MO2 = 2U * MO;
constexpr int FFT_MAX = 23;
using Mint = ModInt<MO>;
constexpr Mint FFT_ROOTS[FFT_MAX + 1] = {1U, 998244352U, 911660635U, 372528824U, 929031873U, 452798380U, 922799308U, 781712469U, 476477967U, 166035806U, 258648936U, 584193783U, 63912897U, 350007156U, 666702199U, 968855178U, 629671588U, 24514907U, 996173970U, 363395222U, 565042129U, 733596141U, 267099868U, 15311432U};
constexpr Mint INV_FFT_ROOTS[FFT_MAX + 1] = {1U, 998244352U, 86583718U, 509520358U, 337190230U, 87557064U, 609441965U, 135236158U, 304459705U, 685443576U, 381598368U, 335559352U, 129292727U, 358024708U, 814576206U, 708402881U, 283043518U, 3707709U, 121392023U, 704923114U, 950391366U, 428961804U, 382752275U, 469870224U};
constexpr Mint FFT_RATIOS[FFT_MAX] = {911660635U, 509520358U, 369330050U, 332049552U, 983190778U, 123842337U, 238493703U, 975955924U, 603855026U, 856644456U, 131300601U, 842657263U, 730768835U, 942482514U, 806263778U, 151565301U, 510815449U, 503497456U, 743006876U, 741047443U, 56250497U, 867605899U};
constexpr Mint INV_FFT_RATIOS[FFT_MAX] = {86583718U, 372528824U, 373294451U, 645684063U, 112220581U, 692852209U, 155456985U, 797128860U, 90816748U, 860285882U, 927414960U, 354738543U, 109331171U, 293255632U, 535113200U, 308540755U, 121186627U, 608385704U, 438932459U, 359477183U, 824071951U, 103369235U};

// as[rev(i)] <- \sum_j \zeta^(ij) as[j]
void fft(Mint *as, int n) {
  assert(!(n & (n - 1))); assert(1 <= n); assert(n <= 1 << FFT_MAX);
  int m = n;
  if (m >>= 1) {
    for (int i = 0; i < m; ++i) {
      const unsigned x = as[i + m].x;  // < MO
      as[i + m].x = as[i].x + MO - x;  // < 2 MO
      as[i].x += x;  // < 2 MO
    }
  }
  if (m >>= 1) {
    Mint prod = 1U;
    for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
      for (int i = i0; i < i0 + m; ++i) {
        const unsigned x = (prod * as[i + m]).x;  // < MO
        as[i + m].x = as[i].x + MO - x;  // < 3 MO
        as[i].x += x;  // < 3 MO
      }
      prod *= FFT_RATIOS[__builtin_ctz(++h)];
    }
  }
  for (; m; ) {
    if (m >>= 1) {
      Mint prod = 1U;
      for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
        for (int i = i0; i < i0 + m; ++i) {
          const unsigned x = (prod * as[i + m]).x;  // < MO
          as[i + m].x = as[i].x + MO - x;  // < 4 MO
          as[i].x += x;  // < 4 MO
        }
        prod *= FFT_RATIOS[__builtin_ctz(++h)];
      }
    }
    if (m >>= 1) {
      Mint prod = 1U;
      for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
        for (int i = i0; i < i0 + m; ++i) {
          const unsigned x = (prod * as[i + m]).x;  // < MO
          as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x;  // < 2 MO
          as[i + m].x = as[i].x + MO - x;  // < 3 MO
          as[i].x += x;  // < 3 MO
        }
        prod *= FFT_RATIOS[__builtin_ctz(++h)];
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x;  // < 2 MO
    as[i].x = (as[i].x >= MO) ? (as[i].x - MO) : as[i].x;  // < MO
  }
}

// as[i] <- (1/n) \sum_j \zeta^(-ij) as[rev(j)]
void invFft(Mint *as, int n) {
  assert(!(n & (n - 1))); assert(1 <= n); assert(n <= 1 << FFT_MAX);
  int m = 1;
  if (m < n >> 1) {
    Mint prod = 1U;
    for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
      for (int i = i0; i < i0 + m; ++i) {
        const unsigned long long y = as[i].x + MO - as[i + m].x;  // < 2 MO
        as[i].x += as[i + m].x;  // < 2 MO
        as[i + m].x = (prod.x * y) % MO;  // < MO
      }
      prod *= INV_FFT_RATIOS[__builtin_ctz(++h)];
    }
    m <<= 1;
  }
  for (; m < n >> 1; m <<= 1) {
    Mint prod = 1U;
    for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
      for (int i = i0; i < i0 + (m >> 1); ++i) {
        const unsigned long long y = as[i].x + MO2 - as[i + m].x;  // < 4 MO
        as[i].x += as[i + m].x;  // < 4 MO
        as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x;  // < 2 MO
        as[i + m].x = (prod.x * y) % MO;  // < MO
      }
      for (int i = i0 + (m >> 1); i < i0 + m; ++i) {
        const unsigned long long y = as[i].x + MO - as[i + m].x;  // < 2 MO
        as[i].x += as[i + m].x;  // < 2 MO
        as[i + m].x = (prod.x * y) % MO;  // < MO
      }
      prod *= INV_FFT_RATIOS[__builtin_ctz(++h)];
    }
  }
  if (m < n) {
    for (int i = 0; i < m; ++i) {
      const unsigned y = as[i].x + MO2 - as[i + m].x;  // < 4 MO
      as[i].x += as[i + m].x;  // < 4 MO
      as[i + m].x = y;  // < 4 MO
    }
  }
  const Mint invN = Mint(n).inv();
  for (int i = 0; i < n; ++i) {
    as[i] *= invN;
  }
}

void fft(vector<Mint> &as) {
  fft(as.data(), as.size());
}
void invFft(vector<Mint> &as) {
  invFft(as.data(), as.size());
}

vector<Mint> convolve(vector<Mint> as, vector<Mint> bs) {
  if (as.empty() || bs.empty()) return {};
  const int len = as.size() + bs.size() - 1;
  int n = 1;
  for (; n < len; n <<= 1) {}
  as.resize(n); fft(as);
  bs.resize(n); fft(bs);
  for (int i = 0; i < n; ++i) as[i] *= bs[i];
  invFft(as);
  as.resize(len);
  return as;
}
vector<Mint> square(vector<Mint> as) {
  if (as.empty()) return {};
  const int len = as.size() + as.size() - 1;
  int n = 1;
  for (; n < len; n <<= 1) {}
  as.resize(n); fft(as);
  for (int i = 0; i < n; ++i) as[i] *= as[i];
  invFft(as);
  as.resize(len);
  return as;
}
////////////////////////////////////////////////////////////////////////////////


char K[1010];
int P, X;

int bn[5010][5010];

vector<Mint> mod(vector<Mint> fs) {
  for (int i = (int)fs.size(); --i >= P - 1; ) fs[i - (P - 1)] += fs[i];
  fs.resize(P - 1);
  return fs;
}

int main() {
  for (; ~scanf("%s%d%d", K, &P, &X); ) {
    vector<int> es(P);
    for (int g = 1; ; ++g) {
      fill(es.begin(), es.end(), -1);
      bool ok = true;
      int gg = 1;
      for (int e = 0; e < P - 1; ++e) {
        if (~es[gg]) {
          ok = false;
          break;
        }
        es[gg] = e;
        (gg *= g) %= P;
      }
      if (ok) break;
    }
    
    for (int n = 0; n < P; ++n) {
      bn[n][0] = bn[n][n] = 1;
      for (int k = 1; k < n; ++k) {
        if ((bn[n][k] = bn[n - 1][k - 1] + bn[n - 1][k]) >= P) {
          bn[n][k] -= P;
        }
      }
    }
    vector<Mint> as(P - 1, 0), bs(P - 1, 0);
    for (int n = 0; n < P; ++n) for (int k = 0; k <= n; ++k) as[es[bn[n][k]]] += 1;
    bs[0] = 1;
    for (char *k = K; *k; ++k) {
      bs = mod(square(bs));
      if (*k == '1') bs = mod(convolve(bs, as));
    }
    printf("%u\n", bs[es[X]].x);
  }
  return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1 7 5

output:

2

result:

ok single line: '2'

Test #2:

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

input:

1 43 17

output:

17

result:

ok single line: '17'

Test #3:

score: 0
Accepted
time: 62ms
memory: 100684kb

input:

1111111111 4999 1954

output:

195378837

result:

ok single line: '195378837'

Test #4:

score: 0
Accepted
time: 2ms
memory: 10552kb

input:

1 353 38

output:

174

result:

ok single line: '174'

Test #5:

score: 0
Accepted
time: 2ms
memory: 12008kb

input:

1 353 133

output:

150

result:

ok single line: '150'

Test #6:

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

input:

1 677 498

output:

360

result:

ok single line: '360'

Test #7:

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

input:

1 137 87

output:

81

result:

ok single line: '81'

Test #8:

score: 0
Accepted
time: 3ms
memory: 12404kb

input:

1 487 457

output:

220

result:

ok single line: '220'

Test #9:

score: 0
Accepted
time: 11ms
memory: 34844kb

input:

1 1511 1478

output:

743

result:

ok single line: '743'

Test #10:

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

input:

1 1129 820

output:

511

result:

ok single line: '511'

Test #11:

score: 0
Accepted
time: 63ms
memory: 100996kb

input:

1 4999 2402

output:

2638

result:

ok single line: '2638'

Test #12:

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

input:

1111111100 3 1

output:

335376610

result:

ok single line: '335376610'

Test #13:

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

input:

1101011101 11 10

output:

8266429

result:

ok single line: '8266429'

Test #14:

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

input:

1111111101 163 3

output:

563569273

result:

ok single line: '563569273'

Test #15:

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

input:

1100010001 109 74

output:

287016541

result:

ok single line: '287016541'

Test #16:

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

input:

1000000011 2 1

output:

652385489

result:

ok single line: '652385489'

Test #17:

score: 0
Accepted
time: 2ms
memory: 9972kb

input:

1011001000 283 85

output:

250237017

result:

ok single line: '250237017'

Test #18:

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

input:

1001101010 179 9

output:

72137339

result:

ok single line: '72137339'

Test #19:

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

input:

1100000100 631 532

output:

541190467

result:

ok single line: '541190467'

Test #20:

score: 0
Accepted
time: 4ms
memory: 26452kb

input:

1110111111 1171 937

output:

55787244

result:

ok single line: '55787244'

Test #21:

score: 0
Accepted
time: 3ms
memory: 8140kb

input:

1111111111111111111111000001111001101001011101101101111111111111111111111100011111101110100111111110 193 18

output:

285954714

result:

ok single line: '285954714'

Test #22:

score: 0
Accepted
time: 2ms
memory: 6068kb

input:

1100010000110100001010000000001001011110001100100001000110010000100000010101000000100000000000110100 139 44

output:

42055570

result:

ok single line: '42055570'

Test #23:

score: 0
Accepted
time: 2ms
memory: 8128kb

input:

1000100100001000000000000000000001000000000000000001000000001000000000000000110001000000000000110000 239 236

output:

486910116

result:

ok single line: '486910116'

Test #24:

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

input:

1010101011010100100011101011111001011000001101111011101111110001101110111101011010101010011100011011 223 103

output:

71689170

result:

ok single line: '71689170'

Test #25:

score: 0
Accepted
time: 6ms
memory: 16388kb

input:

1111111110101011111111111111111111111101111111111110111111011100111100111110111111110011110111001111 593 591

output:

741309874

result:

ok single line: '741309874'

Test #26:

score: 0
Accepted
time: 6ms
memory: 8280kb

input:

1101111111111111111101111111111111111010111111111011111111110111111111111111111111111111111111110001 263 215

output:

864534324

result:

ok single line: '864534324'

Test #27:

score: 0
Accepted
time: 3ms
memory: 16100kb

input:

1000100111010110101100001010010101000100000111111000100111001101011101101000110100000111010011001100 613 253

output:

323804256

result:

ok single line: '323804256'

Test #28:

score: 0
Accepted
time: 25ms
memory: 28796kb

input:

1111111111111101111111101011111010010110111111011101111111111111110111111110111111100111111111110111 1249 1083

output:

287904425

result:

ok single line: '287904425'

Test #29:

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

input:

1000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000 101 31

output:

882058934

result:

ok single line: '882058934'

Test #30:

score: 0
Accepted
time: 155ms
memory: 101028kb

input:

1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 4999 4129

output:

831399667

result:

ok single line: '831399667'

Test #31:

score: 0
Accepted
time: 6ms
memory: 6052kb

input:

110010111110100111101101110010101110110001111000100110011101110111000110111110011111101111000010110101011000010011111110110111010101001110110111100111011011110110000010010010110101111101111111111110001001001111111010111111100011110101010110111101101001010001010111111111101010100001100000110111111110...

output:

488050830

result:

ok single line: '488050830'

Test #32:

score: 0
Accepted
time: 11ms
memory: 6184kb

input:

111111111000111101111111111101011001111111111111111011110110111111111011111111111111111111111011111111101111111111111101010101110111111111111111111111110011110100111101111001011111111001101111111111011111111010111110111111111111111000111101001010111110100111110101111111101011111011010101111111011111...

output:

479176859

result:

ok single line: '479176859'

Test #33:

score: 0
Accepted
time: 15ms
memory: 7860kb

input:

101111100001000101001000011000100010000101011000100000111101110011100110001001101011111000010101011101011001000101010100010101010101101111000101110011000111111100010101000010100110000011100111000110111101010100100101000000100011010110110101110001101100011001001101011011001011011010000111111000001001...

output:

656533380

result:

ok single line: '656533380'

Test #34:

score: 0
Accepted
time: 45ms
memory: 12044kb

input:

110111111101011101110111011111111111111101111111110101111111111111111111111101110111111111110011111100111011111111111111011111101111111001111011011111111100111111111111101111111111111111101011111111110010101111111111111011011111011110111000110101111111011111111111111101111110111111101111011111110011...

output:

653128600

result:

ok single line: '653128600'

Test #35:

score: 0
Accepted
time: 34ms
memory: 10576kb

input:

100111001000101101000000110100001001011100100001010100001100000000110000000011011010000101001101000101010000100001011101010000010110001000001111011100110100010101000000000111100100111000000001011111111000010000010010100011101101001100100010000000000100101010001101010100110001010010100001001000001000...

output:

241078524

result:

ok single line: '241078524'

Test #36:

score: 0
Accepted
time: 70ms
memory: 16428kb

input:

100101101101001010100000101111000011010000111000001111010100011000100011000001101100001110110011110110100000001000001111000010100000000111001010111101001100101100110010010100010110101001000001111000100110100010011101001001011000010110101010000011001001111000000100101000110011010000101100001011111001...

output:

91723864

result:

ok single line: '91723864'

Test #37:

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

input:

110111111111111111111111101111111110011111110111111111011111111111011101111111111111111011111111110011111111111101111001111101101111111101111111101111111111111110101110111101111111111111111111111011111111111111111010111111111011111111111000111110110111011110110111111111011111001111011111000101110111...

output:

9523730

result:

ok single line: '9523730'

Test #38:

score: 0
Accepted
time: 223ms
memory: 26368kb

input:

111111111111110111011111101111111011110111011111011111111111100111111111111101111111110011111111011111110111111111111110111110001101111111111100011101111111111110110111111111101111111111111111111111011010011111111111011111111110111011111110111111111111111101111101011111111111111111111111111111111111...

output:

765488307

result:

ok single line: '765488307'

Test #39:

score: 0
Accepted
time: 287ms
memory: 73928kb

input:

100000000000100011000010000000000001101001100000010111000010000000010001001000000000000001100000010001000000000000000011100000101000000010000001000000000001000001001010010011000010000010000100000001010100000000001000010001100001110001000001010010000100000100100000001010000001001001001011101000000100...

output:

66414147

result:

ok single line: '66414147'

Test #40:

score: 0
Accepted
time: 1076ms
memory: 101296kb

input:

111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

output:

567916510

result:

ok single line: '567916510'

Extra Test:

score: 0
Extra Test Passed