Problem:
Problem H: Nearest Station
うさぎがある電車のチケットを
n
枚持っている. チケットにはそれぞれ0 から
n
− 1 までの番号がついていて,
k
番のチケットを使うと,
p
⋅
a
k
+
q
⋅
b
k
駅進むことができる.
うさぎは今いる駅から
m
駅進んだ駅にあるニンジン食べ放題の店に行きたいが, なるべく歩く距離を短くしたい. 駅は等間隔に並んでいる. チケットを電車の上り線で進むことのみに用いるとき, うさぎは最小何駅分の徒歩で店に着けるか.
Input
1 ≤
n
,
m
,
a
,
b
,
p
,
q
≤ 1 000 000 000 000 (整数)
Output
うさぎは最小何駅分の徒歩で店に着けるか, その数を一行に出力せよ.
Sample Input 1
6 200 2 3 4 5
Sample Output 1
1
Sample Input 2
6 1 2 3 4 5
Sample Output 2
1

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2017/11/29  Problem: AOJ 2239 / Link: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2239  ----- */
/* ------問題------



-----問題ここまで----- */
/* -----解説等-----

場合分けさん…

----解説ここまで---- */
// 繰り返し二乗法
long long  powmod(long long  x, long long  n) {
	long long  res = 1;
	while (n > 0) {
		if (n & 1) {
			res = (res*x);
		}
		x = (x*x);
		n >>= 1;
	}
	return res;
}
LL N, M, A, B, P, Q;

LL ans = 0LL;

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

	cin >> N >> M >> A >> B >> P >> Q;
	// N枚ある M駅に行きたい ticket(k):p⋅a^k + q⋅b^k  (k=0...N-1)

	if (A == 1 && B == 1) {
		LL E = P + Q;
		LL div = M / E;
		if (div > N) { ans = abs(M - N*E); }
		else {
			ans = abs(M - div*E);
			if (M%E != 0 && div + 1 <= N) {
				ans = min(ans, abs(M - (div + 1)*E));
			}
		}
	}// ok
	else { //40枚ぐらいしか使わないので全探索可能 戻れる可能性も忘れない
		ans = numeric_limits<LL>::max();
		VL dist;
		FOR(i, 0, min(N, 41LL)) {
			LL d = P*powmod(A, i) + Q*powmod(B, i);
			if (d > 2 * M)break; // 歩くのは沼
			dist.push_back(d);
		}

		int n1 = SZ(dist) / 2;
		int n2 = SZ(dist) - n1;

		VL distset;
		FOR(i, 0, 1 << n1) {
			LL ret = 0;
			FOR(j, 0, n1) {
				if (i&(1 << j)) {
					ret += dist[j];
				}
			}
			if (!(ret > 2 * M)) {
				distset.push_back(ret);
				ans = min(ans, abs(M - ret));
			}
		}
		SORT(distset);

		FOR(i, 0, 1 << n2) {
			LL ret = 0;
			FOR(j, 0, n2) {
				if (i&(1 << j)) {
					ret += dist[n1 + j];
				}
			}
			if (ret > 2 * M)continue;
			ans = min(ans, abs(M - ret));
			int id = lower_bound(ALL(distset), M - ret) - distset.begin();
			ans = min(ans, abs(M - (ret + distset[id])));
			if (id > 0) {
				id--;
				ans = min(ans, abs(M - (ret + distset[id])));
			}
		}

	}

	cout << ans << "\n";

	return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 11976
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

void chmin(ll &a, ll b) { a = min(a, b); }

