p.s. this is a codechef question, i coded this question in java and was partially correct, but so far I found this new code for optimisation but don't know pyhton.
Given integers N
and M
, find the number of ordered pairs (a,b)
such that 1≤a<b≤N
and ((M mod a) mod b)=((M mod b) mod a)
. t
is the number of test cases.
N = 2304
M = 23498
def fast():
npairs = 0
nfactors = [1] * (M + 1)
for b in range(2, N + 1):
npairs += nfactors[M - M % b]
for i in range(0, M + 1, b):
nfactors[i] += 1
return npairs
def naive():
return sum((M % a) % b == (M % b) % a for b in range(2, N + 1) for a in range(1, b))
print(fast(), naive())