#include <bits/stdc++.h>
#define pb push_back
typedef long long ll;
using namespace std;
const ll MOD = 1e9 + 7;
ll n , m;
ll x[100005] , y[100005];
vector<ll> a;
vector< pair<ll,ll> > v;
stack< pair<ll,ll> > S;
bool check(ll T)
{
ll i = 0;
ll curheight = 0;
ll usedMissiles = 0;
while(i < n) {
ll j = i;
ll maxHeight = v[i].second;
while(j < n && v[j].first + T >= v[i].first) {
maxHeight = max(maxHeight , v[j].second);
j++;
}
usedMissiles++;
curheight = max(curheight , maxHeight + T);
while(j < n && v[j].second <= curheight) j++;
i = j;
}
return (usedMissiles <= m);
}
class TheEmpireStrikesBack {
public:
int find(int ax, int bx, int cx, int ay, int by, int cy, int N, int M) {
ll AX = ax;
ll BX = bx;
ll CX = cx;
ll AY = ay;
ll BY = by;
ll CY = cy;
x[1] = AX;
n = N;
m = M;
for(ll i=2;i<=n;i++) {
x[i] = ((x[i-1]*(1LL * BX))%MOD + (ll)CX)%MOD;
}
y[1] = AY;
for(ll i=2;i<=n;i++) {
y[i] = ((y[i-1]*(1LL * BY))%MOD + (ll)CY)%MOD;
}
for(ll i=1;i<=n;i++) {
v.pb(make_pair(x[i] , y[i]));
//cout << x[i] << " " << y[i] << endl;
}
sort(v.rbegin() , v.rend());
ll lo = 0;
ll hi = MOD;
while(hi > lo) {
ll mid = lo + (hi - lo) / 2;
if(check(mid)) {
hi = mid;
}else {
lo = mid+1;
}
}
return lo;
}
};
<%:testing-code%>
//Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgcGIgcHVzaF9iYWNrCnR5cGVkZWYgbG9uZyBsb25nIGxsOwp1c2luZyBuYW1lc3BhY2Ugc3RkOwpjb25zdCBsbCBNT0QgPSAxZTkgKyA3OwpsbCBuICwgbTsKbGwgeFsxMDAwMDVdICwgeVsxMDAwMDVdOwp2ZWN0b3I8bGw+IGE7CnZlY3RvcjwgcGFpcjxsbCxsbD4gPiB2OwpzdGFjazwgcGFpcjxsbCxsbD4gPiBTOwpib29sIGNoZWNrKGxsIFQpCnsKICAgICAgICBsbCBpID0gMDsKICAgICAgICBsbCBjdXJoZWlnaHQgPSAwOwogICAgICAgIGxsIHVzZWRNaXNzaWxlcyA9IDA7CiAgICAgICAgd2hpbGUoaSA8IG4pIHsKCiAgICAgICAgICAgICAgICBsbCBqID0gaTsKICAgICAgICAgICAgICAgIGxsIG1heEhlaWdodCA9IHZbaV0uc2Vjb25kOwogICAgICAgICAgICAgICAgd2hpbGUoaiA8IG4gJiYgdltqXS5maXJzdCArIFQgPj0gdltpXS5maXJzdCkgewogICAgICAgICAgICAgICAgICAgICAgICBtYXhIZWlnaHQgPSBtYXgobWF4SGVpZ2h0ICwgdltqXS5zZWNvbmQpOwogICAgICAgICAgICAgICAgICAgICAgICBqKys7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB1c2VkTWlzc2lsZXMrKzsKICAgICAgICAgICAgICAgIGN1cmhlaWdodCA9IG1heChjdXJoZWlnaHQgLCBtYXhIZWlnaHQgKyBUKTsKICAgICAgICAgICAgICAgIHdoaWxlKGogPCBuICYmIHZbal0uc2Vjb25kIDw9IGN1cmhlaWdodCkgaisrOwogICAgICAgICAgICAgICAgaSA9IGo7CiAgICAgICAgfQogICAgICAgIHJldHVybiAodXNlZE1pc3NpbGVzIDw9IG0pOwp9CmNsYXNzIFRoZUVtcGlyZVN0cmlrZXNCYWNrIHsKcHVibGljOgoJaW50IGZpbmQoaW50IGF4LCBpbnQgYngsIGludCBjeCwgaW50IGF5LCBpbnQgYnksIGludCBjeSwgaW50IE4sIGludCBNKSB7CiAgICAgICAgICAgICAgICBsbCBBWCA9IGF4OwogICAgICAgICAgICAgICAgbGwgQlggPSBieDsKICAgICAgICAgICAgICAgIGxsIENYID0gY3g7CiAgICAgICAgICAgICAgICBsbCBBWSA9IGF5OwogICAgICAgICAgICAgICAgbGwgQlkgPSBieTsKICAgICAgICAgICAgICAgIGxsIENZID0gY3k7CiAgICAgICAgICAgICAgICB4WzFdID0gQVg7CiAgICAgICAgICAgICAgICBuID0gTjsKICAgICAgICAgICAgICAgIG0gPSBNOwogICAgICAgICAgICAgICAgZm9yKGxsIGk9MjtpPD1uO2krKykgewoKICAgICAgICAgICAgICAgICAgICAgICAgeFtpXSA9ICgoeFtpLTFdKigxTEwgKiBCWCkpJU1PRCArIChsbClDWCklTU9EOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgeVsxXSA9IEFZOwogICAgICAgICAgICAgICAgZm9yKGxsIGk9MjtpPD1uO2krKykgewoKICAgICAgICAgICAgICAgICAgICAgICAgeVtpXSA9ICgoeVtpLTFdKigxTEwgKiBCWSkpJU1PRCArIChsbClDWSklTU9EOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZm9yKGxsIGk9MTtpPD1uO2krKykgewogICAgICAgICAgICAgICAgICAgICAgICB2LnBiKG1ha2VfcGFpcih4W2ldICwgeVtpXSkpOwogICAgICAgICAgICAgICAgICAgICAgICAvL2NvdXQgPDwgeFtpXSA8PCAiICIgPDwgeVtpXSA8PCBlbmRsOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc29ydCh2LnJiZWdpbigpICwgdi5yZW5kKCkpOwogICAgICAgICAgICAgICAgbGwgbG8gPSAwOwogICAgICAgICAgICAgICAgbGwgaGkgPSBNT0Q7CiAgICAgICAgICAgICAgICB3aGlsZShoaSA+IGxvKSB7CgogICAgICAgICAgICAgICAgICAgICAgICBsbCBtaWQgPSBsbyArIChoaSAtIGxvKSAvIDI7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmKGNoZWNrKG1pZCkpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBoaSA9IG1pZDsKICAgICAgICAgICAgICAgICAgICAgICAgfWVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGxvID0gbWlkKzE7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHJldHVybiBsbzsKCX0KfTsKCgo8JTp0ZXN0aW5nLWNvZGUlPgovL1Bvd2VyZWQgYnkgS2F3aWdpRWRpdCAyLjEuNCAoYmV0YSkgbW9kaWZpZWQgYnkgcGl2YW5vZiEK