fn main() { input!(n: usize); let sieve = Sieve::new(2000000); let mut p = vec![]; let mut a = vec![]; let mut ans = 0; for m in 2.. { if 2 * m * (m + 1) > n { break; } a.clear(); let n = n / (2 * m); let up = (m - 1).min(n - m); let mut l = 1; while l <= up { let q = n / (m + l); let r = n / q - m; a.push((l, r.min(up), q)); l = r + 1; } sieve.factorize(m, &mut p); let mul = if m % 2 == 0 {1} else {2}; for (i, f) in p.iter().map(|p| *p * mul).enumerate().filter(|p| p.1 <= up) { let mut pre = 0; let mut prun = f; let mut sum = 0; for &(_, r, q) in a.iter() { if r >= prun { let u = r / f; sum += q * (u - pre); pre = u; prun = u * f + f; } } if i.count_ones() % 2 == 0 { ans += sum; } else { ans -= sum; } } } println!("{ans}"); } fn test() { let n = 10usize.pow(12); let sieve = Sieve::new(2000000); let mut p = vec![]; let mut ans = 0; let mut sum = 0; for m in 2.. { if 2 * m * (m + 1) > n { break; } let mut l = 1; let mut c = 0; while l <= m - 1 { let q = n / (2 * m * (m + l)); let r = n / (2 * m * q); l = r + 1; c += 1; } p.clear(); sieve.factorize(m, &mut p); sum += c * p.len(); /* if m % 2 == 0 { let up = (m - 1).min(n / (2 * m) - m); for (i, f) in p.iter().enumerate() { if i.count_ones() % 2 == 0 { ans += up / *f; } else { ans -= up / *f; } } } else { let up = (m - 1).min(n / (2 * m) - m) / 2; for (i, f) in p.iter().enumerate() { if i.count_ones() % 2 == 0 { ans += up / *f; } else { ans -= up / *f; } } } */ } println!("{}", sum); } // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 #[macro_export] macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } #[macro_export] macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } #[macro_export] macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- // --------- end sieve ---------- pub struct Sieve { size: usize, factor: Vec, } impl Sieve { pub fn new(size: usize) -> Sieve { let mut factor = (0..(size + 1)).collect::>(); for i in (2..).take_while(|p| p * p <= size) { if i == factor[i] { for j in i..(size / i + 1) { factor[j * i] = i; } } } Sieve { size: size, factor: factor, } } pub fn factor(&self, n: usize) -> Option { assert!(n <= self.size); if n == 1 { None } else { Some(self.factor[n]) } } pub fn factorize(&self, mut n: usize, res: &mut Vec) { assert!(n <= self.size); res.clear(); res.push(1); while let Some(p) = self.factor(n) { let len = res.len(); while n % p == 0 { n /= p; } for _ in 0..len { let v = res[res.len() - len] * p; res.push(v); } } } } // --------- end sieve ----------