กำหนดค่า n หาตัวเลขหลักสุดท้ายของลำดับ S:
$$\sum_{i=0\: 2^{i}\leqslant n} \sum_{j=0}^{n} 2^{2^{i}+2j}$$
เช่น n=2 ผลลัพธ์คือ 6 เพราะ i=0,1 เท่านั้นที่ valid: S₀ = 2^(2^0+0) + 2^(2^0+2) + 2^(2^0+4) = 42 S₁ = 2^(2^1+0) + 2^(2^1+2) + 2^(2^1+4) = 84 ผลรวม 126 หลักสุดท้าย 6
วิธีแก้ปัญหา
- total := 0, temp := 1
- while temp <= n
- total := total + (2^temp mod 10)
- temp := temp * 2
- total := total * (1 + (4 ถ้า n คี่ มิฉะนั้น 0)) mod 10
- คืนค่า total
ตัวอย่างโค้ด
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
n = 2
print(solve(n))
Input
2
Output
6