F. Final Boss

纯粹的發表於2024-06-13

原題連結

題解

1.由於一回合可以使用多次技能,所以直接二分回合數即可
2.回合數最多為 \(4^{10}\)

code

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

#define ll long long

ll a[200005],c[200005];
ll h,n;

inline void read(ll &x) {
    x = 0;
    ll flag = 1;
    char c = getchar();
    while(c < '0' || c > '9'){
        if(c == '-') flag = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        x = (x << 3) + (x << 1) + (c ^ 48);
        c = getchar();
    }
    x *= flag;
}

inline void write(ll x)
{
    if(x < 0){
        putchar('-');
        x = -x;
    }
    if(x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}

bool check(ll len)
{
    ll ans = 0;
    for(ll i = 1; i <= n; i++)
    {
        ans += a[i] * ((len-1LL) / c[i]+1LL);
        if(ans>=h) return 1;
    }
    return 0;
}

int main()
{
    ll t;
    read(t);
    while(t--)
    {
        read(h);
        read(n);

        for(ll i = 1; i <= n; i++) read(a[i]);
        for(ll i = 1; i <= n; i++) read(c[i]);

        ll l = 0, r = 4e11;
        while(l + 1LL < r)//時間複雜度最多為40*n
        {
            ll mid = (l + r) / 2LL;
            if(check(mid)) r = mid;
            else l = mid;
        }

        write(r);
        putchar('\n');
    }
    return 0;
}

相關文章