結果

問題 No.1330 Multiply or Divide
ユーザー phsplsphspls
提出日時 2023-02-05 19:49:27
言語 Rust
(1.77.0)
結果
AC  
実行時間 471 ms / 2,000 ms
コード長 1,503 bytes
コンパイル時間 1,960 ms
コンパイル使用メモリ 161,832 KB
実行使用メモリ 10,904 KB
最終ジャッジ日時 2023-09-17 10:29:17
合計ジャッジ時間 6,665 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 438 ms
10,388 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 8 ms
4,380 KB
testcase_07 AC 53 ms
8,188 KB
testcase_08 AC 470 ms
10,904 KB
testcase_09 AC 471 ms
10,836 KB
testcase_10 AC 470 ms
10,836 KB
testcase_11 AC 470 ms
10,840 KB
testcase_12 AC 470 ms
10,888 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 11 ms
5,644 KB
testcase_19 AC 11 ms
5,636 KB
testcase_20 AC 12 ms
5,560 KB
testcase_21 AC 12 ms
5,648 KB
testcase_22 AC 12 ms
5,644 KB
testcase_23 AC 16 ms
8,892 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
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,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 9 ms
4,616 KB
testcase_35 AC 3 ms
4,376 KB
testcase_36 AC 8 ms
4,388 KB
testcase_37 AC 7 ms
4,376 KB
testcase_38 AC 5 ms
4,376 KB
testcase_39 AC 4 ms
4,380 KB
testcase_40 AC 4 ms
4,384 KB
testcase_41 AC 3 ms
4,380 KB
testcase_42 AC 7 ms
4,376 KB
testcase_43 AC 8 ms
4,384 KB
testcase_44 AC 11 ms
7,216 KB
testcase_45 AC 11 ms
7,140 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `n`
 --> 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`
  --> 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
  --> 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

warning: 3 warnings emitted

ソースコード

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