For problem statement at 1000-1999/1900-1999/1990-1999/1993/problemF1.txt this is a correct solution, but verifier at 1000-1999/1900-1999/1990-1999/1993/verifierF1.go ends with All 27 tests passed. can you fix the verifier? package main
import (
"bufio"
"io"
"os"
"strconv"
)
type FastScanner struct {
data []byte
idx int
n int
}
func NewFastScanner() *FastScanner {
data, _ := io.ReadAll(os.Stdin)
return &FastScanner{data: data, n: len(data)}
}
func (fs *FastScanner) skip() {
for fs.idx < fs.n && fs.data[fs.idx] <= ' ' {
fs.idx++
}
}
func (fs *FastScanner) NextInt() int64 {
fs.skip()
sign := int64(1)
if fs.idx < fs.n && fs.data[fs.idx] == '-' {
sign = -1
fs.idx++
}
val := int64(0)
for fs.idx < fs.n {
c := fs.data[fs.idx]
if c < '0' || c > '9' {
break
}
val = val*10 + int64(c-'0')
fs.idx++
}
return val * sign
}
func (fs *FastScanner) NextString() string {
fs.skip()
start := fs.idx
for fs.idx < fs.n && fs.data[fs.idx] > ' ' {
fs.idx++
}
return string(fs.data[start:fs.idx])
}
func gcd(a, b int64) int64 {
for b != 0 {
a, b = b, a%b
}
if a < 0 {
a = -a
}
return a
}
func exgcd(a, b int64) (int64, int64, int64) {
x0, y0 := int64(1), int64(0)
x1, y1 := int64(0), int64(1)
for b != 0 {
q := a / b
a, b = b, a%b
x0, x1 = x1, x0-q*x1
y0, y1 = y1, y0-q*y1
}
return x0, y0, a
}
func invMod(a, m int64) int64 {
x, _, _ := exgcd(a, m)
x %= m
if x < 0 {
x += m
}
return x
}
func modNorm(a, m int64) int64 {
a %= m
if a < 0 {
a += m
}
return a
}
type Eq struct {
g int64
m int64
inv int64
}
func newEq(delta, mod int64) Eq {
g := gcd(delta, mod)
m := mod / g
inv := int64(0)
if m > 1 {
inv = invMod(delta/g, m)
}
return Eq{g: g, m: m, inv: inv}
}
func solveEq(eq Eq, c int64) (int8, int64) {
if c%eq.g != 0 {
return 0, 0
}
if eq.m == 1 {
return 1, 0
}
r := (c / eq.g) % eq.m
r = r * eq.inv % eq.m
return 2, r
}
func countResid(k, res, mod int64) int64 {
if res >= k {
return 0
}
return 1 + (k-1-res)/mod
}
func main() {
fs := NewFastScanner()
out := bufio.NewWriterSize(os.Stdout, 1<<20)
defer out.Flush()
t := int(fs.NextInt())
for ; t > 0; t-- {
n := int(fs.NextInt())
k := fs.NextInt()
w := fs.NextInt()
h := fs.NextInt()
s := fs.NextString()
aMod := int64(2) * w
bMod := int64(2) * h
ax, ay := int64(0), int64(0)
for i := 0; i < n; i++ {
switch s[i] {
case 'L':
ax--
if ax < 0 {
ax += aMod
}
case 'R':
ax++
if ax == aMod {
ax = 0
}
case 'D':
ay--
if ay < 0 {
ay += bMod
}
case 'U':
ay++
if ay == bMod {
ay = 0
}
}
}
eqx := newEq(ax, aMod)
eqy := newEq(ay, bMod)
cg, cmod2, cinv, l := int64(0), int64(0), int64(0), int64(0)
if eqx.m > 1 && eqy.m > 1 {
cg = gcd(eqx.m, eqy.m)
cmod2 = eqy.m / cg
if cmod2 > 1 {
cinv = invMod((eqx.m/cg)%cmod2, cmod2)
}
l = eqx.m / cg * eqy.m
}
px, py := int64(0), int64(0)
ans := int64(0)
for i := 0; i < n; i++ {
switch s[i] {
case 'L':
px--
if px < 0 {
px += aMod
}
case 'R':
px++
if px == aMod {
px = 0
}
case 'D':
py--
if py < 0 {
py += bMod
}
case 'U':
py++
if py == bMod {
py = 0
}
}
cx := aMod - px
if cx == aMod {
cx = 0
}
sx, rx := solveEq(eqx, cx)
if sx == 0 {
continue
}
cy := bMod - py
if cy == bMod {
cy = 0
}
sy, ry := solveEq(eqy, cy)
if sy == 0 {
continue
}
if sx == 1 && sy == 1 {
ans += k
} else if sx == 1 {
ans += countResid(k, ry, eqy.m)
} else if sy == 1 {
ans += countResid(k, rx, eqx.m)
} else {
diff := ry - rx
if diff%cg != 0 {
continue
}
tt := int64(0)
if cmod2 > 1 {
tt = modNorm(diff/cg, cmod2)
tt = tt * cinv % cmod2
}
r0 := rx + eqx.m*tt
r0 %= l
if r0 < 0 {
r0 += l
}
ans += countResid(k, r0, l)
}
}
out.WriteString(strconv.FormatInt(ans, 10))
out.WriteByte('\n')
}
}