'''
(a+1)^n = 0Cn*a^n+1Cn*a^(n-1)....(n-2)Cn*a^2+(n-1)Cn*a+1
when n>=2
(a+1)^n mod a^2 == (n-1)Cn*a+1 = 1Cn*a+1=n*a+1
(a-1)^n = 0Cn*a^n-1Cn*a^(n-1).... (-1)^x*(n-2)Cn*a^2+(-1)^x*(n-1)Cn*a+(-1)^x*1
(a-1)^n mod a^2 == (n-1)Cn*a+1 = (-1)^(n-1)1Cn*a+(-1)^n*1= -n*a+1 (n is even) or n*a-a (n is odd)
n = 1 2a/a^2 ===2na
so
if odd(n): (a+1)^n+(a-1)^n mod a^2 === 2na mod a^2 ===> max remainder is (nearest even number of a)*a
if even(n): === 2 mod a^2
'''
def rmax(a):
remainder = max(2%a, ((a-1)//2)*2*a) #nearest even number of a
return remainder
s = 0
for a in range(3, 1001):
s += rmax(a)
print(s) #333082500
答案是333082500 |