← Home
For problem statement at 0-999/700-799/770-779/773/problemA.txt this is a correct solution, but verifier at 0-999/700-799/770-779/773/verifierA.go ends with mismatch on test 1: expected 1023009080 got -1
exit status 1 can you fix the verifier? ```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 x, y, p, q int64
		fmt.Fscan(reader, &x, &y, &p, &q)

		// We need to find minimum additional submissions `d` such that:
		// There exist a, b >= 0 where a + b = d
		// (x + a) / (y + d) = p / q
		// i.e., (x + a) * q = p * (y + d)
		// 
		// We need k such that k*p >= x, k*q >= y
		// a = k*p - x >= 0
		// b = k*q - y - a = k*q - y - (k*p - x) = k*(q-p) - (y-x) >= 0
		// additional submissions = k*q - y
		//
		// So we need:
		// k*p >= x => k >= ceil(x/p) if p > 0, else x must be 0
		// k*q >= y => k >= ceil(y/q)
		// k*(q-p) >= y-x => if q-p > 0: k >= ceil((y-x)/(q-p)), else y-x must be 0
		//
		// Binary search on k

		// Check if it's possible at all
		// If p = 0: x must remain 0 (can't decrease x), so x must be 0... wait
		// Actually x can't decrease, so if p=0 we need x+a=0 => a=0 => x=0
		// If p=q (i.e., p/q=1): x+a = y+d, and a<=d, so we need x+(d-b)=y+d => x-b=y => b=x-y
		// but b>=0 so y<=x, but x<=y so x=y. Otherwise impossible.

		// Binary search for minimum k
		// k must satisfy:
		// 1. k*p >= x
		// 2. k*(q-p) >= y-x  (since q-p >= 0 and y-x >= 0)
		// 3. k*q >= y

		// Find minimum k >= 1 such that all conditions are met
		// Then answer is k*q - y

		// Edge case: p=0 => condition 1: 0>=x => x must be 0
		// condition 2: k*(q-0)>=y-x => k*q>=y => same as condition 3
		// Edge case: p=q => condition 2: 0>=y-x => y<=x => x=y
		// condition 1: k*q>=x=y => same as condition 3

		check := func(k int64) bool {
			if p > 0 && k*p < x {
				return false
			}
			if p == 0 && x > 0 {
				return false
			}
			qmp := q - p
			yMx := y - x
			if qmp > 0 && k*qmp < yMx {
				return false
			}
			if qmp == 0 && yMx > 0 {
				return false
			}
			if k*q < y {
				return false
			}
			return true
		}

		if !check(1<<62 - 1) {
			// impossible
			// check if truly impossible
			// p=0 and x>0: impossible
			// p=q and y>x: impossible
			fmt.Fprintln(writer, -1)
			continue
		}

		// Binary search for minimum k
		lo, hi := int64(1), int64(2e9+10)
		// but we need k >= 0, let's start from 0
		lo = 0
		for lo < hi {
			mid := (lo + hi) / 2
			if check(mid) {
				hi = mid
			} else {
				lo = mid + 1
			}
		}

		if !check(lo) {
			fmt.Fprintln(writer, -1)
		} else {
			fmt.Fprintln(writer, lo*q-y)
		}
	}
}
```