read_line.to_i.times do puts solve() ? "Yes" : "No" end def solve n, k, x = read_line.split.map(&.to_i) h = read_line.split.map(&.to_i64) min = h.min h.map! { |v| v - min } if h.any? { |v| v % x != 0 } return false end h.map! { |v| v // x } diff = Array.new(n - 1) { |i| h[i + 1] - h[i] } ok = true k.times.all? do |i| if i == k - 1 && (diff.size - i) % k == 0 next end c = 0i64 if i == k - 1 c = -(1i64 << 50) end i.step(to: diff.size - 1, by: k) do |j| if c <= 0 c = diff[j] + c else ok = false break end end if i == k - 1 if c > -(1i64 << 50) ok = false end elsif (diff.size - i) % k == 0 if c < 0 ok = false end else if c != 0 ok = false end end if !ok break end end ok end