Looking at this problem, I need to figure out which balls eventually end up in a pocket (corner of the square table).
Key insight: A ball moving at 45 degrees on a square table can be analyzed using the reflection/unfolding technique. Instead of bouncing the ball, we unfold the table.
A ball starts at (x, y) with direction (dx, dy) where dx, dy ∈ {-1, 1}. After unfolding reflections, the ball travels in a straight line. It reaches a corner when both coordinates simultaneously hit multiples of s.
After time t, the ball is at position (x + dx*t, y + dy*t) in the unfolded plane. For it to be at a corner of the original table, we need:
- (x + dx*t) ≡ 0 (mod s), meaning x + dx*t is a multiple of s
- (y + dy*t) ≡ 0 (mod s), meaning y + dy*t is a multiple of s
So we need: x + dx*t = a*s and y + dy*t = b*s for some integers a, b, with t > 0.
From these: t = (a*s - x)/dx and t = (b*s - y)/dy
So (a*s - x)/dx = (b*s - y)/dy
This means: dy*(a*s - x) = dx*(b*s - y)
Since dx, dy ∈ {-1,1}: dy*a*s - dy*x = dx*b*s - dx*y
We need t > 0, so a*s - x has the same sign as dx, and b*s - y has the same sign as dy.
For t to exist (ball reaches a corner at some finite time), we need the equation dy*(a*s - x) = dx*(b*s - y) to have integer solutions a, b with t > 0.
Rearranging: dy*a*s - dx*b*s = dy*x - dx*y
s*(dy*a - dx*b) = dy*x - dx*y
Let me set u = dy*a - dx*b. Then s*u = dy*x - dx*y.
So we need s | (dy*x - dx*y).
If (dy*x - dx*y) % s == 0, then there exists such u, and we can find a, b. We also need t > 0, which means a*s > x (if dx=1) or a*s < x (if dx=-1), similarly for b.
Since u = (dy*x - dx*y)/s, we have dy*a - dx*b = u. There are infinitely many solutions (a,b), so we can always find one giving t > 0.
Wait, but we need t to be a positive integer (or positive value). Since t = (a*s - x) * dx (when dx=±1, dividing by dx is multiplying by dx), t = dx*(a*s - x). We need this positive. For any valid u, we can choose a freely (and b = (dy*a - u)/dx), so we can make t as large as we want. Hence if the divisibility condition holds, the ball is potted.
So the answer is: count balls where (dy*x - dx*y) mod s == 0.
Let me verify with examples:
- Test 1: dx=1, dy=1, x=1, y=1, s=2. dy*x - dx*y = 1-1 = 0. 0 % 2 = 0. ✓ Potted.
- Test 2, ball 1: dx=1,dy=-1,x=1,y=1,s=4. -1-1=-2. -2%4 ≠ 0. Not potted.
- Ball 2: dx=1,dy=-1,x=2,y=2. -2-2=-4. -4%4=0. Potted.
- Ball 3: dx=-1,dy=1,x=2,y=3. 2-(-3)=2+3=5. 5%4≠0. Not potted.
- Ball 4: dx=1,dy=-1,x=1,y=3. -1-3=-4. -4%4=0. Potted.
- Ball 5: dx=-1,dy=1,x=3,y=1. 3-(-1)=3+1=4. 4%4=0. Potted.
That gives 3 potted. ✓
```go
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var t int
fmt.Fscan(reader, &t)
for ; t > 0; t-- {
var n, s int
fmt.Fscan(reader, &n, &s)
count := 0
for i := 0; i < n; i++ {
var dx, dy, x, y int
fmt.Fscan(reader, &dx, &dy, &x, &y)
val := (dy*x - dx*y) % s
if val < 0 {
val += s
}
if val == 0 {
count++
}
}
fmt.Fprintln(writer, count)
}
}
```