結果

問題 No.1233 割り切れない気持ち
ユーザー tzyvrntzyvrn
提出日時 2020-09-19 15:37:19
言語 Rust
(1.77.0 + proconio)
結果
TLE  
実行時間 -
コード長 3,425 bytes
コンパイル時間 13,037 ms
コンパイル使用メモリ 379,204 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-06-23 16:11:20
合計ジャッジ時間 28,584 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
9,344 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 57 ms
5,376 KB
testcase_08 AC 95 ms
5,376 KB
testcase_09 AC 200 ms
5,376 KB
testcase_10 AC 199 ms
5,376 KB
testcase_11 AC 43 ms
5,376 KB
testcase_12 AC 238 ms
5,376 KB
testcase_13 AC 238 ms
5,376 KB
testcase_14 AC 245 ms
5,376 KB
testcase_15 AC 255 ms
5,376 KB
testcase_16 AC 269 ms
5,376 KB
testcase_17 AC 8 ms
5,376 KB
testcase_18 AC 247 ms
5,376 KB
testcase_19 AC 15 ms
5,376 KB
testcase_20 AC 8 ms
5,376 KB
testcase_21 AC 32 ms
5,376 KB
testcase_22 AC 32 ms
5,376 KB
testcase_23 AC 32 ms
5,376 KB
testcase_24 AC 30 ms
5,376 KB
testcase_25 AC 30 ms
5,376 KB
testcase_26 AC 30 ms
5,376 KB
testcase_27 AC 935 ms
5,376 KB
testcase_28 AC 814 ms
5,376 KB
testcase_29 AC 741 ms
5,376 KB
testcase_30 AC 686 ms
5,376 KB
testcase_31 AC 621 ms
5,376 KB
testcase_32 AC 586 ms
5,376 KB
testcase_33 AC 556 ms
5,376 KB
testcase_34 AC 543 ms
5,376 KB
testcase_35 AC 495 ms
5,376 KB
testcase_36 AC 454 ms
5,376 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 TLE -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: use of deprecated method `core::str::<impl str>::trim_right`: superseded by `trim_end`
  --> src/main.rs:8:23
   |
8  |             let s = s.trim_right();
   |                       ^^^^^^^^^^
...
37 |     let n = getl!(usize);
   |             ------------ in this macro invocation
   |
   = note: `#[warn(deprecated)]` on by default
   = note: this warning originates in the macro `getl` (in Nightly builds, run with -Z macro-backtrace for more info)
help: replace the use of the deprecated method
   |
8  |             let s = s.trim_end();
   |                       ~~~~~~~~

warning: use of deprecated method `core::str::<impl str>::trim_right`: superseded by `trim_end`
  --> src/main.rs:20:19
   |
20 |         let s = s.trim_right();
   |                   ^^^^^^^^^^
...
38 |     let mut a = getl_vec!(i64);
   |                 -------------- in this macro invocation
   |
   = note: this warning originates in the macro `getl_vec` (in Nightly builds, run with -Z macro-backtrace for more info)
help: replace the use of the deprecated method
   |
20 |         let s = s.trim_end();
   |                   ~~~~~~~~

warning: value assigned to `r` is never read
  --> src/main.rs:49:17
   |
49 |         let mut r = None;
   |                 ^
   |
   = help: maybe it is overwritten before being read?
   = note: `#[warn(unused_assignments)]` on by default

ソースコード

diff #

//{{{
#[allow(unused_macros)]
macro_rules! getl {
    ( $( $t:ty ),* ) => {
        {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            let s = s.trim_right();
            let mut ws = s.split_whitespace();
            ($(ws.next().unwrap().parse::<$t>().unwrap()),*)
        }
    };
}

#[allow(unused_macros)]
macro_rules! getl_vec {
    ( $t:ty ) => {{
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        let s = s.trim_right();
        s.split_whitespace()
            .map(|x| x.parse().unwrap())
            .collect::<Vec<$t>>()
    }};
}
//}}}

// \sum_i \sum_j a_i % a_j
// \sum_i ai % d = s
// s === (\sum_i a_i) % d
// s - (\sum_i a_i) % d は?
// 各a_iに対して floor(a_i / d) * d だけ差が出る
// m * d <= a_i < (m + 1) * d となるような個数cを求めて c * m * d
// O(log(n)^2 * n)

fn main() {
    let n = getl!(usize);
    let mut a = getl_vec!(i64);
    a.sort();

    let s: i64 = a.iter().sum();

    let mut ans: i64 = 0;
    for a_i in a.iter().copied() {
        let mut now = s;

        let d = a_i;
        let mut l: Option<usize> = Some(0);
        let mut r = None;
        let mut m = 0;
        loop {
            r = a.binary_search_left(|a_i| *a_i >= (m + 1) * d);
            // dbg!(m, l, r);

            if l.is_none() {
                break;
            }

            match (l, r) {
                (Some(l), Some(r)) => {
                    now -= (r as i64 - l as i64) * m * d;
                }
                (None, Some(r)) => {
                    now -= (r as i64) * m * d;
                }
                (Some(l), None) => {
                    now -= (n as i64 - l as i64) * m * d;
                },
                (None, None) => {},
            }

            l = r;
            m += 1;
        }
        ans += now;
        // dbg!(a_i, ans, now);
    }

    println!("{}", ans);
}

trait BinarySearch<T, F> {
    fn binary_search_right(&self, condition: F) -> Option<usize>;
    fn binary_search_left(&self, condition: F) -> Option<usize>;
}

impl<T, F> BinarySearch<T, F> for Vec<T>
where
    F: Fn(&T) -> bool,
{
    fn binary_search_right(&self, condition: F) -> Option<usize> {
        if self.is_empty() {
            return None;
        }

        let len = self.len();
        if condition(&self[len - 1]) {
            return Some(len - 1);
        }
        if !condition(&self[0]) {
            return None;
        }

        let mut left = 0;
        let mut right = len - 1;
        while left + 1 < right {
            let mid = (left + right) / 2;
            if condition(&self[mid]) {
                left = mid;
            } else {
                right = mid;
            }
        }

        Some(left)
    }

    fn binary_search_left(&self, condition: F) -> Option<usize> {
        if self.is_empty() {
            return None;
        }

        let len = self.len();

        if condition(&self[0]) {
            return Some(0);
        }
        if !condition(&self[len - 1]) {
            return None;
        }

        let mut left = 0;
        let mut right = len - 1;
        while left + 1 < right {
            let mid = (left + right) / 2;
            if condition(&self[mid]) {
                right = mid;
            } else {
                left = mid;
            }
        }

        Some(right)
    }
}
0