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.
An average has two ingredients: a total sum and a count. How many bases are you averaging over?
Do you remember how to pull out the digits of a number when you write it in base ? Think about remainders and division.
, which means there are fewer than bases to check. Simulating the conversion for each base is more than fast enough.
The answer must be an irreducible fraction. Once you have the total sum of all digit-sums, what arithmetic operation do you need to perform on both the numerator and the denominator before printing?
Loop from to . For each base, start with and repeatedly add to your answer while replacing with . Your final fraction is , simplified by their greatest common divisor.
Petya wants the average digit sum across bases . An average is just sum divided by count. The count of bases is . That's the easy part.
Converting to base is kindergarten stuff: while your number is still breathing, grab the last digit with , add it to your running total, then kill it with . Repeat until it's zero.
total = 0.Lemma 1: For a fixed base , the inner loop computes the exact digit sum of in base . Proof: Each iteration peels off the current least significant digit () and shifts the number right one digit (). The accumulator adds every digit exactly once.
Lemma 2: After processing all bases, total equals , where is the digit sum in base .
Proof: By Lemma 1, each outer-loop iteration contributes exactly , and we visit every base in .
Theorem: The algorithm outputs the required average as an irreducible fraction. Proof: From Lemma 2, the numerator is the correct total. The denominator is , the exact number of terms. Dividing by removes any common factors, giving the unique reduced form.
. The outer loop runs times; the inner loop needs steps per base. Total time is which for these constraints is basically a rounding error. Memory is .
#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 A;
if (!(cin >> A)) return 0;
ll total = 0;
for (int b = 2; b <= A - 1; ++b) {
int n = A;
while (n > 0) {
total += n % b;
n /= b;
}
}
ll denom = A - 2;
ll g = gcd(total, denom);
cout << total / g << '/' << denom / g << '\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();
int A;
if (!(cin >> A)) return 0;
ll total = 0;
for (int b = 2; b <= A - 1; ++b) {
int n = A;
while (n > 0) {
total += n % b;
n /= b;
}
}
ll denom = A - 2;
ll g = gcd(total, denom);
cout << total / g << '/' << denom / g << '\n';
return 0;
}