for i in range(2,(n//2)+1):
for i in range(2,int(math.sqrt(n))+1):
For a coding contest, the answer is almost never to implement the problem the same way as it is given.
For example here, functions A and B are actually intentionally misleading. Function A is also known as Euler's Totient, and function B computes the sum of totients of divisors which is the same as the original number. Detailed review of them is therefore a ...