#include <iostream>
#include <random>
#include <cstdint>
#include <algorithm>
static const std::uint64_t Select = 22;
std::random_device rd;
std::mt19937 mt(0);
//std::mt19937 mt(rd());
std::uniform_int_distribution<> uid(2, Select);
std::uniform_int_distribution<> uid10n10(1, 1000000000);
std::uint64_t MakeHoge(std::uint64_t N){
std::uint64_t Total = 0;
std::uint64_t P = N;
std::uint64_t S = 0;
std::uint64_t D = 0;
do{
D = uid(mt) - 1;
S = Select % (D + 1);
if (S == (D + 1)){
Total += P - P%D;
P = P%D;
}
else{
Total += (P / D) * std::max<std::int64_t>(0, S - 1);
P = P / D;
}
} while (P > Select);
return Total + (Select % P);
}
int main(){
std::uint64_t N = 0;
std::uint64_t R = 0;
N = uid10n10(mt);
R = MakeHoge(N);
std::cout << N << " -> " << R <<"th"<< std::endl;
N = uid10n10(mt);
R = MakeHoge(N);
std::cout << N << " -> " << R <<"th"<< std::endl;
N = uid10n10(mt);
R = MakeHoge(N);
std::cout << N << " -> " << R <<"th"<< std::endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8cmFuZG9tPgojaW5jbHVkZSA8Y3N0ZGludD4KI2luY2x1ZGUgPGFsZ29yaXRobT4KCnN0YXRpYyBjb25zdCBzdGQ6OnVpbnQ2NF90IFNlbGVjdCA9IDIyOwoKc3RkOjpyYW5kb21fZGV2aWNlIHJkOwpzdGQ6Om10MTk5MzcgbXQoMCk7Ci8vc3RkOjptdDE5OTM3IG10KHJkKCkpOwpzdGQ6OnVuaWZvcm1faW50X2Rpc3RyaWJ1dGlvbjw+IHVpZCgyLCBTZWxlY3QpOwpzdGQ6OnVuaWZvcm1faW50X2Rpc3RyaWJ1dGlvbjw+IHVpZDEwbjEwKDEsIDEwMDAwMDAwMDApOwoKc3RkOjp1aW50NjRfdCBNYWtlSG9nZShzdGQ6OnVpbnQ2NF90IE4pewoJc3RkOjp1aW50NjRfdCBUb3RhbCA9IDA7CglzdGQ6OnVpbnQ2NF90IFAgPSBOOw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