Go 中的模块化算术实现 - 错误答案

问题描述

我一直在研究 Kattis modular arithmetic problem。下面是我使用 package big 的 Golang 解决方案。代码通过了已发布的测试用例,但未能通过秘密测试用例(“错误答案”)。我正在努力找出问题所在。

func ModularArithmetic() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    for true {
        n := ScanInt64(scanner)
        t := ScanInt64(scanner) 
        if n == 0 && t == 0 {
            return
        }
        nBig := big.NewInt(int64(n))
        for i := int64(0); i < t; i++ {
            x := ScanInt64(scanner)
            scanner.Scan()
            op := scanner.Bytes()
            y := ScanInt64(scanner)
            xBig := big.NewInt(int64(x))
            yBig := big.NewInt(int64(y))
            switch op[0] {
            case '+':
                xBig.Add(xBig,yBig)
            case '-':
                xBig.Sub(xBig,yBig)
            case '*':
                xBig.Mul(xBig,yBig)
            case '/':
                yInv := big.NewInt(0)
                yInvRes := yInv.ModInverse(yBig,nBig)
                if yInvRes == nil {
                    fmt.Println(-1)
                    continue
                } else {
                    xBig.Mul(xBig,yInv)
                }
            }
            xBig.Mod(xBig,nBig)
            fmt.Println(xBig)
        }
    }
}

func ScanInt64(scanner *bufio.Scanner) (n int64) {
    scanner.Scan()
    n,_ = strconv.ParseInt(scanner.Text(),10,64)
    return
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)