結果

問題 No.1330 Multiply or Divide
ユーザー phspls
提出日時 2023-01-16 04:14:28
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 1,745 bytes
コンパイル時間 15,417 ms
コンパイル使用メモリ 379,432 KB
実行使用メモリ 15,232 KB
最終ジャッジ日時 2024-12-30 01:29:38
合計ジャッジ時間 26,060 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 36 WA * 8 TLE * 2
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `n`
  --> src/main.rs:10:9
   |
10 |     let n = nmp[0];
   |         ^ help: if this is intentional, prefix it with an underscore: `_n`
   |
   = note: `#[warn(unused_variables)]` on by default

ソースコード

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 val2cnt = a.into_iter().map(|v| {
            let mut cnt = 1usize;
            let mut val = v;
            while val % p == 0 {
                val /= p;
                cnt += 1;
            }
            (val, cnt)
        })
        .collect::<Vec<_>>();
    let val2cnt = val2cnt.into_iter().filter(|&(v, _)| v > 1).collect::<Vec<_>>();
    if val2cnt.is_empty() {
        println!("-1");
        return;
    }
    let limit = m / maxval;
    let mut dp = HashMap::new();
    dp.insert(1usize, 0usize);
    for &(val, cnt) in val2cnt.iter() {
        let mut next_dp = HashMap::new();
        for (&cval, &ccnt) in dp.iter() {
            for use_cnt in 0.. {
                let nkey = cval * val.pow(use_cnt as u32);
                let nkey = if nkey > limit { INF } else { nkey };
                let ncnt = cnt + ccnt * use_cnt;
                let count = *next_dp.get(&nkey).unwrap_or(&INF);
                if count > ncnt {
                    next_dp.insert(nkey, ncnt);
                }
                if nkey == INF { break; }
            }
        }
        dp = next_dp;
    }
    println!("{}", dp.get(&INF).unwrap() + 1);
}
0