QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#1077#683471#9527. A Brand New Geometric Problemucup-team4504woodie_0064Success!2024-10-28 01:11:232024-10-28 01:11:24

詳細信息

Extra Test:

Time Limit Exceeded

input:

1000 10000000000 9340531200
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 86400 934053120 3316950 16896 224640 9434880 2779920 591360 243 224 3120...

output:


result:


ID题目提交者结果用时内存语言文件大小提交时间测评时间
#683471#9527. A Brand New Geometric Problemwoodie_0064#TL 904ms4524kbC++202.2kb2024-10-27 21:19:392024-11-06 13:19:27

answer

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn = 1e5 + 5;
const ll inf = 1e18;
int n, cnt1;
ll S, M, ans, a[maxn];
vector<ll> d;
vector<int> pos;
void dfs(ll x, int lim, int now, int res, ll sum) {
    sum += x;
    if(S - sum >= 0) {
        int to = now, p = lower_bound(d.begin(), d.end(), x) - d.begin();
        ll nres = res;
        if(!d.empty() && to <= n && a[to] < d[p]) {
            nres += pos[p] - to;
            to = pos[p];
        }
        // a[to] >= x
        if(to <= n && a[to] == x) {
            to++;
        }
        else {
            nres++;
        }
        nres += n - to + 1;
        nres += abs(S - sum - cnt1);
        ans = min(ans, nres); 
    }
    sum -= x;
    ll t = sqrt(x);
    for(int i = lim; i < (int)d.size() && d[i] <= t; i++) {
        if(x % d[i] == 0) {
            int to = now, nres = res;
            if(to <= n && a[to] < d[i]) {
                nres += pos[i] - to;
                to = pos[i];
            }
            // a[to] >= d[i]
            if(to <= n && a[to] == d[i]) {
                to++;
            }
            else {
                nres++;
            }
            dfs(x / d[i], i, to, nres, sum + d[i]);
        }
    }
}
int main() {
#ifdef yczDEBUG
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    int N;
    cin >> N >> S >> M;
    n = cnt1 = 0;
    int t = sqrt(M);
    for(int i = 2; i <= t; i++) {
        if(M % i == 0) {
            d.push_back(i);
            if(M / i != i) {
                d.push_back(M / i);
            }
        }
    }
    if(M != 1) {
        d.push_back(M);
    }
    sort(d.begin(), d.end());
    for(int i = 1; i <= N; i++) {
        ll x;
        cin >> x;
        if(x == 1) {
            cnt1++;
        }
        else {
            a[++n] = x;
        }
    }
    sort(a + 1, a + 1 + n);
    for(int i = 0; i < (int)d.size(); i++) {
        pos.push_back(lower_bound(a + 1, a + 1 + n, d[i]) - a);
    }
    ans = inf;
    dfs(M, 0, 1, 0, 0);
    if(ans == inf) {
        cout << "-1\n";
    }
    else {
        cout << ans << '\n';
    }
    return 0;
}