提出 #19235690


ソースコード 拡げる

use std::cmp::{Ordering, Reverse};
use std::collections::BinaryHeap;

const MOD: i64 = 1e9 as i64 + 7;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let n: usize = sc.read();
    let a: i64 = sc.read();
    let mut b: usize = sc.read();
    let mut arr: Vec<i64> = sc.vec(n);

    if a == 1 {
        arr.sort();
        for i in 0..n {
            println!("{}", arr[i]);
        }
        return;
    }

    let mut heap = BinaryHeap::new();
    let mut cur_max = 0.0f64;

    for i in 0..n {
        let a = (arr[i] as f64).log2();
        heap.push((Reverse(F64(a)), 0, i));
        cur_max = cur_max.max(a);
    }

    while b > 0 && heap.peek().cloned().unwrap().0 .0 .0 + (a as f64).log2() < cur_max {
        let mut minimum = heap.pop().unwrap();
        minimum.0 .0 .0 += (a as f64).log2();
        minimum.1 += 1;
        cur_max = cur_max.max(minimum.0 .0 .0);
        heap.push(minimum);
        b -= 1;
    }

    let one_b = b / n;
    for _ in 0..n {
        let mut minimum = heap.pop().unwrap();
        minimum.0 .0 .0 += (a as f64).log2() * one_b as f64;
        minimum.1 += one_b;
        heap.push(minimum);
    }
    for _ in 0..(b % n) {
        let mut minimum = heap.pop().unwrap();
        minimum.0 .0 .0 += (a as f64).log2();
        minimum.1 += 1;
        heap.push(minimum);
    }

    while let Some((_, count, i)) = heap.pop() {
        let pow = mod_pow(a, count);
        println!("{}", (pow * arr[i]) % MOD);
    }
}

fn mod_pow(mut x: i64, mut e: usize) -> i64 {
    let mut result = 1;
    while e > 0 {
        if e & 1 == 1 {
            result = (result * x) % MOD;
        }
        x = (x * x) % MOD;
        e >>= 1;
    }
    result
}

#[derive(PartialEq, Copy, Clone)]
struct F64(f64);

impl Eq for F64 {}
impl PartialOrd for F64 {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.0.partial_cmp(&other.0)
    }
}

impl Ord for F64 {
    fn cmp(&self, other: &Self) -> Ordering {
        self.partial_cmp(&other).unwrap()
    }
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        Self(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write_all(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}

提出情報

提出日時
問題 C - 掛け算
ユーザ kenkoooo
言語 Rust (1.42.0)
得点 100
コード長 3297 Byte
結果 AC
実行時間 7 ms
メモリ 2692 KiB

ジャッジ結果

セット名 all
得点 / 配点 100 / 100
結果
AC × 18
セット名 テストケース
all example_0.txt, example_1.txt, example_2.txt, one_0.txt, one_1.txt, one_2.txt, one_3.txt, one_4.txt, random_0.txt, random_1.txt, random_2.txt, random_3.txt, random_4.txt, smallB_0.txt, smallB_1.txt, smallB_2.txt, smallB_3.txt, smallB_4.txt
ケース名 結果 実行時間 メモリ
example_0.txt AC 7 ms 2676 KiB
example_1.txt AC 2 ms 2420 KiB
example_2.txt AC 2 ms 2664 KiB
one_0.txt AC 1 ms 2340 KiB
one_1.txt AC 2 ms 2476 KiB
one_2.txt AC 2 ms 2516 KiB
one_3.txt AC 2 ms 2468 KiB
one_4.txt AC 2 ms 2384 KiB
random_0.txt AC 2 ms 2564 KiB
random_1.txt AC 2 ms 2632 KiB
random_2.txt AC 2 ms 2588 KiB
random_3.txt AC 2 ms 2584 KiB
random_4.txt AC 2 ms 2588 KiB
smallB_0.txt AC 2 ms 2460 KiB
smallB_1.txt AC 4 ms 2640 KiB
smallB_2.txt AC 2 ms 2564 KiB
smallB_3.txt AC 2 ms 2692 KiB
smallB_4.txt AC 2 ms 2652 KiB