1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
pub struct LinearSieve {
    lpf: Vec<usize>,
    lpf_e: Vec<(usize, u32)>,
    pr: Vec<usize>,
}

impl LinearSieve {
    pub fn new(n: usize) -> Self {
        let mut lpf = vec![1; n + 1];
        let mut pr = vec![];
        for i in 2..=n {
            if lpf[i] == 1 {
                lpf[i] = i;
                pr.push(i);
            }
            let lpf_i = lpf[i];
            for &j in pr.iter().take_while(|&&j| j <= lpf_i.min(n / i)) {
                lpf[i * j] = j;
            }
        }
        let mut lpf_e = vec![(1, 0); n + 1];
        for i in 2..=n {
            let p = lpf[i];
            let j = i / p;
            lpf_e[i] = if lpf[j] == p {
                (lpf_e[j].0 * p, lpf_e[j].1 + 1)
            } else {
                (lpf[i], 1)
            };
        }
        Self { lpf, lpf_e, pr }
    }

    pub fn is_prime(&self, i: usize) -> bool { i >= 2 && self.lpf[i] == i }
    pub fn lpf(&self, i: usize) -> Option<usize> {
        (i >= 2).then(|| self.lpf[i])
    }

    pub fn factors_dup(&self, i: usize) -> impl Iterator<Item = usize> + '_ {
        std::iter::successors(Some(i), move |&i| Some(i / self.lpf[i]))
            .take_while(|&i| i > 1)
            .map(move |i| self.lpf[i])
    }

    pub fn factors(&self, i: usize) -> impl Iterator<Item = (usize, u32)> + '_ {
        std::iter::successors(Some(i), move |&i| Some(i / self.lpf_e[i].0))
            .take_while(|&i| i > 1)
            .map(move |i| (self.lpf[i], self.lpf_e[i].1))
    }

    pub fn euler_phi(&self, i: usize) -> usize {
        std::iter::successors(Some(i), move |&i| Some(i / self.lpf_e[i].0))
            .take_while(|&i| i > 1)
            .map(|i| self.lpf_e[i].0 / self.lpf[i] * (self.lpf[i] - 1))
            .product()
    }

    pub fn euler_phi_star(&self, i: usize) -> usize {
        match i {
            0..=2 => i / 2,
            _ => 1 + self.euler_phi_star(self.euler_phi(i)),
        }
    }

    pub fn divisors(
        &self,
        i: usize,
    ) -> impl Iterator<Item = usize> + DoubleEndedIterator {
        let mut res = vec![1];
        for (p, e) in self.factors(i) {
            let mut tmp = vec![];
            let mut pp = 1;
            for _ in 1..=e {
                pp *= p;
                tmp.extend(res.iter().map(|&x| x * pp));
            }
            res.extend(tmp);
        }
        res.sort_unstable();
        res.into_iter()
    }

    pub fn divisors_count(&self, i: usize) -> usize {
        self.factors(i).map(|(_, e)| e as usize + 1).product()
    }

    pub fn divisors_sum(&self, i: usize) -> usize {
        std::iter::successors(Some(i), move |&i| Some(i / self.lpf_e[i].0))
            .take_while(|&i| i > 1)
            .map(|i| (self.lpf_e[i].0 * self.lpf[i] - 1) / (self.lpf[i] - 1))
            .product()
    }

    pub fn primes(
        &self,
    ) -> impl Iterator<Item = usize> + DoubleEndedIterator + '_ {
        self.pr.iter().copied()
    }

    pub fn dp<T>(
        &self,
        zero: T,
        one: T,
        eq: impl Fn(&T, usize) -> T,
        gt: impl Fn(&T, usize) -> T,
    ) -> Vec<T> {
        let n = self.lpf.len() - 1;

        let mut res = vec![zero, one];
        if n <= 1 {
            res.truncate(n + 1);
            return res;
        }

        res.reserve(n + 1);
        for i in 2..=n {
            let lpf = self.lpf[i];
            let j = i / lpf;
            let prev = &res[j];
            let cur =
                if lpf == self.lpf[j] { eq(prev, lpf) } else { gt(prev, lpf) };
            res.push(cur);
        }
        res
    }
}

#[test]
fn sanity_check() {
    let ls = LinearSieve::new(60);

    let primes =
        vec![2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59];

    assert!(ls.primes().eq(primes.iter().copied()));
    assert!((1..=60).all(|i| primes.contains(&i) == ls.is_prime(i)));

    assert_eq!(ls.lpf(1), None);
    assert_eq!(ls.lpf(3), Some(3));
    assert_eq!(ls.lpf(24), Some(2));

    assert!(ls.factors_dup(1).eq(None));
    assert!(ls.factors_dup(60).eq([2, 2, 3, 5]));

    assert!(ls.factors(1).eq(None));
    assert!(ls.factors(60).eq([(2, 2), (3, 1), (5, 1)]));

    assert_eq!(ls.euler_phi(1), 1);
    assert_eq!(ls.euler_phi(35), 24);
    assert_eq!(ls.euler_phi(60), 16);

    assert_eq!(ls.euler_phi_star(1), 0);
    assert_eq!(ls.euler_phi_star(35), 5);
    assert_eq!(ls.euler_phi_star(60), 5);

    assert!(ls.divisors(1).eq([1]));
    assert!(ls.divisors(60).eq([1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60]));

    assert_eq!(ls.divisors_count(1), 1);
    assert_eq!(ls.divisors_count(60), 12);

    assert_eq!(ls.divisors_sum(1), 1);
    assert_eq!(ls.divisors_sum(60), 168);
}

#[test]
fn dp() {
    let ls = LinearSieve::new(10);

    // Moebius mu
    let mu = ls.dp(0, 1, |_, _| 0, |&x, _| -x);
    assert_eq!(mu, [0, 1, -1, -1, 0, -1, 1, -1, 0, 0, 1]);

    // Euler phi
    let phi = ls.dp(0, 1, |&x, p| x * p, |&x, p| x * (p - 1));
    assert_eq!(phi, [0, 1, 1, 2, 2, 4, 2, 6, 4, 6, 4]);

    // # of distinct prime factors
    let omega = ls.dp(0, 0, |&x, _| x, |&x, _| x + 1);
    assert_eq!(omega, [0, 0, 1, 1, 1, 1, 2, 1, 1, 1, 2]);

    // # of prime factors
    let cap_omega = ls.dp(0, 0, |&x, _| x + 1, |&x, _| x + 1);
    assert_eq!(cap_omega, [0, 0, 1, 1, 2, 1, 2, 1, 3, 2, 2]);

    // sum of divisors
    let eq = |&(prod, sum, pow): &_, p| (prod, sum + pow * p, pow * p);
    let gt = |&(prod, sum, _): &_, p| (prod * sum, 1 + p, p);
    let sigma: [_; 11] =
        ls.dp((0, 0, 0), (1, 1, 1), eq, gt).try_into().unwrap();
    let sigma = sigma.map(|(prod, sum, _)| prod * sum);
    assert_eq!(sigma, [0, 1, 3, 4, 7, 6, 12, 8, 15, 13, 18]);
}