How the Codeforces setup works, why each part exists, and how to use it. Download as PDF.
Codeforces judges your solution on hidden tests. You only see a few sample tests in the statement. cf-kit gives you a local loop that catches most mistakes before you submit:
It works in VS Code or Sublime Text, both with Vim keys. macOS is the tested platform.
curl -fsSL https://raw.githubusercontent.com/bobikenobi12/cf-kit/main/install.sh | bash
The installer clones the repository to ~/cf-kit, asks a few yes/no questions, and then:
Your problems are saved in contests/. Git ignores that folder, so your solutions are never committed.
Options: --yes accepts all defaults, --skip-editors leaves your editors alone. You can run the installer again at any time. It is safe to repeat.
After that, install the Competitive Companion extension in your browser. Its project page is github.com/jmerle/competitive-companion.
Keep the listener running in a terminal (python3 ~/cf-kit/scripts/listen.py), then:
contests/<contest>/<letter>/.sol.cpp and write the solution.Each problem folder contains:
sol.cpp your solution brute.cpp a slow but simple solution, for stress testing gen.py a random input generator, for stress testing problem.json name, URL and time limit tests/ the sample inputs (N.in) and expected outputs (N.ans) build/ compiled programs (created automatically)
The samples in a statement are usually small and easy. A solution can pass them and still fail on large or unusual inputs. That is why cf-kit has two checks: the judge runs the samples, and the stress tester searches for inputs where your solution disagrees with a brute force. A brute force is slow, but simple enough to trust.
Codeforces compiles C++ with GCC on Windows. On macOS the default g++ is Apple's clang, which does not include bits/stdc++.h, the header most competitive programmers use. So cf-kit uses GCC from Homebrew (g++-16).
Every compile uses the same flags, defined in scripts/common.py:
| Flag | Meaning |
|---|---|
-std=gnu++20 | The C++ standard used for most submissions. |
-Wall -Wextra -Wshadow | Warnings. -Wshadow warns when a variable hides another one. |
-O2 | Optimisation, like the real judge. Without it, timings mean little. |
-DLOCAL | Defines the macro LOCAL, so local-only code runs here and is skipped on Codeforces. |
Use ll, not long. On Windows long is 32 bits; on macOS it is 64 bits. Code that uses long for large numbers can pass here and fail on Codeforces. ll means long long and is 64 bits everywhere.
Including bits/stdc++.h makes the compiler read a large amount of standard library code every time. A precompiled header is that code, already processed and saved to disk as include/bits/stdc++.h.gch. The compiler loads the saved version instead of reading the source.
It saves about half a second per compile on this Mac. It is optional: without it, everything still works, just a little slower. It must be rebuilt after a compiler upgrade, with python3 scripts/pch.py.
Contests with a million numbers can time out on slow input. The template fixes this with two lines:
ios::sync_with_stdio(false); // do not sync C++ streams with C stdio cin.tie(nullptr); // do not flush cout before each read
Also print with '\n' instead of endl. endl flushes the output on every line, which is slow for large output.
A common bug is writing one element past the end of an array. The program often still gives the right answer, because the memory next to the array happens to be free. The release build hides the bug.
The debug build (Space d) compiles with AddressSanitizer, UBSan and bounds-checked containers. It stops at the first bad access and prints where it happened:
Error: attempt to subscript container with out-of-bounds index 5, but container only holds 2 elements.
Run it when a sample fails for no obvious reason, and before submitting problems that involve indexes or large numbers.
Stress testing needs three files:
The stress tester runs seeds 1, 2, 3 and so on, feeds each input to both programs, and compares the outputs. On the first mismatch it stops and saves the input to stress_fail/input.txt. Run your solution on that file to reproduce the problem.
Keep the inputs small. The brute force must finish, and most bugs show up on small inputs anyway.
The installer enables the Vim extension and the C/C++ extension. The leader key is Space.
| Keys | Action |
|---|---|
| Space b | Run the samples |
| Space d | Run the samples with the debug build |
| Space s | Stress test the problem |
| Space l | Start the listener |
| j k | Leave insert mode |
| Cmd Shift B | Run the samples (the default build task, works without Vim) |
Sublime's built-in Vim mode (Vintage) is enabled. Use Cmd B to run the samples. Choose Tools > Build System > CF-C++ once, and save the folder as a project to keep the choice. For the debug build, use Tools > Build With > Debug.
The Sublime setup has been checked by generating and validating its files. It has not been run inside the editor by the project.
Type the prefix and press Tab.
| Prefix | Expands to |
|---|---|
| cp | The full template |
| forn / forr | Loop from 0 up to n, or from n-1 down to 0 |
| vin | Read n values into a vector |
| bs | Binary search for the smallest x where a condition holds |
| mpow | Modular power |
| yn | Print YES or NO |
| dbg | Debug print to stderr, ignored on Codeforces |
The snippets are defined once, in .vscode/cpp.code-snippets. The installer converts them for Sublime.
sol.cpp.brute.cpp and gen.py first.sol.cpp. The dbg lines do nothing on Codeforces.Start with python3 scripts/doctor.py. It checks the compiler, the header, the port, the editors and the example, and explains each problem.
| Problem | Fix |
|---|---|
| No GCC found | brew install gcc. Or set CP_CXX to the path of your GCC. |
| Clicking the extension does nothing | The listener must be running and print saved .... Open http://127.0.0.1:27121/ in a browser to check it. Another program may be using port 27121. |
| Compiles are slow | Run python3 scripts/pch.py. |
| Vim keys do nothing in VS Code | Open the repository folder itself, not a subfolder, and check that the Vim extension is enabled. |
| Your answer is right but the judge says WA | The problem may accept several answers. The judge compares against one stored answer, so check the output by hand. |
dbg and the debug build.Source: github.com/bobikenobi12/cf-kit. MIT License.