Cho n. Tìm chữ số cuối của S = Σ_{i: 2^i ≤ n} Σ_{j=0}^{n} 2^{2^i + 2j}.
Quan sát
- 2^k mod 10 chu kỳ 4: 2,4,8,6.
- Tổng nội tính mod 10, tổng ngoại nhân thêm hệ số (1 nếu n chẵn, 5 nếu n lẻ).
Ví dụ
def solve(n):
total = 0
temp = 1
while temp <= n:
total += pow(2, temp, 10)
temp *= 2
total = total * (1 + (4 if n % 2 == 1 else 0)) % 10
return total
print(solve(2)) # Output: 6
Giải thích
n=2: i=0 (2^0=1≤2), i=1 (2^1=2≤2). S0=2^1+2^3+2^5=42, S1=2^2+2^4+2^6=84. Tổng=126 → chữ số cuối 6.