QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#648175#9463. 基础 ABC 练习题hos_lyric#0 2958ms89820kbC++145.8kb2024-10-17 17:29:422024-10-17 17:29:42

Judging History

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

  • [2024-10-17 17:29:42]
  • 评测
  • 测评结果:0
  • 用时:2958ms
  • 内存:89820kb
  • [2024-10-17 17:29:42]
  • 提交

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")


namespace exper {
constexpr int N = 5;
int id(int x, int y) {
  return x * (N + 1) + y;
}
map<string, Int> cache;
Int solve(const string &s) {
  auto it = cache.find(s);
  if (it != cache.end()) return it->second;
  Int ret = 0;
  
  assert((int)s.size() % 3 == 0);
  const int n = s.size() / 3;
  if (n == 0) {
    ret |= 1LL << id(0, 0);
  } else {
    for (int i = 1; i < 3*n; ++i) if ((s[i] - s[0] - 1) % 3 == 0) {
      for (int j = i + 1; j < 3*n; ++j) if ((s[j] - s[i] - 1) % 3 == 0) {
        const Int res = solve(s.substr(1, i - 1) + s.substr(i + 1, j - (i + 1)) + s.substr(j + 1));
        ret |= res << ((s[0] == 'A') ? (N + 1) : (s[0] == 'B') ? 1 : 0);
      }
    }
  }
  
  return ret;
}
void run() {
  for (int n = 1; n <= N; ++n) {
    string s = string(n, 'A') + string(n, 'B') + string(n, 'C');
    do {
      const Int res = solve(s);
      int now[3] = {}, mxs[3] = {};
      for (int i = 0; i < 3*n; ++i) {
        ++now[s[i] - 'A'];
        chmax(mxs[0], now[0] - now[2]);
        chmax(mxs[1], now[1] - now[0]);
        chmax(mxs[2], now[2] - now[1]);
      }
      if (res) {
        printf("%s %d %d %d\n", s.c_str(), mxs[0], mxs[1], mxs[2]);
        for (int x = 0; x <= n; ++x) {
          for (int y = 0; x + y <= n; ++y) putchar(".#"[res >> id(x, y) & 1]);
          puts("");
        }
        assert(mxs[0] + mxs[1] + mxs[2] <= n);
        for (int x = 0; x <= n; ++x) for (int y = 0; x + y <= n; ++y) {
          const int z = n - x - y;
          assert((bool)(res >> id(x, y) & 1) == (x >= mxs[0] && y >= mxs[1] && z >= mxs[2]));
        }
      } else {
        assert(mxs[0] + mxs[1] + mxs[2] > n);
      }
    } while (next_permutation(s.begin(), s.end()));
    fflush(stdout);
    cerr << "DONE n = " << n << endl;
  }
}
}  // exper


constexpr int MAX_N = 35;
constexpr int MAX_U = (MAX_N + 1) * (MAX_N + 2) * (MAX_N + 3) / 6;

int subtaskId;
int N;
char A[210], B[210], S[210];

int U;
int ids[MAX_N + 1][MAX_N + 1][MAX_N + 1];
int xs[MAX_U], ys[MAX_U], zs[MAX_U];
unsigned dp[2][MAX_N + 1][MAX_N + 1][MAX_U + 1];

