
我真的服了,注意数据范围!!!!!!!!!!!!!!!!!!!!
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> PII;
#define pb emplace_back
//#define int ll
#define all(a) a.begin(),a.end()
#define x first
#define y second
#define ps push_back
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
void solve();
const int N = 1e6 + 10;
signed main() {
    IOS;
    ll t;
    cin >> t;
    while(t--)
    solve();
    return 0;
}
void solve() {
    ll a,b,c,n,w;
    cin >> a >> b >> c >> n >> w;
    ll k1 = a-c,k2 = b-c;
    ll ans = LONG_LONG_MAX;
    for(int x = 0; x <= n; ++ x)
    {
        ll f1,f2;
        ll pa1 = k1*x + c*n - w;
        ll l = 0,r = n-x;
        while(l <= r)
        {
            ll lp = l + (r - l)/3;
            ll rp = r - (r - l)/3;
            f1 = abs(pa1 + k2*lp);
            f2 = abs(pa1 + k2*rp);
            if(f1 > f2)
                l = lp + 1;
            else
                r = rp - 1;
        }
        ans = min(ans,min(f1,f2));
    }
    cout << ans << endl;
}三分y,其中z被消掉了,x是枚举,求导易得为常数,为线性函数。
void GordenGhost() {
    ll ans=inf;
    int a,b,c,n,w;
    cin>>a>>b>>c>>n>>w;
//    if (c>b) swap(b,c);
    for (int x = 0; x <= n ; ++x) {
        int l=0,r=n-x;
        while (l<r){
            int mid=(l+r)>>1;
//            if (x+mid>n){
//                r=mid-1;
//                continue;
//            }
            auto check = [&](int k){
                int kl=n-x-k;
                return abs(x*a+k*b+kl*c-w);
            };
            int m1=check(mid),m2=check(mid+1);
            if (m2>m1) r=mid;
            else l=mid+1;
        }
//        for(int i=l;i<=r;i++)
        ans= min(ans, abs(x*a+l*b+(n-x-l)*c-w));
    }
    cout<<ans<<'\n';
}


















