Your first program
Typed input, ranges, let and var, loops, print, and what a trap is.
A CP Lang program is the statements you would write inside main. There
is no main, no includes and no fast-I/O incantation. This is a whole
program:
Press Run (Ctrl+Enter).
Press Run. Then open the C++17 tab: that single file is what you submit to the judge. Your source is quoted at the top of it.
Reading input
read() reads whatever type the left-hand side asks for. The type is
where you write down what the statement told you. If the statement says
"1 ≤ n ≤ 2·10⁵, then n integers between 1 and 10⁹", you write exactly
that:
Press Run (Ctrl+Enter).
Three things to notice.
1..=2e5 is a type: an integer in that range. 2e5 is an exact integer,
not a float. The range does real work. While you test, input outside it
stops the run and tells you where:
error[R0001]: 5 is outside `1..=3` (input line 1, column 1)The compiler also uses the range to decide how many bytes each element
needs: [0..n]1..=1e9 is stored as 32-bit values without you asking.
Open the C++17 tab and look for std::vector<uint32_t> a.
[0..n]T is an array of n values of type T, indexed from 0.
Reading it reads n values. You never write the read loop.
print writes its arguments separated by spaces and ends the line.
Every string interpolates, so "sum {total}" works without a format
call.
let, var and arithmetic
let names a value that never changes; var one that does. Assigning
to a let is a compile error that tells you the fix:
error[E0401]: `x` is a `let`, so it can't be assigned
= help: declare it with `var x` to change itBlocks are indented and opened with :, as in Python. if is also an
expression:
Press Run (Ctrl+Enter).
The last line prints -4 1 1024. Division rounds down and % is never
negative, which is what contest maths wants and not what C++ and Java
do. ** is power. Integers are 64-bit; if a calculation overflows while
you test, the run traps on that line instead of wrapping silently.
Logic is spelled and, or, not. Comparisons chain: 0 <= i < n.
Loops over things, not over counters
for x in a walks the values. for i in 0..n walks a half-open range,
0..=n an inclusive one. Most loops that only compute one number have a
name already:
Press Run (Ctrl+Enter).
x => x % 2 == 1 is a lambda. Methods that do work take parentheses;
that is a rule of the language, so a.sum() visibly costs a pass over
the array.
What a trap is
When you press Run (or use cpl run locally) the program runs with every
check on: array bounds, ranges, overflow, division by zero. A failed
check is a trap: the run stops and names the line and the values.
The C++ you submit has those checks removed and is as fast as
hand-written C++. A program that never traps behaves the same in both.
Problem: Halving game
You are given an integer n (1 ≤ n ≤ 10¹⁸). While n is even, halve it. Print how many times you halved it, then the odd number that is left. For 40 the answer is 3 5.
More like this: Odd one out, Collatz length. Next: Arrays and index spaces.