QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#65784#4888. Decoding The Messagejapan022022#RE 14ms3532kbC++147.0kb2022-12-03 16:54:232022-12-03 16:54:26

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-03 16:54:26]
  • 评测
  • 测评结果:RE
  • 用时:14ms
  • 内存:3532kb
  • [2022-12-03 16:54:23]
  • 提交

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


int C[256];
int N;

namespace modP {

template <int M> ModInt<M> run() {
  using Mint = ModInt<M>;
  Mint val = 0;
  for (int i = 0; i < 256; ++i) {
    val += Mint(C[i]) * Mint(i);
  }
  for (int n = 1; n <= N; ++n) {
    if (val.x == 0 || val.x == 1) {
      break;
    }
    val = val.pow(n);
  }
  return val;
}

}  // modP

namespace mod257 {

using Mint = ModInt<257>;

int mod(int x) {
  return (x >= 1) ? (1 + (x - 1) % 256) : x;
}
int bn[12][12];
int crt[6][257], nxt[6][257];

bitset<257> dp[2][257];

bool canZero() {
  const int im = max_element(C, C + 256) - C;
  if (N >= 2 * 257 && N - C[im] >= 257) {
    return true;
  }
  int s = 0;
  for (int r = 0; r < 257; ++r) {
    dp[s][r].reset();
  }
  dp[s][0][0] = true;
  for (int i = 0; i < N; ++i) if (i != im) {
    for (int r = 0; r < 257; ++r) {
      dp[s ^ 1][r].reset();
    }
    for (int r = 0; r < 257; ++r) {
      for (int a = 0; a <= C[i]; ++a) {
        const int rr = Mint(r + (C[i] - 2 * a) * i).x;
        dp[s ^ 1][rr] |= dp[s][r] << a;
      }
    }
    s ^= 1;
  }
  for (int r = 0; r < 257; ++r) for (int k = 0; k < 257; ++k) if (dp[s][r][k]) {
    const int am = N / 2 - k;
    if (0 <= am && am <= C[im]) {
      if (!Mint(r + (C[im] - 2 * am) * im)) {
        return true;
      }
    }
  }
  return false;
}

Mint run() {
// cerr<<"canZero = "<<canZero()<<endl;
  if (N <= 11) {
    for (int n = 0; n <= N; ++n) {
      bn[n][0] = bn[n][n] = 1;
      for (int k = 1; k < n; ++k) {
        bn[n][k] = mod(bn[n - 1][k - 1] + bn[n - 1][k]);
      }
    }
    int fac = 1;
    for (int n = 1; n <= N / 2; ++n) {
      if (!fac) break;
      fac = mod(fac * n);
    }
    for (int n = 1; n <= N - N / 2; ++n) {
      if (!fac) break;
      fac = mod(fac * n);
    }
// cerr<<"N = "<<N<<": fac = "<<fac<<endl;
    memset(crt, 0, sizeof(crt));
    crt[0][0] = fac;
    for (int i = 0; i < 256; ++i) if (C[i]) {
      memset(nxt, 0, sizeof(nxt));
      for (int k = 0; k <= N / 2; ++k) {
        for (int r = 0; r < 257; ++r) {
          for (int a = 0; a <= C[i] && k + a <= N / 2; ++a) {
            const int rr = Mint(r + (C[i] - 2 * a) * i).x;
            nxt[k + a][rr] = mod(nxt[k + a][rr] + crt[k][r] * bn[C[i]][a]);
          }
        }
      }
      memcpy(crt, nxt, sizeof(nxt));
    }
    Mint ans = 1;
    for (int r = 0; r < 257; ++r) {
      ans *= Mint(r).pow(crt[N / 2][r]);
    }
    return ans;
  } else {
    return canZero() ? 0 : 1;
  }
}

}  // mod257

int main() {
  for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    memset(C, 0, sizeof(C));
    int K;
    scanf("%d", &K);
    for (; K--; ) {
      int i;
      scanf("%d", &i);
      scanf("%d", &C[i]);
    }
    N = 0;
    for (int i = 0; i < 256; ++i) {
      N += C[i];
    }
    
    const auto ans3 = modP::run<3>();
    const auto ans5 = modP::run<5>();
    const auto ans17 = modP::run<17>();
    const auto ans257 = mod257::run();
// cerr<<"ans3 = "<<ans3<<endl;
// cerr<<"ans5 = "<<ans5<<endl;
// cerr<<"ans17 = "<<ans17<<endl;
// cerr<<"ans257 = "<<ans257<<endl;
    for (int x = ans257.x; ; x += 257) {
      if (!(ans3 - x) && !(ans5 - x) && !(ans17 - x)) {
        printf("%d\n", x);
        break;
      }
    }
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 3532kb

input:

5
1
42 1
2
0 1
1 1
1
239 2
2
1 1
2 1
3
1 1
2 2
3 2

output:

42
256
514
1284
61726

result:

ok 5 number(s): "42 256 514 1284 61726"

Test #2:

score: 0
Accepted
time: 14ms
memory: 3512kb

input:

100
1
213 79
1
54 69
1
218 55
1
248 80
1
101 8
1
153 79
1
240 45
1
112 70
1
217 5
1
208 64
1
48 30
1
0 19
1
53 40
1
63 17
1
179 65
1
221 22
1
135 84
1
138 20
1
54 29
1
114 19
1
253 94
1
240 36
1
40 94
1
244 93
1
239 24
1
133 8
1
105 91
1
45 43
1
241 74
1
206 17
1
100 73
1
133 44
1
57 70
1
56 72
1
47...

output:

21846
21846
26215
59110
32896
6426
48060
59110
43570
32896
15420
0
59110
6426
26215
17476
15420
15420
21846
21846
32896
15420
59110
21846
54741
32896
48060
48060
32896
50116
26215
32896
15420
54741
6426
17476
15420
21846
54741
39321
54741
54741
6426
54741
1
59110
59110
26215
54741
15420
15420
22101
...

result:

ok 100 numbers

Test #3:

score: -100
Runtime Error

input:

100
1
208 74003161
1
108 37880052
1
102 289342450
1
190 16254190
1
20 507132853
1
1 842472902
1
212 226854721
1
33 797732105
1
213 114087750
1
128 914592259
1
27 779924279
1
203 425018504
1
217 458915584
1
139 710603120
1
226 84538604
1
50 602470204
1
150 228443262
1
48 593328022
1
24 35949157
1
148...

output:


result: