結果

問題 No.1330 Multiply or Divide
ユーザー ikdikd
提出日時 2021-01-08 22:31:00
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 2,793 bytes
コンパイル時間 16,303 ms
コンパイル使用メモリ 380,752 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-28 03:56:29
合計ジャッジ時間 16,017 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 14 ms
5,248 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 10 ms
5,376 KB
testcase_07 WA -
testcase_08 AC 19 ms
5,888 KB
testcase_09 AC 21 ms
5,888 KB
testcase_10 AC 18 ms
5,888 KB
testcase_11 AC 17 ms
5,888 KB
testcase_12 AC 19 ms
5,888 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 1 ms
5,376 KB
testcase_17 WA -
testcase_18 AC 14 ms
6,016 KB
testcase_19 AC 14 ms
5,888 KB
testcase_20 AC 14 ms
5,888 KB
testcase_21 AC 14 ms
5,888 KB
testcase_22 AC 14 ms
5,760 KB
testcase_23 AC 17 ms
6,400 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 11 ms
5,376 KB
testcase_35 AC 4 ms
5,376 KB
testcase_36 AC 10 ms
5,376 KB
testcase_37 AC 9 ms
5,376 KB
testcase_38 AC 6 ms
5,376 KB
testcase_39 AC 5 ms
5,376 KB
testcase_40 AC 5 ms
5,376 KB
testcase_41 AC 3 ms
5,376 KB
testcase_42 AC 8 ms
5,376 KB
testcase_43 AC 9 ms
5,376 KB
testcase_44 WA -
testcase_45 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let stdin = std::io::stdin();
    let mut rd = ProconReader::new(stdin.lock());

    let n: usize = rd.get();
    let m: u64 = rd.get();
    let p: u64 = rd.get();
    let a: Vec<u64> = rd.get_vec(n);

    let a: Vec<u64> = a.into_iter().filter(|&y| y != 1).collect();
    if a.is_empty() {
        println!("-1");
        return;
    }
    let max = *a.iter().max().unwrap();
    if max > m {
        println!("1");
        return;
    }
    let a: Vec<u64> = a
        .into_iter()
        .filter(|&y| {
            let mut y = y;
            while y % p == 0 {
                y /= p;
            }
            y != 1
        })
        .collect();
    if a.is_empty() {
        println!("-1");
        return;
    }
    let mut ans = std::u32::MAX;
    for y in a {
        let mut x = 1;
        let mut cnt = 0;
        while x <= m {
            if x * max > m {
                cnt += 1;
                break;
            }
            if x % p == 0 {
                x /= p;
            } else {
                x *= y;
            }
            cnt += 1;
        }
        ans = ans.min(cnt);
    }
    assert!(ans < std::u32::MAX);
    println!("{}", ans);
}

pub struct ProconReader<R> {
    r: R,
    line: String,
    i: usize,
}

impl<R: std::io::BufRead> ProconReader<R> {
    pub fn new(reader: R) -> Self {
        Self {
            r: reader,
            line: String::new(),
            i: 0,
        }
    }
    pub fn get<T>(&mut self) -> T
    where
        T: std::str::FromStr,
        <T as std::str::FromStr>::Err: std::fmt::Debug,
    {
        self.skip_blanks();
        assert!(self.i < self.line.len());
        assert_ne!(&self.line[self.i..=self.i], " ");
        let line = &self.line[self.i..];
        let end = line.find(' ').unwrap_or_else(|| line.len());
        let s = &line[..end];
        self.i += end;
        s.parse()
            .unwrap_or_else(|_| panic!("parse error `{}`", self.line))
    }
    fn skip_blanks(&mut self) {
        loop {
            let start = self.line[self.i..].find(|ch| ch != ' ');
            match start {
                Some(j) => {
                    self.i += j;
                    break;
                }
                None => {
                    self.line.clear();
                    self.i = 0;
                    let num_bytes = self.r.read_line(&mut self.line).unwrap();
                    assert!(num_bytes > 0, "reached EOF :(");
                    self.line = self.line.trim_end_matches(&['\r', '\n'][..]).to_string();
                }
            }
        }
    }
    pub fn get_vec<T>(&mut self, n: usize) -> Vec<T>
    where
        T: std::str::FromStr,
        <T as std::str::FromStr>::Err: std::fmt::Debug,
    {
        (0..n).map(|_| self.get()).collect()
    }
}
0