Skip to content

Commit c2d07be

Browse files
authored
Added some more problems (#1)
1 parent 15d2251 commit c2d07be

15 files changed

Lines changed: 1221 additions & 0 deletions

File tree

algorithmic/problems/111/chk.cc

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type: default
2+
time: 1s
3+
memory: 128m
4+
5+
checker: chk.cc
6+
7+
subtasks:
8+
- score: 100
9+
n_cases: 1 # auto-picks 1.in…10.in and (logically) 1.out…10.out,
10+
# but your JudgeEngine first tries "X.ans" before "X.out".
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Problem: Distinct Pairwise XOR Set
2+
3+
Time Limit: 1 second
4+
Memory Limit: 512 MB
5+
6+
Description
7+
Given an integer n, find a subset S ⊆ {1, 2, ..., n} such that:
8+
1) For all pairs (a, b) with a, b ∈ S and a < b, the values (a XOR b) are all distinct (i.e., no two different unordered pairs produce the same XOR).
9+
2) |S| ≥ floor(sqrt(n / 2)).
10+
11+
Input
12+
A single integer n (1 ≤ n ≤ 10^7).
13+
14+
Output
15+
- First line: an integer m — the size of the set S.
16+
- Second line: m distinct integers in the range [1, n] — the elements of S, in any order.
17+
18+
Notes
19+
- Any valid S is accepted. You do NOT need to maximize m; you only need m ≥ floor(sqrt(n/2)).
20+
- The pairwise XOR distinctness means the set {a_i XOR a_j | 1 ≤ i < j ≤ m} has size m*(m-1)/2.
21+
- Multiple correct outputs may exist for the same n.
22+
- Print out the sequence with the longest length.
23+
24+
Sample
25+
Input
26+
49
27+
Output
28+
4
29+
1 2 3 4

0 commit comments

Comments
 (0)