結果

問題 No.2847 Birthday Attack
ユーザー 👑 AngrySadEightAngrySadEight
提出日時 2024-07-05 01:29:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 808 ms / 3,000 ms
コード長 1,909 bytes
コンパイル時間 869 ms
コンパイル使用メモリ 84,652 KB
実行使用メモリ 54,296 KB
最終ジャッジ日時 2024-07-05 01:29:54
合計ジャッジ時間 11,439 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 344 ms
53,232 KB
testcase_01 AC 343 ms
52,872 KB
testcase_02 AC 362 ms
52,740 KB
testcase_03 AC 359 ms
53,032 KB
testcase_04 AC 344 ms
53,208 KB
testcase_05 AC 808 ms
53,268 KB
testcase_06 AC 554 ms
53,356 KB
testcase_07 AC 733 ms
53,656 KB
testcase_08 AC 434 ms
53,112 KB
testcase_09 AC 596 ms
52,740 KB
testcase_10 AC 476 ms
53,204 KB
testcase_11 AC 426 ms
53,072 KB
testcase_12 AC 694 ms
52,912 KB
testcase_13 AC 738 ms
53,940 KB
testcase_14 AC 607 ms
52,872 KB
testcase_15 AC 515 ms
52,932 KB
testcase_16 AC 542 ms
54,296 KB
testcase_17 AC 477 ms
53,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

ll my_gcd(ll a, ll b) {
    ll ret = 0;
    if (a < b) {
        swap(a, b);
    }
    if (b == 0) {
        ret = a;
    } else {
        ret = my_gcd(b, a % b);
    }
    return ret;
}

int main() {
    ll X, Y, M;
    cin >> X >> Y >> M;
    ll ans = 0;
    for (ll i = 1; i <= X; i++) {
        ans += 2 * max(X - 2 * i, 0LL) * Y;
        ans %= M;
    }
    for (ll i = 1; i <= Y; i++) {
        ans += 2 * max(Y - 2 * i, 0LL) * X;
        ans %= M;
    }
    // ピタゴラス数を列挙する
    vector<pair<ll, pll>> p(0);
    for (ll m = 1; m * m <= 4000000; m++) {
        for (ll n = m + 1; n * n <= 4000000; n++) {
            if (m * m + n * n > 8000000) {
                continue;
            }
            if (m % 2 == n % 2) {
                continue;
            }
            ll g = my_gcd(n, m);
            if (g != 1) {
                continue;
            }
            ll min_p = 2 * m * n;
            ll max_p = n * n - m * m;
            pll pp1 = pll(min_p, max_p);
            pll pp2 = pll(max_p, min_p);
            pair<ll, pll> ppp1 = pair<ll, pll>(m * m + n * n, pp1);
            pair<ll, pll> ppp2 = pair<ll, pll>(m * m + n * n, pp2);
            p.push_back(ppp1);
            p.push_back(ppp2);
        }
    }
    sort(p.begin(), p.end());
    // 列挙されたピタゴラス数の全てに対して、斜めのぶんの数え上げを行う
    ll len_p = p.size();
    for (ll i = 0; i < len_p; i++) {
        ll x1 = p[i].second.first;
        ll y1 = p[i].second.second;
        for (ll j = 1; j <= min(X / y1, Y / x1); j++) {
            ll x = x1 * j;
            ll y = y1 * j;
            ans += 4 * max(Y - 2 * x, 0LL) * max(X - 2 * y, 0LL);
            ans %= M;
        }
    }
    cout << ans << endl;
}
0