結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー phsplsphspls
提出日時 2023-02-18 05:01:33
言語 Rust
(1.77.0)
結果
AC  
実行時間 147 ms / 2,000 ms
コード長 1,774 bytes
コンパイル時間 1,635 ms
コンパイル使用メモリ 159,524 KB
実行使用メモリ 8,648 KB
最終ジャッジ日時 2023-09-27 00:58:47
合計ジャッジ時間 4,475 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 15 ms
5,196 KB
testcase_11 AC 16 ms
4,756 KB
testcase_12 AC 147 ms
5,452 KB
testcase_13 AC 11 ms
4,376 KB
testcase_14 AC 20 ms
4,380 KB
testcase_15 AC 13 ms
4,376 KB
testcase_16 AC 19 ms
5,348 KB
testcase_17 AC 9 ms
4,376 KB
testcase_18 AC 140 ms
5,416 KB
testcase_19 AC 38 ms
4,376 KB
testcase_20 AC 28 ms
8,648 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `m`
  --> Main.rs:13:9
   |
13 |     let m = nmk[1];
   |         ^ help: if this is intentional, prefix it with an underscore: `_m`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: 1 warning emitted

ソースコード

diff #

use std::collections::HashMap;

fn gcd(a: usize, b: usize) -> usize {
    if b == 0 { return a; }
    gcd(b, a%b)
}

fn main() {
    let mut nmk = String::new();
    std::io::stdin().read_line(&mut nmk).ok();
    let nmk: Vec<usize> = nmk.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nmk[0];
    let m = nmk[1];
    let k = nmk[2];
    let mut temp = String::new();
    std::io::stdin().read_line(&mut temp).ok();
    let temp: Vec<&str> = temp.trim().split_whitespace().collect();
    let op = temp[0].chars().nth(0).unwrap();
    let b = temp.into_iter().skip(1).map(|v| v.parse::<usize>().unwrap()).collect::<Vec<_>>();
    let a = (0..n).map(|_| {
            let mut temp = String::new();
            std::io::stdin().read_line(&mut temp).ok();
            let temp: usize = temp.trim().parse().unwrap();
            temp
        })
        .collect::<Vec<_>>();

    if op == '+' {
        let mut cntmap = HashMap::new();
        for &v in b.iter() {
            *cntmap.entry(v%k).or_insert(0usize) += 1;
        }
        let mut result = 0usize;
        for &v in a.iter() {
            if let Some(&x) = cntmap.get(&((k-v%k)%k)) {
                result += x;
            }
        }
        println!("{}", result);
        return;
    }
    let mut bmap = HashMap::new();
    for &v in b.iter() {
        *bmap.entry(gcd(k, v)).or_insert(0usize) += 1;
    }
    let mut amap = HashMap::new();
    for &v in a.iter() {
        *amap.entry(gcd(k, v)).or_insert(0usize) += 1;
    }
    let mut result = 0usize;
    for (&aval, &acnt) in amap.iter() {
        for (&bval, &bcnt) in bmap.iter() {
            if k == gcd(aval*bval, k) {
                result += acnt * bcnt;
            }
        }
    }
    println!("{}", result);
}
0