結果

問題 No.1330 Multiply or Divide
ユーザー StrorkisStrorkis
提出日時 2021-01-09 11:05:56
言語 Rust
(1.77.0)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 3,628 bytes
コンパイル時間 2,333 ms
コンパイル使用メモリ 173,708 KB
実行使用メモリ 9,484 KB
最終ジャッジ日時 2023-08-11 01:19:20
合計ジャッジ時間 5,710 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 15 ms
9,208 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 9 ms
4,376 KB
testcase_07 AC 56 ms
9,228 KB
testcase_08 AC 40 ms
9,384 KB
testcase_09 AC 42 ms
9,436 KB
testcase_10 AC 38 ms
9,464 KB
testcase_11 AC 38 ms
9,484 KB
testcase_12 AC 39 ms
9,448 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,376 KB
testcase_18 AC 10 ms
4,380 KB
testcase_19 AC 10 ms
4,376 KB
testcase_20 AC 10 ms
4,380 KB
testcase_21 AC 10 ms
4,380 KB
testcase_22 AC 10 ms
4,380 KB
testcase_23 AC 16 ms
9,184 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,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 8 ms
4,376 KB
testcase_35 AC 3 ms
4,376 KB
testcase_36 AC 7 ms
4,376 KB
testcase_37 AC 6 ms
4,380 KB
testcase_38 AC 5 ms
4,380 KB
testcase_39 AC 3 ms
4,380 KB
testcase_40 AC 3 ms
4,384 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 6 ms
4,376 KB
testcase_43 AC 6 ms
4,376 KB
testcase_44 AC 30 ms
9,200 KB
testcase_45 AC 40 ms
9,232 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused return value of `std::cmp::Ord::max` that must be used
   --> Main.rs:100:9
    |
100 |         max_a.max(a);
    |         ^^^^^^^^^^^^
    |
    = note: `#[warn(unused_must_use)]` on by default

warning: 1 warning emitted

ソースコード

diff #

struct Scanner<R> { r: R, buf: Vec<u8> }

impl<R: std::io::BufRead> Scanner<R> {
    fn new(r: R) -> Scanner<R> {
        Scanner { r, buf: Vec::with_capacity(8000) }
    }

    fn next(&mut self) -> &str {
        self.buf.clear();
        loop {
            let (done, used) = {
                let available = self.r.fill_buf().unwrap();
                match available.iter().position(|b| !b.is_ascii_whitespace()) {
                    Some(i) => (true, i),
                    None => (false, available.len()),
                }
            };
            self.r.consume(used);
            if done || used == 0 { break; }
        }
        loop {
            let (done, used) = {
                let available = self.r.fill_buf().unwrap();
                match available.iter().position(|b| b.is_ascii_whitespace()) {
                    Some(i) => {
                        self.buf.extend_from_slice(&available[..i]);
                        (true, i)
                    }
                    None => {
                        self.buf.extend_from_slice(&available);
                        (false, available.len())
                    }
                }
            };
            self.r.consume(used);
            if done || used == 0 {
                unsafe {
                    return std::str::from_utf8_unchecked(&self.buf);
                }
            }
        }
    }
}

macro_rules! parse {
    ($s:expr, Chars) => ($s.chars().collect::<Vec<_>>());
    ($s:expr, Bytes) => ($s.bytes().collect::<Vec<_>>());
    ($s:expr, Usize1) => (parse!($s, usize) - 1);
    ($s:expr, $t:ty) => ($s.parse::<$t>().unwrap());
}

macro_rules! read {
    ($sc:expr, [$t:tt; $n:expr]) => (
        (0..$n).map(|_| read!($sc, $t)).collect::<Vec<_>>()
    );
    ($sc:expr, ($($t:tt),*)) => (($(read!($sc, $t)),*));
    ($sc:expr, $t:ident) => (parse!($sc.next(), $t));
    ($sc:expr, $($t:tt),*) => (($(read!($sc, $t)),*));
}

fn main() {
    use std::io::Write;
    let (stdin, stdout) = (std::io::stdin(), std::io::stdout());
    let mut sc = Scanner::new(stdin.lock());
    let mut wr = std::io::BufWriter::new(stdout.lock());
    macro_rules! scan {
        ($($t:tt)*) => (read!(sc, $($t)*));
    }
    macro_rules! println {
        ($($arg:tt)*) => (writeln!(wr, $($arg)*).ok());
    }

    let (n, m, p) = scan!(usize, usize, usize);
    let a = scan!([usize; n]);

    let max_a = a.iter().max().unwrap();
    if m < *max_a {
        println!("1");
        return;
    }
    let m = m / max_a;

    let mut b = Vec::with_capacity(n);
    for mut a in a {
        let mut c = 1;
        while a % p == 0 {
            a /= p;
            c += 1;
        }
        if a == 1 { continue; }
        b.push((c, a));
    }
    b.sort_by_key(|key| (std::cmp::Reverse(key.0), key.1));

    let mut v = Vec::new();
    let max_a = 0;
    for (c, a) in b {
        if a <= max_a { continue; }
        v.push((c, a));
        max_a.max(a);
    }

    let mut ans = std::usize::MAX;
    let mut map = std::collections::BTreeMap::new();
    map.insert(0usize, 1);
    for cost in 0.. {
        if let Some(&x) = map.get(&cost) {
            for (c, a) in &v {
                let next_cost = cost + c;
                let next_x = x * a;
                if next_x > m {
                    ans = ans.min(next_cost + 1);
                } else {
                    let value = map.entry(next_cost).or_default();
                    *value = std::cmp::max(*value, next_x);
                }
            }
            map.remove(&cost);
            if map.is_empty() { break; }
        }
    }
    println!("{}", ans as isize);
}
0