結果

問題 No.1330 Multiply or Divide
ユーザー StrorkisStrorkis
提出日時 2021-01-09 11:40:09
言語 Rust
(1.77.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 3,710 bytes
コンパイル時間 15,167 ms
コンパイル使用メモリ 379,232 KB
実行使用メモリ 6,656 KB
最終ジャッジ日時 2024-04-28 19:02:20
合計ジャッジ時間 17,277 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 12 ms
6,528 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 9 ms
5,376 KB
testcase_07 AC 46 ms
6,528 KB
testcase_08 AC 48 ms
6,528 KB
testcase_09 AC 50 ms
6,528 KB
testcase_10 AC 48 ms
6,528 KB
testcase_11 AC 46 ms
6,528 KB
testcase_12 AC 48 ms
6,528 KB
testcase_13 AC 0 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 8 ms
5,376 KB
testcase_19 AC 9 ms
5,376 KB
testcase_20 AC 9 ms
5,376 KB
testcase_21 AC 8 ms
5,376 KB
testcase_22 AC 8 ms
5,376 KB
testcase_23 AC 12 ms
6,656 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 1 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 7 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 6 ms
5,376 KB
testcase_37 AC 5 ms
5,376 KB
testcase_38 AC 3 ms
5,376 KB
testcase_39 AC 3 ms
5,376 KB
testcase_40 AC 3 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 5 ms
5,376 KB
testcase_43 AC 6 ms
5,376 KB
testcase_44 AC 10 ms
6,528 KB
testcase_45 AC 11 ms
6,528 KB
権限があれば一括ダウンロードができます

ソースコード

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 reader = std::io::BufReader::new(stdin.lock());
    let mut scanner = Scanner::new(reader);
    let mut writer = std::io::BufWriter::new(stdout.lock());
    macro_rules! scan {
        ($($t:tt)*) => (read!(scanner, $($t)*));
    }
    macro_rules! println {
        ($($arg:tt)*) => (writeln!(writer, $($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: usize = 1;
        while a % p == 0 {
            a /= p;
            c += 1;
        }
        if a == 1 { continue; }
        b.push((c, a));
    }
    b.sort_by_key(|key| (key.0, std::cmp::Reverse(key.1)));

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

    let mut ans = std::usize::MAX;
    let mut map = std::collections::BTreeMap::new();
    map.insert(0, 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