結果

問題 No.1036 Make One With GCD 2
ユーザー phsplsphspls
提出日時 2023-01-23 02:48:10
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 2,294 bytes
コンパイル時間 3,051 ms
コンパイル使用メモリ 136,592 KB
実行使用メモリ 90,736 KB
最終ジャッジ日時 2023-09-07 08:57:18
合計ジャッジ時間 41,739 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 982 ms
88,200 KB
testcase_01 AC 833 ms
84,684 KB
testcase_02 AC 999 ms
90,660 KB
testcase_03 AC 53 ms
37,976 KB
testcase_04 AC 92 ms
77,848 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 202 ms
40,328 KB
testcase_08 AC 170 ms
39,352 KB
testcase_09 AC 903 ms
84,020 KB
testcase_10 AC 842 ms
83,168 KB
testcase_11 AC 935 ms
85,540 KB
testcase_12 AC 854 ms
83,244 KB
testcase_13 AC 1,036 ms
87,980 KB
testcase_14 AC 1,053 ms
88,120 KB
testcase_15 AC 974 ms
87,092 KB
testcase_16 AC 987 ms
87,200 KB
testcase_17 AC 1,023 ms
87,580 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 4 ms
4,380 KB
testcase_22 AC 973 ms
86,896 KB
testcase_23 AC 713 ms
83,636 KB
testcase_24 AC 1,007 ms
89,264 KB
testcase_25 AC 918 ms
85,976 KB
testcase_26 AC 955 ms
86,932 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,380 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,376 KB
testcase_34 AC 1 ms
4,376 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 1,016 ms
88,720 KB
testcase_39 AC 952 ms
88,680 KB
testcase_40 AC 720 ms
82,904 KB
testcase_41 TLE -
testcase_42 TLE -
testcase_43 TLE -
testcase_44 TLE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: associated function `find_closed` is never used
  --> Main.rs:37:8
   |
37 |     fn find_closed(&self, l: usize, r: usize) -> T {
   |        ^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

ソースコード

diff #

#[derive(Debug, Clone)]
struct SparseTable<T, F> {
    vals: Vec<Vec<T>>,
    operator: F,
}

impl<T, F> SparseTable<T, F> where
T: std::fmt::Debug + Copy,
F: Fn(T, T) -> T,
{
    fn new(v: &Vec<T>, unit: T, operator: F) -> Self {
        let size = format!("{:b}", v.len()).len();
        let limit = 1usize << size;
        let mut vals = Vec::with_capacity(size+1);
        vals.push(vec![unit; limit]);
        for i in 0..v.len() {
            vals[0][i] = v[i];
        }
        for i in 1..=size {
            let length = 1usize << i;
            let jsize = limit+1-length;
            vals.push(vec![unit; jsize]);
            for start in 0..jsize {
                let left = start;
                let right = start + (1usize << (i-1));
                vals[i][start] = operator(vals[i - 1][left], vals[i - 1][right]);
            }
        }

        Self {
            vals,
            operator
        }
    }

    fn find_closed(&self, l: usize, r: usize) -> T {
        self.find(l, r+1)
    }

    fn find(&self, l: usize, r: usize) -> T {
        let length = r - l;
        if length == 1 {
            return self.vals[0][l];
        }
        let idx = format!("{:b}", length).len()-1;
        let left = self.vals[idx][l];
        let right = self.vals[idx][r - (1usize << idx)];
        (self.operator)(left, right)
    }
}

fn gcd(a: usize, b: usize) -> usize {
    if b == 0 { return a; }
    gcd(b, 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 stable = SparseTable::new(&a, 0usize, |x, y| gcd(x, y));
    let mut result = 0usize;
    for i in 0..n {
        if a[i] == 1 {
            result += n - i;
            continue;
        }
        let mut lower = i+1;
        let mut upper = n;
        while upper > lower {
            let middle = (upper + lower + 1) / 2;
            if stable.find(i, middle) > 1 {
                lower = middle;
            } else {
                upper = middle - 1;
            }
        }
        result += n - upper;
    }
    println!("{}", result);}
0