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.
Start from the equation literally: if is friendly for , then . So is only a little bigger than , not some random huge number.
The digit sum of a number near is tiny compared to the number itself. Even for a 10-digit candidate, .
Instead of trying every , try every possible value . Then the candidate number is forced to be .
For each from to , compute and check whether . If yes, that works. If no, that guessed digit sum was lying to your face.
There is no double-counting: each possible gives exactly one candidate , and a valid has exactly one digit sum. So the count of valid guesses is the answer.
Research check: the official Codeforces editorial for Round 1079 uses the same bounded brute force idea, checking only a small window after (official editorial). A public explanation also phrases it as trying possible digit sums and links a submitted solution (write-up). The supplied statement is still the source of truth.
We need count integers such that
where is the sum of digits of .
Rearrange it:
That is the whole problem. A friendly number cannot be far from ; the only extra amount is its digit sum.
Now bound that digit sum. Suppose has digits. Then
If , then
which is impossible because the statement guarantees . So every valid has at most digits, meaning
Therefore every valid friendly number has the form
for some integer with , where is supposed to equal .
So we just try every possible :
Why this is correct:
Every valid friendly number has exactly one digit sum . From , it must satisfy , so our loop checks that exact candidate. If the candidate passes , then
so it is definitely friendly. If it fails, that guessed digit sum was nonsense. Brutal, but fair.
There is no double-counting because each produces only one candidate , and every integer has exactly one digit sum.
Edge cases:
long long, because is still small, but 64-bit integers keep the code clean and avoid dumb overflow worries.Complexity per test case is
with memory. With , this is basically instant.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int digitSum(ll n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll x;
cin >> x;
int ans = 0;
for (int s = 1; s <= 90; s++) {
ll y = x + s;
if (digitSum(y) == s) ans++;
}
cout << ans << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int digitSum(ll n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll x;
cin >> x;
int ans = 0;
for (int s = 1; s <= 90; s++) {
ll y = x + s;
if (digitSum(y) == s) ans++;
}
cout << ans << '\n';
}
}