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.
Stop and think: what is in terms of ? For two positive numbers, when does ?
Use that to rewrite Billy's check. Show that is the same thing as . So a 'mistake' is a triple where yet .
Fix a pair . How many satisfy ? It depends only on the residue of . What if you precomputed, for each residue , how many numbers in are ?
Since is determined by and , you can group all three variables by residue. If is that count, the total number of triples passing Billy's test is .
The only triples counted above that are not mistakes are those with . Count those by summing for to . Subtract that sum from the double-sum above. That's your answer, computed in .
For , the digital root is exactly , except when the remainder is you write instead. Therefore
Since and , Billy's check collapses to This is necessary for , but not sufficient. We need ordered triples with such that the congruence holds but .
Only residues modulo matter. Define If and , then . For this pair there are exactly choices of satisfying the congruence. Grouping all pairs by residue gives That is a puny iterations.
Among those triples, the ones with are not mistakes. For a fixed , the admissible are those with ; there are of them, and is forced to be . Hence For a plain loop is more than fast enough.
Complexity: time and additional memory.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
ll n;
if (!(cin >> n)) return 0;
array<ll, 9> freq{};
for (int r = 0; r < 9; ++r) {
if (r == 0) {
freq[r] = n / 9;
} else {
freq[r] = n / 9 + (n % 9 >= r ? 1 : 0);
}
}
ll congruent = 0;
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
int k = (i * j) % 9;
congruent += freq[i] * freq[j] * freq[k];
}
}
ll exact = 0;
for (ll a = 1; a <= n; ++a) {
exact += n / a;
}
cout << congruent - exact << '\n';
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
ll n;
if (!(cin >> n)) return 0;
array<ll, 9> freq{};
for (int r = 0; r < 9; ++r) {
if (r == 0) {
freq[r] = n / 9;
} else {
freq[r] = n / 9 + (n % 9 >= r ? 1 : 0);
}
}
ll congruent = 0;
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
int k = (i * j) % 9;
congruent += freq[i] * freq[j] * freq[k];
}
}
ll exact = 0;
for (ll a = 1; a <= n; ++a) {
exact += n / a;
}
cout << congruent - exact << '\n';
return 0;
}