結果

問題 No.1330 Multiply or Divide
ユーザー fukafukatanifukafukatani
提出日時 2021-01-09 19:55:01
言語 Rust
(1.77.0)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 1,767 bytes
コンパイル時間 14,776 ms
コンパイル使用メモリ 375,952 KB
実行使用メモリ 9,636 KB
最終ジャッジ日時 2024-04-29 08:39:43
合計ジャッジ時間 16,331 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 20 ms
9,496 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 12 ms
7,576 KB
testcase_07 AC 217 ms
9,368 KB
testcase_08 AC 26 ms
9,440 KB
testcase_09 AC 29 ms
9,504 KB
testcase_10 AC 27 ms
9,500 KB
testcase_11 AC 24 ms
9,496 KB
testcase_12 AC 26 ms
9,500 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 24 ms
9,500 KB
testcase_19 AC 23 ms
9,504 KB
testcase_20 AC 23 ms
9,628 KB
testcase_21 AC 23 ms
9,504 KB
testcase_22 AC 22 ms
9,500 KB
testcase_23 AC 23 ms
9,636 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 0 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 18 ms
8,332 KB
testcase_35 AC 5 ms
5,376 KB
testcase_36 AC 15 ms
7,812 KB
testcase_37 AC 12 ms
5,656 KB
testcase_38 AC 8 ms
5,376 KB
testcase_39 AC 6 ms
5,376 KB
testcase_40 AC 7 ms
5,376 KB
testcase_41 AC 5 ms
5,376 KB
testcase_42 AC 12 ms
5,376 KB
testcase_43 AC 13 ms
5,728 KB
testcase_44 AC 29 ms
7,320 KB
testcase_45 AC 40 ms
7,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<i64>();
    let (n, m, p) = (v[0] as usize, v[1], v[2]);
    let a = read_vec::<i64>();
    let amax = *a.iter().max().unwrap();
    let mut aa = vec![];
    let mut counts = vec![];
    for mut num in a {
        let mut count = 1usize;
        while num % p == 0 {
            num /= p;
            count += 1usize;
        }
        aa.push(num);
        counts.push(count);
    }
    // debug!(aa);
    let aa_max = *aa.iter().max().unwrap();
    if aa_max == 1 && amax <= m {
        println!("-1");
        return;
    }
    if m == 1 {
        println!("1");
        return;
    }

    let mut dp = vec![0i64; 1000];
    dp[0] = 1;
    for i in 0..dp.len() - 1 {
        if dp[i] > m {
            break;
        }
        for j in 0..n {
            if i + counts[j] >= dp.len() {
                continue;
            }
            dp[i + counts[j]] = max(dp[i + counts[j]], dp[i] * aa[j]);
        }
    }
    for i in 0..dp.len() {
        if dp[i] * amax > m {
            println!("{}", i + 1);
            return;
        }
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
0