Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
The three nephews must all receive the same amount, so the final total number of candies has to be split into 3 equal groups.
A number can be split evenly among 3 people exactly when it is divisible by .
You are allowed to buy candies, but you must use all the original candies. So you need the smallest such that is divisible by .
Look at . If it is , you buy . If it is , buy . If it is , buy .
The whole answer is just The second modulo turns the divisible case from back into .
This problem is just divisibility by wearing a tiny fake mustache.
We have candies, and Monocarp has exactly three nephews. He wants each nephew to get the same number of candies, and he must give away all candies he already has. If he buys extra candies, then the final total is:
For this total to be split equally among three nephews, it must be divisible by :
So the task is: find the smallest nonnegative that makes a multiple of .
Checking the remainder
Only the remainder of after division by matters.
There are three cases:
Example: .
So we buy , giving a total of , and splits into .
Example: .
Already good. Buy nothing.
Compact formula
The casework can be written as:
Why the extra modulo at the end? Because if , then , but buying candies is not minimal. Buying is. The final modulo converts that back to .
So for every test case, compute the remainder and print the smallest number needed to reach the next multiple of .
The constraints are tiny, but even if they were huge, this would still be per test case. No loops over candies, no simulation, no nonsense. Just modulo and done.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
cout << (3 - n % 3) % 3 << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
cout << (3 - n % 3) % 3 << '\n';
}
}