#include #include #include #include #include using namespace std; const long long MOD = 100000009; long long xs[10005]; int find_max(int seed, int N, int K, int B); int main() { cin.tie(0); ios::sync_with_stdio(false); int Q = 0; cin >> Q; vector result(Q, 0); int seed, N, K, B; for (int i = 0; i != Q; ++i){ cin >> seed >> N >> K >> B; result[i] = find_max(seed, N, K, B); } for (auto res : result) cout << res << '\n'; return 0; } int find_max(int seed, int N, int K, int B){ xs[0] = seed; for (int i = 0; i != N; ++i){ xs[i + 1] = 1 + (xs[i] + 12345) * xs[i] % MOD; } int ps[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; int counts[40] = {0}; int record = 100000000; for (auto p : ps){ int c = 0; while (B % p == 0){ ++c; B /= p; } if (c){ memset(counts, 0, 40); for (int i = 0; i <= N; ++i){ int x = xs[i]; int f = 0; for (; x % p == 0; ++f) x /= p; counts[f] += 1; } int Kcopy = K; int cum = 0; for (int i = 0; Kcopy; ++i){ Kcopy -= counts[i]; if (Kcopy < 0){ cum += (counts[i] + Kcopy) * i; break; }else{ cum += counts[i] * i; } } record = min(record, cum / c); } } return record; }