QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#93599#5503. Euclidean AlgorithmHCPS42#TL 18641ms3388kbC++174.2kb2023-04-01 20:19:462023-04-01 20:19:47

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-01 20:19:47]
  • 评测
  • 测评结果:TL
  • 用时:18641ms
  • 内存:3388kb
  • [2023-04-01 20:19:46]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef __int128_t LL;

ll my_sqrt(ll x) {
    ll lef = 0, rig = x;
    while (lef < rig) {
        ll mid = (lef + rig) / 2;
        if (LL(mid + 1) * (mid + 1) <= x) {
            lef = mid + 1;
        } else {
            rig = mid;
        }
    }
    return lef;
}

int slow(int x, int y) {
    set<int> all;
    queue<int> q;
    q.push(x);
    q.push(y);
    while (!q.empty()) {
        int a = q.front();
        q.pop();
        if (all.count(a)) {
            continue;
        }
        if (a > x * y + x + y + 200) {
            continue;
        }
        for (int b : all) {
            if (2 * a - b >= 1 && !all.count(2 * a - b)) {
                q.push(2 * a - b);
            }
            if (2 * b - a >= 1 && !all.count(2 * b - a)) {
                q.push(2 * b - a);
            }
        }
        all.insert(a);
    }
    return *all.begin();
}

int slow2(int x, int y) {
    if (y >= 2 * x) {
        return slow(x, y);
    } else {
        return slow2(2 * x - y, x);
    }
}

int slow3(int x, int y) {
    if (x == y) {
        return true;
    } else if (2 * x <= y) {
        return y % x == 0;
    } else {
        return slow3(2 * x - y, x);
    }
}

int slow4(int n) {
    int res = 0;
    for (int x = 1; x <= n; x++) {
        for (int y = x + 1; y <= n; y++) {
            if (slow3(x, y)) {
                res++;
            }
        }
    }
    return res;
}

void foo() {
    int n = 50;
    for (int x = 1; x <= n; x++) {
        cout << x << " : ";
        for (int y = x + 1; y <= n; y++) {
            /*
            if (__gcd(x, y) == slow(x, y)) {
                cout << y << " ";
            }
             */
            if ((slow(x, y) == __gcd(x, y)) != slow3(x, y)) {
                cout << y << " ";
            }
        }
        cout << "\n";
    }
}

ll slow5(ll n) {
    ll ans = 0;
    for (ll x = 1; x <= n; x++) {
        for (ll k = 2; k <= n; k++) {
            ans += (n / x - 1) / (k - 1);
        }
    }
    return ans;
}

ll solve(ll n) {
    ll ans = 0;
    ll sqrt_1 = my_sqrt(n);
    for (ll x = 1; x <= sqrt_1; x++) {
        ll sqrt_2 = my_sqrt(n / x - 1);
        for (ll k = 2; k <= sqrt_2 + 1; k++) {
            ans += (n / x - 1) / (k - 1);
            // cout << x << " : " << k << "\n";
        }
        for (ll dk = 1; dk <= sqrt_2; dk++) {
            ll lk = (n / x + dk) / (dk + 1) + 1;
            ll rk = (n / x - 1) / dk + 1;
            lk = max(lk, sqrt_2 + 2);
            if (lk <= rk) {
                // cout << x << " : " << lk << " " << rk << " | " << dk << "\n";
                // cout << x << " " << lk << " " << rk << " " << dk << "\n";
                ans += dk * (rk - lk + 1);
            }
        }
    }
    for (ll dx = 1; dx <= sqrt_1; dx++) {
        ll lx = (n + 1 + dx) / (dx + 1);
        ll rx = n / dx;
        lx = max(lx, sqrt_1 + 1);
        if (lx > rx) {
            continue;
        }
        ll sqrt_2 = my_sqrt(dx - 1);
        for (ll k = 2; k <= sqrt_2 + 1; k++) {
            ans += (dx - 1) / (k - 1) * (rx - lx + 1);
            // cout << lx << " " << rx << " : " << k << "\n";
        }
        for (ll dk = 1; dk <= sqrt_2; dk++) {
            ll lk = (dx + dk) / (dk + 1) + 1;
            ll rk = (dx - 1) / dk + 1;
            lk = max(lk, sqrt_2 + 2);
            if (lk <= rk) {
                // cout << lx << " " << rx << " : " << lk << " " << rk << "\n";
                ans += dk * (rk - lk + 1) * (rx - lx + 1);
            }
        }
    }
    return ans;
}

void stress() {
    for (int n = 1; n <= 1000; n++) {
        if (solve(n) != slow5(n)) {
            cout << n << "\n";
        }
    }
    exit(0);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif

    // foo();

    // stress();

    int t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;
        cout << solve(n) << "\n";
    }

#ifdef LOCAL
    cout << "\nTime elapsed: " << double(clock()) / CLOCKS_PER_SEC << " s.\n";
#endif
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 3388kb

input:

3
2
5
14

output:

1
9
62

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 18641ms
memory: 3340kb

input:

3
29107867360
65171672278
41641960535

output:

8921593237533
21300450379732
13136180138425

result:

ok 3 lines

Test #3:

score: -100
Time Limit Exceeded

input:

3
90076809172
100000000000
99913139559

output:


result: