#include #include #include #include #include #include #include #include #include #include #include #include #include namespace lib { templatestruct modnum; templateconstexpr T pow(T base, std::size_t p) { if (p == 0) { return T(1); } else if (p == 1) { return base; } else if (p == 2) { return base*base; } else if (p % 2 == 0) { return pow(pow(base, p / 2), 2); } else { return pow(pow(base, p / 2), 2)*base; } } templateconstexpr auto inverse(modnum const&); templatestruct modnum { static constexpr auto mod = Mod; std::uint64_t val; modnum() = default; constexpr modnum(std::uint64_t v) :val(v%Mod) { } constexpr modnum& operator+=(modnum const& v) { val += v.val; val %= mod; return *this; } constexpr modnum& operator-=(modnum const& v) { val += mod - v.val; val %= mod; return *this; } constexpr modnum& operator*=(modnum const& v) { val *= v.val; val %= mod; return *this; } constexpr modnum& operator/=(modnum const& v) { return operator*=(inverse(v)); } }; templateconstexpr auto operator+(modnum lhs, modnumconst& rhs) { return lhs += rhs; } templateconstexpr auto operator-(modnum lhs, modnumconst& rhs) { return lhs -= rhs; } templateconstexpr auto operator*(modnum lhs, modnumconst& rhs) { return lhs *= rhs; } templateconstexpr auto operator/(modnum lhs, modnumconst& rhs) { return lhs /= rhs; } templateconstexpr auto inverse(modnumconst& base) { return pow(base, Mod - 2); } templateconstexpr auto clamp(T v) { return std::max(v, T()); } templatevoid sort(std::vector& vec) { std::sort(vec.begin(), vec.end()); } templateauto lower_bound(std::vectorconst& vec, T v) { return std::distance(vec.begin(), std::lower_bound(vec.begin(), vec.end(), v)); } templateauto upper_bound(std::vectorconst& vec, T v) { return std::distance(vec.begin(), std::upper_bound(vec.begin(), vec.end(), v)); } templatestruct dmod { std::uint64_t val; static std::uint64_t mod; dmod(std::uint64_t v = 0) :val(v%mod) { } dmod& operator+=(dmod const& rhs) { val += rhs.val; val %= mod; return *this; } dmod& operator-=(dmod const& rhs) { val += mod - rhs.val; val %= mod; return *this; } dmod& operator*=(dmod const& rhs) { val *= rhs.val; val %= mod; return *this; } }; templatedmod operator+(dmod lhs, dmod const& rhs) { return lhs += rhs; } templatedmod operator-(dmod lhs, dmod const& rhs) { return lhs -= rhs; } templatedmod operator*(dmod lhs, dmod const& rhs) { return lhs *= rhs; } templatestd::uint64_t dmod::mod; struct scope_exit { std::function func; scope_exit(std::function f) :func(f) { } ~scope_exit() { func(); } }; templateusing int_tag = std::integral_constant; } void Main(); int main() { std::cin.tie(nullptr); std::cin.sync_with_stdio(false); Main(); } int count(int val, int prime) { int ret = 0; while (val%prime == 0) { ++ret; val /= prime; } return ret; } void Main() { int Q; std::cin >> Q; int primes[] = { 2,3,5,7,11,13,17,19,23,29,31 }; std::array, 11> counter; for (int i = 0; i < Q; ++i) { std::uint64_t s, n, k, b; std::cin >> s >> n >> k >> b; for (auto& v : counter) { for (auto& c : v) { c = 0; } } for (int i = 0; i < 11; ++i) { ++counter[i][count(s, primes[i])]; } for (int i = 0; i < n; ++i) { s = (s*s + s * 12345) % 100000009 + 1; for (int x = 0; x < 11; ++x) { ++counter[x][count(s, primes[x])]; } } int min = std::numeric_limits::max(); for (int i = 0; i < 11; ++i) { auto c = count(b, primes[i]); if (c == 0) { continue; } int count = 0; int rest = k; for (int j = 0; j < 64; ++j) { if (counter[i][j] >= rest) { count += j*rest; break; } else { count += counter[i][j] * j; rest -= counter[i][j]; } } min = std::min(min, count / c); } std::cout << min << std::endl; } }