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.
Forget the pages for a second. How many base- numbers of exactly length are there if leading zeroes are forbidden?
If the total number of written numbers is , then the last page contains only the remainder of modulo — except that remainder means the last page is completely full, so the answer is .
The count is So you only need . Since and are enormous, treat them as decimal strings and keep only modular information.
Computing from a decimal string is easy. The real pain is when the exponent has up to digits. Don't blindly summon Euler here — and need not be coprime, and that rabbit hole gets ugly fast.
Process the exponent string digit by digit. If the current decimal prefix is and the next digit is , then the new exponent is , so Precompute , decrement the huge decimal string to get , compute and output if that residue is , otherwise output the residue itself. That's the whole trick. The rest is just not screwing up string subtraction.
Nick wants all base- numbers of exactly length with no leading zeroes.
So the total count is
Yes, that's it. The problem is not counting. The problem is that and are so huge they look like they were generated by a keyboard falling down the stairs.
Each page stores exactly numbers, and Nick fills pages continuously with no gaps.
Therefore the number of entries on the page containing the last number is:
A compact formula is
So all we really need is .
We want
Since , modulo arithmetic is cheap. The monsters are and , which are given as decimal strings with up to digits.
Standard decimal scan:
That gives us .
Then
This is the only real trick.
Let
and let the exponent be given as a decimal string.
Suppose we've processed some prefix of and its value is . If the next digit is , then the new exponent becomes
Therefore,
Modulo this becomes
So we scan the decimal digits of from left to right and maintain
Transition for next digit :
Precompute once. Then each digit is work.
This works for any modulus . No coprimality, no Euler's theorem, no Chinese remainder wizardry. Just plain algebra. Sometimes the blunt hammer is the best hammer.
Since the exponent is , we decrement the huge decimal string by one manually:
"0".For example:
Let be the total number of valid numbers.
res stays true after each processed digit.That nails the result.
Let and be the number of decimal digits of and .
Total:
with extra memory besides the input strings.
For inputs of size up to digits, that's exactly the kind of no-nonsense solution you want.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
ll modString(const string& s, ll mod) {
ll res = 0;
for (char ch : s) {
res = (res * 10 + (ch - '0')) % mod;
}
return res;
}
string dec1(string s) {
int i = (int)s.size() - 1;
while (s[i] == '0') {
s[i] = '9';
--i;
}
--s[i];
int pos = 0;
while (pos + 1 < (int)s.size() && s[pos] == '0') ++pos;
return s.substr(pos);
}
ll raise10(ll x, ll mod) {
ll x2 = x * x % mod;
ll x4 = x2 * x2 % mod;
ll x8 = x4 * x4 % mod;
return x8 * x2 % mod;
}
// Computes a^e mod mod, where e is a huge non-negative decimal string.
ll powHugeDecimal(ll a, const string& e, ll mod) {
if (mod == 1) return 0;
ll pw[10];
pw[0] = 1 % mod;
for (int i = 1; i <= 9; ++i) pw[i] = pw[i - 1] * a % mod;
ll res = 1 % mod;
for (char ch : e) {
int d = ch - '0';
res = raise10(res, mod);
res = res * pw[d] % mod;
}
return res;
}
int main() {
setIO();
string b, n;
ll c;
cin >> b >> n >> c;
ll bmod = modString(b, c);
ll first = (bmod - 1 + c) % c; // (b - 1) mod c
string exp = dec1(n); // n - 1
ll pw = powHugeDecimal(bmod, exp, c);
ll totalMod = first * pw % c;
ll ans = (totalMod == 0 ? c : totalMod);
cout << ans << '\n';
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
ll modString(const string& s, ll mod) {
ll res = 0;
for (char ch : s) {
res = (res * 10 + (ch - '0')) % mod;
}
return res;
}
string dec1(string s) {
int i = (int)s.size() - 1;
while (s[i] == '0') {
s[i] = '9';
--i;
}
--s[i];
int pos = 0;
while (pos + 1 < (int)s.size() && s[pos] == '0') ++pos;
return s.substr(pos);
}
ll raise10(ll x, ll mod) {
ll x2 = x * x % mod;
ll x4 = x2 * x2 % mod;
ll x8 = x4 * x4 % mod;
return x8 * x2 % mod;
}
// Computes a^e mod mod, where e is a huge non-negative decimal string.
ll powHugeDecimal(ll a, const string& e, ll mod) {
if (mod == 1) return 0;
ll pw[10];
pw[0] = 1 % mod;
for (int i = 1; i <= 9; ++i) pw[i] = pw[i - 1] * a % mod;
ll res = 1 % mod;
for (char ch : e) {
int d = ch - '0';
res = raise10(res, mod);
res = res * pw[d] % mod;
}
return res;
}
int main() {
setIO();
string b, n;
ll c;
cin >> b >> n >> c;
ll bmod = modString(b, c);
ll first = (bmod - 1 + c) % c; // (b - 1) mod c
string exp = dec1(n); // n - 1
ll pw = powHugeDecimal(bmod, exp, c);
ll totalMod = first * pw % c;
ll ans = (totalMod == 0 ? c : totalMod);
cout << ans << '\n';
}