結果

問題 No.1330 Multiply or Divide
ユーザー tonyu0
提出日時 2021-01-09 10:41:23
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 1,401 bytes
コンパイル時間 13,687 ms
コンパイル使用メモリ 402,752 KB
実行使用メモリ 11,840 KB
最終ジャッジ日時 2025-06-20 01:23:54
合計ジャッジ時間 16,605 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 51
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::*;
const INF: usize = 1 << 30;
const MAX: usize = 1000;

fn main() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.trim().split_whitespace();
    let n: usize = itr.next().unwrap().parse().unwrap();
    let m: usize = itr.next().unwrap().parse().unwrap();
    let p: usize = itr.next().unwrap().parse().unwrap();
    let a: Vec<usize> = (0..n)
        .map(|_| itr.next().unwrap().parse().unwrap())
        .collect();

    let mut max = 0;
    let mut b = Vec::new();
    for &e in a.iter() {
        max = std::cmp::max(max, e);
        let mut cnt = 1;
        let mut x = e;
        while x % p == 0 {
            x /= p;
            cnt += 1;
        }
        if x == 1 {
            continue;
        }
        b.push((cnt, x));
    }
    b.sort();

    let mut dp = vec![0; MAX];
    dp[0] = 1;
    for &(cnt, x) in b.iter() {
        for i in 0..MAX {
            if dp[i] > m || cnt + i >= MAX {
                break;
            }
            dp[i + cnt] = std::cmp::max(dp[i + cnt], dp[i] * x);
        }
    }

    let mut ans = INF;
    for i in 0..MAX {
        if dp[i] > m {
            ans = std::cmp::min(ans, i);
        } else if dp[i] * max > m {
            ans = std::cmp::min(ans, i + 1);
        }
    }
    if ans == INF {
        println!("-1");
    } else {
        println!("{}", ans);
    }
}
0