← Home
For problem statement at 0-999/100-199/180-189/185/problemD.txt this is a correct solution, but verifier at 0-999/100-199/180-189/185/verifierD.go ends with case 1 failed: expected 0 got 3
input:
1
45046 5 8 13
exit status 1 can you fix the verifier? package main

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

type Scanner struct {
	buf []byte
	pos int
}

func NewScanner() *Scanner {
	buf, _ := io.ReadAll(os.Stdin)
	return &Scanner{buf: buf, pos: 0}
}

func (s *Scanner) nextInt64() int64 {
	for s.pos < len(s.buf) && s.buf[s.pos] <= ' ' {
		s.pos++
	}
	if s.pos >= len(s.buf) {
		return 0
	}
	var res int64 = 0
	for s.pos < len(s.buf) && s.buf[s.pos] > ' ' {
		res = res*10 + int64(s.buf[s.pos]-'0')
		s.pos++
	}
	return res
}

func powMod(base, exp, mod int64) int64 {
	res := int64(1)
	base %= mod
	for exp > 0 {
		if exp&1 == 1 {
			res = res * base % mod
		}
		base = base * base % mod
		exp >>= 1
	}
	return res
}

func main() {
	sc := NewScanner()
	if sc.pos >= len(sc.buf) {
		return
	}

	t := sc.nextInt64()

	out := make([]byte, 0, 1024*1024)

	for i := int64(0); i < t; i++ {
		k := sc.nextInt64()
		l := sc.nextInt64()
		r := sc.nextInt64()
		p := sc.nextInt64()

		if p == 2 {
			if k%2 == 0 {
				out = append(out, '1', '\n')
			} else {
				out = append(out, '0', '\n')
			}
			continue
		}

		var P int64
		if k%p == 0 {
			P = 1
		} else {
			expX := powMod(2, l, p-1)
			x := powMod(k, expX, p)
			if x == 1 {
				P = powMod(2, r-l+1, p)
			} else {
				expY := powMod(2, r+1, p-1)
				y := powMod(k, expY, p)
				num := (y - 1 + p) % p
				den := (x - 1 + p) % p
				invDen := powMod(den, p-2, p)
				P = num * invDen % p
			}
		}

		var ans int64
		if k%2 == 1 {
			inv2 := powMod(2, p-2, p)
			powInv2 := powMod(inv2, (r-l)%(p-1), p)
			ans = P * powInv2 % p
		} else {
			ans = P
		}

		out = strconv.AppendInt(out, ans, 10)
		out = append(out, '\n')
	}
	os.Stdout.Write(out)
}