結果
問題 | No.2895 Zero XOR Subset |
ユーザー |
![]() |
提出日時 | 2024-09-20 22:23:42 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,744 bytes |
コンパイル時間 | 3,105 ms |
コンパイル使用メモリ | 250,984 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-09-20 22:23:51 |
合計ジャッジ時間 | 8,699 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 14 WA * 21 |
ソースコード
#include <bits/stdc++.h>using namespace std;using ll = long long;const int MAX_BITS = 60;vector<ll> basis(MAX_BITS, 0); // Linear basis array of size 60vector<int> basis_idx(MAX_BITS); // To store one index corresponding to each basis vector// Function to insert number into the basisbool insertBasis(ll x, int idx) {vector<int> result;for (int i = MAX_BITS - 1; i >= 0; --i) {if (!(x & (1LL << i))) continue; // Skip if bit is not setif (!basis[i]) { // If this bit is not yet in the basisbasis[i] = x;basis_idx[i] = idx; // Store the index of this elementreturn true; // Inserted successfully}// Reduce x by the basis elementx ^= basis[i];result.push_back(basis_idx[i]);}// If x becomes 0, it means this element is a combination of basis elementsresult.push_back(idx);// Output the result indicesprintf("%d\n", (int)result.size());for (int i : result) {printf("%d ", i + 1); // Output 1-based index}printf("\n");return false; // This element was not added to the basis}int main() {int n;scanf("%d", &n);vector<ll> a(n);// Input the arrayfor (int i = 0; i < n; ++i) {scanf("%lld", &a[i]);}// Insert each element into the basisfor (int i = 0; i < n; ++i) {if (a[i] == 0) { // Handle edge case where element is already zeroprintf("1\n%d\n", i + 1);return 0;}if (!insertBasis(a[i], i)) {return 0; // Found a subset that XORs to zero, so stop}}// If no zero XOR subset was found, output -1puts("-1");return 0;}