int main() {
  // exper::run();
  
  for (int numCases; ~scanf("%d%d", &numCases, &subtaskId); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    scanf("%d", &N);
    scanf("%s", A);
    scanf("%s", B);
    scanf("%s", S);
    
    if (N <= MAX_N) {
      U = 0;
      for (int x = 0; x <= N; ++x) for (int y = 0; x + y <= N; ++y) for (int z = 0; x + y + z <= N; ++z) {
        const int u = ids[x][y][z] = U++;
        xs[u] = x;
        ys[u] = y;
        zs[u] = z;
      }
      assert(U <= MAX_U);
      for (int x = 0; x <= N; ++x) for (int y = 0; y <= N; ++y) for (int z = 0; z <= N; ++z) if (x + y + z > N) ids[x][y][z] = U;
      for (int a = 0; a <= N; ++a) for (int b = 0; b <= N; ++b) memset(dp[0][a][b], 0, (U + 1) * sizeof(unsigned));
      dp[0][0][0][ids[0][0][0]] = 1;
      for (int i = 0; i < 3*N; ++i) {
        auto crt = dp[i & 1], nxt = dp[(i + 1) & 1];
        for (int a = 0; a <= N; ++a) for (int b = 0; b <= N; ++b) memset(nxt[a][b], 0, (U + 1) * sizeof(unsigned));
        for (int a = 0; a <= N; ++a) for (int b = 0; b <= N; ++b) {
          const int c = i - a - b;
          if (c >= 0) {
            const int x0 = max(a - c, 0);
            const int y0 = max(b - a, 0);
            const int z0 = max(c - b, 0);
            for (int x = x0; x + y0 + z0 <= N; ++x) for (int y = y0; x + y + z0 <= N; ++y) for (int z = z0; x + y + z <= N; ++z) {
              const int u = ids[x][y][z];
              if (crt[a][b][u]) {
                if (a < N && (S[i] == '?' || S[i] == 'A')) nxt[a + 1][b][ids[max(x, a+1-c)][y][z]] += crt[a][b][u];
                if (b < N && (S[i] == '?' || S[i] == 'B')) nxt[a][b + 1][ids[x][max(y, b+1-a)][z]] += crt[a][b][u];
                if (c < N && (S[i] == '?' || S[i] == 'C')) nxt[a][b]    [ids[x][y][max(z, c+1-b)]] += crt[a][b][u];
              }
            }
          }
        }
      }
      unsigned ans = 0;
      for (int x = 0; x <= N; ++x) for (int y = 0; x + y <= N; ++y) for (int z = 0; x + y + z <= N; ++z) {
        const int d = N - (x + y + z);
        bool ok = false;
        for (int dx = 0; dx <= d; ++dx) for (int dy = 0; dx + dy <= d; ++dy) {
          ok = ok || (A[x + dx] == '1' && B[y + dy] == '1');
        }
        if (ok) {
// cerr<<"ok "<<x<<" "<<y<<" "<<z<<": "<<dp[(3*N) & 1][N][N][ids[x][y][z]]<<endl;
          ans += dp[(3*N) & 1][N][N][ids[x][y][z]];
        }
      }
      printf("%u\n", ans);
    } else {
      puts("-1");
    }
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Time Limit Exceeded

Test #1:

score: 15
Acceptable Answer
time: 2941ms
memory: 89512kb

input:

60 1
1
11
11
ABC
2
111
111
CABABC
3
1111
1111
CAABBCBAC
4
11111
11111
BACBBACBACAC
5
111111
111111
CABCCBBAABCCBAA
6
1111111
1111111
ABABABCACBCBCCACBA
7
11111111
11111111
BCAABACBBCBBABCCAACAC
8
111111111
111111111
CCBCBBBCAABCBCAAAAACBCBA
9
1111111111
1111111111
CCCCACABCBABAABCCAABABBCBBA
10
1111...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

result:

points 0.75 the max n you choose to answer is 35

Test #2:

score: 15
Acceptable Answer
time: 2915ms
memory: 89508kb

input:

60 1
1
11
11
CBA
2
111
111
BACACB
3
1111
1111
BCBCACABA
4
11111
11111
CCBACABBBCAA
5
111111
111111
BCACBBABBCCAACA
6
1111111
1111111
BBCBACCAACBCBCAABA
7
11111111
11111111
ACBCCBBAABAABCACCACBB
8
111111111
111111111
BAACACBACCCBAACCBABABBCB
9
1111111111
1111111111
BABCBCAAAAABBCCCACBCBBABACC
10
1111...

output:

0
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

result:

points 0.75 the max n you choose to answer is 35

Test #3:

score: 15
Acceptable Answer
time: 2954ms
memory: 89464kb

input:

60 1
1
11
11
BCA
2
111
111
BCABCA
3
1111
1111
CBACCAABB
4
11111
11111
BACBCBBCCAAA
5
111111
111111
BCCCBABACCBABAA
6
1111111
1111111
ACAACBABABBCACBCCB
7
11111111
11111111
BBBCABCCCAABCACBACAAB
8
111111111
111111111
ACCACAABACBAABBCBCBBACBC
9
1111111111
1111111111
BCCBACBBACCCBCCAABAACABAABB
10
1111...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

result:

points 0.75 the max n you choose to answer is 35

Test #4:

score: 15
Acceptable Answer
time: 2913ms
memory: 89516kb

input:

60 1
1
11
11
BCA
2
111
111
ACABCB
3
1111
1111
BABCABCCA
4
11111
11111
CCABACABBACB
5
111111
111111
ABBBCBBCACCAACA
6
1111111
1111111
CACBABCABCCBABAACB
7
11111111
11111111
BACBCABACBBCCCBAAACAB
8
111111111
111111111
CABABBCAACABCBACBABACBCC
9
1111111111
1111111111
BCBAACBABABCBACBABABCCACACC
10
1111...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

result:

points 0.75 the max n you choose to answer is 35

Test #5:

score: 15
Acceptable Answer
time: 2958ms
memory: 89464kb

input:

60 1
1
11
11
ABC
2
111
111
BBCACA
3
1111
1111
ACBBCBAAC
4
11111
11111
ABACACCCABBB
5
111111
111111
ACCCCCAAABABBBB
6
1111111
1111111
ABAABBBBCCCCCCAABA
7
11111111
11111111
ACBBBACCCCCCAABABBBAA
8
111111111
111111111
CAAABAAACCCCBBCBBBCACBAB
9
1111111111
1111111111
ABAAACBBCCCCCCCBAAABAACBBBB
10
1111...

output:

1
1
0
0
0
1
1
0
1
1
1
1
1
0
1
1
1
0
0
0
0
1
1
1
0
1
1
1
1
1
0
0
1
0
1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

result:

points 0.75 the max n you choose to answer is 35

Test #6:

score: 15
Acceptable Answer
time: 2919ms
memory: 89820kb

input:

60 1
1
11
11
BCA
2
111
111
ACCBAB
3
1111
1111
BACCACBBA
4
11111
11111
AAABBCBCBACC
5
111111
111111
AABBBBCCBCCAACA
6
1111111
1111111
AAACCBCCCAABACBBBB
7
11111111
11111111
AAACACCACBBAABBCBCCBB
8
111111111
111111111
AAACAAABBBBBBBCBACCCACCC
9
1111111111
1111111111
BBCCACCCACCACCBAABBAAAABBBB
10
1111...

output:

1
0
0
1
1
0
1
1
1
1
1
0
0
0
1
1
1
0
0
1
0
1
1
1
1
1
1
0
1
1
1
1
1
0
0
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

result:

points 0.75 the max n you choose to answer is 35

Test #7:

score: 0
Time Limit Exceeded

input:

60 1
1
11
11
BCA
2
111
111
ACCABB
3
1111
1111
CAABBBCAC
4
11111
11111
BBCCBCCABAAA
5
111111
111111
BAABBBCCCCCAABA
6
1111111
1111111
AACACCCCBABBBCAABB
7
11111111
11111111
AACBBCBBBCCCCCAAABBAA
8
111111111
111111111
AAAABBCBBBBCBBCCCCCCAAAA
9
1111111111
1111111111
ABBBBBBBBCCBCAACCACAACCAAAC
10
1111...

output:

1
0
1
1
1
0
1
1
1
1
0
1
1
0
1
1
0
0
1
0
1
1
0
1
1
0
0
0
0
1
1
0
0
1
0
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

result:


Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Time Limit Exceeded

Test #22:

score: 0
Time Limit Exceeded

input:

60 3
1
11
11
???
2
111
111
??????
3
1111
1111
?????????
4
11111
11111
????????????
5
111111
111111
???????????????
6
1111111
1111111
??????????????????
7
11111111
11111111
?????????????????????
8
111111111
111111111
????????????????????????
9
1111111111
1111111111
???????????????????????????
10
1111...

output:


result:


Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 0
Skipped

Dependency #1:

0%