|
| 1 | +// chk.cc — Checker for "Distinct Pairwise XOR Set" |
| 2 | +// Style: modeled after the knight-path checker; prints |
| 3 | +// quitp(ratio, "Valid XOR set. Your=%d Best=%lld Ratio: %.8f", ...) |
| 4 | +// Ratio = (size of participant set) / (size of best set from ans.txt) |
| 5 | + |
| 6 | +#include "testlib.h" |
| 7 | +#include <bits/stdc++.h> |
| 8 | +using namespace std; |
| 9 | + |
| 10 | +static bool readMaybeLong(InStream& S, long long &x){ |
| 11 | + try { x = S.readLong(); return true; } |
| 12 | + catch(...) { return false; } |
| 13 | +} |
| 14 | +static bool readMaybeInt(InStream& S, int &x){ |
| 15 | + try { x = S.readInt(); return true; } |
| 16 | + catch(...) { return false; } |
| 17 | +} |
| 18 | + |
| 19 | +// Compute K = ceil(log2(n+1)) so that all XORs of numbers in [1..n] fit in [0..2^K-1] |
| 20 | +static int xorBitWidth(long long n){ |
| 21 | + unsigned long long nu = (unsigned long long)max(1LL, n); |
| 22 | + int k = 64 - __builtin_clzll(nu); // ceil(log2(nu+?)), good for n>=1 |
| 23 | + if ((1ULL << k) <= (unsigned long long)n) ++k; // ensure 2^k > n when n is exact power-1 edge |
| 24 | + return max(1, k); |
| 25 | +} |
| 26 | + |
| 27 | +struct ReadResult { |
| 28 | + vector<int> a; |
| 29 | + long long m = 0; |
| 30 | +}; |
| 31 | + |
| 32 | +// Read "length-first" set: |
| 33 | +// m |
| 34 | +// v1 v2 ... vm |
| 35 | +// who = "participant" or "answer" |
| 36 | +static ReadResult readSetLenOnly(InStream& S, long long n, const char* who, bool allowZero){ |
| 37 | + ReadResult R; |
| 38 | + long long m; |
| 39 | + if (!readMaybeLong(S, m)){ |
| 40 | + if (string(who)=="participant") quitp(0.0, "Empty output (no m). Score=0.0"); |
| 41 | + quitf(_fail, "Answer file is empty (no m)."); |
| 42 | + } |
| 43 | + if (m < 0 || m > n){ |
| 44 | + if (string(who)=="participant") quitp(0.0, "Invalid m=%lld. Score=0.0", m); |
| 45 | + quitf(_fail, "Answer file: invalid m=%lld.", m); |
| 46 | + } |
| 47 | + if (m == 0 && !allowZero){ |
| 48 | + if (string(who)=="participant") quitp(0.0, "m=0 not allowed here. Score=0.0"); |
| 49 | + quitf(_fail, "Answer file: m=0 not allowed."); |
| 50 | + } |
| 51 | + |
| 52 | + R.m = m; |
| 53 | + R.a.reserve((size_t)m); |
| 54 | + vector<unsigned char> used((size_t)n + 1, 0); |
| 55 | + |
| 56 | + for (long long i = 0; i < m; ++i){ |
| 57 | + int x; |
| 58 | + if (!readMaybeInt(S, x)){ |
| 59 | + if (string(who)=="participant") quitp(0.0, "Output ended before %lld numbers. Score=0.0", m); |
| 60 | + quitf(_fail, "Answer file: ended before %lld numbers.", m); |
| 61 | + } |
| 62 | + if (x < 1 || x > n){ |
| 63 | + if (string(who)=="participant") quitp(0.0, "Number out of range at pos %lld: %d (need 1..%lld). Score=0.0", i+1, x, n); |
| 64 | + quitf(_fail, "Answer file: number out of range at pos %lld: %d (need 1..%lld).", i+1, x, n); |
| 65 | + } |
| 66 | + if (used[x]){ |
| 67 | + if (string(who)=="participant") quitp(0.0, "Duplicate number: %d. Score=0.0", x); |
| 68 | + quitf(_fail, "Answer file: duplicate number: %d.", x); |
| 69 | + } |
| 70 | + used[x] = 1; |
| 71 | + R.a.push_back(x); |
| 72 | + } |
| 73 | + // Ignore any extra tokens after m numbers. |
| 74 | + return R; |
| 75 | +} |
| 76 | + |
| 77 | +static void checkXorDistinct(const vector<int>& a, long long n, const char* who){ |
| 78 | + long long m = (long long)a.size(); |
| 79 | + if (m <= 1) return; // vacuously distinct |
| 80 | + |
| 81 | + int K = xorBitWidth(n); |
| 82 | + long long cap = 1LL << K; // number of distinct XOR values available [0..2^K-1] |
| 83 | + long long pairs = m * (m - 1) / 2; |
| 84 | + |
| 85 | + // Quick impossibility check |
| 86 | + if (pairs > cap){ |
| 87 | + if (string(who)=="participant") |
| 88 | + quitp(0.0, "Impossible: m=%lld yields %lld pairs > %lld available XORs. Score=0.0", m, pairs, cap); |
| 89 | + quitf(_fail, "Answer file: m=%lld yields %lld pairs > %lld available XORs.", m, pairs, cap); |
| 90 | + } |
| 91 | + |
| 92 | + vector<unsigned char> seen((size_t)cap, 0); |
| 93 | + for (long long i = 0; i < m; ++i){ |
| 94 | + for (long long j = i + 1; j < m; ++j){ |
| 95 | + int v = a[i] ^ a[j]; |
| 96 | + if (seen[(size_t)v]){ |
| 97 | + if (string(who)=="participant") |
| 98 | + quitp(0.0, "XOR collision: a[%lld]=%d XOR a[%lld]=%d = %d already seen. Score=0.0", |
| 99 | + i+1, a[i], j+1, a[j], v); |
| 100 | + quitf(_fail, "Answer file: XOR collision between indices %lld and %lld (value %d).", i+1, j+1, v); |
| 101 | + } |
| 102 | + seen[(size_t)v] = 1; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +int main(int argc, char* argv[]){ |
| 108 | + registerTestlibCmd(argc, argv); |
| 109 | + if (argc < 4) { |
| 110 | + quitf(_fail, "Usage: %s in.txt out.txt ans.txt", argv[0]); |
| 111 | + } |
| 112 | + |
| 113 | + // Optional: ensure ans.txt exists and is not 0 bytes |
| 114 | + { |
| 115 | + ifstream f(argv[3], ios::binary); |
| 116 | + if (!f) quitf(_fail, "Cannot open %s", argv[3]); |
| 117 | + f.seekg(0, ios::end); |
| 118 | + if (f.tellg() == 0) quitf(_fail, "ans.txt is empty (0 bytes)."); |
| 119 | + } |
| 120 | + |
| 121 | + // Read input: n |
| 122 | + long long n; |
| 123 | + try { n = inf.readLong(1LL, 10000000LL, "n"); } |
| 124 | + catch(...) { quitf(_fail, "Failed to read valid n from input."); } |
| 125 | + |
| 126 | + // Read best (answer) and your (participant) sets |
| 127 | + auto best = readSetLenOnly(ans, n, "answer", /*allowZero=*/false); |
| 128 | + auto yours = readSetLenOnly(ouf, n, "participant", /*allowZero=*/true); |
| 129 | + |
| 130 | + // Validate XOR-distinctness for both |
| 131 | + checkXorDistinct(best.a, n, "answer"); |
| 132 | + checkXorDistinct(yours.a, n, "participant"); |
| 133 | + |
| 134 | + long long Best = best.m; |
| 135 | + int Your = (int)yours.m; |
| 136 | + |
| 137 | + double ratio = (Best == 0) ? 0.0 : (double)Your / (double)Best; |
| 138 | + if (ratio < 0) ratio = 0; |
| 139 | + if (ratio > 1) ratio = 1; |
| 140 | + |
| 141 | + quitp(ratio, "Valid XOR set. Your=%d Best=%lld Ratio: %.8f", Your, Best, ratio); |
| 142 | +} |
0 commit comments