結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー akakimidoriakakimidori
提出日時 2020-02-14 21:39:50
言語 Rust
(1.77.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 2,094 bytes
コンパイル時間 3,451 ms
コンパイル使用メモリ 150,012 KB
実行使用メモリ 9,252 KB
最終ジャッジ日時 2023-08-10 01:45:18
合計ジャッジ時間 3,196 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 14 ms
4,860 KB
testcase_11 AC 8 ms
4,376 KB
testcase_12 AC 58 ms
6,012 KB
testcase_13 AC 11 ms
4,376 KB
testcase_14 AC 20 ms
4,436 KB
testcase_15 AC 12 ms
4,380 KB
testcase_16 AC 18 ms
5,320 KB
testcase_17 AC 9 ms
4,380 KB
testcase_18 AC 59 ms
6,016 KB
testcase_19 AC 25 ms
4,376 KB
testcase_20 AC 35 ms
9,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Read;

fn run_length_encoding<T: Clone + Eq>(a: &[T]) -> Vec<(T, usize)> {
    if a.len() == 0 {
        return vec![];
    }
    let mut x = a[0].clone();
    let mut cnt = 1;
    let mut ans = vec![];
    for y in &a[1..] {
        if x == *y {
            cnt += 1;
        } else {
            ans.push((x, cnt));
            x = y.clone();
            cnt = 1;
        }
    }
    ans.push((x, cnt));
    ans
}

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

fn run() {
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let h: usize = it.next().unwrap().parse().unwrap();
    let w: usize = it.next().unwrap().parse().unwrap();
    let k: u64 = it.next().unwrap().parse().unwrap();
    let op = it.next().unwrap().chars().next().unwrap();
    let mut b: Vec<u64> = (0..w).map(|_| it.next().unwrap().parse().unwrap()).collect();
    let mut a: Vec<u64> = (0..h).map(|_| it.next().unwrap().parse().unwrap()).collect();
    let ans = if op == '+' {
        for a in a.iter_mut().chain(b.iter_mut()) {
            *a %= k;
        }
        a.sort();
        let a = run_length_encoding(&a);
        b.sort();
        let b = run_length_encoding(&b);
        let mut ans = 0;
        for (a, x) in a.iter() {
            if let Ok(k) = b.binary_search_by(|p| p.0.cmp(&(k - a))) {
                ans += x * b[k].1;
            }
        }
        if a[0].0 == 0 && b[0].0 == 0 {
            ans += a[0].1 * b[0].1;
        }
        ans
    } else {
        for a in a.iter_mut().chain(b.iter_mut()) {
            *a = gcd(*a, k);
        }
        a.sort();
        let a = run_length_encoding(&a);
        b.sort();
        let b = run_length_encoding(&b);
        let mut ans = 0;
        for (a, x) in a.iter() {
            for (b, y) in b.iter() {
                if *a * *b % k == 0 {
                    ans += x * y;
                }
            }
        }
        ans
    };
    println!("{}", ans);
}

fn main() {
    run();
}
0