0

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())
Akash Roy
  • 37
  • 8
  • One of the M's in the MOD case, should that have been an N? – Surt May 15 '21 at 18:10
  • @Surt No, we are just simplifying the mod operation and the last relation was that code and that it correct. – Akash Roy May 15 '21 at 18:12
  • what about some kind of precomputation on values of x and a? values of x can only be 0...m; the most inner for could reuse some previously calculated data, and only calculate the mod for values not yet encountered of x and a. – tremendous7 May 15 '21 at 18:38

0 Answers0