結果

問題 No.1330 Multiply or Divide
ユーザー o2c
提出日時 2021-01-15 15:27:29
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 3,314 bytes
コンパイル時間 12,906 ms
コンパイル使用メモリ 377,664 KB
実行使用メモリ 7,680 KB
最終ジャッジ日時 2024-11-26 02:13:14
合計ジャッジ時間 13,078 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 38 WA * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::*;

#[allow(dead_code)]
mod scanner {
    use std;
    use std::io::Read;
    use std::str::FromStr;
    use std::str::SplitWhitespace;
    pub struct Scanner<'a> {
        it: SplitWhitespace<'a>,
    }
    impl<'a> Scanner<'a> {
        pub fn new(s: &'a String) -> Scanner<'a> {
            Scanner {
                it: s.split_whitespace(),
            }
        }
        pub fn next<T: FromStr>(&mut self) -> T {
            match self.it.next().unwrap().parse::<T>() {
                Ok(v) => v,
                _ => panic!("Scanner error"),
            }
        }
        pub fn next_chars(&mut self) -> Vec<char> { self.next::<String>().chars().collect() }
        pub fn next_vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
            (0..len).map(|_| self.next()).collect()
        }
    }
    pub fn read_string() -> String {
        let mut s = String::new();
        std::io::stdin().read_to_string(&mut s).unwrap();
        s
    }
}

trait Monoid {
    fn id() -> Self;
    fn f(a: Self, b: Self) -> Self;
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
enum MinInt {
    Val(i64),
    Maximal,
}
#[allow(dead_code)]
impl MinInt {
    fn unwrap(self) -> i64 {
        if let Self::Val(x) = self {
            x
        } else {
            panic!();
        }
    }
    fn map<F>(self, f: F) -> Self
    where
        F: Fn(i64) -> i64,
    {
        if let Self::Val(x) = self {
            Self::Val(f(x))
        } else {
            Self::Maximal
        }
    }
    fn map2<F>(self, b: Self, f: F) -> Self
    where
        F: Fn(i64, i64) -> i64,
    {
        match (self, b) {
            (Self::Val(x), Self::Val(y)) => Self::Val(f(x, y)),
            _ => Self::Maximal,
        }
    }
    fn get_or(self, default: i64) -> i64 {
        match self {
            Self::Val(x) => x,
            _ => default,
        }
    }
}
impl std::ops::Mul for MinInt {
    type Output = Self;
    fn mul(self, other: Self) -> Self {
        if self < other {
            self
        } else {
            other
        }
    }
}
impl Monoid for MinInt {
    fn id() -> Self { MinInt::Maximal }
    fn f(a: Self, b: Self) -> Self { a * b }
}
use MinInt::*;

fn main() {
    let sc = scanner::read_string();
    let mut sc = scanner::Scanner::new(&sc);

    let n: usize = sc.next();
    let m: usize = sc.next();
    let p: usize = sc.next();
    let mut a = sc.next_vec::<usize>(n);

    let mut b = vec![];
    let mut c = vec![];
    for i in 0..n {
        if a[i] % p == 0 {
            while a[i] % p == 0 {
                a[i] /= p;
            }
            b.push(a[i]);
        } else {
            c.push(a[i]);
        }
    }

    b.sort();
    b.reverse();
    c.sort();
    c.reverse();

    let mut ans = Maximal;
    if !b.is_empty() && b[0] != 1 {
        let mut x = 1;
        let mut y = 0;
        while x <= m {
            if !c.is_empty() && x * c[0] > m {
                y += 1;
                break;
            }
            x *= b[0];
            y += 2;
        }
        ans = ans * Val(y);
    }

    if !c.is_empty() && c[0] != 1 {
        let mut x = 1;
        let mut y = 0;
        while x <= m {
            x *= c[0];
            y += 1;
        }
        ans = ans * Val(y);
    }

    println!("{}", ans.get_or(-1));
}
0