結果
問題 | No.1036 Make One With GCD 2 |
ユーザー | phspls |
提出日時 | 2023-01-25 02:05:55 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 362 ms / 2,000 ms |
コード長 | 2,008 bytes |
コンパイル時間 | 25,153 ms |
コンパイル使用メモリ | 378,488 KB |
実行使用メモリ | 23,424 KB |
最終ジャッジ日時 | 2024-06-26 09:35:43 |
合計ジャッジ時間 | 25,353 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 72 ms
15,232 KB |
testcase_01 | AC | 60 ms
11,008 KB |
testcase_02 | AC | 82 ms
23,424 KB |
testcase_03 | AC | 9 ms
5,376 KB |
testcase_04 | AC | 13 ms
5,376 KB |
testcase_05 | AC | 1 ms
5,376 KB |
testcase_06 | AC | 1 ms
5,376 KB |
testcase_07 | AC | 20 ms
5,760 KB |
testcase_08 | AC | 16 ms
5,376 KB |
testcase_09 | AC | 177 ms
10,496 KB |
testcase_10 | AC | 164 ms
9,856 KB |
testcase_11 | AC | 178 ms
10,752 KB |
testcase_12 | AC | 165 ms
9,984 KB |
testcase_13 | AC | 354 ms
14,464 KB |
testcase_14 | AC | 362 ms
14,592 KB |
testcase_15 | AC | 338 ms
13,696 KB |
testcase_16 | AC | 346 ms
13,952 KB |
testcase_17 | AC | 357 ms
14,336 KB |
testcase_18 | AC | 1 ms
5,376 KB |
testcase_19 | AC | 2 ms
5,376 KB |
testcase_20 | AC | 1 ms
5,376 KB |
testcase_21 | AC | 1 ms
5,376 KB |
testcase_22 | AC | 337 ms
13,696 KB |
testcase_23 | AC | 247 ms
10,752 KB |
testcase_24 | AC | 349 ms
14,208 KB |
testcase_25 | AC | 318 ms
13,056 KB |
testcase_26 | AC | 331 ms
13,440 KB |
testcase_27 | AC | 1 ms
5,376 KB |
testcase_28 | AC | 1 ms
5,376 KB |
testcase_29 | AC | 1 ms
5,376 KB |
testcase_30 | AC | 1 ms
5,376 KB |
testcase_31 | AC | 1 ms
5,376 KB |
testcase_32 | AC | 1 ms
5,376 KB |
testcase_33 | AC | 1 ms
5,376 KB |
testcase_34 | AC | 1 ms
5,376 KB |
testcase_35 | AC | 1 ms
5,376 KB |
testcase_36 | AC | 1 ms
5,376 KB |
testcase_37 | AC | 1 ms
5,376 KB |
testcase_38 | AC | 75 ms
23,040 KB |
testcase_39 | AC | 77 ms
15,232 KB |
testcase_40 | AC | 246 ms
10,624 KB |
testcase_41 | AC | 297 ms
15,104 KB |
testcase_42 | AC | 299 ms
15,104 KB |
testcase_43 | AC | 246 ms
17,280 KB |
testcase_44 | AC | 269 ms
16,256 KB |
コンパイルメッセージ
warning: unused variable: `i` --> src/main.rs:74:9 | 74 | for i in 0..n { | ^ help: if this is intentional, prefix it with an underscore: `_i` | = note: `#[warn(unused_variables)]` on by default warning: method `len` is never used --> src/main.rs:28:8 | 14 | / impl<T, F> SWAG<T, F> where 15 | | T: std::fmt::Debug + Copy, 16 | | F: Fn(T, T) -> T, | |_________________- method in this implementation ... 28 | fn len(&self) -> usize { | ^^^ | = note: `#[warn(dead_code)]` on by default
ソースコード
/* Sliding Window AGgregation */ #[derive(Debug, Clone)] struct SWAG<T, F> { front: Vec<T>, back: Vec<T>, unit: T, operator: F, back_agg: T, } impl<T, F> SWAG<T, F> where T: std::fmt::Debug + Copy, F: Fn(T, T) -> T, { fn new(unit: T, operator: F) -> Self { Self { front: vec![], back: vec![], unit: unit, operator: operator, back_agg: unit } } fn len(&self) -> usize { self.front.len() + self.back.len() } fn push(&mut self, val: T) { self.back.push(val); self.back_agg = (self.operator)(self.back_agg, val); } fn pop(&mut self) -> Option<T> { if self.front.is_empty() { while let Some(p) = self.back.pop() { let fagg = *self.front.last().unwrap_or(&self.unit); self.front.push((self.operator)(p, fagg)); } self.back_agg = self.unit; } self.front.pop() } fn fold(&self) -> T { (self.operator)(*self.front.last().unwrap_or(&self.unit), self.back_agg) } } fn gcd(a: usize, b: usize) -> usize { fn _gcd(a: usize, b: usize) -> usize { if b == 0 { return a; } gcd(b, a%b) } let (a, b) = (a.max(b), a.min(b)); _gcd(a, b) } fn main() { let mut n = String::new(); std::io::stdin().read_line(&mut n).ok(); let n: usize = n.trim().parse().unwrap(); let mut a = String::new(); std::io::stdin().read_line(&mut a).ok(); let a: Vec<usize> = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut result = 0usize; let mut swag = SWAG::new(0usize, |x, y| gcd(x, y)); swag.push(a[0]); let mut ridx = 1usize; for i in 0..n { while ridx < n && swag.fold() != 1 { swag.push(a[ridx]); ridx += 1; } result += n + if swag.fold() == 1 { 1 } else { 0 } - ridx; swag.pop(); } println!("{}", result); }