The ".0" part was the issue. I didn't realize modulo turned integers into 
non-integer integers. Here's my final working solution, although I still don't 
know why it doesn't work if I just write return x%M instead of return 
math.floor(x%M). I'm using integer division (which the code jam editor 
recognizes as a syntax error) so there shouldn't be any non-integers coming out.

local function inv(a, b)
        local m, c, d = b, 1, 0
        while b ~= 1 do
                a, b, c, d = b%a, a, d - c*(b//a), c
        end
        return d%m
end
 
function remainder(m, b)
    local M = 1
    for i = 1, #m do M = M*m[i] end
    local x, Mi = 0
    for i = 1, #m do
        Mi = M//m[i]
        x = x + b[i]*inv(Mi, m[i])*Mi
    end
    return math.floor(x%M)
end

local T = tonumber(io.read():match("%d+"))
for _ = 1, T do
    local n, a = {7, 11, 13, 15, 16, 17}, {}
    for i = 1, #n do
        print((n[i].." "):rep(18):sub(1, -2))
        local count = 0
        for v in io.read():gmatch("%d+") do
                        count = count + v
                end
        a[i] = count%n[i]
    end
    print(remainder(n, a))
    io.read()
end

-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/d0499de3-38ec-4604-a563-9bc546366db0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to