結果

問題 No.2847 Birthday Attack
ユーザー koba-e964koba-e964
提出日時 2024-08-25 13:03:25
言語 Rust
(1.77.0)
結果
AC  
実行時間 373 ms / 3,000 ms
コード長 1,855 bytes
コンパイル時間 12,463 ms
コンパイル使用メモリ 402,060 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-08-25 13:03:49
合計ジャッジ時間 17,081 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 163 ms
6,816 KB
testcase_01 AC 159 ms
6,944 KB
testcase_02 AC 156 ms
6,940 KB
testcase_03 AC 168 ms
6,940 KB
testcase_04 AC 167 ms
6,944 KB
testcase_05 AC 373 ms
6,944 KB
testcase_06 AC 258 ms
6,940 KB
testcase_07 AC 336 ms
6,944 KB
testcase_08 AC 203 ms
6,944 KB
testcase_09 AC 265 ms
6,940 KB
testcase_10 AC 226 ms
6,944 KB
testcase_11 AC 190 ms
6,940 KB
testcase_12 AC 337 ms
6,940 KB
testcase_13 AC 343 ms
6,944 KB
testcase_14 AC 301 ms
6,940 KB
testcase_15 AC 250 ms
6,940 KB
testcase_16 AC 252 ms
6,940 KB
testcase_17 AC 228 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::Read;

fn get_word() -> String {
    let stdin = std::io::stdin();
    let mut stdin=stdin.lock();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

fn gcd(a: i64, b: i64) -> i64 {
    if b == 0 {
        a
    } else {
        gcd(b, a % b)
    }
}

fn calc(x: i64, y: i64, a: i64, b: i64) -> i64 {
    // eprintln!("{} {} ", a, b);
    (x - a).max(0) * (y - b).max(0)
}

// https://yukicoder.me/problems/no/2847 (3.5)
fn main() {
    let x: i64 = get();
    let y: i64 = get();
    let m: i64 = get();
    let mut tot = 0;
    for a in 1..=x / 2 {
        tot += calc(x, y, 2 * a, 0);
        tot %= m;
    }
    for a in 1..=y / 2 {
        tot += calc(x, y, 0, 2 * a);
        tot %= m;
    }
    for a in 1..2001 {
        for b in 1..2001 {
            if (a + b) % 2 == 0 {
                continue;
            }
            if gcd(a, b) != 1 {
                continue;
            }
            let c = a * a - b * b;
            let d = 2 * a * b;
            let (c, d) = if c < 0 {
                (d, -c)
            } else {
                (c, d)
            };
            for s in 1..=(x / 2 / c).min(y / 2 / d) {
                tot += calc(x, y, c * 2 * s, d * 2 * s) * 2;
                tot %= m;
            }
        }
    }
    println!("{}", tot * 2 % m);
}
0