tinycodinglm / cp2 /cp-0037.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
12 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-08T10:20:03.537Z

Geometric Chains

Topic: Hashing and counting (maps/sets) | Difficulty: medium

Problem

Given a sequence a_1..a_n of positive integers and a ratio r, count the index triples i < j < k such that a_j = a_i * r and a_k = a_j * r.

Input

The first line contains n and r. The second line contains n integers.

Output

One integer, the number of triples.

Constraints

  • 1 <= n <= 2*10^5
  • 1 <= r <= 10^9
  • 1 <= a_i <= 10^9

Examples

Example 1

Input:

markdown
4 2
1 2 2 4

Output:

markdown
2

Example 2

Input:

markdown
4 1
5 5 5 5

Output:

markdown
4

Note: With r = 1 every triple of equal values counts: C(4,3) = 4.

Example 3

Input:

markdown
5 3
1 3 9 3 9

Output:

markdown
3

Solve this task. Write solutions in TypeScript, Python 3, Rust, 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-08T10:20:05.511Z

Writing the TypeScript solution and testing it on the examples.

Assistant 2026-11-08T10:20:09.870Z

Saving the example inputs.

3 tool calls (write_file)
Assistant 2026-11-08T10:20:18.432Z

Compiling the TypeScript solution.

Assistant 2026-11-08T10:20:22.635Z

Running every example.

3 tool calls (run_shell)
Assistant 2026-11-08T10:20:31.785Z

Cross-checking with the Python 3 version.

Assistant 2026-11-08T10:20:35.832Z

Running every example.

3 tool calls (run_shell)
Assistant 2026-11-08T10:20:44.670Z

Cross-checking with the Rust version.

Assistant 2026-11-08T10:20:48.561Z

Compiling the Rust solution.

Assistant 2026-11-08T10:20:53.000Z

Running every example.

3 tool calls (run_shell)
Assistant 2026-11-08T10:21:01.722Z

Approach. Fix the middle index j. Maintain a map of counts of values before j and a map of counts after j, and add left[a_j / r] * right[a_j * r] when r divides a_j.

Correctness. Every valid triple has a unique middle index, and the product of the two counts is the number of choices of the outer indices on each side.

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

All solutions below passed every example.

TypeScript

typescript
declare const require: any;
const t: number[] = require('fs').readFileSync(0, 'utf8').split(/\s+/).filter((x: string) => x.length).map(Number);
const n = t[0], r = t[1];
const a: number[] = t.slice(2, 2 + n);
const left = new Map<number, number>();
const right = new Map<number, number>();
for (const v of a) right.set(v, (right.get(v) || 0) + 1);
let ans = 0;
for (const v of a) {
  right.set(v, right.get(v)! - 1);
  if (v % r === 0) {
    ans += (left.get(v / r) || 0) * (right.get(v * r) || 0);
  }
  left.set(v, (left.get(v) || 0) + 1);
}
console.log(String(ans));

Python 3

python
import sys
from collections import Counter
data = sys.stdin.read().split()
n, r = int(data[0]), int(data[1])
a = list(map(int, data[2:2 + n]))
right = Counter(a)
left = Counter()
ans = 0
for v in a:
    right[v] -= 1
    if v % r == 0:
        ans += left[v // r] * right[v * r]
    left[v] += 1
print(ans)

Rust

rust
use std::collections::HashMap;
use std::io::*;

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    let t: Vec<u64> = s.split_whitespace().map(|x| x.parse().unwrap()).collect();
    let n = t[0] as usize;
    let r = t[1];
    let a = &t[2..2 + n];
    let mut right: HashMap<u64, u64> = HashMap::new();
    for &v in a {
        *right.entry(v).or_insert(0) += 1;
    }
    let mut left: HashMap<u64, u64> = HashMap::new();
    let mut ans: u64 = 0;
    for &v in a {
        *right.get_mut(&v).unwrap() -= 1;
        if v % r == 0 {
            let l = *left.get(&(v / r)).unwrap_or(&0);
            let rr = *right.get(&(v * r)).unwrap_or(&0);
            ans += l * rr;
        }
        *left.entry(v).or_insert(0) += 1;
    }
    println!("{}", ans);
}