結果

問題 No.757 チャンパーノウン定数 (2)
ユーザー koba-e964koba-e964
提出日時 2021-11-14 00:22:47
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 1,256 ms / 2,000 ms
コード長 3,628 bytes
コンパイル時間 13,950 ms
コンパイル使用メモリ 378,640 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-06 04:55:55
合計ジャッジ時間 32,820 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 1 ms
5,248 KB
testcase_04 AC 0 ms
5,248 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 0 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 1,047 ms
5,376 KB
testcase_29 AC 789 ms
5,376 KB
testcase_30 AC 42 ms
5,376 KB
testcase_31 AC 113 ms
5,376 KB
testcase_32 AC 9 ms
5,376 KB
testcase_33 AC 203 ms
5,376 KB
testcase_34 AC 425 ms
5,376 KB
testcase_35 AC 94 ms
5,376 KB
testcase_36 AC 61 ms
5,376 KB
testcase_37 AC 757 ms
5,376 KB
testcase_38 AC 1,050 ms
5,376 KB
testcase_39 AC 1,146 ms
5,376 KB
testcase_40 AC 1,135 ms
5,376 KB
testcase_41 AC 1,134 ms
5,376 KB
testcase_42 AC 1,256 ms
5,376 KB
testcase_43 AC 1,178 ms
5,376 KB
testcase_44 AC 1,120 ms
5,376 KB
testcase_45 AC 1,165 ms
5,376 KB
testcase_46 AC 1,180 ms
5,376 KB
testcase_47 AC 1,165 ms
5,376 KB
testcase_48 AC 1,236 ms
5,376 KB
testcase_49 AC 1,191 ms
5,376 KB
testcase_50 AC 1 ms
5,376 KB
testcase_51 AC 1 ms
5,376 KB
testcase_52 AC 1 ms
5,376 KB
testcase_53 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); }
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) { if *self < x { *self = x; } }
    fn chmin(&mut self, x: T) { if *self > x { *self = x; } }
}

// Finds sum_{1 <= l < b^x} |l|. None if the result is >= b^n.
fn calc(b: i32, x: usize, n: usize) -> Option<Vec<i32>> {
    if x >= n + 1 {
        return None;
    }
    let mut ans = vec![0; n];
    for i in (0..x).rev() {
        let mut c = (i + 1) as i32 * (b - 1);
        let mut pos = n - i;
        while pos > 0 && c > 0 {
            ans[pos - 1] += c;
            c = ans[pos - 1] / b;
            ans[pos - 1] %= b;
            pos -= 1;
        }
        if pos == 0 && c != 0 {
            return None;
        }
    }
    Some(ans)
}

fn main() {
    input! {
        b: i32,
        d: chars,
    }
    let n = d.len();
    let d: Vec<i32> = d.into_iter().map(|x| (x as u8 - b'0') as _).collect();
    let mut pass = 0;
    let mut fail = n + 1;
    while fail - pass > 1 {
        let mid = (pass + fail) / 2;
        let res = calc(b, mid, n);
        if res.is_some() && res.unwrap() < d {
            pass = mid;
        } else {
            fail = mid;
        }
    }
    eprintln!("{}", fail);
    for i in 0..3 {
        eprintln!("{} => {:?}", i, calc(b, i, n));
    }
    let lt = calc(b, pass, n).unwrap();
    let mut rem = d.clone();
    let mut c = 0;
    for i in (0..n).rev() {
        c = c + rem[i] - lt[i];
        let mut nc = 0;
        if c < 0 {
            c += b;
            nc -= 1;
        }
        rem[i] = c;
        c = nc;
    }
    eprintln!("rem = {:?}", rem);
    // q = (rem - 1) / fail, r = (rem - 1) % fail
    // the (r+1)-st digit of (b^pass + q) is the answer.
    c = -1;
    for i in (0..n).rev() {
        c = rem[i] + c;
        let mut nc = 0;
        if c < 0 {
            c += b;
            nc -= 1;
        }
        rem[i] = c;
        c = nc;
    }
    c = 0;
    let mut quo = vec![0; n];
    for i in 0..n {
        c = b * c + rem[i];
        quo[i] = c / fail as i32;
        c %= fail as i32;
    }
    eprintln!("quo = {:?}, rem = {}", quo, c);
    quo[n - fail] += 1;
    println!("{}", quo[n - fail + c as usize]);
}
0