pub struct HarmonicFloorSum { /* private fields */ }
Expand description

i=1nm/i\sum_{i=1}^n \lfloor m/i\rfloor および i=1n(mmodi)\sum_{i=1}^n (m\bmod i).

Idea

m/\lfloor m/\bullet\rfloor の値は O(m)O(\sqrt{m}) 通りである。 i[ql,qr]i\in[q_l, q_r] において m/i\lfloor m/i\rfloor の値が共通であるとき、 i=qlqrm/i\sum_{i=q_l}^{q_r} \lfloor m/i\rfloor の値は簡単に求められる。 また、この範囲で (mmodi)(m\bmod i) は等差数列を成すことから、 i=qlqr(mmodi)\sum_{i=q_l}^{q_r} (m\bmod i) も簡単に求められる。 前計算でこれらの累積和を求めておき、差分計算によってクエリ処理を行う。

Notes

考察を進めれば i=1nmai+b\sum_{i=1}^n \lfloor\frac{m}{ai+b}\rfloor を求めることも可能?

Complexity

O(m)O(\sqrt{m}) preprocess, O(1)O(1) query time.

Examples

use nekolib::math::HarmonicFloorSum;

let m = 100;
let hs = HarmonicFloorSum::new(m);
assert_eq!(hs.quot(1..=m), (1..=m).map(|i| m / i).sum());
assert_eq!(hs.rem(1..=m), (1..=m).map(|i| m % i).sum());

let n = 60;
assert_eq!(hs.quot(..=n), (1..=n).map(|i| m / i).sum());

Implementations§

source§

impl HarmonicFloorSum

source

pub fn new(m: usize) -> Self

前処理を行う。

Examples
use nekolib::math::HarmonicFloorSum;

let m = 100;
let hs = HarmonicFloorSum::new(m);
source

pub fn quot(&self, r: impl RangeBounds<usize>) -> usize

i=sem/i\sum_{i=s}^e \lfloor m/i\rfloor を返す。

i=sm/i=i=smm/i\sum_{i=s}^{\infty} \lfloor m/i\rfloor = \sum_{i=s}^m \lfloor m/i\rfloor なので、上限を指定しない場合は mm までの和を求める。下限を指定しない場合は 11 からの和を求める。

Examples
use nekolib::math::HarmonicFloorSum;

let m = 100;
let hs = HarmonicFloorSum::new(m);
assert_eq!(hs.quot(1..=m), (1..=m).map(|i| m / i).sum());
assert_eq!(hs.quot(..), (1..=m).map(|i| m / i).sum());
assert_eq!(hs.quot(1..=m), hs.quot(1..=m + 1));
source

pub fn rem(&self, r: impl RangeBounds<usize>) -> usize

i=se(mmodi)\sum_{i=s}^e (m\bmod i) を返す。

下限を指定しない場合は 11 からの和を求める。

Panics

i=s(mmodi)=\sum_{i=s}^{\infty} (m\bmod i) = \infty なので、上限が Unbounded の場合は panic する。

Examples
use nekolib::math::HarmonicFloorSum;

let m = 100;
let hs = HarmonicFloorSum::new(m);
assert_eq!(hs.rem(1..=m), (1..=m).map(|i| m % i).sum());
assert_eq!(hs.rem(..=m), (1..=m).map(|i| m % i).sum());
assert_ne!(hs.rem(1..=m), hs.rem(1..=m + 1)); // m % (m + 1) = m > 0
use nekolib::math::HarmonicFloorSum;
let m = 100;
let hs = HarmonicFloorSum::new(m);
let infty = hs.rem(1..); // diverges

Trait Implementations§

source§

impl Clone for HarmonicFloorSum

source§

fn clone(&self) -> HarmonicFloorSum

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for HarmonicFloorSum

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V