← Home
For problem statement at 0-999/100-199/170-179/177/problemE1.txt this is a correct solution, but verifier at 0-999/100-199/170-179/177/verifierE1.go ends with All 106 tests passed. can you fix the verifier? ```go
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)

	var n, c int
	fmt.Fscan(reader, &n, &c)

	a := make([]int64, n)
	b := make([]int64, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i], &b[i])
	}

	// For each planet i, the beaver brings a[i] suitcases with x presents each = a[i]*x presents.
	// He spends 1 day walking, then gives b[i] presents per day.
	// Days on planet i = 1 + floor(a[i]*x / b[i])
	// Total days = n + sum over i of floor(a[i]*x / b[i]) = c
	// So sum of floor(a[i]*x / b[i]) = c - n

	target := int64(c) - int64(n)

	// If all a[i] are 0, then sum is always 0 regardless of x
	allZero := true
	for i := 0; i < n; i++ {
		if a[i] != 0 {
			allZero = false
			break
		}
	}

	if allZero {
		if target == 0 {
			fmt.Println(-1)
		} else {
			fmt.Println(0)
		}
		return
	}

	if target < 0 {
		fmt.Println(0)
		return
	}

	// f(x) = sum of floor(a[i]*x / b[i])
	// f is non-decreasing. We need to find the number of positive integers x such that f(x) = target.
	// Since f is non-decreasing, we can binary search for the first x where f(x) >= target
	// and the first x where f(x) >= target+1, then subtract.

	// f(x) for a given x:
	computeF := func(x int64) int64 {
		var sum int64
		for i := 0; i < n; i++ {
			if a[i] == 0 {
				continue
			}
			// a[i]*x / b[i] could overflow if a[i] and x are both up to 10^9
			// a[i] up to 10^9, x up to ~10^18 potentially? No.
			// Actually target is up to 10^9, and a[i]*x/b[i] grows, so x won't be too large.
			// But a[i]*x could overflow int64 if both are large.
			// Let's be careful. a[i] <= 10^9, x could be up to around 10^9 * 10^9 / 1 but that's too much.
			// Actually since target <= 10^9, and sum has n terms each >= a[i]*x/b[i] - 1,
			// x is at most around (target + n) * max(b[i]) / sum(a[i]) which could be large.
			// But a[i]*x: if a[i]=10^9 and x=10^9, that's 10^18 which fits in int64.
			// If x could be larger... let's think. target up to 10^9, n up to 10^4.
			// If there's one planet with a[i]=1, b[i]=10^9, then floor(x/10^9). To get target=10^9, x=10^18.
			// Then a[i]*x = 1*10^18, fine.
			// But if a[i]=10^9 and we need x=10^18, then a[i]*x = 10^27, overflow!
			// We need to handle overflow. If a[i]*x/b[i] > target, we can cap it.

			// Use big arithmetic or detect overflow
			// If a[i] > 0 and x > 0:
			// floor(a[i]*x / b[i])
			// = (a[i]/b[i])*x + floor((a[i]%b[i])*x / b[i])
			q := a[i] / b[i]
			r := a[i] % b[i]
			term := q * x
			// r*x might overflow: r < b[i] <= 10^9, x could be up to ~10^18
			// r*x up to 10^27, overflow. Use: r*x/b[i] = r*(x/b[i]) + floor(r*(x%b[i])/b[i])
			term += r * (x / b[i])
			rem := x % b[i]
			term += r * rem / b[i]

			sum += term
			if sum > target {
				return sum
			}
		}
		return sum
	}

	// Binary search for smallest x >= 1 where f(x) >= target
	lo, hi := int64(1), int64(2)
	for computeF(hi) < target {
		hi *= 2
		if hi > int64(2e18) {
			fmt.Println(0)
			return
		}
	}
	for lo < hi {
		mid := lo + (hi-lo)/2
		if computeF(mid) >= target {
			hi = mid
		} else {
			lo = mid + 1
		}
	}
	left := lo

	lo2, hi2 := left, hi*2
	for computeF(hi2) < target+1 {
		hi2 *= 2
		if hi2 > int64(2e18) {
			// f never reaches target+1 => infinite?
			// Only if f is bounded. Since not all a[i]=0, f grows unbounded. So this shouldn't happen in practice.
			break
		}
	}
	// Check if f eventually exceeds target
	if computeF(hi2) <= target {
		// f is always = target for x >= left => infinite
		fmt.Println(-1)
		return
	}
	lo2 = left
	for lo2 < hi2 {
		mid := lo2 + (hi2-lo2)/2
		if computeF(mid) >= target+1 {
			hi2 = mid
		} else {
			lo2 = mid + 1
		}
	}
	right := lo2

	fmt.Println(right - left)
}
```