Scala
\nobject Solution {\n  def countDistinct(nums: Array[Int], k: Int, p: Int): Int = {\n    val cumulativeDivisible = nums.scanLeft(0) {\n      case (numDivisible,
```\nobject Solution {\n  def countDistinct(nums: Array[Int], k: Int, p: Int): Int = {\n    val cumulativeDivisible = nums.scanLeft(0) {\n      case (numDivisible, num) if num % p == 0 => numDivisible + 1\n      case (numDivisible, _) => numDivisible\n    }\n    val view = nums.view\n    (1 to nums.length)\n      .iterator\n      .map { length =>\n        (0 to nums.length - length)\n          .iterator\n          .collect { case i if cumulativeDivisible(i + length) - cumulativeDivisible(i) <= k => (i, i + length) }\n          .distinctBy { case (from, until) => view.slice(from, until).toSeq }\n          .size\n      }\n      .sum\n  }\n}\n```
['Prefix Sum', 'Scala']