vector<ll> solve(ll n, ll M, ll A, ll B, ll P, ll Q) {
    using Int = __int128;
    Int lim = M * 2;
    Int a = A;
    Int b = B;
    Int p = P;
    Int q = Q;
    vector<Int> v;
    Int r1 = p, r2 = q;
    for(ll i = 0; i < n; i ++) {
        if(r1 + r2 > lim) break;
        v.pb(r1 + r2);
        r1 *= a;
        r2 *= b;
    }
    vector<ll> vec;
    foa(e, v) vec.pb((ll)e);
    sort(all(vec));
    return vec;
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, m, a, b, p, q;
    cin >> n >> m >> a >> b >> p >> q;
    if(max(a, b) == 1LL) {
        ll num = p + q;
        ll val = m / num;
        val = min(val, n);
        ll ans = abs(val * num - m);
        val ++;
        val = min(val, n);
        chmin(ans, abs(val * num - m));
        cout << ans << endl;
        return 0;
    }
    ll ans = INF;
    vector<ll> v = solve(n, m, a, b, p, q);

    n = v.size();
    ll mid = n / 2;
    vector<ll> vec;
    rep(bit, 1 << mid) {
        ll sum = 0;
        rep(i, mid) if(bit >> i & 1) {
            sum += v[i];
        }
        vec.pb(sum);
    }
    sort(all(vec));
    rep(bit, 1 << (n - mid)) {
        ll sum = 0;
        rep(i, n - mid) if(bit >> i & 1) {
            sum += v[i + mid];
        }
        auto itr = lower_bound(all(vec), m - sum);
        if(itr != vec.end()) chmin(ans, abs(*itr + sum - m));
        if(itr != vec.begin()) {
            itr --;
            chmin(ans, abs(*itr + sum - m));
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 12164
final_rank: 1

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

using f64 = long double;

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

int main() {
    const i128 max = 1e12;
    i128 n = in(), m = in(), a = in(), b = in(), p = in(), q = in();
    auto f = [&](i128 x) { return 0 <= m - x ? m - x : x - m; };
    i128 ans = f(0);
    if(a == 1 and b == 1) {
        chmin(ans, f(min(n, floor(m, p + q)) * (p + q)));
        chmin(ans, f(min(n, ceil (m, p + q)) * (p + q)));
    } else {
        vector<i128> xs;
        i128 pow_a = 1, pow_b = 1;
        for(i128 k : rep(n)) {
            i128 x = p * pow_a + q * pow_b;
            if(x <= m + max) {
                xs.push_back(x);
            } else break;
            pow_a *= a, pow_b *= b;
        }

        vector<i128> x0, x1;
        const int n0 = xs.size() / 2;
        const int n1 = xs.size() - n0;
        for(int i : rep(n0)) x0.push_back(xs[i]);
        for(int i : rep(n1)) x1.push_back(xs[n0 + i]);
        
        vector<i128> s0, s1;
        for(int s : rep(1 << n0)) {
            i128 sum = 0;
            for(int i : rep(n0)) if(s & (1 << i)) sum += x0[i];
            if(sum <= m + max) s0.push_back(sum);
        }
        for(int s : rep(1 << n1)) {
            i128 sum = 0;
            for(int i : rep(n1)) if(s & (1 << i)) sum += x1[i];
            if(sum <= m + max) s1.push_back(sum);
        }

        sort(s0);
        sort(s1);
        for(i128 x : s0) {
            auto it = lower_bound(s1.begin(), s1.end(), m - x);
            if(it != s1.end()) chmin(ans, f(x + *it));
            if(it != s1.begin()) chmin(ans, f(x + *prev(it)));
        }
    }
    print(ans);
}
accuracy: 1
time_ms: 210
memory_kb: 52680
final_rank: 8

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

using f64 = long double;

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

int main() {
    i128 n = in(), m = in(), a = in(), b = in(), p = in(), q = in();
    auto f = [&](i128 x) { return 0 <= m - x ? m - x : x - m; };
    i128 ans = f(0);
    if(a == 1 and b == 1) {
        chmin(ans, f(min(n, floor(m, p + q)) * (p + q)));
        chmin(ans, f(min(n, ceil (m, p + q)) * (p + q)));
    } else {
        vector<i128> xs;
        i128 pow_a = 1, pow_b = 1;
        for(i128 k : rep(n)) {
            i128 x = p * pow_a + q * pow_b;
            if(x <= m) {
                xs.push_back(x);
            } else break;
            pow_a *= a, pow_b *= b;
        }

        vector<i128> x0, x1;
        const int n0 = xs.size() / 2;
        const int n1 = xs.size() - n0;
        for(int i : rep(n0)) x0.push_back(xs[i]);
        for(int i : rep(n1)) x1.push_back(xs[n0 + i]);
        
        vector<i128> s0, s1;
        for(int s : rep(1 << n0)) {
            i128 sum = 0;
            for(int i : rep(n0)) if(s & (1 << i)) sum += x0[i];
            if(sum <= m + i128(1e12)) s0.push_back(sum);
        }
        for(int s : rep(1 << n1)) {
            i128 sum = 0;
            for(int i : rep(n1)) if(s & (1 << i)) sum += x1[i];
            if(sum <= m + i128(1e12)) s1.push_back(sum);
        }

        sort(s0);
        sort(s1);
        for(i128 x : s0) {
            auto it = lower_bound(s1.begin(), s1.end(), m - x);
            if(it != s1.end()) chmin(ans, f(x + *it));
            if(it != s1.begin()) chmin(ans, f(x + *prev(it)));
        }
    }
    print(ans);
}
accuracy: 0.6
time_ms: 150
memory_kb: 44656
final_rank: 12

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

using f64 = long double;

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

int main() {
    i128 n = in(), m = in(), a = in(), b = in(), p = in(), q = in();
    auto f = [&](i128 x) { return 0 <= m - x ? m - x : x - m; };
    i128 ans = f(0);
    if(a == 1 and b == 1) {
        chmin(ans, f(min(n, floor(m, p + q)) * (p + q)));
        chmin(ans, f(min(n, ceil (m, p + q)) * (p + q)));
    } else {
        vector<i128> xs;
        i128 pow_a = 1, pow_b = 1;
        for(i128 k : rep(n)) {
            i128 x = p * pow_a + q * pow_b;
            if(x <= m) {
                xs.push_back(x);
            } else break;
            pow_a *= a, pow_b *= b;
        }

        vector<i128> x0, x1;
        const int n0 = xs.size() / 2;
        const int n1 = xs.size() - n0;
        for(int i : rep(n0)) x0.push_back(xs[i]);
        for(int i : rep(n1)) x1.push_back(xs[n0 + i]);
        
        vector<i128> s0, s1;
        for(int s : rep(1 << n0)) {
            i128 sum = 0;
            for(int i : rep(n0)) if(s & (1 << i)) sum += x0[i];
            if(sum <= m) s0.push_back(sum);
        }
        for(int s : rep(1 << n1)) {
            i128 sum = 0;
            for(int i : rep(n1)) if(s & (1 << i)) sum += x1[i];
            if(sum <= m) s1.push_back(sum);
        }

        sort(s0);
        sort(s1);
        for(i128 x : s0) {
            auto it = lower_bound(s1.begin(), s1.end(), m - x);
            if(it != s1.end()) chmin(ans, f(x + *it));
            if(it != s1.begin()) chmin(ans, f(x + *prev(it)));
        }
    }
    print(ans);
}
accuracy: 0.3142857142857143
time_ms: 140
memory_kb: 44736
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
using i32 = int;
using i64 = long long;
using i128 = __int128_t;

i64 n, m, a, b, p, q; 

i64 solve1() {
    i64 res = m;
    { // 手前
        i64 v = min(n, m / (p + q)) * (p + q);
        res = min(res, abs(m - v));
    }
    { // 通り越す
        i64 v = min(n, (m + p + q + 1) / (p + q)) * (p + q);
        res = min(res, abs(m - v));
    }
    return res;
}

const i64 sup = 1e15;

i128 f(i64 k) {
    i128 pa = p;
    for (i32 _ = 0 ; _ < k ; _++) {
        if (pa > sup) return -1;
        pa *= a;
    }
    i128 qb = q;
    for (i32 _ = 0 ; _ < k ; _++) {
        if (qb > sup) return -1;
        qb *= b;
    }

    i128 res = pa + qb;
    if (res > sup) return -1;
    return res;
}

i64 solve2() {
    vector<i64> T{}; 
    for (i32 i = 0 ; i < n and f(i) > 0 ; i++) {
        T.push_back((i64)f(i));
        if (T.back() > m) break;
    }
    const i32 size = 20;
    vector<i64> F{}, B{};
    
    for (i32 bit = 0 ; bit < (1 << size) ; bit++) {
        i64 fr = 0LL, bk = 0LL;
        for (i32 i = 0 ; i < size ; i++) if (bit & (1 << i)) {
            fr = (i < (i32)T.size() ? fr + T[i] : -1LL);
            bk = (size + i < (i32)T.size() ? bk + T[i + size] : -1LL);
        }
        if (0 <= fr) F.push_back(fr);
        if (0 <= bk) B.push_back(bk);
    }
    sort(F.begin(), F.end());
    sort(B.begin(), B.end());

    i64 ans = m;
    for (auto fr : F) {
        auto it = lower_bound(B.begin(), B.end(), m - fr);
        if (it != B.end()) ans = min(ans, abs(*it + fr - m));
        if (it != B.begin()) ans = min(ans, abs(*prev(it) + fr - m));
    }

    return ans;

    return -1;
}

int main() {
    cin >> n >> m >> a >> b >> p >> q;
    if (a == 1 and b == 1) {
        cout << solve1() << endl;
    }
    else {
        cout << solve2() << endl;
    }
}
accuracy: 1
time_ms: 90
memory_kb: 19536
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;

vector<ll> all(vector<ll> xs){
  vector<ll> res;
  for(int i=0;i<(1<<xs.size());++i){
    ll add = 0;
    for(int j=0;j<(int)xs.size();++j){
      if((1<<j) & i){
        add += xs[j];
      }
    }
    res.push_back(add);
  }
  sort(res.begin(),res.end());
  return res;
}

ll func(){
  ll n;
  ll m;
  ll a;
  ll b;
  ll p;
  ll q;
  cin >> n >> m >> a >> b >> p >> q;
  if(a == 1 and b == 1){
    ll low = m / (p + q);
    ll high = low + 1;
    low = min(low, n);
    high = min(high, n);
    ll dist_l = abs(m - low * (p + q));
    ll dist_h = abs(m - high * (p + q));
    return min(dist_l, dist_h);
  }
  ll ak = 1;
  ll bk = 1;
  vector<ll> line;
  for(int i=0;i<n;++i){
    line.push_back(p * ak + q * bk);
    if((double)p * ak * a + (double) q * bk * b > m * 3)break;
    ak *= a;
    bk *= b;
  }
  int N = line.size();
  if(N == 1){
    return min(m, abs(m - line[0]));
  }
  vector<ll> as(line.begin(),line.begin()+N/2);
  vector<ll> bs(line.begin()+N/2,line.end());
  vector<ll> ass = all(as);
  ll res = m;
  for(auto i:all(bs)){
    int p = lower_bound(ass.begin(),ass.end(),m - i) - ass.begin();
    if(p != (int)ass.size()){
      res = min(res, abs(m - (i + ass[p])));
    }
    if(p > 0){
      res = min(res, abs(m - (i + ass[p-1])));
    }
  }
  return res;
}

int main(){
  cout << func() << endl;
}
accuracy: 1
time_ms: 240
memory_kb: 45444
final_rank: 7

code_snippet: // #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
// #include<atcoder/dsu>
using namespace std;
// using namespace atcoder;


void solve(){
	long long n, m, a, b, p, q;
	cin >> n >> m >> a >> b >> p >> q;
	if(a == 1 && b == 1){
		long long d = p + q;
		if(n <= m / d) cout << m - n * d << "\n";
		else{
			long long ans = min(m % d, (-m % d + d) % d);
			cout << ans << "\n";
		}
		return;
	}
	vector<long long> A;
	long long pp = p;
	long long qq = q;
	for(int i = 0; i < n; i++){
		A.push_back(pp + qq);
		if(pp + qq > m) break;
		if(pp > 2 * m / a) break;
		if(qq > 2 * m / b) break;
		pp *= a;
		qq *= b;
	}

	n = A.size();
	vector<long long> L, R;
	for(int i = 0; i < n; i++){
		if(i < n / 2) L.push_back(A[i]);
		else R.push_back(A[i]);
	}

	auto f=[&m](vector<long long> &A){
		vector<long long> res;
		res.push_back(0);
		for(auto a:A){
			int sz = res.size();
			for(int i = 0; i < sz; i++) res.push_back(a + res[i]);
		}
		sort(res.begin(), res.end());
		swap(A, res);
	};

	f(L);
	f(R);
	R.push_back(1LL << 60);
	long long ans = 1LL << 60;
	int r = 0;
	for(int i = L.size() - 1; i >= 0; i--){
		long long l = L[i];
		while(l + R[r] <= m) r++;
		if(m >= (l + R[r - 1])) ans = min(ans, m - (l + R[r - 1]));
		ans = min(ans, l + R[r] - m);
	}
	cout << ans << "\n";
}

int main(){
	cin.tie(0)->sync_with_stdio(0);
	int t;
	t = 1;
	// cin >> t;
	while(t--) solve();
}
accuracy: 1
time_ms: 40
memory_kb: 45136
final_rank: 4

code_snippet: // #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
// #include<atcoder/dsu>
using namespace std;
// using namespace atcoder;


void solve(){
	long long n, m, a, b, p, q;
	cin >> n >> m >> a >> b >> p >> q;
	if(a == 1 && b == 1){
		long long d = p + q;
		if(n <= m / d) cout << m - n * d << "\n";
		else{
			long long ans = min(m % d, (-m % d + d) % d);
			cout << ans << "\n";
		}
		return;
	}
	vector<long long> A;
	long long pp = p;
	long long qq = q;
	for(int i = 0; i < n; i++){
		A.push_back(pp + qq);
		if(pp + qq > m) break;
		if(pp > 2 * m / a) break;
		if(qq > 2 * m / b) break;
		pp *= a;
		qq *= b;
	}

	n = A.size();
	vector<long long> L, R;
	for(int i = 0; i < n; i++){
		if(i < n / 2) L.push_back(A[i]);
		else R.push_back(A[i]);
	}

	auto f=[&m](vector<long long> &A){
		vector<long long> res;
		res.push_back(0);
		for(auto a:A){
			int sz = res.size();
			for(int i = 0; i < sz; i++) res.push_back(a + res[i]);
		}
		sort(res.begin(), res.end());
		swap(A, res);
		while(A[A.size() - 1] > m) A.pop_back();
	};

	f(L);
	f(R);
	R.push_back(1LL << 60);
	long long ans = 1LL << 60;
	int r = 0;
	for(int i = L.size() - 1; i >= 0; i--){
		long long l = L[i];
		while(l + R[r] <= m) r++;
		ans = min(ans, m - (l + R[r - 1]));
		ans = min(ans, l + R[r] - m);
	}
	cout << ans << "\n";
}

int main(){
	cin.tie(0)->sync_with_stdio(0);
	int t;
	t = 1;
	// cin >> t;
	while(t--) solve();
}
accuracy: 0.3142857142857143
time_ms: 40
memory_kb: 27800
final_rank: 14

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
#define all(A) A.begin(),A.end()
#define ciN cin
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
using pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;

template<class T>
bool chmax(T& p, T q) {
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}
template<class T>
bool chmin(T& p, T q) {
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}
ll gcd(ll(a), ll(b)) {
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}


#define ss second.second
#define sf second.first
using paii = pair<int, int>;
//chmin必要
/*
有向木の最小全域木を求める.
O(|V|*|E|)らしい.知らんけど
*/
template<class T>
T Chu_Liu_Edmonds_algorithm(const vector<pair<T, paii>>& E, int v, int root = 0) {//E:{重み,{start,end}} //v:|V| //root:根

    vector<pair<T, int>> Vm(v, pair<T, int>(1e9, -1));
    vector<int> gr(v, 0);
    vector<bool> P(v, false), f(v, false);
    int cnt = 0;
    T res = 0;
    vector<pair<T, paii>> nE;

    for (auto e : E) {
        chmin(Vm[e.ss], { e.first, e.sf });
    }
    Vm[root] = { -1, -1 };

    rep(i, v) {
        if (f[i]) continue;
        vector<int> ch;
        int x = i;
        for (; x != -1 && !f[x];) {
            f[x] = 1;
            ch.push_back(x);
            x = Vm[x].second;
        }
        if (x == -1) {
            rep(j, ch.size()) {
                gr[ch[j]] = cnt;
                cnt++;
            }
        }
        else {
            bool Q = 0;
            for (long long j : ch) {
                gr[j] = cnt;
                if (j == x) {
                    P[cnt] = 1;
                    Q = 1;
                }
                if (!Q) cnt++;
            }
            if (Q) cnt++;
        }
    }
    if (cnt == v) {
        T ans = 1;
        rep(i, v)ans += Vm[i].first;
        //cout<<ans<<endl;
        return ans;
    }

    rep(i, v) {
        if (i != root && P[gr[i]]) res += Vm[i].first;
    }

    for (auto e : E) {
        int p = e.ss;
        int s = gr[e.sf];
        int t = gr[e.ss];
        if (s == t) continue;
        else if (P[t]) {
            nE.push_back({ e.first - Vm[p].first, { s, t } });
        }
        else {
            nE.push_back({ e.first, { s, t } });
        }
    }
    //cout<<res<<endl;
    return res + Chu_Liu_Edmonds_algorithm(nE, cnt, gr[root]);
}


int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N, M, A, B, P, Q;
    cin >> N >> M >> A >> B >> P >> Q;
    ll an = M;
    if (A == 1 && B == 1) {
        ll d = (P + Q);

        ll D = M / d;
        if (D <= N) {
            chmin(an, abs(M - D * d));
        }
        D++;
        if (D <= N) {
            chmin(an, abs(M - D * d));
        }
        if (N <= 1e18 / d)chmin(an, abs(M - N * d));
        cout << an << endl;
        return 0;
    }

    vll D;
    ll a = 1, b = 1;
    while (N!=0) {
        ll S = P * a + Q * b;
        if (S > 2 * M)break;
        D.push_back(S);
        if (a > 1e18 / A || b > 1e18 / B)break;
        a *= A;
        b *= B;
        N--;
    }

    //cout<<D.size()<<endl;

    N = D.size();

    set<ll> S;

    rep(bit, (1 << N / 2)) {
        ll res = 0;
        rep(i, N / 2) {
            if (bit & (1 << i))res += D[i];
        }
        S.insert(res);
    }
    S.insert(2e18);
    ll Z = N - N / 2;

    rep(bit, (1 << Z)) {
        ll res = 0;
        rep(i, Z) {
            if (bit & (1 << i))res += D[i + (N / 2)];
        }
        if (res >= M) {
            chmin(an, res - M);
            continue;
        }
        auto re = S.upper_bound(M - res);
        chmin(an, abs(M - res - *re));
        re--;
        chmin(an, abs(M - res - *re));
    }

    cout << an << endl;

}
accuracy: 1
time_ms: 260
memory_kb: 40096
final_rank: 5

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
#define all(A) A.begin(),A.end()
#define ciN cin
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
using pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;

template<class T>
bool chmax(T& p, T q) {
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}
template<class T>
bool chmin(T& p, T q) {
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}
ll gcd(ll(a), ll(b)) {
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}


#define ss second.second
#define sf second.first
using paii = pair<int, int>;
//chmin必要
/*
有向木の最小全域木を求める.
O(|V|*|E|)らしい.知らんけど
*/
template<class T>
T Chu_Liu_Edmonds_algorithm(const vector<pair<T, paii>>& E, int v, int root = 0) {//E:{重み,{start,end}} //v:|V| //root:根

    vector<pair<T, int>> Vm(v, pair<T, int>(1e9, -1));
    vector<int> gr(v, 0);
    vector<bool> P(v, false), f(v, false);
    int cnt = 0;
    T res = 0;
    vector<pair<T, paii>> nE;

    for (auto e : E) {
        chmin(Vm[e.ss], { e.first, e.sf });
    }
    Vm[root] = { -1, -1 };

    rep(i, v) {
        if (f[i]) continue;
        vector<int> ch;
        int x = i;
        for (; x != -1 && !f[x];) {
            f[x] = 1;
            ch.push_back(x);
            x = Vm[x].second;
        }
        if (x == -1) {
            rep(j, ch.size()) {
                gr[ch[j]] = cnt;
                cnt++;
            }
        }
        else {
            bool Q = 0;
            for (long long j : ch) {
                gr[j] = cnt;
                if (j == x) {
                    P[cnt] = 1;
                    Q = 1;
                }
                if (!Q) cnt++;
            }
            if (Q) cnt++;
        }
    }
    if (cnt == v) {
        T ans = 1;
        rep(i, v)ans += Vm[i].first;
        //cout<<ans<<endl;
        return ans;
    }

    rep(i, v) {
        if (i != root && P[gr[i]]) res += Vm[i].first;
    }

    for (auto e : E) {
        int p = e.ss;
        int s = gr[e.sf];
        int t = gr[e.ss];
        if (s == t) continue;
        else if (P[t]) {
            nE.push_back({ e.first - Vm[p].first, { s, t } });
        }
        else {
            nE.push_back({ e.first, { s, t } });
        }
    }
    //cout<<res<<endl;
    return res + Chu_Liu_Edmonds_algorithm(nE, cnt, gr[root]);
}


int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N, M, A, B, P, Q;
    cin >> N >> M >> A >> B >> P >> Q;
    ll an = M;
    if (A == 1 && B == 1) {
        ll d = (P + Q);

        ll D = M / d;
        if (D <= N) {
            chmin(an, abs(M - D * d));
        }
        D++;
        if (D <= N) {
            chmin(an, abs(M - D * d));
        }
        if (N <= 1e18 / d)chmin(an, abs(M - N * d));
        cout << an << endl;
        return 0;
    }

    vll D;
    ll a = 1, b = 1;
    while (1||N==0) {
        ll S = P * a + Q * b;
        if (S > 2 * M)break;
        D.push_back(S);
        if (a > 1e18 / A || b > 1e18 / B)break;
        a *= A;
        b *= B;
        N--;
    }

    //cout<<D.size()<<endl;

    N = D.size();

    set<ll> S;

    rep(bit, (1 << N / 2)) {
        ll res = 0;
        rep(i, N / 2) {
            if (bit & (1 << i))res += D[i];
        }
        S.insert(res);
    }
    S.insert(2e18);
    ll Z = N - N / 2;

    rep(bit, (1 << Z)) {
        ll res = 0;
        rep(i, Z) {
            if (bit & (1 << i))res += D[i + (N / 2)];
        }
        if (res >= M) {
            chmin(an, res - M);
            continue;
        }
        auto re = S.upper_bound(M - res);
        chmin(an, abs(M - res - *re));
        re--;
        chmin(an, abs(M - res - *re));
    }

    cout << an << endl;

}
accuracy: 0.7142857142857143
time_ms: 260
memory_kb: 40068
final_rank: 10

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
#define all(A) A.begin(),A.end()
#define ciN cin
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
using pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;

template<class T>
bool chmax(T& p, T q) {
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}
template<class T>
bool chmin(T& p, T q) {
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}
ll gcd(ll(a), ll(b)) {
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}


#define ss second.second
#define sf second.first
using paii = pair<int, int>;
//chmin必要
/*
有向木の最小全域木を求める.
O(|V|*|E|)らしい.知らんけど
*/
template<class T>
T Chu_Liu_Edmonds_algorithm(const vector<pair<T, paii>>& E, int v, int root = 0) {//E:{重み,{start,end}} //v:|V| //root:根

    vector<pair<T, int>> Vm(v, pair<T, int>(1e9, -1));
    vector<int> gr(v, 0);
    vector<bool> P(v, false), f(v, false);
    int cnt = 0;
    T res = 0;
    vector<pair<T, paii>> nE;

    for (auto e : E) {
        chmin(Vm[e.ss], { e.first, e.sf });
    }
    Vm[root] = { -1, -1 };

    rep(i, v) {
        if (f[i]) continue;
        vector<int> ch;
        int x = i;
        for (; x != -1 && !f[x];) {
            f[x] = 1;
            ch.push_back(x);
            x = Vm[x].second;
        }
        if (x == -1) {
            rep(j, ch.size()) {
                gr[ch[j]] = cnt;
                cnt++;
            }
        }
        else {
            bool Q = 0;
            for (long long j : ch) {
                gr[j] = cnt;
                if (j == x) {
                    P[cnt] = 1;
                    Q = 1;
                }
                if (!Q) cnt++;
            }
            if (Q) cnt++;
        }
    }
    if (cnt == v) {
        T ans = 1;
        rep(i, v)ans += Vm[i].first;
        //cout<<ans<<endl;
        return ans;
    }

    rep(i, v) {
        if (i != root && P[gr[i]]) res += Vm[i].first;
    }

    for (auto e : E) {
        int p = e.ss;
        int s = gr[e.sf];
        int t = gr[e.ss];
        if (s == t) continue;
        else if (P[t]) {
            nE.push_back({ e.first - Vm[p].first, { s, t } });
        }
        else {
            nE.push_back({ e.first, { s, t } });
        }
    }
    //cout<<res<<endl;
    return res + Chu_Liu_Edmonds_algorithm(nE, cnt, gr[root]);
}


int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N, M, A, B, P, Q;
    cin >> N >> M >> A >> B >> P >> Q;
    ll an = M;
    if (A == 1 && B == 1) {
        ll d = (P + Q);

        ll D = M / d;
        if (D <= N) {
            chmin(an, abs(M - D * d));
        }
        D++;
        if (D <= N) {
            chmin(an, abs(M - D * d));
        }
        if (N <= 1e18 / d)chmin(an, abs(M - N * d));
        cout << an << endl;
        return 0;
    }

    vll D;
    ll a = 1, b = 1;
    while (1) {
        ll S = P * a + Q * b;
        if (S > 2 * M)break;
        D.push_back(S);
        if (a > 1e18 / A || b > 1e18 / B)break;
        a *= A;
        b *= B;
    }

    //cout<<D.size()<<endl;

    N = D.size();

    set<ll> S;

    rep(bit, (1 << N / 2)) {
        ll res = 0;
        rep(i, N / 2) {
            if (bit & (1 << i))res += D[i];
        }
        S.insert(res);
    }
    S.insert(2e18);
    ll Z = N - N / 2;

    rep(bit, (1 << Z)) {
        ll res = 0;
        rep(i, Z) {
            if (bit & (1 << i))res += D[i + (N / 2)];
        }
        if (res >= M) {
            chmin(an, res - M);
            continue;
        }
        auto re = S.upper_bound(M - res);
        chmin(an, abs(M - res - *re));
        re--;
        chmin(an, abs(M - res - *re));
    }

    cout << an << endl;

}
accuracy: 0.7142857142857143
time_ms: 270
memory_kb: 40084
final_rank: 11

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
#define all(A) A.begin(),A.end()
#define ciN cin
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
using pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;

