**Kattis Help: C++** # General information You start out by finding a suitable problem to solve. Then you write code to solve the problem. After this, you submit the code to us for review. We will then compile your code and run it on some secret input. After some careful deliberation, you will get a [judgement](https://open.kattis.com/help/judgements) informing you whether your code behaved as expected or not. ## Input/Output Your program should read its input from standard input and produce output on standard output. This can for instance be done using `cin` / `cout`. Anything written on standard error (`cerr`) will be ignored. This can be used for debugging your program during development (i.e., you do not have to remove debug output before submitting if you use standard error for debug output). Of course, writing to standard error will take some runtime. Input will always follow the input specification (so you do not need to validate the input). Your output must follow the output specification. ## Compiler settings For C++, we use gcc version g++ (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 with the following flags: `-g -O2 -std=gnu++17 -static -lrt -Wl,--whole-archive -lpthread -Wl,--no-whole-archive {files}` ## System libraries You are allowed to use all standard libraries included with C++. ## Hardware We are currently using Dell PowerEdge R230 servers for judging. These are equipped with an Intel Xeon E3-1220V6 CPU running at 3.0 GHz and 8 GB RAM. A 64-bit Linux kernel is used. ## Exiting We will inspect the exit code of your program. If it is non-zero, we will judge your submission as Run Time Error. # Solving a problem Now lets get down to business and write some code. The short tutorial below goes through the solution of [A Different Problem](https://open.kattis.com/problems/different). 1. The problem 2. Reading the input 3. Computing the answer 4. The solution ## Step 1: The problem You are tasked with writing a program that computes the difference between integers. Sounds simple, doesn't it? Well, as we will see, the problem still holds some small difficulties. ## Step 2: Reading the input One thing to note is that the integers can be fairly large, as large as $10^{15}$, which is a lot larger than the maximum value of an `int` (which is $2^{31}-1$). Luckily, there is a 64 bit integer type in C++, `long long`. Now that we have determined a suitable type, we just have to read the data. Reading is done from standard input. In this problem, we should read until the end of the file (in other problems, there might be an integer at the beginning of the input, specifying how much to read, or there might be a special indicator denoting that there is nothing more to read). Using `cin`, this can be done as below: ```c++ long long a, b; while (cin >> a >> b) { // solve test case and output answer } ``` ## Step 3: Computing the answer Now that we've read the input, it's time to actually solve the problem. Since $0 \leq a, b \leq 10^{15}$, we have that $-(10^{15}) \leq a - b \leq 10^{15}$, which means that there is no danger of overflow involved in just subtracting the two numbers `a` and `b`. Then, we can just take the absolute value by using the builtin `std::abs` function. Finally, it's time to print the result. Using `cout` (assuming the `long long` variable `res` holds the result): ```c++ cout << res << endl; ``` ## Step 4: The solution Now we are basically done, all that remains is to combine the above parts. Here is a version of the complete solution. [different.cc](https://open.kattis.com/download/different/different.cc?70e957=) # External documentation 1. [C++](http://cppreference.com/)