結果

問題 No.1330 Multiply or Divide
ユーザー phspls
提出日時 2023-02-05 19:49:27
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 554 ms / 2,000 ms
コード長 1,503 bytes
コンパイル時間 13,743 ms
コンパイル使用メモリ 386,500 KB
実行使用メモリ 10,888 KB
最終ジャッジ日時 2025-06-20 01:41:48
合計ジャッジ時間 17,912 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 51
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `n`
 --> src/main.rs:7:9
  |
7 |     let n = nmp[0];
  |         ^ help: if this is intentional, prefix it with an underscore: `_n`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `cnt`
  --> src/main.rs:28:25
   |
28 |         .filter(|&(val, cnt)| val > 1)
   |                         ^^^ help: if this is intentional, prefix it with an underscore: `_cnt`

warning: variable `INF` should have a snake case name
  --> src/main.rs:36:9
   |
36 |     let INF = m + 1;
   |         ^^^ help: convert the identifier to snake case: `inf`
   |
   = note: `#[warn(non_snake_case)]` on by default

ソースコード

diff #

use std::collections::BTreeSet;

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 = a.into_iter().map(|v| {
            let mut ret = v;
            let mut cnt = 1usize;
            while ret % p == 0 {
                ret /= p;
                cnt += 1;
            }
            (ret, cnt)
        })
        .filter(|&(val, cnt)| val > 1)
        .collect::<BTreeSet<_>>()
        .into_iter()
        .collect::<Vec<_>>();
    if a.is_empty() {
        println!("-1");
        return;
    }
    let INF = m + 1;
    let mut dp = vec![0usize; 900];
    dp[1] = maxval;
    for i in 0..a.len() {
        let mut next_dp = vec![0usize; 900];
        let (val, cnt) = a[i];
        for j in 0..900 {
            next_dp[j] = next_dp[j].max(dp[j]).min(INF);
            if j + cnt < 900 {
                next_dp[j+cnt] = next_dp[j+cnt].max(next_dp[j] * val).min(INF);
            }
        }
        dp = next_dp;
    }
    println!("{}", (0..900).filter(|&v| dp[v] == INF).nth(0).map(|v| v as isize).unwrap_or(-1));
}
0