← Home
For problem statement at 2000-2999/2000-2099/2090-2099/2097/problemC.txt this is a correct solution, but verifier at 2000-2999/2000-2099/2090-2099/2097/verifierC.go ends with deterministic test 1 failed: case 1: expected 3 got 2
input:
6
6 2 2 5 2
6 2 2 20 8
4 1 2 1 1
4 1 1 1 2
4 1 1 2 1
6 2 3 2 3 can you fix the verifier? package main

import (
	"bufio"
	"io"
	"os"
	"strconv"
)

var data []byte
var idx int

func nextInt64() int64 {
	for idx < len(data) {
		c := data[idx]
		if c != ' ' && c != '\n' && c != '\r' && c != '\t' {
			break
		}
		idx++
	}
	sign := int64(1)
	if data[idx] == '-' {
		sign = -1
		idx++
	}
	var n int64
	for idx < len(data) {
		c := data[idx]
		if c < '0' || c > '9' {
			break
		}
		n = n*10 + int64(c-'0')
		idx++
	}
	return sign * n
}

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	if a < 0 {
		return -a
	}
	return a
}

func exgcd(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	g, x1, y1 := exgcd(b, a%b)
	return g, y1, x1-(a/b)*y1
}

func mod(a, m int64) int64 {
	a %= m
	if a < 0 {
		a += m
	}
	return a
}

func modInv(a, m int64) int64 {
	_, x, _ := exgcd(a, m)
	return mod(x, m)
}

func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

func main() {
	data, _ = io.ReadAll(os.Stdin)
	t := int(nextInt64())
	out := bufio.NewWriterSize(os.Stdout, 1<<20)
	defer out.Flush()

	for ; t > 0; t-- {
		n := nextInt64()
		x := nextInt64()
		y := nextInt64()
		vx := nextInt64()
		vy := nextInt64()

		g := gcd(vx, vy)
		den := n * g
		num := vx*y - vy*x

		if num%den != 0 {
			out.WriteString("-1\n")
			continue
		}

		m := num / den
		a := vx / g
		b := vy / g

		var K, L int64
		if a == 1 {
			K = 1
			L = b + m
		} else {
			inv := modInv(mod(b, a), a)
			K = mod(mod(-m, a)*inv, a)
			if K == 0 {
				K = a
			}
			L = (b*K + m) / a
		}

		ans := K + L - 2 + max(K, L) - ((K + L) & 1)
		out.WriteString(strconv.FormatInt(ans, 10))
		out.WriteByte('\n')
	}
}