結果

問題 No.2847 Birthday Attack
ユーザー tottoripaper
提出日時 2024-08-24 20:02:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 520 ms / 3,000 ms
コード長 1,176 bytes
コンパイル時間 2,217 ms
コンパイル使用メモリ 199,144 KB
最終ジャッジ日時 2025-02-24 00:42:07
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using ll = std::int64_t;
using P = std::tuple<ll, ll>;

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    ll X, Y, M;
    std::cin >> X >> Y >> M;

    ll res = 0;
    for(ll x=1;x<=X;x++){
        ll r = std::min(x - 1, X - x);
        res += 2ll * r * Y % M;
        if(res >= M){res -= M;}
    }

    for(ll y=1;y<=Y;y++){
        ll r = std::min(y - 1, Y - y);
        res += 2ll * r * X % M;
        if(res >= M){res -= M;}
    }

    std::vector<P> v;
    ll Z = std::max(X, Y);
    for(ll a=1;a<=(Z-1)/2;a++){
        for(ll b=1+a%2;b<=(Z-1)/2/a;b+=2){
            if(a <= b){break;}
            if(std::gcd(a, b) > 1){continue;}

            ll c = a * a - b * b, d = 2ll * a * b;
            if(c < X && d < Y){
                v.emplace_back(c, d);
            }

            if(c < Y && d < X){
                v.emplace_back(d, c);
            }
        }
    }

    for(auto [c, d] : v){
        for(int k=2;c*k<X&&d*k<Y;k+=2){
            ll t = 4ll * (X - c * k) * (Y - d * k) % M;
            res += t;
            if(res >= M){res -= M;}         
        }
    }

    std::cout << res << std::endl;
}
0