結果
| 問題 |
No.1233 割り切れない気持ち
|
| コンテスト | |
| ユーザー |
tzyvrn
|
| 提出日時 | 2020-09-19 15:37:19 |
| 言語 | Rust (1.83.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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 36 TLE * 1 -- * 2 |
コンパイルメッセージ
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
ソースコード
//{{{
#[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)
}
}
tzyvrn