結果

問題 No.1330 Multiply or Divide
ユーザー phsplsphspls
提出日時 2023-01-22 22:29:13
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 1,914 bytes
コンパイル時間 5,247 ms
コンパイル使用メモリ 153,208 KB
実行使用メモリ 15,336 KB
最終ジャッジ日時 2023-09-07 06:10:43
合計ジャッジ時間 9,818 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 8 ms
4,376 KB
testcase_07 AC 61 ms
8,176 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 12 ms
5,656 KB
testcase_19 AC 12 ms
5,656 KB
testcase_20 AC 13 ms
5,656 KB
testcase_21 AC 12 ms
5,624 KB
testcase_22 AC 12 ms
5,548 KB
testcase_23 AC 23 ms
8,996 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 WA -
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,384 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 10 ms
4,620 KB
testcase_35 AC 3 ms
4,376 KB
testcase_36 AC 9 ms
4,460 KB
testcase_37 AC 8 ms
4,380 KB
testcase_38 AC 6 ms
4,380 KB
testcase_39 AC 4 ms
4,380 KB
testcase_40 AC 4 ms
4,376 KB
testcase_41 AC 3 ms
4,380 KB
testcase_42 AC 7 ms
4,380 KB
testcase_43 AC 8 ms
4,380 KB
testcase_44 AC 17 ms
7,232 KB
testcase_45 AC 17 ms
7,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::HashMap;

const INF: usize = 1usize << 60;

fn main() {
    let mut nmp = String::new();
    std::io::stdin().read_line(&mut nmp).ok();
    let nmp: Vec<usize> = nmp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nmp[0];
    let m = nmp[1];
    let p = nmp[2];
    let mut a = String::new();
    std::io::stdin().read_line(&mut a).ok();
    let a: Vec<usize> = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();

    let maxval = *a.iter().max().unwrap();
    if maxval > m {
        println!("1");
        return;
    }
    let a = (0..n).map(|i| {
            let mut target = a[i];
            let mut cnt = 1usize;
            while target % p == 0 {
                target /= p;
                cnt += 1;
            }
            (target, cnt)
        })
        .filter(|&(v, _)| v > 1)
        .collect::<Vec<_>>();
    if a.is_empty() {
        println!("-1");
        return;
    }
    let mut amap = HashMap::new();
    for &(key, cnt) in a.iter() {
        if !amap.contains_key(&key) || amap.get(&key).unwrap() > &cnt {
            amap.insert(key, cnt);
        }
    }
    let mut dp = HashMap::new();
    dp.insert(maxval, 1usize);
    for (&key, &cnt) in amap.iter() {
        let mut next_dp = dp.clone();
        for (&val, &total) in dp.iter() {
            if val == INF { continue; }
            for i in 1.. {
                let key_val = key.pow(i);
                let key_val = if key_val > m || key_val * val > m { INF } else { key_val * val };
                let total_cnt = total + cnt * i as usize;
                if !next_dp.contains_key(&key_val) || next_dp.get(&key_val).unwrap() < &total_cnt {
                    next_dp.insert(key_val, total_cnt);
                }
                if key_val == INF { break; }
            }
        }
        dp = next_dp;
    }
    println!("{}", dp.get(&INF).unwrap());
}
0