template<class T>
bool chmax(T& p, T q) {
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}
template<class T>
bool chmin(T& p, T q) {
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}
ll gcd(ll(a), ll(b)) {
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}


#define ss second.second
#define sf second.first
using paii = pair<int, int>;
//chmin必要
/*
有向木の最小全域木を求める.
O(|V|*|E|)らしい.知らんけど
*/
template<class T>
T Chu_Liu_Edmonds_algorithm(const vector<pair<T, paii>>& E, int v, int root = 0) {//E:{重み,{start,end}} //v:|V| //root:根

    vector<pair<T, int>> Vm(v, pair<T, int>(1e9, -1));
    vector<int> gr(v, 0);
    vector<bool> P(v, false), f(v, false);
    int cnt = 0;
    T res = 0;
    vector<pair<T, paii>> nE;

    for (auto e : E) {
        chmin(Vm[e.ss], { e.first, e.sf });
    }
    Vm[root] = { -1, -1 };

    rep(i, v) {
        if (f[i]) continue;
        vector<int> ch;
        int x = i;
        for (; x != -1 && !f[x];) {
            f[x] = 1;
            ch.push_back(x);
            x = Vm[x].second;
        }
        if (x == -1) {
            rep(j, ch.size()) {
                gr[ch[j]] = cnt;
                cnt++;
            }
        }
        else {
            bool Q = 0;
            for (long long j : ch) {
                gr[j] = cnt;
                if (j == x) {
                    P[cnt] = 1;
                    Q = 1;
                }
                if (!Q) cnt++;
            }
            if (Q) cnt++;
        }
    }
    if (cnt == v) {
        T ans = 1;
        rep(i, v)ans += Vm[i].first;
        //cout<<ans<<endl;
        return ans;
    }

    rep(i, v) {
        if (i != root && P[gr[i]]) res += Vm[i].first;
    }

    for (auto e : E) {
        int p = e.ss;
        int s = gr[e.sf];
        int t = gr[e.ss];
        if (s == t) continue;
        else if (P[t]) {
            nE.push_back({e.first - Vm[p].first, { s, t }});
        }
        else {
            nE.push_back({e.first, { s, t }});
        }
    }
    //cout<<res<<endl;
    return res + Chu_Liu_Edmonds_algorithm(nE, cnt, gr[root]);
}


