tinycodinglm / cp2 /cp-0005.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
11.2 kB
cp-practice-sim trace
System You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Give only a concise rationale: approach, correctness idea, and time/space com...

You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Give only a concise rationale: approach, correctness idea, and time/space complexity. Tools: write_file(path, content) writes a UTF-8 file (creating directories); run_shell(command) runs a bash command and returns its output and exit status. Test solutions on the provided examples before answering.

User 2026-11-07T02:20:03.537Z

Balanced Crate Stretches

Topic: Arrays and prefix sums | Difficulty: medium

Problem

A conveyor carries n crates; crate i has signed weight a[i] (negative means a counterweight). A stretch is a non-empty contiguous block of crates. A stretch is balanced if its total weight is divisible by k. Count the balanced stretches.

Input

The first line contains n and k. The second line contains n integers a[1..n].

Output

Print the number of balanced stretches.

Constraints

  • 1 <= n <= 2*10^5
  • 1 <= k <= 10^9
  • |a[i]| <= 10^9

Examples

Example 1

Input:

markdown
6 5
4 5 0 -2 -3 1

Output:

markdown
7

Example 2

Input:

markdown
3 1
1 2 3

Output:

markdown
6

Note: With k = 1 every stretch is balanced, so the answer is n(n+1)/2.

Solve this task. Write solutions in Go, Kotlin, Java 17, test what you can run on the examples, and send back the final programs. Include a concise rationale (approach, correctness idea, time/space complexity).

Assistant 2026-11-07T02:20:05.511Z

Writing the Go solution and testing it on the examples.

Assistant 2026-11-07T02:20:09.870Z

Saving the example inputs.

2 tool calls (write_file)
Assistant 2026-11-07T02:20:16.536Z

Compiling the Go solution.

Assistant 2026-11-07T02:20:20.465Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T02:20:27.386Z

Cross-checking with the Kotlin version.

Assistant 2026-11-07T02:20:31.785Z

Compiling the Kotlin solution.

Assistant 2026-11-07T02:20:35.832Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T02:20:42.930Z

Cross-checking with the Java 17 version.

Assistant 2026-11-07T02:20:46.547Z

Compiling the Java 17 solution.

Assistant 2026-11-07T02:20:50.712Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T02:20:57.987Z

Approach. Track prefix sums modulo k (normalised to be non-negative) and count how many earlier prefixes share each residue. Each new prefix adds that count to the answer.

Correctness. A stretch (i, j] has sum divisible by k exactly when P[j] and P[i] are congruent mod k, so the answer is the number of equal-residue pairs among the n+1 prefixes.

Complexity. Time: O(n). Space: O(n).

All solutions below passed every example.

Go

go
package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    in := bufio.NewReaderSize(os.Stdin, 1<<20)
    var n int
    var k int64
    fmt.Fscan(in, &n, &k)
    cnt := map[int64]int64{0: 1}
    var cur, ans int64
    for i := 0; i < n; i++ {
        var x int64
        fmt.Fscan(in, &x)
        cur = ((cur+x)%k + k) % k
        ans += cnt[cur]
        cnt[cur]++
    }
    fmt.Println(ans)
}

Kotlin

kotlin
import java.util.StringTokenizer

fun main() {
    val st = StringTokenizer(System.`in`.bufferedReader().readText())
    val n = st.nextToken().toInt()
    val k = st.nextToken().toLong()
    val cnt = HashMap<Long, Long>()
    cnt[0L] = 1L
    var cur = 0L
    var ans = 0L
    repeat(n) {
        val x = st.nextToken().toLong()
        cur = ((cur + x) % k + k) % k
        val c = cnt.getOrDefault(cur, 0L)
        ans += c
        cnt[cur] = c + 1
    }
    println(ans)
}

Java 17

java
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer st = new StreamTokenizer(new BufferedInputStream(System.in));
        st.nextToken();
        int n = (int) st.nval;
        st.nextToken();
        long k = (long) st.nval;
        HashMap<Long, Long> cnt = new HashMap<>();
        cnt.put(0L, 1L);
        long cur = 0, ans = 0;
        for (int i = 0; i < n; i++) {
            st.nextToken();
            long x = (long) st.nval;
            cur = ((cur + x) % k + k) % k;
            long c = cnt.getOrDefault(cur, 0L);
            ans += c;
            cnt.put(cur, c + 1);
        }
        System.out.println(ans);
    }
}