// m^2-n^2, 2mn, m^2+n^2 // m>n // m, nは互いに素かつ偶奇が違う // 2m(m+n) // // N/(2km(m+n)) // の総和 // // 1 <= n <= m-1 について商列挙っぽいことするとどうなる? // N=L の時Qとして // Q = N/(2m(m+n)) // m+n= N/(2mQ) // n=N/(2mQ)-m // 4secならギリギリ間に合うか? 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 up = (m - 1).min(n / (2 * m) - m); let mut l = 1; while l <= up { let q = n / (2 * m * (m + l)); let r = n / (2 * m * q) - m; a.push((l as u32 - 1, r.min(up) as u32, q)); l = r + 1; } sieve.factorize(m, &mut p); if m % 2 == 0 { for (i, &f) in p.iter().enumerate() { let f = f as u32; let mut pre = 0; if i.count_ones() % 2 == 0 { for &(_, r, q) in a.iter().filter(|p| f <= p.1) { let u = r / f; ans += q * (u - pre) as usize; pre = u; } } else { for &(_, r, q) in a.iter().filter(|p| f <= p.1) { let u = r / f; ans -= q * (u - pre) as usize; pre = u; } } } } else { for (i, f) in p.iter().enumerate() { let f = *f as u32 * 2; let mut pre = 0; if i.count_ones() % 2 == 0 { for &(_, r, q) in a.iter().filter(|p| f <= p.1) { let u = r / f; ans += q * (u - pre) as usize; pre = u; } } else { for &(_, r, q) in a.iter().filter(|p| f <= p.1) { let u = r / f; ans -= q * (u - pre) as usize; pre = u; } } } } } 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 ----------