結果

問題 No.800 四平方定理
ユーザー finefine
提出日時 2019-03-17 22:39:57
言語 C++14
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 522 ms / 2,000 ms
コード長 1,167 bytes
コンパイル時間 1,415 ms
使用メモリ 43,316 KB
最終ジャッジ日時 2023-02-11 09:48:24
合計ジャッジ時間 9,685 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 5 ms
3,608 KB
testcase_01 AC 9 ms
3,864 KB
testcase_02 AC 6 ms
3,660 KB
testcase_03 AC 7 ms
3,748 KB
testcase_04 AC 6 ms
3,732 KB
testcase_05 AC 7 ms
3,660 KB
testcase_06 AC 10 ms
3,960 KB
testcase_07 AC 10 ms
4,236 KB
testcase_08 AC 12 ms
4,056 KB
testcase_09 AC 9 ms
3,936 KB
testcase_10 AC 250 ms
23,088 KB
testcase_11 AC 271 ms
25,632 KB
testcase_12 AC 281 ms
25,948 KB
testcase_13 AC 227 ms
19,296 KB
testcase_14 AC 252 ms
24,196 KB
testcase_15 AC 285 ms
26,024 KB
testcase_16 AC 251 ms
24,404 KB
testcase_17 AC 252 ms
24,468 KB
testcase_18 AC 310 ms
27,268 KB
testcase_19 AC 309 ms
27,344 KB
testcase_20 AC 1 ms
3,480 KB
testcase_21 AC 1 ms
3,396 KB
testcase_22 AC 312 ms
27,268 KB
testcase_23 AC 522 ms
43,316 KB
testcase_24 AC 449 ms
34,664 KB
testcase_25 AC 522 ms
43,208 KB
testcase_26 AC 2 ms
3,532 KB
testcase_27 AC 1 ms
3,448 KB
testcase_28 AC 445 ms
34,512 KB
testcase_29 AC 489 ms
43,104 KB
testcase_30 AC 450 ms
34,572 KB
testcase_31 AC 416 ms
32,904 KB
testcase_32 AC 462 ms
43,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, d;
    cin >> n >> d;
    vector<int> v1;
    for (int x = 1; x <= n; x++) {
        for (int y = 1; y <= n; y++) {
            v1.push_back(x * x + y * y);
        }
    }
    sort(v1.begin(), v1.end());

    vector<int> v2;
    for (int w = 1; w <= n; w++) {
        int lim = min(n * n, w * w + d);
        for (int z = 1; z * z <= lim; z++) {
            v2.push_back(d + w * w - z * z);
        }
    }
    sort(v2.begin(), v2.end());

    int i = 0, j = 0;
    ll ans = 0;
    while (i < v1.size() && j < v2.size()) {
        while (i < v1.size() && j < v2.size()) {
            if (v1[i] == v2[j]) break;
            if (v1[i] < v2[j]) i++;
            else j++;
        }

        int ii = i, jj = j;
        while (i < v1.size()) {
            if (v1[i] != v1[ii]) break;
            i++;
        }

        while (j < v2.size()) {
            if (v2[j] != v2[jj]) break;
            j++;
        }

        ll tmp = i - ii;
        tmp *= j - jj;
        ans += tmp;
    }
    cout << ans << endl;
    return 0;
}
0