結果

問題 No.1233 割り切れない気持ち
ユーザー tzyvrntzyvrn
提出日時 2020-09-19 15:37:19
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 3,425 bytes
コンパイル時間 2,162 ms
コンパイル使用メモリ 155,592 KB
実行使用メモリ 8,752 KB
最終ジャッジ日時 2023-09-05 20:46:11
合計ジャッジ時間 16,423 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,368 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 53 ms
4,380 KB
testcase_08 AC 91 ms
4,376 KB
testcase_09 AC 192 ms
4,376 KB
testcase_10 AC 199 ms
4,524 KB
testcase_11 AC 42 ms
4,376 KB
testcase_12 AC 242 ms
4,936 KB
testcase_13 AC 229 ms
4,948 KB
testcase_14 AC 243 ms
4,944 KB
testcase_15 AC 242 ms
4,968 KB
testcase_16 AC 254 ms
4,964 KB
testcase_17 AC 9 ms
4,376 KB
testcase_18 AC 238 ms
4,972 KB
testcase_19 AC 14 ms
5,048 KB
testcase_20 AC 9 ms
4,376 KB
testcase_21 AC 27 ms
4,488 KB
testcase_22 AC 28 ms
4,492 KB
testcase_23 AC 28 ms
4,484 KB
testcase_24 AC 27 ms
4,492 KB
testcase_25 AC 27 ms
4,428 KB
testcase_26 AC 27 ms
4,384 KB
testcase_27 AC 776 ms
4,684 KB
testcase_28 AC 686 ms
4,668 KB
testcase_29 AC 632 ms
4,628 KB
testcase_30 AC 582 ms
4,692 KB
testcase_31 AC 549 ms
4,668 KB
testcase_32 AC 513 ms
4,628 KB
testcase_33 AC 476 ms
4,692 KB
testcase_34 AC 446 ms
4,592 KB
testcase_35 AC 424 ms
4,708 KB
testcase_36 AC 403 ms
4,632 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 TLE -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: use of deprecated associated function `core::str::<impl str>::trim_right`: superseded by `trim_end`
  --> 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 associated function
   |
8  |             let s = s.trim_end();
   |                       ~~~~~~~~

warning: use of deprecated associated function `core::str::<impl str>::trim_right`: superseded by `trim_end`
  --> 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 associated function
   |
20 |         let s = s.trim_end();
   |                   ~~~~~~~~

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

warning: 3 warnings emitted

ソースコード

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