Function floor_sum

Source
pub fn floor_sum(n: u64, a: u64, b: u64, m: u64) -> u64
Expand description

Sum of Floor of Linear mod 2^64

$$\sum_{i=0}^{n-1}\left\lfloor\frac{a\times i+b}{m}\right\rfloor$$

Examples found in repository?
crates/library_checker/src/math/sum_of_floor_of_linear.rs (line 11)
6pub fn sum_of_floor_of_linear(reader: impl Read, mut writer: impl Write) {
7    let s = read_all_unchecked(reader);
8    let mut scanner = Scanner::new(&s);
9    scan!(scanner, t, query: [(u64, u64, u64, u64)]);
10    for (n, m, a, b) in query.take(t) {
11        writeln!(writer, "{}", floor_sum(n, a, b, m)).ok();
12    }
13}
More examples
Hide additional examples
crates/competitive/src/math/floor_sum.rs (line 75)
52pub fn floor_sum_i64(l: i64, r: i64, a: i64, b: i64, m: u64) -> i64 {
53    let mut ans = Wrapping(0i64);
54    let (n, m, a, b) = (
55        Wrapping((r - l) as u64),
56        m as i64,
57        a,
58        (Wrapping(l) * a + b).0,
59    );
60    let a = if a < 0 {
61        let r = a.rem_euclid(m);
62        let nc2 = choose2(n);
63        ans -= Wrapping(nc2.0 as _) * ((Wrapping(r) - a) / m);
64        r
65    } else {
66        a
67    };
68    let b = if b < 0 {
69        let r = b.rem_euclid(m);
70        ans -= Wrapping(n.0 as _) * ((Wrapping(r) - b) / m);
71        r
72    } else {
73        b
74    };
75    (ans + floor_sum(n.0, a as u64, b as u64, m as u64) as i64).0
76}