I got accepted in 5 sec by writing my own next_permutation procedure that calculates new remainder on the fly.fh wrote:the first thing i did was next_permutation(), but TLE
Also you can precalculate mod[x] = x mod d; up to 1000000 or so, to get rid of expensive mod operation.