結果
問題 | No.1036 Make One With GCD 2 |
ユーザー | akakimidori |
提出日時 | 2021-02-03 20:56:55 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 179 ms / 2,000 ms |
コード長 | 3,648 bytes |
コンパイル時間 | 14,197 ms |
コンパイル使用メモリ | 379,176 KB |
実行使用メモリ | 34,944 KB |
最終ジャッジ日時 | 2024-09-16 14:34:44 |
合計ジャッジ時間 | 22,129 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 123 ms
15,872 KB |
testcase_01 | AC | 102 ms
11,008 KB |
testcase_02 | AC | 85 ms
34,944 KB |
testcase_03 | AC | 11 ms
5,376 KB |
testcase_04 | AC | 19 ms
5,376 KB |
testcase_05 | AC | 1 ms
5,376 KB |
testcase_06 | AC | 1 ms
5,376 KB |
testcase_07 | AC | 31 ms
5,760 KB |
testcase_08 | AC | 26 ms
5,376 KB |
testcase_09 | AC | 104 ms
10,496 KB |
testcase_10 | AC | 96 ms
9,856 KB |
testcase_11 | AC | 106 ms
10,624 KB |
testcase_12 | AC | 89 ms
9,984 KB |
testcase_13 | AC | 155 ms
14,464 KB |
testcase_14 | AC | 154 ms
14,592 KB |
testcase_15 | AC | 143 ms
13,824 KB |
testcase_16 | AC | 149 ms
13,824 KB |
testcase_17 | AC | 150 ms
14,208 KB |
testcase_18 | AC | 1 ms
5,376 KB |
testcase_19 | AC | 1 ms
5,376 KB |
testcase_20 | AC | 1 ms
5,376 KB |
testcase_21 | AC | 2 ms
5,376 KB |
testcase_22 | AC | 143 ms
13,568 KB |
testcase_23 | AC | 108 ms
10,368 KB |
testcase_24 | AC | 151 ms
14,080 KB |
testcase_25 | AC | 138 ms
13,056 KB |
testcase_26 | AC | 146 ms
13,440 KB |
testcase_27 | AC | 1 ms
5,376 KB |
testcase_28 | AC | 1 ms
5,376 KB |
testcase_29 | AC | 0 ms
5,376 KB |
testcase_30 | AC | 1 ms
5,376 KB |
testcase_31 | AC | 1 ms
5,376 KB |
testcase_32 | AC | 0 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 | 0 ms
5,376 KB |
testcase_37 | AC | 1 ms
5,376 KB |
testcase_38 | AC | 75 ms
34,688 KB |
testcase_39 | AC | 135 ms
15,104 KB |
testcase_40 | AC | 111 ms
10,496 KB |
testcase_41 | AC | 179 ms
15,232 KB |
testcase_42 | AC | 174 ms
15,232 KB |
testcase_43 | AC | 176 ms
20,248 KB |
testcase_44 | AC | 171 ms
17,720 KB |
ソースコード
// ---------- begin Foldable Deque ---------- struct FoldableDeque<T, F> { front: Vec<(T, T)>, back: Vec<(T, T)>, op: F, } #[allow(dead_code)] impl<T, F> FoldableDeque<T, F> where T: Clone, F: Fn(&T, &T) -> T, { fn new(op: F) -> Self { FoldableDeque { front: Vec::new(), back: Vec::new(), op: op, } } fn find(&self) -> Option<T> { match (self.front.last(), self.back.last()) { (Some(a), Some(b)) => Some((self.op)(&a.1, &b.1)), (Some(a), None) => Some(a.1.clone()), (None, Some(b)) => Some(b.1.clone()), (None, None) => None, } } fn clear(&mut self) { self.front.clear(); self.back.clear(); } fn len(&self) -> usize { self.front.len() + self.back.len() } fn push_back(&mut self, val: T) { let sum = if let Some(p) = self.back.last() { (self.op)(&p.1, &val) } else { val.clone() }; self.back.push((val, sum)); } fn push_front(&mut self, val: T) { let sum = if let Some(p) = self.front.last() { (self.op)(&val, &p.1) } else { val.clone() }; self.front.push((val, sum)); } fn pop_front(&mut self) -> Option<T> { if self.len() == 0 { return None; } if self.front.is_empty() { let a = self.back.clone(); let m = (self.back.len() + 1) / 2; self.back.clear(); let (f, b) = a.split_at(m); for v in f.iter().rev() { self.push_front(v.0.clone()); } for v in b.iter() { self.push_back(v.0.clone()); } } self.front.pop().map(|p| p.0) } fn pop_back(&mut self) -> Option<T> { if self.len() == 0 { return None; } if self.back.is_empty() { let a = self.front.clone(); let m = self.front.len() / 2; self.front.clear(); let (f, b) = a.split_at(m); for v in f.iter().rev() { self.push_front(v.0.clone()); } for v in b.iter() { self.push_back(v.0.clone()); } } self.back.pop().map(|p| p.0) } } // ---------- end Foldable Deque ---------- use std::io::Read; // ---------- begin binary_gcd ---------- pub fn gcd(a: u64, b: u64) -> u64 { if a == 0 || b == 0 { return a + b; } let x = a.trailing_zeros(); let y = b.trailing_zeros(); let mut a = a >> x; let mut b = b >> y; while a != b { let x = (a ^ b).trailing_zeros(); if a < b { std::mem::swap(&mut a, &mut b); } a = (a - b) >> x; } a << x.min(y) } // ---------- end binary_gcd ---------- fn run() { let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); let mut it = s.trim().split_whitespace(); let n: usize = it.next().unwrap().parse().unwrap(); let a: Vec<u64> = it.map(|s| s.parse().unwrap()).collect(); let mut deq = FoldableDeque::new(|a, b| gcd(*a, *b)); let mut ans = 0usize; let mut r = 0; for i in 0..n { if r == i { deq.push_back(a[r]); r += 1; } while r < n && deq.find().unwrap() > 1 { deq.push_back(a[r]); r += 1; } if deq.find().unwrap() == 1 { ans += n + 1 - r; } deq.pop_front(); } println!("{}", ans); } fn main() { run(); }