int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    ll N,M,A,B,P,Q;
    cin>>N>>M>>A>>B>>P>>Q;
    ll an=M;
    if(A==1&&B==1){
        ll d=(P+Q);

        ll D=M/d;
        if(D<=N){
            chmin(an,abs(M-D*d));
        }
        D++;
        if(D<=N){
            chmin(an,abs(M-D*d));
        }
        if(N<=1e18/d)chmin(an,abs(M-N*d));
        cout<<an<<endl;
        return 0;
    }

    vll D;
    ll a=1,b=1;
    while(1){
        ll S=P*a+Q*b;
        if(S>2*M)break;
        D.push_back(S);
        a*=A;
        b*=B;
    }

    //cout<<D.size()<<endl;

    N=D.size();

    set<ll> S;

    rep(bit,(1<<N/2)){
        ll res=0;
        rep(i,N/2){
            if(bit&(1<<i))res+=D[i];
        }
        S.insert(res);
    }
    S.insert(2e18);
    ll Z=N-N/2;

    rep(bit,(1<<Z)){
        ll res=0;
        rep(i,Z){
            if(bit&(1<<i))res+=D[i+N/2];
        }
        if(res>M){
            chmin(an,res-M);
            continue;
        }
        auto re=S.upper_bound(M-res);
        chmin(an,abs(M-res-*re));
        re--;
        chmin(an,abs(M-res-*re));
    }

    cout<<an<<endl;

}
accuracy: 0.7142857142857143
time_ms: 260
memory_kb: 39880
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    ll n, m, a, b, p, q;
    cin >> n >> m >> a >> b >> p >> q;
    if (a == 1 && b == 1) {
        if (m/(p+q)>=n) {
            cout << m-n*(p+q) << endl;
        } else {
            ll r = m % (p + q);
            cout << min(r, p+q-r) << endl;
        }
        return 0;
    }
    chmin(n, 40LL);
    vector<ll> first;
    rep(S,0,1<<(n/2)) {
        ll s = 0;
        ll pa = p, qb = q;
        bool fa = false, fb = false;
        bool ok = true;
        rep(i,0,n/2) {
            if (S>>i&1) {
                if (fa || fb) {
                    ok = false;
                    break;
                }
                s += pa+qb;
            }
            if (fa || pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (fb || qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        if (ok && s < 1e15) {
            first.push_back(s);
        }
    }
    sort(all(first));
    ll ans = m;
    rep(S,0,1<<(n-n/2)) {
        ll pa = p, qb = q;
        bool fa = false, fb = false;
        rep(_,0,n/2) {
            if (fa || pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (fb || qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        ll s = 0;
        bool ok = true;
        rep(i,0,n-n/2) {
            if (S>>i&1) {
                if (fa || fb) {
                    ok = false;
                    break;
                }
                s += pa+qb;
            }
            if (pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        if (ok && s < 1e15) {
            auto it = lower_bound(all(first), m-s);
            if (it != first.end()) chmin(ans, abs(s+*it-m));
            if (it != first.begin()) chmin(ans, abs(s+*--it-m));
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 830
memory_kb: 12488
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    ll n, m, a, b, p, q;
    cin >> n >> m >> a >> b >> p >> q;
    if (a == 1 && b == 1) {
        if (m/(p+q)>n) {
            cout << m-n*(p+q) << endl;
        } else {
            ll r = m % (p + q);
            cout << min(r, p+q-r) << endl;
        }
        return 0;
    }
    chmin(n, 40LL);
    vector<ll> first;
    rep(S,0,1<<(n/2)) {
        ll s = 0;
        ll pa = p, qb = q;
        bool fa = false, fb = false;
        bool ok = true;
        rep(i,0,n/2) {
            if (S>>i&1) {
                if (fa || fb) {
                    ok = false;
                    break;
                }
                s += pa+qb;
            }
            if (fa || pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (fb || qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        if (ok && s < 1e15) {
            first.push_back(s);
        }
    }
    sort(all(first));
    ll ans = m;
    rep(S,0,1<<(n-n/2)) {
        ll pa = p, qb = q;
        bool fa = false, fb = false;
        rep(_,0,n/2) {
            if (fa || pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (fb || qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        ll s = 0;
        bool ok = true;
        rep(i,0,n-n/2) {
            if (S>>i&1) {
                if (fa || fb) {
                    ok = false;
                    break;
                }
                s += pa+qb;
            }
            if (pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        if (ok && s < 1e15) {
            auto it = lower_bound(all(first), m-s);
            if (it != first.end()) chmin(ans, abs(s+*it-m));
            if (it != first.begin()) chmin(ans, abs(s+*--it-m));
        }
    }
    cout << ans << endl;
}
accuracy: 0.5428571428571428
time_ms: 830
memory_kb: 12504
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    ll n, m, a, b, p, q;
    cin >> n >> m >> a >> b >> p >> q;
    if (a == 1 && b == 1) {
        if (m/(p+q)>n) {
            cout << m-n*(p+q) << endl;
        } else {
            ll r = m % (p + q);
            cout << min(r, p+q-r) << endl;
        }
        return 0;
    }
    vector<ll> first;
    int L = min(21LL, n);
    rep(S,0,1<<L) {
        ll s = 0;
        ll pa = p, qb = q;
        bool fa = false, fb = false;
        bool ok = true;
        rep(i,0,L) {
            if (S>>i&1) {
                if (fa || fb) {
                    ok = false;
                    break;
                }
                s += pa+qb;
            }
            if (fa || pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (fb || qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        if (ok && s < 1e15) {
            first.push_back(s);
        }
    }
    sort(all(first));
    ll ans = m;
    if (L == n) {
        auto it = lower_bound(all(first), m);
        if (it != first.end()) chmin(ans, abs(*it-m));
        if (it != first.begin()) chmin(ans, abs(*--it-m));
        cout << ans << endl;
        return 0;
    }
    L = min(21, L-21);
    rep(S,0,1<<L) {
        ll pa = p, qb = q;
        bool fa = false, fb = false;
        rep(_,0,21) {
            if (fa || pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (fb || qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        ll s = 0;
        bool ok = true;
        rep(i,0,L) {
            if (S>>i&1) {
                if (fa || fb) {
                    ok = false;
                    break;
                }
                s += pa+qb;
            }
            if (pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        if (ok && s < 1e15) {
            auto it = lower_bound(all(first), m-s);
            if (it != first.end()) chmin(ans, abs(s+*it-m));
            if (it != first.begin()) chmin(ans, abs(s+*--it-m));
        }
    }
    cout << ans << endl;
}
accuracy: 0.2
time_ms: 630
memory_kb: 20564
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    ll n, m, a, b, p, q;
    cin >> n >> m >> a >> b >> p >> q;
    if (a == 1 && b == 1) {
        if (m-n*(p+q)>0)  {
            cout << m-n*(p+q) << endl;
        } else {
            ll r = m % (p + q);
            cout << min(r, p+q-r) << endl;
        }
        return 0;
    }
    vector<ll> first;
    int L = min(20LL, n);
    rep(S,0,1<<L) {
        ll s = 0;
        ll pa = p, qb = q;
        bool fa = false, fb = false;
        bool ok = true;
        rep(i,0,L) {
            if (S>>i&1) {
                if (fa || fb) {
                    ok = false;
                    break;
                }
                s += pa+qb;
            }
            if (fa || pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (fb || qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        if (ok && s < 1e15) {
            first.push_back(s);
        }
    }
    sort(all(first));
    ll ans = m;
    if (L == n) {
        auto it = lower_bound(all(first), m);
        if (it != first.end()) chmin(ans, abs(*it-m));
        if (it != first.begin()) chmin(ans, abs(*--it-m));
        cout << ans << endl;
        return 0;
    }
    L = min(20, L-20);
    rep(S,0,1<<L) {
        ll pa = p, qb = q;
        bool fa = false, fb = false;
        rep(_,0,L) {
            if (fa || pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (fb || qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        ll s = 0;
        bool ok = true;
        rep(i,0,L) {
            if (S>>i&1) {
                if (fa || fb) {
                    ok = false;
                    break;
                }
                s += pa+qb;
            }
            if (pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        if (ok && s < 1e15) {
            auto it = lower_bound(all(first), m-s);
            if (it != first.end()) chmin(ans, abs(s+*it-m));
            if (it != first.begin()) chmin(ans, abs(s+*--it-m));
        }
    }
    cout << ans << endl;
}
accuracy: 0.2
time_ms: 290
memory_kb: 12628
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    ll n, m, a, b, p, q;
    cin >> n >> m >> a >> b >> p >> q;
    if (a == 1 && b == 1) {
        ll r = m % (p + q);
        cout << min(r, p+q-r) << endl;
        return 0;
    }
    vector<ll> first;
    int L = 20;
    rep(S,0,1<<L) {
        ll s = 0;
        ll pa = p, qb = q;
        bool fa = false, fb = false;
        bool ok = true;
        rep(i,0,L) {
            if (S>>i&1) {
                if (fa || fb) {
                    ok = false;
                    break;
                }
                s += pa+qb;
            }
            if (fa || pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (fb || qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        if (ok && s < 1e15) {
            first.push_back(s);
        }
    }
    sort(all(first));
    ll ans = m;
    rep(S,0,1<<L) {
        ll pa = p, qb = q;
        bool fa = false, fb = false;
        rep(_,0,L) {
            if (fa || pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (fb || qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        ll s = 0;
        bool ok = true;
        rep(i,0,L) {
            if (S>>i&1) {
                if (fa || fb) {
                    ok = false;
                    break;
                }
                s += pa+qb;
            }
            if (pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        if (ok && s < 1e15) {
            auto it = lower_bound(all(first), m-s);
            if (it != first.end()) chmin(ans, abs(s+*it-m));
            if (it != first.begin()) chmin(ans, abs(s+*--it-m));
        }
    }
    cout << ans << endl;
}
accuracy: 0.17142857142857143
time_ms: 830
memory_kb: 13012
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    ll n, m, a, b, p, q;
    cin >> n >> m >> a >> b >> p >> q;
    if (a == 1 && b == 1) {
        ll r = m % (p + q);
        cout << min(r, m-r) << endl;
        return 0;
    }
    vector<ll> first;
    int L = 20;
    rep(S,0,1<<L) {
        ll s = 0;
        ll pa = p, qb = q;
        bool fa = false, fb = false;
        bool ok = true;
        rep(i,0,L) {
            if (S>>i&1) {
                if (fa || fb) {
                    ok = false;
                    break;
                }
                s += pa+qb;
            }
            if (fa || pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (fb || qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        if (ok && s < 1e15) {
            first.push_back(s);
        }
    }
    sort(all(first));
    ll ans = m;
    rep(S,0,1<<L) {
        ll pa = p, qb = q;
        bool fa = false, fb = false;
        rep(_,0,L) {
            if (fa || pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (fb || qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        ll s = 0;
        bool ok = true;
        rep(i,0,L) {
            if (S>>i&1) {
                if (fa || fb) {
                    ok = false;
                    break;
                }
                s += pa+qb;
            }
            if (pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        if (ok && s < 1e15) {
            auto it = lower_bound(all(first), m-s);
            if (it != first.end()) chmin(ans, abs(s+*it-m));
            if (it != first.begin()) chmin(ans, abs(s+*--it-m));
        }
    }
    cout << ans << endl;
}
accuracy: 0.11428571428571428
time_ms: 790
memory_kb: 12692
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    ll n, m, a, b, p, q;
    cin >> n >> m >> a >> b >> p >> q;
    if (a == 1 && b == 1) {
        ll r = m % (p + q);
        cout << min(r, m-r) << endl;
    }
    vector<ll> first;
    int L = 20;
    rep(S,0,1<<L) {
        ll s = 0;
        ll pa = p, qb = q;
        bool fa = false, fb = false;
        bool ok = true;
        rep(i,0,L) {
            if (S>>i&1) {
                if (fa || fb) {
                    ok = false;
                    break;
                }
                s += pa+qb;
            }
            if (fa || pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (fb || qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        if (ok && s < 1e15) {
            first.push_back(s);
        }
    }
    sort(all(first));
    ll ans = m;
    rep(S,0,1<<L) {
        ll pa = p, qb = q;
        bool fa = false, fb = false;
        rep(_,0,L) {
            if (fa || pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (fb || qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        ll s = 0;
        bool ok = true;
        rep(i,0,L) {
            if (S>>i&1) {
                if (fa || fb) {
                    ok = false;
                    break;
                }
                s += pa+qb;
            }
            if (pa > (ll)1e18/a) {
                fa = true;
            } else {
                pa *= a;
            }
            if (qb > (ll)1e18/b) {
                fb = true;
            } else {
                qb *= b;
            }
        }
        if (ok && s < 1e15) {
            auto it = lower_bound(all(first), m-s);
            if (it != first.end()) chmin(ans, abs(s+*it-m));
            if (it != first.begin()) chmin(ans, abs(s+*--it-m));
        }
    }
    cout << ans << endl;
}
accuracy: 0.11428571428571428
time_ms: 830
memory_kb: 12848
final_rank: 20