結果

問題 No.1036 Make One With GCD 2
ユーザー phsplsphspls
提出日時 2023-02-22 00:16:54
言語 Rust
(1.77.0)
結果
AC  
実行時間 357 ms / 2,000 ms
コード長 1,921 bytes
コンパイル時間 1,463 ms
コンパイル使用メモリ 150,176 KB
実行使用メモリ 23,600 KB
最終ジャッジ日時 2023-09-29 13:04:46
合計ジャッジ時間 12,520 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
15,332 KB
testcase_01 AC 53 ms
11,168 KB
testcase_02 AC 80 ms
23,600 KB
testcase_03 AC 7 ms
4,380 KB
testcase_04 AC 12 ms
5,072 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 21 ms
5,756 KB
testcase_08 AC 18 ms
5,140 KB
testcase_09 AC 155 ms
10,484 KB
testcase_10 AC 147 ms
9,888 KB
testcase_11 AC 172 ms
10,584 KB
testcase_12 AC 153 ms
9,936 KB
testcase_13 AC 342 ms
14,560 KB
testcase_14 AC 357 ms
14,664 KB
testcase_15 AC 327 ms
13,772 KB
testcase_16 AC 326 ms
13,888 KB
testcase_17 AC 326 ms
14,424 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 317 ms
13,680 KB
testcase_23 AC 226 ms
10,528 KB
testcase_24 AC 320 ms
14,048 KB
testcase_25 AC 290 ms
12,972 KB
testcase_26 AC 305 ms
13,584 KB
testcase_27 AC 0 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 81 ms
23,128 KB
testcase_39 AC 79 ms
16,572 KB
testcase_40 AC 225 ms
11,744 KB
testcase_41 AC 261 ms
15,296 KB
testcase_42 AC 264 ms
15,264 KB
testcase_43 AC 222 ms
18,188 KB
testcase_44 AC 249 ms
16,088 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> Main.rs:69:9
   |
69 |     for i in 0..n {
   |         ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: associated function `len` is never used
  --> Main.rs:25:8
   |
25 |     fn len(&self) -> usize {
   |        ^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: 2 warnings emitted

ソースコード

diff #

#[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(x: usize, y: usize) -> usize {
        if y == 0 { return x; }
        _gcd(y, x%y)
    }
    _gcd(a.max(b), a.min(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 swag = SWAG::new(0usize, |x, y| gcd(x, y));
    let mut result = 0usize;
    let mut idx = 0usize;
    for i in 0..n {
        while idx < n && swag.fold() != 1 {
            swag.push(a[idx]);
            idx += 1;
        }
        result += n - idx + if swag.fold() == 1 { 1 } else { 0 };
        swag.pop();
    }
    println!("{}", result);
}
0