// 初期化配列 // 初期値とサイズを与えて適当にやる系 // new(size, zero): zero埋めした長さsizeの配列を返す // init(&mut self): 初期化 // index(mut) でアクセスしたときその履歴を溜め込む // それ以外でアクセスすると死ぬので注意 // // 考えるべきこと // 1. deref で dataへアクセスできるようにしていいか // derefmut はダメ // 2. 今のままだと二次元配列の初期化とかには対応できない // なんか方法を考えたい // ---------- begin init array ---------- #[derive(Clone)] pub struct InitArray { data: Vec, used: Vec, list: Vec, zero: T, } impl InitArray { pub fn new(zero: T, size: usize) -> Self { InitArray { data: vec![zero; size], used: vec![false; size], list: vec![], zero: zero, } } pub fn init(&mut self) { for x in self.list.drain(..) { self.used[x] = false; self.data[x] = self.zero; } } pub fn init_with(&mut self, mut f: F) where F: FnMut(usize, T), { for x in self.list.drain(..) { self.used[x] = false; let v = std::mem::replace(&mut self.data[x], self.zero); f(x, v); } } } impl std::ops::Index for InitArray { type Output = T; fn index(&self, pos: usize) -> &Self::Output { &self.data[pos] } } impl std::ops::IndexMut for InitArray { fn index_mut(&mut self, pos: usize) -> &mut Self::Output { if !self.used[pos] { self.used[pos] = true; self.list.push(pos); } &mut self.data[pos] } } // ---------- end init array ---------- // ---------- begin prime count ---------- // 処理が終わった時 // large[i]: pi(floor(n / i)) // small[i]: pi(i) // となっている // O(N^(3/4)/log N) pub fn prime_count(n: usize) -> (Vec, Vec) { if n <= 1 { return (vec![0, 0], vec![0, 0]); } let sqrt = (1..).find(|p| p * p > n).unwrap() - 1; let mut large = vec![0; sqrt + 1]; let mut small = vec![0; sqrt + 1]; for (i, (large, small)) in large.iter_mut().zip(&mut small).enumerate().skip(1) { *large = n / i - 1; *small = i - 1; } for p in 2..=sqrt { if small[p] == small[p - 1] { continue; } let pi = small[p] - 1; let q = p * p; for i in 1..=sqrt.min(n / q) { large[i] -= *large.get(i * p).unwrap_or_else(|| &small[n / (i * p)]) - pi; } for i in (q..=sqrt).rev() { small[i] -= small[i / p] - pi; } } (small, large) } // ---------- end prime count ---------- // ---------- begin enumerate prime ---------- fn enumerate_prime(n: usize, mut f: F) where F: FnMut(usize), { assert!(1 <= n && n <= 5 * 10usize.pow(8)); let batch = (n as f64).sqrt().ceil() as usize; let mut is_prime = vec![true; batch + 1]; for i in (2..).take_while(|p| p * p <= batch) { if is_prime[i] { let mut j = i * i; while let Some(p) = is_prime.get_mut(j) { *p = false; j += i; } } } let mut prime = vec![]; for (i, p) in is_prime.iter().enumerate().skip(2) { if *p && i <= n { f(i); prime.push(i); } } let mut l = batch + 1; while l <= n { let r = std::cmp::min(l + batch, n + 1); is_prime.clear(); is_prime.resize(r - l, true); for &p in prime.iter() { let mut j = (l + p - 1) / p * p - l; while let Some(is_prime) = is_prime.get_mut(j) { *is_prime = false; j += p; } } for (i, _) in is_prime.iter().enumerate().filter(|p| *p.1) { f(i + l); } l += batch; } } // ---------- end enumerate prime ---------- use std::marker::*; use std::ops::*; pub trait Modulo { fn modulo() -> u32; } pub struct ConstantModulo; impl Modulo for ConstantModulo<{ M }> { fn modulo() -> u32 { M } } pub struct ModInt(u32, PhantomData); impl Clone for ModInt { fn clone(&self) -> Self { Self::new_unchecked(self.0) } } impl Copy for ModInt {} impl Add for ModInt { type Output = ModInt; fn add(self, rhs: Self) -> Self::Output { let mut v = self.0 + rhs.0; if v >= T::modulo() { v -= T::modulo(); } Self::new_unchecked(v) } } impl AddAssign for ModInt { fn add_assign(&mut self, rhs: Self) { *self = *self + rhs; } } impl Sub for ModInt { type Output = ModInt; fn sub(self, rhs: Self) -> Self::Output { let mut v = self.0 - rhs.0; if self.0 < rhs.0 { v += T::modulo(); } Self::new_unchecked(v) } } impl SubAssign for ModInt { fn sub_assign(&mut self, rhs: Self) { *self = *self - rhs; } } impl Mul for ModInt { type Output = ModInt; fn mul(self, rhs: Self) -> Self::Output { let v = self.0 as u64 * rhs.0 as u64 % T::modulo() as u64; Self::new_unchecked(v as u32) } } impl MulAssign for ModInt { fn mul_assign(&mut self, rhs: Self) { *self = *self * rhs; } } impl Neg for ModInt { type Output = ModInt; fn neg(self) -> Self::Output { if self.is_zero() { Self::zero() } else { Self::new_unchecked(T::modulo() - self.0) } } } impl std::fmt::Display for ModInt { fn fmt<'a>(&self, f: &mut std::fmt::Formatter<'a>) -> std::fmt::Result { write!(f, "{}", self.0) } } impl std::fmt::Debug for ModInt { fn fmt<'a>(&self, f: &mut std::fmt::Formatter<'a>) -> std::fmt::Result { write!(f, "{}", self.0) } } impl Default for ModInt { fn default() -> Self { Self::zero() } } impl std::str::FromStr for ModInt { type Err = std::num::ParseIntError; fn from_str(s: &str) -> Result { let val = s.parse::()?; Ok(ModInt::new(val)) } } impl From for ModInt { fn from(val: usize) -> ModInt { ModInt::new_unchecked((val % T::modulo() as usize) as u32) } } impl From for ModInt { fn from(val: u64) -> ModInt { ModInt::new_unchecked((val % T::modulo() as u64) as u32) } } impl ModInt { pub fn new_unchecked(n: u32) -> Self { ModInt(n, PhantomData) } pub fn zero() -> Self { ModInt::new_unchecked(0) } pub fn one() -> Self { ModInt::new_unchecked(1) } pub fn is_zero(&self) -> bool { self.0 == 0 } } impl ModInt { pub fn new(d: u32) -> Self { ModInt::new_unchecked(d % T::modulo()) } pub fn pow(&self, mut n: u64) -> Self { let mut t = Self::one(); let mut s = *self; while n > 0 { if n & 1 == 1 { t *= s; } s *= s; n >>= 1; } t } pub fn inv(&self) -> Self { assert!(!self.is_zero()); self.pow(T::modulo() as u64 - 2) } } type M = ModInt>; use std::collections::*; type Map = HashMap; fn read() -> (usize, usize) { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let a = s .trim() .split_whitespace() .flat_map(|s| s.parse()) .collect::>(); (a[0], a[1]) } fn solve(n: usize, m: usize) { let mut pow = vec![M::zero(); 40 + 1]; for i in 1..=40 { pow[i] = M::from(i).pow(n as u64); } let sqrt = (2..).find(|p| p * p > m).unwrap() - 1; let pi = prime_count(m); let pi = |x: usize| -> usize { assert!(x <= m); if x <= sqrt { return pi.0[x]; } let q = m / x; let r = m / q; assert_eq!(x, r); pi.1[q] }; let mut prime = vec![]; enumerate_prime(sqrt + 200, |p| prime.push(p)); let mut large = InitArray::new(M::zero(), sqrt + 1); let mut small = InitArray::new(M::zero(), sqrt + 1); let mut next_large = InitArray::new(M::zero(), sqrt + 1); let mut next_small = InitArray::new(M::zero(), sqrt + 1); large[1] = M::one(); let mut ans = M::zero(); for (i, &p) in prime.iter().enumerate() { large.init_with(|d, v| { let m = m / d; if p.pow(2) > m { ans += v * M::from(m); let mut l = p; let mut q = m / l; while q > 0 { let r = m / q; let cnt = pi(r) - if l == p { i } else { pi(l - 1) }; ans += v * M::from(q * cnt) * (pow[2] - M::one()); l = r + 1; q -= 1; } } else { for k in (0usize..).take_while(|k| p.pow(*k as u32) <= m) { let v = v * (pow[k + 1] - pow[k]); let pp = p.pow(k as u32); if pp * d <= sqrt { next_large[d * pp] += v; } else { next_small[m / pp] += v; } } } }); small.init_with(|m, v| { if p.pow(2) > m { ans += v * M::from(m); let mut l = p; let mut q = m / l; while q > 0 { let r = m / q; let cnt = pi(r) - if l == p { i } else { pi(l - 1) }; ans += v * M::from(q * cnt) * (pow[2] - M::one()); l = r + 1; q -= 1; } } else { for k in (0usize..).take_while(|k| p.pow(*k as u32) <= m) { let v = v * (pow[k + 1] - pow[k]); let pp = p.pow(k as u32); next_small[m / pp] += v; } } }); std::mem::swap(&mut small, &mut next_small); std::mem::swap(&mut large, &mut next_large); } println!("{}", ans); } fn naive(n: usize, m: usize) { let mut dp = vec![M::one(); m + 1]; enumerate_prime(m, |p| { for i in 1..=(m / p) { dp[p * i] = dp[p * i] + dp[i]; } }); for dp in dp.iter_mut() { *dp = dp.pow(n as u64); } enumerate_prime(m, |p| { for i in (1..=(m / p)).rev() { dp[p * i] = dp[p * i] - dp[i]; } }); let mut ans = M::zero(); for i in 1..=m { ans += M::from(m / i) * dp[i]; } println!("{}", ans); } fn main() { let (n, m) = read(); solve(n, m); }