1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <bits/stdc++.h>
#include "dzilib.h"
#define dbg(x) " [" << #x << ": " << (x) << "] "
using namespace std;
using ll = long long;
template<typename A, typename B>
ostream& operator<<(ostream& out, const pair<A,B>& p) {
    return out << "(" << p.first << ", " << p.second << ")";
}
template<typename T>
ostream& operator<<(ostream& out, const vector<T>& c) {
    out << "{";
    for(auto it = c.begin(); it != c.end(); it++) {
        if(it != c.begin()) out << ", ";
        out << *it;
    }
    return out << "}";
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAX_DIV = 5e5;

int total_ask = 0;
vector<ll> prm;
vector<vector<int>> sign[MAX_DIV];
vector<int> nums[MAX_DIV];
vector<int> num_div, p;

void rec_sign(vector<int>& v, int mx, ll bound, ll prod, ll num_div) {
    sign[num_div].push_back(v);
    int id = v.size();
    v.push_back(0);
    __int128 pw = prm[id];
    for(int i = 1; i <= mx; i++) {
        if(pw > bound / prod) break;
        v[id] = i;
        rec_sign(v, i, bound, prod * pw, num_div * (i + 1));
        pw *= prm[id];
    }
    v.pop_back();
}

ll bp(ll a, ll b, ll m) {
    ll r = 1;
    while(b) {
        if(b & 1) r = r * a % m;
        b >>= 1;
        a = a * a % m;
    }
    return r;
}

bool check_composite(ll n, ll a, ll d, int s) {
    ll x = bp(a, d, n);
    if(x == 1 || x == n - 1) return false;
    for(int r = 1; r < s; r++) {
        x = x * x % n;
        if(x == n - 1) return false;
    }
    return true;
};

bool MillerRabin(ll n, int iter = 5) {
    if(n < p.size()) return p[n] == n;
    int s = 0;
    ll d = n - 1;
    while((d & 1) == 0) {
        d >>= 1;
        s++;
    }
    for(int i = 0; i < iter; i++) {
        int a = 2 + rng() % (n - 3);
        if(check_composite(n, a, d, s)) return false;
    }
    return true;
}

int count_div(ll n) {
    if(n < num_div.size()) return num_div[n];
    int r = 1;
    for(int i = 0; i < prm.size(); i++) {
        ll pr = prm[i];
        if(pr * pr * pr > n) break;
        int a = 1;
        while(n % pr == 0) {
            n /= pr;
            a++;
        }
        r *= a;
    }
    if(n < num_div.size()) return r * num_div[n];
    ll sq = sqrt(n - 1);
    while(sq * sq < n) sq++;
    if(sq * sq == n && MillerRabin(sq)) return 3 * r;
    if(MillerRabin(n)) return 2 * r;
    return 4 * r;
}

void solve(ll n, ll c) {
    c = min(c, n);
    ll y, div;

    int min_exp = 8;
    if(n < 1e9) min_exp = 2;
    while(true) {
        y = rng() % c;
        div = Ask(y);
        total_ask++;
        if(sign[div][0][0] >= min_exp) {
            break;
        }
    }

    vector<int> cand;
    for(int e = min_exp; e <= div - 1; e++) {
        if(div % (e + 1) == 0) {
            int other = div / (e + 1);
            for(auto pr : prm) {
                ll pw = 1;
                bool ok = true;
                for(int i = 0; i < e; i++) {
                    pw *= pr;
                    if(pw > n + y) {
                        ok = false;
                        break;
                    }
                }
                if(!ok) break;
                for(ll x : nums[other]) {
                    if(x > (n + y) / pw) break;
                    ll cur = x * pw;
                    if(cur < y + 1 || x % pr == 0) continue;
                    cand.push_back(cur - y);
                }
            }
        }
    }
    sort(cand.begin(), cand.end());
    cand.erase(unique(cand.begin(), cand.end()), cand.end());

    while(cand.size() > 1) {
        y = rng() % c;
        div = Ask(y);
        total_ask++;
        vector<int> new_cand;
        for(auto x : cand) {
            if(count_div(x + y) == div) {
                new_cand.push_back(x);
            }
        }
        cand = new_cand;
    }

    Answer(cand[0]);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int t = GetT();
    int q = GetQ();
    ll c = GetC();
    ll n = GetN();

    ll bnd = 31250000;
    p.resize(bnd + 1);
    for(int i = 2; i <= bnd; i++) {
        if(!p[i]) {
            prm.push_back(i);
            for(int j = i; j <= bnd; j += i) {
                if(!p[j]) p[j] = i;
            }
        }
    }

    vector<int> v;
    rec_sign(v, 60, n + c, 1, 1);

    num_div.resize(bnd + 1);
    num_div[1] = 1;
    nums[1].push_back(1);
    for(int i = 2; i <= bnd; i++) {
        int x = i;
        int pr = p[x];
        int a = 0;
        while(x % pr == 0) {
            x /= pr;
            a++;
        }
        num_div[i] = num_div[x] * (a + 1);
        nums[num_div[i]].push_back(i);
    }

    while(t--) {
        solve(n, c);
    }

    return 0;
}