Problem · Array

Configuration System

Learn this problem
HardPostman logoPostmanFULLTIMEOA

Problem statement

A building company offers wall side lengths from a strictly increasing sequence s. Generate n lengths beginning with s[0] = s0. For each 1 ≤ i < n:

s[i] = ((k * s[i - 1] + b) mod m) + 1 + s[i - 1].

A rectangular house configuration chooses an ordered pair of offered lengths (s[i], s[j]). The ceiling is painted for free when s[i] * s[j] ≤ a.

Return the number of ordered configurations that qualify. Because orientation matters, (x, y) and (y, x) are counted separately when x != y.

Function

variantsCount(n: int, s0: int, k: int, b: int, m: int, a: long) → long

Examples

Example 1

n = 3s0 = 1k = 1b = 1m = 2a = 4return = 6

The generated lengths are [1, 2, 4]. The qualifying ordered pairs are (1,1), (1,2), (1,4), (2,1), (2,2), and (4,1).

Example 2

n = 1s0 = 2k = 3b = 4m = 5a = 3return = 0

The only configuration has area 2 * 2 = 4, which is greater than 3.

Constraints

  • 1 ≤ n ≤ 6 × 10^7
  • 1 ≤ s[i] ≤ 10^9 for every generated value.
  • 1 ≤ k, b, m ≤ 10^9
  • 1 ≤ a ≤ 10^18
  • The answer fits in a signed 64-bit integer.

More Postman problems

drafts saved locally
public long variantsCount(int n, int s0, int k, int b, int m, long a) {
    // write your code here
}
n3
s01
k1
b1
m2
a4
expected6
checking account