2 条题解

  • 0
    @ 2025-11-9 8:59:19

    按题意直接模拟即可。

    AC code:

    // Paste your Program A here
    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        string S;
        cin >> S;
        int X = 0;
        for (int i = 0; i < S.size(); ++i) {
            if (S[i] == '1') {
                X ^= (i + 1);
            }
        }
        
        cout << X << endl;
        return 0;
    }
    
    /* ATTENTION!!! THIS IS THE BARRIER!!! */
    
    // Paste your Program B here
    #include <bits/stdc++.h>
    using namespace std;
    
    int main()
    {
        string T;
        int X;
        cin >> T >> X;
        int D = 0;
        int t_xor = 0;
        for (int i = 0; i < T.size(); ++i) {
            if (T[i] == '1') {
                t_xor ^= (i + 1);
            }
        }
        int diff = X ^ t_xor;
        if (diff > 0 && diff <= T.size()) {
            D = diff;
        }
        cout << D << endl;
        return 0;
    }
    
    • 0
      @ 2025-9-4 21:59:17

      感谢 对本题的加强建议。

      如果 A 程序可以向 B 程序传递一个不大于 231−12^{31}-1 的非负整数,那么这就是一个 Hash 模板题。弱化后的题目随便用什么 Hash 函数都能过,比如「所有 1\texttt{1} 的位置编号之和 mod 12345678\text{mod } 12345678」。

      然而,这道题目只允许 A 程序向 B 程序传递一个不大于 220−12^{20}-1 的非负整数,因此我们要优化 Hash 函数——把 Hash 函数改为「所有 1\texttt{1} 的位置编号之异或和」就可以通过此题了。

      #include <bits/stdc++.h>
      using namespace std;
      int main()
      {
          string S;
          cin >> S;
          int X = 0;
          int n = S.size();
          S = ' ' + S;
          for (int i = 1; i <= n; i++)
              if (S[i] == '1') X ^= i;
          cout << X << endl;
          return 0;
      }
      
      /* ATTENTION!!! THIS IS THE BARRIER!!! */
      
      #include <bits/stdc++.h>
      using namespace std;
      int main()
      {
          string T;
          int X;
          cin >> T >> X;
          int D = 0;
          int n = T.size();
          T = ' ' + T;
          for (int i = 1; i <= n; i++)
              if (T[i] == '1') X ^= i;
          D = X;
          cout << D << endl;
          return 0;
      }
      
      • 1

      信息

      ID
      14
      时间
      1000ms
      内存
      256MiB
      难度
      6
      标签
      递交数
      108
      已通过
      11
      上传者