結果

問題 No.1036 Make One With GCD 2
ユーザー tonyu0tonyu0
提出日時 2020-04-25 10:03:07
言語 Rust
(1.77.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,962 bytes
コンパイル時間 12,058 ms
コンパイル使用メモリ 153,744 KB
実行使用メモリ 100,520 KB
最終ジャッジ日時 2023-10-14 19:44:25
合計ジャッジ時間 20,254 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 325 ms
100,376 KB
testcase_01 AC 234 ms
92,152 KB
testcase_02 AC 206 ms
100,312 KB
testcase_03 AC 75 ms
31,324 KB
testcase_04 AC 134 ms
56,324 KB
testcase_05 AC 1 ms
4,372 KB
testcase_06 AC 1 ms
4,368 KB
testcase_07 AC 109 ms
39,252 KB
testcase_08 AC 84 ms
31,044 KB
testcase_09 AC 323 ms
90,404 KB
testcase_10 AC 317 ms
84,640 KB
testcase_11 AC 329 ms
91,960 KB
testcase_12 AC 322 ms
85,292 KB
testcase_13 AC 467 ms
96,580 KB
testcase_14 AC 472 ms
97,360 KB
testcase_15 AC 446 ms
92,468 KB
testcase_16 AC 445 ms
92,900 KB
testcase_17 AC 458 ms
95,248 KB
testcase_18 AC 1 ms
4,372 KB
testcase_19 AC 2 ms
4,372 KB
testcase_20 AC 3 ms
4,372 KB
testcase_21 AC 2 ms
4,372 KB
testcase_22 AC 435 ms
91,420 KB
testcase_23 AC 337 ms
63,808 KB
testcase_24 AC 453 ms
94,468 KB
testcase_25 AC 412 ms
79,588 KB
testcase_26 AC 445 ms
82,044 KB
testcase_27 AC 1 ms
4,372 KB
testcase_28 AC 1 ms
4,372 KB
testcase_29 AC 1 ms
4,372 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,372 KB
testcase_32 AC 1 ms
4,368 KB
testcase_33 AC 1 ms
4,368 KB
testcase_34 AC 1 ms
4,372 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 204 ms
100,336 KB
testcase_39 AC 318 ms
100,440 KB
testcase_40 AC 339 ms
63,808 KB
testcase_41 AC 1,784 ms
100,272 KB
testcase_42 AC 1,678 ms
100,340 KB
testcase_43 AC 1,893 ms
100,332 KB
testcase_44 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::*;

mod sparse_table {
    pub struct SparseTable<T, F> {
        table: Vec<Vec<T>>,
        func: F,
    }
    impl<T: Copy + Clone, F: Fn(T, T) -> T> SparseTable<T, F> {
        pub fn new(a: &Vec<T>, id: T, f: F) -> SparseTable<T, F> {
            let n = a.len();
            let max_log = (32 - (n as u32).leading_zeros()) as usize;
            let mut table = vec![vec![id; n]; max_log];
            table[0] = a.clone();
            for j in 1..max_log {
                for i in 0..n - (1 << j) + 1 {
                    table[j][i] = f(table[j - 1][i], table[j - 1][i + (1 << (j - 1))]);
                }
            }

            SparseTable {
                table: table,
                func: f,
            }
        }
        pub fn get(&self, from: usize, to: usize) -> T {
            assert!(from <= to && to <= self.table[0].len() - 1);
            let lg = (32 - ((to - from + 1) as u32).leading_zeros() - 1) as usize;
            (self.func)(self.table[lg][from], self.table[lg][to + 1 - (1 << lg)])
        }
    }
}

fn gcd(a: u64, b: u64) -> u64 {
    if b == 0 {
        return a;
    }
    gcd(b, a % b)
}

fn main() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.trim().split_whitespace();
    let n: usize = itr.next().unwrap().parse().unwrap();
    let a: Vec<u64> = (0..n)
        .map(|_| itr.next().unwrap().parse().unwrap())
        .collect();

    let st = sparse_table::SparseTable::new(&a, 1, gcd);
    let mut ans = a.iter().filter(|&e| *e == 1).count();
    for i in 0..n {
        if st.get(i, n - 1) != 1 {
            continue;
        }
        let mut ng = i;
        let mut ok = n;
        while ok - ng > 1 {
            let mid = (ok + ng) >> 1;
            if st.get(i, mid) == 1 {
                ok = mid
            } else {
                ng = mid
            }
        }
        ans += n - ok;
    }
    println!("{}", ans);
}
0