結果

問題 No.1262 グラフを作ろう!
ユーザー koba-e964koba-e964
提出日時 2020-10-22 19:52:30
言語 Rust
(1.77.0)
結果
AC  
実行時間 334 ms / 3,000 ms
コード長 3,346 bytes
コンパイル時間 3,204 ms
コンパイル使用メモリ 149,496 KB
実行使用メモリ 32,288 KB
最終ジャッジ日時 2023-09-28 14:46:43
合計ジャッジ時間 36,150 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 199 ms
18,480 KB
testcase_01 AC 146 ms
14,300 KB
testcase_02 AC 227 ms
22,708 KB
testcase_03 AC 173 ms
13,196 KB
testcase_04 AC 137 ms
20,048 KB
testcase_05 AC 147 ms
17,424 KB
testcase_06 AC 171 ms
23,480 KB
testcase_07 AC 38 ms
5,512 KB
testcase_08 AC 218 ms
24,620 KB
testcase_09 AC 114 ms
9,240 KB
testcase_10 AC 114 ms
12,216 KB
testcase_11 AC 244 ms
26,928 KB
testcase_12 AC 187 ms
16,968 KB
testcase_13 AC 180 ms
14,068 KB
testcase_14 AC 114 ms
13,012 KB
testcase_15 AC 87 ms
7,188 KB
testcase_16 AC 203 ms
23,480 KB
testcase_17 AC 176 ms
19,608 KB
testcase_18 AC 248 ms
25,332 KB
testcase_19 AC 118 ms
18,288 KB
testcase_20 AC 210 ms
26,092 KB
testcase_21 AC 241 ms
23,236 KB
testcase_22 AC 101 ms
16,412 KB
testcase_23 AC 119 ms
10,364 KB
testcase_24 AC 211 ms
24,284 KB
testcase_25 AC 69 ms
11,088 KB
testcase_26 AC 92 ms
13,272 KB
testcase_27 AC 170 ms
21,984 KB
testcase_28 AC 142 ms
18,728 KB
testcase_29 AC 139 ms
20,060 KB
testcase_30 AC 107 ms
13,800 KB
testcase_31 AC 212 ms
19,616 KB
testcase_32 AC 148 ms
16,360 KB
testcase_33 AC 52 ms
5,532 KB
testcase_34 AC 215 ms
26,392 KB
testcase_35 AC 169 ms
14,520 KB
testcase_36 AC 58 ms
5,264 KB
testcase_37 AC 254 ms
25,864 KB
testcase_38 AC 169 ms
20,328 KB
testcase_39 AC 108 ms
9,496 KB
testcase_40 AC 114 ms
11,864 KB
testcase_41 AC 165 ms
21,908 KB
testcase_42 AC 238 ms
25,604 KB
testcase_43 AC 145 ms
17,408 KB
testcase_44 AC 143 ms
16,356 KB
testcase_45 AC 251 ms
26,884 KB
testcase_46 AC 196 ms
25,064 KB
testcase_47 AC 113 ms
8,968 KB
testcase_48 AC 209 ms
23,496 KB
testcase_49 AC 248 ms
25,064 KB
testcase_50 AC 236 ms
25,600 KB
testcase_51 AC 137 ms
11,340 KB
testcase_52 AC 179 ms
23,744 KB
testcase_53 AC 59 ms
8,444 KB
testcase_54 AC 197 ms
17,160 KB
testcase_55 AC 159 ms
16,364 KB
testcase_56 AC 152 ms
21,120 KB
testcase_57 AC 299 ms
29,568 KB
testcase_58 AC 175 ms
15,300 KB
testcase_59 AC 165 ms
22,164 KB
testcase_60 AC 271 ms
27,672 KB
testcase_61 AC 107 ms
12,128 KB
testcase_62 AC 225 ms
22,432 KB
testcase_63 AC 193 ms
16,088 KB
testcase_64 AC 149 ms
15,308 KB
testcase_65 AC 88 ms
13,268 KB
testcase_66 AC 115 ms
13,236 KB
testcase_67 AC 114 ms
15,560 KB
testcase_68 AC 188 ms
24,816 KB
testcase_69 AC 113 ms
14,520 KB
testcase_70 AC 252 ms
27,416 KB
testcase_71 AC 188 ms
21,372 KB
testcase_72 AC 260 ms
27,976 KB
testcase_73 AC 251 ms
24,808 KB
testcase_74 AC 209 ms
20,068 KB
testcase_75 AC 210 ms
22,692 KB
testcase_76 AC 180 ms
20,668 KB
testcase_77 AC 209 ms
21,348 KB
testcase_78 AC 207 ms
23,216 KB
testcase_79 AC 284 ms
29,104 KB
testcase_80 AC 255 ms
27,972 KB
testcase_81 AC 204 ms
20,576 KB
testcase_82 AC 237 ms
23,224 KB
testcase_83 AC 232 ms
26,388 KB
testcase_84 AC 186 ms
21,112 KB
testcase_85 AC 294 ms
29,564 KB
testcase_86 AC 216 ms
22,700 KB
testcase_87 AC 262 ms
26,928 KB
testcase_88 AC 202 ms
20,660 KB
testcase_89 AC 220 ms
25,120 KB
testcase_90 AC 317 ms
32,288 KB
testcase_91 AC 317 ms
32,192 KB
testcase_92 AC 315 ms
32,196 KB
testcase_93 AC 334 ms
32,268 KB
testcase_94 AC 314 ms
32,276 KB
testcase_95 AC 313 ms
32,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused)]
macro_rules! debug {
    ($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap());
}
#[allow(unused)]
macro_rules! debugln {
    ($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap());
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($($format:tt)*) => (let _ = write!(out,$($format)*););
    }
    input! {
        n: usize,
        a: [usize],
    }
    let n = n + 1;
    // Problem: find \sum_{x: a} \sum_{j = 1}^x (gcd(x, j) - 1)
    let mut phi = vec![1i64; n];
    let mut fac = vec![0; n];
    for i in 1..n {
        fac[i] = i;
    }
    for i in 2..n {
        if fac[i] != i {
            continue;
        }
        for j in 2..(n - 1) / i + 1 {
            fac[i * j] = i;
        }
    }
    for i in 1..n {
        let mut v = i;
        let mut a = 1;
        while v > 1 {
            let p = fac[v];
            let mut e = 0;
            while v % p == 0 {
                e += 1;
                v /= p;
            }
            a *= p - 1;
            for _ in 0..e - 1 {
                a *= p;
            }
        }
        phi[i] = a as i64;
    }
    // \sum_[j = 1}^x gcd(x, i) = \sum_{g | x} g * phi(x / g)
    let mut vals = vec![0; n];
    for g in 1..n {
        for j in 1..(n - 1) / g + 1 {
            vals[g * j] += g as i64 * phi[j];
        }
    }
    let mut tot = 0;
    // (RHS) = \sum {x: a} vals[x] - x
    for a in a {
        tot += vals[a] - a as i64;
    }
    puts!("{}\n", tot);
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}
0