結果

問題 No.1862 Copy and Paste
ユーザー koba-e964koba-e964
提出日時 2022-03-06 18:31:32
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 161 ms / 2,000 ms
コード長 3,831 bytes
コンパイル時間 12,780 ms
コンパイル使用メモリ 391,100 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-09-13 09:35:35
合計ジャッジ時間 16,327 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,820 KB
testcase_03 AC 1 ms
6,812 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 142 ms
6,940 KB
testcase_10 AC 11 ms
6,944 KB
testcase_11 AC 151 ms
6,940 KB
testcase_12 AC 89 ms
6,940 KB
testcase_13 AC 146 ms
6,940 KB
testcase_14 AC 9 ms
6,940 KB
testcase_15 AC 74 ms
6,940 KB
testcase_16 AC 137 ms
6,944 KB
testcase_17 AC 73 ms
6,944 KB
testcase_18 AC 47 ms
6,940 KB
testcase_19 AC 145 ms
6,944 KB
testcase_20 AC 129 ms
6,944 KB
testcase_21 AC 85 ms
6,944 KB
testcase_22 AC 130 ms
6,940 KB
testcase_23 AC 132 ms
6,940 KB
testcase_24 AC 161 ms
6,944 KB
testcase_25 AC 1 ms
6,944 KB
testcase_26 AC 161 ms
6,940 KB
testcase_27 AC 160 ms
6,940 KB
testcase_28 AC 158 ms
6,940 KB
testcase_29 AC 127 ms
6,948 KB
testcase_30 AC 21 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::*;
use std::io::Read;

fn get_word() -> String {
    let stdin = std::io::stdin();
    let mut stdin=stdin.lock();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

// DP table indexed by {floor(n / i) | 1 <= i <= n}.
// Verified by: https://atcoder.jp/contests/abc239/submissions/29553511
type K = i64;
type V = i64;
struct QuoDP {
    // stores dp[n], dp[n/2], ..., dp[n/b].
    dp_big: Vec<V>,
    dp: Vec<V>,
    n: K,
    b: K,
}

impl QuoDP {
    pub fn new(n: K, b: K) -> Self {
        let dp_big = vec![0.into(); b as usize + 1];
        let dp = vec![0.into(); ((n + b - 1) / b) as usize];
        QuoDP {
            dp_big: dp_big,
            dp: dp,
            n: n,
            b: b,
        }
    }
    #[allow(unused)]
    pub fn keys(&self) -> Vec<K> {
        (1..(self.n + self.b - 1) / self.b).chain((1..=self.b).rev().map(|x| (self.n + x - 1) / x)).collect()
    }
    // pos should be of form ceil(n / ???).
    pub fn upd<F>(&mut self, pos: K, f: F) where F: Fn(V) -> V {
        if pos >= (self.n + self.b - 1) / self.b {
            let idx = (self.n + pos - 1) / pos;
            debug_assert_eq!(pos, (self.n + idx - 1) / idx);
            self.dp_big[idx as usize] = f(self.dp_big[idx as usize]);
            return;
        }
        let idx = pos as usize;
        self.dp[idx] = f(self.dp[idx]);
    }
    pub fn get(&self, pos: K) -> V {
        if pos >= (self.n + self.b - 1) / self.b {
            let idx = (self.n + pos - 1) / pos;
            debug_assert_eq!(pos, (self.n + idx - 1) / idx);
            return self.dp_big[idx as usize];
        }
        let idx = pos as usize;
        self.dp[idx]
    }
    #[allow(unused)]
    pub fn init<F>(&mut self, f: F) where F: Fn(K) -> V {
        self.upd_all(|k, _| f(k));
    }
    pub fn upd_all<F>(&mut self, f: F) where F: Fn(K, V) -> V {
        for i in 0..self.dp.len() {
            self.dp[i] = f(i as K, self.dp[i]);
        }
        for i in (1..self.dp_big.len()).rev() {
            self.dp_big[i] = f((self.n + i as K - 1) / i as K, self.dp_big[i]);
        }
    }
}

impl std::fmt::Debug for QuoDP {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        for i in 0..self.dp.len() {
            writeln!(f, "{}: {}", i, self.dp[i])?;
        }
        for i in (1..self.dp_big.len()).rev() {
            writeln!(f, "{}: {}", (self.n + i as K - 1) / i as K, self.dp_big[i])?;
        }
        Ok(())
    }
}

// Tags: variant-of-lucys-sieve
// Complexity: O(n^{3/4})
fn main() {
    let a: i64 = get();
    let b: i64 = get();
    let n: i64 = get();
    let inf = a + (n - 1) * b;
    let mut sqn = 0;
    while sqn * sqn <= n {
        sqn += 1;
    }
    sqn -= 1;
    let mut dp = QuoDP::new(n, sqn);
    let keys = dp.keys();
    dp.upd(1, |_| 0);
    for pos in keys {
        if pos == 1 { continue; }
        let mut x = 1;
        while x * x <= pos {
            x += 1;
        }
        x -= 1;
        let mut tmp = inf;
        // Iterate over ceil(pos / i) for 1 <= i <= pos
        for j in 2..=x {
            tmp = min(tmp, dp.get((pos + j - 1) / j) + a + (j - 1) * b);
        }
        for j in 1..pos / x {
            let l = (pos + j - 1) / j;
            tmp = min(tmp, dp.get(j) + a + (l - 1) * b);
        }
        dp.upd(pos, |_| tmp);
    }
    println!("{}", dp.get(n));
}
0