QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#129153#5874. Mystery Squarenhuang6850 2094ms3580kbC++203.7kb2023-07-22 01:50:102023-07-22 01:50:11

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-22 01:50:11]
  • 评测
  • 测评结果:0
  • 用时:2094ms
  • 内存:3580kb
  • [2023-07-22 01:50:10]
  • 提交

answer

/**
 * @file qoj5874F1.cpp
 * @author n685
 * @brief
 * @date 2023-07-09
 *
 *
 */
#include <bits/stdc++.h>

#ifdef LOCAL
std::ifstream cin;
std::ofstream cout;
using std::cerr;
#else
using std::cin;
using std::cout;
#define cerr                                                                   \
  if (false)                                                                   \
  std::cerr
#endif

using U = __int128_t;
int n;
const __int128_t ONE = 1;
U ans;
U s, a;
bool g = false;

void print(U v) {
  std::string ss;
  for (int i = 0; i < n; ++i) {
    if ((ONE << i) & v)
      ss += "1";
    else
      ss += "0";
  }
  std::reverse(ss.begin(), ss.end());
  cout << ss << '\n';
}

bool good(U val) {
  return (((val * val) & s) == s && ((val * val) | s | a) == (s | a));
}

void solve1() {
  auto gen = [&](auto &self, int node) -> void {
    if (node == n) {
      U v = s | ((ONE << (n >> 1)) - 1);
      U l = 0, r = std::numeric_limits<int64_t>::max();
      while (l < r) {
        U mid = (l + r + 1) / 2;
        if (mid * mid <= v)
          l = mid;
        else
          r = mid - 1;
      }
      if (good(l)) {
        // print(l * l);
        ans = l * l;
        g = true;
      }
      return;
    }
    if (g)
      return;
    self(self, node + 1);
    if (g)
      return;
    if (a & (ONE << node)) {
      s ^= (ONE << node);
      self(self, node + 1);
      s ^= (ONE << node);
    }
  };
  gen(gen, n / 2);
}

void solve2() {
  auto root = [&](U val, bool second) -> U {
    U rt = 1;
    if (second)
      rt ^= 2;
    U v = 0;
    for (int i = 2; i < (n + 1) / 2; ++i) {
      U m = (ONE << (i + 2));
      v = rt;
      v = ((v * v) & (m - 1));
      U res = val & (m - 1);
      if (v != res)
        rt ^= (ONE << i);
    }
    return rt;
  };
  auto gen = [&](auto &self, int node) -> void {
    if (node == (n + 1) / 2 + 2) {
      U rt1 = root(s, false);
      U rt2 = root(s, true);
      if (good(rt1)) {
        ans = rt1 * rt1;
        g = true;
      } else if (good(rt2)) {
        ans = rt2 * rt2;
        g = true;
      }
      return;
    }
    if (g)
      return;
    self(self, node + 1);
    if (g)
      return;
    if ((a & (ONE << node))) {
      s ^= (ONE << node);
      self(self, node + 1);
      s ^= (ONE << node);
    }
  };
  gen(gen, 0);
}

int popcount(U val) {
  int ans = 0;
  while (val > 0) {
    if (val % 2)
      ans++;
    val /= 2;
  }
  return ans;
}

void solve() {
  std::string ss;
  cin >> ss;
  g = false;
  std::reverse(ss.begin(), ss.end());
  n = (int)ss.size();
  int cnt = 0;
  ans = 0;
  s = 0, a = 0;
  for (int i = 0; i < n; ++i) {
    if (ss[i] == '1')
      s ^= (ONE << i);
    else if (ss[i] == '?')
      a ^= (ONE << i);
  }

  while (a & 1) {
    s++;
    a--;
    int cnt1 = popcount(a >> (n >> 1));
    int cnt2 = popcount(a & ((ONE << ((n + 1) / 2 + 1)) - 1));

    if (cnt2 < cnt1)
      solve2();
    else
      solve1();
    n += cnt;
    if (g) {
      print(ans << cnt);
      return;
    }

    a >>= 2;
    s >>= 2;
    cnt += 2;
    n -= 2;
  }
  int cnt1 = popcount(a >> (n >> 1));
  int cnt2 = popcount(a & ((ONE << ((n + 1) / 2 + 1)) - 1));

  if (cnt2 < cnt1)
    solve2();
  else
    solve1();
  n += cnt;
  if (g) {
    print(ans << cnt);
    return;
  }
}

int main() {
#ifdef LOCAL
  cin.open("input.txt");
  cout.rdbuf()->pubsetbuf(0, 0);
  cout.open("output.txt");
#else
  cin.tie(nullptr)->sync_with_stdio(false);
#endif

  int t;
  cin >> t;
  for (int tt = 1; tt <= t; ++tt) {
    cout << "Case #" << tt << ": ";
    solve();
  }
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3580kb

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: Case #6: 1111111111111111111111111000000000000000000000000001
Case #7: 0010000000000000000000000000000000000000000000000000000
Case #8: 1011001110110001000110111110100100010001...

result:

wrong answer 5th lines differ - expected: 'Case #5: 1101111110000101100101010111011001110010011000010000100', found: 'Case #5: Case #6: 111111111111...1111000000000000000000000000001'

Subtask #2:

score: 0
Wrong Answer

Test #2:

score: 0
Wrong Answer
time: 2094ms
memory: 3516kb

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: Case #4: Case #5: 1111111...

result:

wrong answer 3rd lines differ - expected: 'Case #3: 100010011110010011001...0000000000000000000000000000000', found: 'Case #3: Case #4: Case #5: 111...0000000000000000000000000000001'