結果

問題 No.1036 Make One With GCD 2
ユーザー akakimidoriakakimidori
提出日時 2021-02-03 20:56:55
言語 Rust
(1.77.0)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 3,648 bytes
コンパイル時間 1,432 ms
コンパイル使用メモリ 149,204 KB
実行使用メモリ 41,892 KB
最終ジャッジ日時 2023-10-14 20:43:44
合計ジャッジ時間 8,486 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 142 ms
23,572 KB
testcase_01 AC 110 ms
14,076 KB
testcase_02 AC 94 ms
41,876 KB
testcase_03 AC 12 ms
4,368 KB
testcase_04 AC 20 ms
5,424 KB
testcase_05 AC 1 ms
4,372 KB
testcase_06 AC 1 ms
4,368 KB
testcase_07 AC 36 ms
7,640 KB
testcase_08 AC 28 ms
5,384 KB
testcase_09 AC 105 ms
13,876 KB
testcase_10 AC 98 ms
13,604 KB
testcase_11 AC 108 ms
13,880 KB
testcase_12 AC 100 ms
13,632 KB
testcase_13 AC 181 ms
22,088 KB
testcase_14 AC 183 ms
22,084 KB
testcase_15 AC 172 ms
21,800 KB
testcase_16 AC 173 ms
21,820 KB
testcase_17 AC 178 ms
22,068 KB
testcase_18 AC 1 ms
4,372 KB
testcase_19 AC 1 ms
4,368 KB
testcase_20 AC 1 ms
4,368 KB
testcase_21 AC 1 ms
4,372 KB
testcase_22 AC 170 ms
21,748 KB
testcase_23 AC 124 ms
12,620 KB
testcase_24 AC 177 ms
21,800 KB
testcase_25 AC 159 ms
13,376 KB
testcase_26 AC 164 ms
13,640 KB
testcase_27 AC 1 ms
4,372 KB
testcase_28 AC 1 ms
4,368 KB
testcase_29 AC 1 ms
4,368 KB
testcase_30 AC 1 ms
4,368 KB
testcase_31 AC 1 ms
4,372 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,372 KB
testcase_34 AC 1 ms
4,368 KB
testcase_35 AC 1 ms
4,372 KB
testcase_36 AC 1 ms
4,368 KB
testcase_37 AC 1 ms
4,372 KB
testcase_38 AC 92 ms
41,892 KB
testcase_39 AC 154 ms
22,328 KB
testcase_40 AC 124 ms
12,588 KB
testcase_41 AC 196 ms
22,504 KB
testcase_42 AC 195 ms
22,360 KB
testcase_43 AC 194 ms
27,160 KB
testcase_44 AC 198 ms
24,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// ---------- 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();
}
0