結果

問題 No.1730 GCD on Blackboard in yukicoder
ユーザー StrorkisStrorkis
提出日時 2021-11-05 22:09:49
言語 Rust
(1.77.0)
結果
AC  
実行時間 1,090 ms / 2,000 ms
コード長 2,538 bytes
コンパイル時間 1,391 ms
コンパイル使用メモリ 175,544 KB
実行使用メモリ 11,448 KB
最終ジャッジ日時 2024-04-25 17:25:02
合計ジャッジ時間 10,564 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,090 ms
7,768 KB
testcase_01 AC 4 ms
6,940 KB
testcase_02 AC 5 ms
6,944 KB
testcase_03 AC 5 ms
8,520 KB
testcase_04 AC 4 ms
8,304 KB
testcase_05 AC 4 ms
8,456 KB
testcase_06 AC 5 ms
6,940 KB
testcase_07 AC 6 ms
8,548 KB
testcase_08 AC 4 ms
6,944 KB
testcase_09 AC 6 ms
6,944 KB
testcase_10 AC 5 ms
8,384 KB
testcase_11 AC 5 ms
6,940 KB
testcase_12 AC 4 ms
6,944 KB
testcase_13 AC 619 ms
11,268 KB
testcase_14 AC 629 ms
11,324 KB
testcase_15 AC 614 ms
11,136 KB
testcase_16 AC 611 ms
11,440 KB
testcase_17 AC 624 ms
11,320 KB
testcase_18 AC 303 ms
6,944 KB
testcase_19 AC 855 ms
11,448 KB
testcase_20 AC 196 ms
7,696 KB
testcase_21 AC 191 ms
7,708 KB
testcase_22 AC 931 ms
7,820 KB
testcase_23 AC 34 ms
6,940 KB
testcase_24 AC 34 ms
7,656 KB
testcase_25 AC 546 ms
9,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

pub mod io {
    pub fn scan<R: std::io::BufRead>(r: &mut R) -> Vec<u8> {
        let mut res = Vec::new();
        loop {
            let buf = match r.fill_buf() {
                Ok(buf) => buf,
                Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
                Err(e) => panic!("{}", e), 
            };
            let (done, used, buf) = match buf.iter().position(u8::is_ascii_whitespace) {
                Some(i) => (i > 0 || res.len() > 0, i + 1, &buf[..i]),
                None => (buf.is_empty(), buf.len(), buf),
            };
            res.extend_from_slice(buf);
            r.consume(used);
            if done { return res; }
        }
    }

    #[macro_export]
    macro_rules! scan {
        ($r:expr, [$t:tt; $n:expr]) => ((0..$n).map(|_| scan!($r, $t)).collect::<Vec<_>>());
        ($r:expr, [$t:tt]) => (scan!($r, [$t; scan!($r, usize)]));
        ($r:expr, ($($t:tt),*)) => (($(scan!($r, $t)),*));
        ($r:expr, Usize1) => (scan!($r, usize) - 1);
        ($r:expr, String) => (String::from_utf8(scan!($r, Bytes)).unwrap());
        ($r:expr, Bytes) => (io::scan($r));
        ($r:expr, $t:ty) => (scan!($r, String).parse::<$t>().unwrap());
    }

    #[macro_export]
    macro_rules! input {
        ($r:expr, $($($v:ident)* : $t:tt),* $(,)?) => {
            $(let $($v)* = scan!($r, $t);)*
        };
    }
}

fn get_divisors(n: usize) -> Vec<usize> {
    let (mut a, mut b) = (Vec::new(), Vec::new());
    for i in (1..).take_while(|i| i * i <= n) {
        if n % i > 0 { continue; }
        a.push(i);
        let j = n / i;
        if j == i { continue; }
        b.push(j);
    }
    a.into_iter().chain(b.into_iter().rev()).collect::<Vec<_>>()
}

const MAX: usize = 1_000_000;

fn run<R: std::io::BufRead, W: std::io::Write>(reader: &mut R, writer: &mut W) {
    input! {
        reader,
        n: usize,
        a: [usize; n],
    }

    let mut v = vec![0; MAX + 1];
    for a in a {
        for d in get_divisors(a) {
            v[d] += 1;
        }
    }

    let mut ans = vec![0; n + 1];
    for (i, v) in v.into_iter().enumerate() {
        ans[v] = ans[v].max(i);
    }
    for i in (0..n).rev() {
        ans[i] = ans[i].max(ans[i + 1]);
    }

    for k in 0..n {
        writeln!(writer, "{}", ans[n - k]).ok();
    }
}

fn main() {
    let (stdin, stdout) = (std::io::stdin(), std::io::stdout());
    let ref mut reader = std::io::BufReader::new(stdin.lock());
    let ref mut writer = std::io::BufWriter::new(stdout.lock());
    run(reader, writer);
}
0