QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#405743#5874. Mystery Squarewsyear41 ✓918ms13540kbC++174.1kb2024-05-06 13:03:562024-05-06 13:04:00

Judging History

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

  • [2024-05-06 13:04:00]
  • 评测
  • 测评结果:41
  • 用时:918ms
  • 内存:13540kb
  • [2024-05-06 13:03:56]
  • 提交

answer

#include <bits/stdc++.h>

#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;

template<class T>inline void chkmn(T &x, T y) { if (y < x) x = y; }
template<class T>inline void chkmx(T &x, T y) { if (y > x) x = y; }

using namespace std;

const int maxn = 130;

int n, a[maxn], cao;
string str;

void solve1(int m) {
  __int128 L = 0, R = 0;
  rep (i, 1, m) L = L * 2 + a[i], R = R * 2 + a[i];
  rep (i, m + 1, n) L = L * 2, R = R * 2 + 1;
  __int128 l = sqrtl(L), r = sqrtl(R);
  while (l * l < L) l++;
  while (r * r > R) r--;
  for (__int128 x = l; x <= r; x++) {
    __int128 res = x * x;
    int ok = 1;
    rep (i, 1, n) if (a[i] != -1) ok &= (a[i] == (res >> (n - i) & 1));
    if (ok) {
      per (i, n - 1, 0) cout << int(res >> i & 1);
      cao = 1;
      return;
    }
  }
}

bool check(__int128 x, int s, int t, int del) {
  rep (i, s, t) if (a[i] != -1 && a[i] != (x >> (n - i - del) & 1)) return 0;
  return 1;
}

void solve2(int m) {
  int pos = n + 1;
  while (pos - 1 > 0 && a[pos - 1] == 0) pos--;
  pos = max(pos, m + 1);
  if ((n - pos + 1) & 1) return;
  int zero = (n - pos + 1) / 2;
  pos--;
  vector<__int128> vec;
  vec.emplace_back(0);
  int cop = (n - 2 * zero) / 2;
  int w = 0;
  per (i, pos, max(1, pos - cop)) if (a[i] == -1) w++;
  if (w > 20) {
    int cur = max(1, pos - cop);
    vector<int> dpos;
    rep (i, 1, cur) if (a[i] == -1) dpos.emplace_back(i);
    rep (S, 0, (1 << SZ(dpos)) - 1) {
      rep (i, 0, SZ(dpos) - 1) a[dpos[i]] = (S >> i & 1);
      __int128 L = 0, R = 0;
      rep (i, 1, cur) L = L * 2 + a[i], R = R * 2 + a[i];
      rep (i, cur + 1, pos) L = L * 2, R = R * 2 + 1;
      __int128 l = sqrtl(L), r = sqrtl(R);
      while (l * l < L) l++;
      while (r * r > R) r--;
      for (__int128 x = l; x <= r; x++) {
        __int128 res = x * x;
        res = res * (((__int128)1) << (zero * 2));
        int ok = 1;
        rep (i, 1, n) if (a[i] != -1) ok &= (a[i] == (res >> (n - i) & 1));
        if (ok) {
          per (i, n - 1, 0) cout << int(res >> i & 1);
          cao = 1;
          return;
        }
      }
    }
    for (int x : dpos) a[x] = -1;
    return;
  }
  per (i, pos, max(1, pos - cop)) {
    vector<__int128> nxt;
    for (__int128 x : vec) {
      if (check(x * x, i - 1, i, zero * 2)) nxt.emplace_back(x);
      x |= (((__int128)1) << (pos - i));
      if (check(x * x, i - 1, i, zero * 2)) nxt.emplace_back(x);
    }
    vec = nxt;
  }
  for (__int128 x : vec) {
    x = x * (((__int128)1) << zero);
    __int128 res = x * x;
    if (res >= (((__int128)1) << n)) continue;
    int ok = 1;
    rep (i, 1, n) if (a[i] != -1) ok &= (a[i] == (res >> (n - i) & 1));
    if (ok) {
      per (i, n - 1, 0) cout << int(res >> i & 1);
      cao = 1;
      return;
    }
  }
}

void work() {
  cao = 0;
  cin >> str, n = str.length();
  rep (i, 1, n) {
    if (str[i - 1] == '?') a[i] = -1;
    else a[i] = str[i - 1] - '0';
  }
  int nn = n;
  while (nn >= 4 && a[nn] == a[nn - 1] && a[nn] == 0) nn -= 2;
  int tmp = n - nn;
  n = nn;
  int m = n / 2, c1 = 0, c2 = 0;
  rep (i, 1, m) c1 += (a[i] == -1);
  rep (i, m + 1, n) c2 += (a[i] == -1);
  if (c1 <= c2) {
    vector<int> pos;
    rep (i, 1, m) if (a[i] == -1) pos.emplace_back(i);
    rep (S, 0, (1 << c1) - 1) {
      if (cao) break;
      rep (i, 0, c1 - 1) a[pos[i]] = (S >> i & 1);
      solve1(m);
    }
  } else {
    int pos = n + 1;
    while (pos - 1 > 0 && (a[pos - 1] == -1 || a[pos - 1] == 0)) pos--;
    per (i, n, pos) {
      if (cao) break;
      if (a[i] == -1) a[i] = 1, solve2(m);
      a[i] = 0;
    }
    if (!cao) solve2(m);
  }
  rep (i, 1, tmp) cout << 0;
  cout << '\n';
}

int main() {
  cin.tie(nullptr) -> ios::sync_with_stdio(false);
  int t; cin >> t;
  rep (i, 1, t) cout << "Case #" << i << ": ", work();
}

详细

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 1ms
memory: 3664kb

input:

25
1???
1
10??110??00??1000??
1??010?0110?1010?0?010?0111011?11100?100010?0??0??1
1??11????00??1?1?0?1??01??110110?11?00100110?00100?0?00
11?1?1???11111?11?1??11110000?00?????00??0?000?000?1
10??000000?0?00000?00000000??0?0000???00??????0000???
101???11??11000?????1??1?1??10??0?0100011?0001?01011001...

output:

Case #1: 1001
Case #2: 1
Case #3: 1011110110000100001
Case #4: 111010001100101000001010111011011100110001000110001
Case #5: 1101111110000101100101010111011001110010011000010000100
Case #6: 1111111111111111111111111000000000000000000000000001
Case #7: 1000000000000000000000000000000000000000000000000...

result:

ok 25 lines

Subtask #2:

score: 31
Accepted

Test #2:

score: 31
Accepted
time: 918ms
memory: 13540kb

input:

25
1????????????????????111101010000011100110101111000001011111100110000011000101100000010010110100101000????????????????????
10?11100?000111??11?01010110100??1?100111?001000000??0101?110?0111?011?11?1??00010111?010??100?100??10?010?001001110111110?1
1000100111100100110011010111100001111010?????????...

output:

Case #1: 11100101110101010110111110101000001110011010111100000101111110011000001100010110000001001011010010100001101011000010100001
Case #2: 1011110000001110111001010110100001110011100010000001101010110001111011111110000010111101010100010010101010100100111011111001
Case #3: 1000100111100100110011010...

result:

ok 25 lines

Extra Test:

score: 0
Extra Test Passed