#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);
}
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+IGE7CnZlY3RvcjwgcGFpcjxsbCxsbD4gPiB2OwpzdGFjazwgcGFpcjxsbCxsbD4gPiBTOwpib29sIGNoZWNrKGxsIFQpCnsKICAgICAgICBsbCBpID0gMDsKICAgICAgICBsbCBjdXJoZWlnaHQgPSAwOwogICAgICAgIGxsIHVzZWRNaXNzaWxlcyA9IDA7CiAgICAgICAgd2hpbGUoaSA8IG4pIHsKCiAgICAgICAgICAgICAgICBsbCBqID0gaTsKICAgICAgICAgICAgICAgIGxsIG1heEhlaWdodCA9IHZbaV0uc2Vjb25kOwogICAgICAgICAgICAgICAgd2hpbGUoaiA8IG4gJiYgdltqXS5maXJzdCArIFQgPj0gdltpXS5maXJzdCkgewogICAgICAgICAgICAgICAgICAgICAgICBtYXhIZWlnaHQgPSBtYXgobWF4SGVpZ2h0ICwgdltqXS5zZWNvbmQpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgdXNlZE1pc3NpbGVzKys7CiAgICAgICAgICAgICAgICBjdXJoZWlnaHQgPSBtYXgoY3VyaGVpZ2h0ICwgbWF4SGVpZ2h0ICsgVCk7CiAgICAgICAgICAgICAgICB3aGlsZShqIDwgbiAmJiB2W2pdLnNlY29uZCA8PSBjdXJoZWlnaHQpIGorKzsKICAgICAgICAgICAgICAgIGkgPSBqOwogICAgICAgIH0KICAgICAgICByZXR1cm4gKHVzZWRNaXNzaWxlcyA8PSBtKTsKfQpjbGFzcyBUaGVFbXBpcmVTdHJpa2VzQmFjayB7CnB1YmxpYzoKCWludCBmaW5kKGludCBheCwgaW50IGJ4LCBpbnQgY3gsIGludCBheSwgaW50IGJ5LCBpbnQgY3ksIGludCBOLCBpbnQgTSkgewogICAgICAgICAgICAgICAgbGwgQVggPSBheDsKICAgICAgICAgICAgICAgIGxsIEJYID0gYng7CiAgICAgICAgICAgICAgICBsbCBDWCA9IGN4OwogICAgICAgICAgICAgICAgbGwgQVkgPSBheTsKICAgICAgICAgICAgICAgIGxsIEJZID0gYnk7CiAgICAgICAgICAgICAgICBsbCBDWSA9IGN5OwogICAgICAgICAgICAgICAgeFsxXSA9IEFYOwogICAgICAgICAgICAgICAgbiA9IE47CiAgICAgICAgICAgICAgICBtID0gTTsKICAgICAgICAgICAgICAgIGZvcihsbCBpPTI7aTw9bjtpKyspIHsKCiAgICAgICAgICAgICAgICAgICAgICAgIHhbaV0gPSAoKHhbaS0xXSooMUxMICogQlgpKSVNT0QgKyAobGwpQ1gpJU1PRDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHlbMV0gPSBBWTsKICAgICAgICAgICAgICAgIGZvcihsbCBpPTI7aTw9bjtpKyspIHsKCiAgICAgICAgICAgICAgICAgICAgICAgIHlbaV0gPSAoKHlbaS0xXSooMUxMICogQlkpKSVNT0QgKyAobGwpQ1kpJU1PRDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGZvcihsbCBpPTE7aTw9bjtpKyspIHsKICAgICAgICAgICAgICAgICAgICAgICAgdi5wYihtYWtlX3BhaXIoeFtpXSAsIHlbaV0pKTsKICAgICAgICAgICAgICAgICAgICAgICAgLy9jb3V0IDw8IHhbaV0gPDwgIiAiIDw8IHlbaV0gPDwgZW5kbDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHNvcnQodi5yYmVnaW4oKSAsIHYucmVuZCgpKTsKICAgICAgICAgICAgICAgIGxsIGxvID0gMDsKICAgICAgICAgICAgICAgIGxsIGhpID0gTU9EOwogICAgICAgICAgICAgICAgd2hpbGUoaGkgPiBsbykgewoKICAgICAgICAgICAgICAgICAgICAgICAgbGwgbWlkID0gbG8gKyAoaGkgLSBsbykgLyAyOwogICAgICAgICAgICAgICAgICAgICAgICBpZihjaGVjayhtaWQpKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaGkgPSBtaWQ7CiAgICAgICAgICAgICAgICAgICAgICAgIH1lbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBsbyA9IG1pZCsxOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICByZXR1cm4gbG87Cgl9Cn07CgoKPCU6dGVzdGluZy1jb2RlJT4KLy9Qb3dlcmVkIGJ5IEthd2lnaUVkaXQgMi4xLjQgKGJldGEpIG1vZGlmaWVkIGJ5IHBpdmFub2YhCg==