Submit solution
Points:
1.00 (partial)
Time limit:
1.0s
Memory limit:
512M
Input:
stdin
Output:
stdout
Author:
Problem type
Allowed languages
Python, Scratch
Cho dãy số có quy luật sau:
~1~ , ~2~ , ~3~ , ~6~ , ~5~ , ~4~ , ~7~ , ~8~ , ~9~ , ~12~ , ~11~ , ~10~ , ~13~ , ~14~ , ~15~ , ~18~ , ~17~ , ~16~ , ~...~
Yêu cầu: Cho hai số nguyên dương ~L~, ~R~. Hãy tính tổng các số từ vị trí ~L~ đến vị trí ~R~ của dãy số trên.
Dữ liệu
- Gồm hai số tự nhiên ~L~ và ~R~ ~(1 ≤ L ≤ R ≤ 10^8)~, mỗi số trên một dòng.
Kết quả
- Gồm một số tự nhiên là kết quả của bài toán.
Chấm điểm:
- Có 50% số test ứng với 50% số điểm với ~R ≤ 10^5~;
- 50% số test còn lại ứng với 50% số điểm không có ràng buộc gì thêm.
Sample Input
3
8
Sample Output
33
Note
- Tổng các số từ vị trí ~3~ đến vị trí ~8~ là ~3+6+5+4+7+8= 33~
Comments
l=int(input()) r=int(input()) t=(l+r)*(r-l+1)//2 if l%6==5 or l%6==0: t-=2 if r%6==5 or r%6==4: t+=2 print(t)
def prefixsum(n): if n <= 0: return 0 g = n // 6 r = n % 6 total = 36 * g * (g - 1) // 2 + 21 * g a = g * 6 + 1 if r >= 1: total += a if r >= 2: total += a + 1 if r >= 3: if r >= 4: total += a + 5 if r >= 5: total += a + 4 return total L = int(input()) R = int(input()) print(prefixsum(R) - prefix_sum(L - 1))
code
def prefix_sum(n): if n <= 0: return 0 g = n // 6 r = n % 6 total = 36 * g * (g - 1) // 2 + 21 * g a = g * 6 + 1 if r >= 1: total += a if r >= 2: total += a + 1 if r >= 3: total += a + 2 if r >= 4: total += a + 5 if r >= 5: total += a + 4 return total
L = int(input()) R = int(input()) print(prefixsum(R) - prefixsum(L - 1))
code này dùng CHATGPT
l=int(input()) r=int(input()) def tt(n): sl=(n//6)6 tong=(sl+1)sl//2 if n%6==1:tong+=n elif n%6==2:tong+=2n-1 elif n%6==3:tong+=3n-3 elif n%6==4:tong+=4n-4 elif n%6==5:tong+=5n-8 return tong print(tt(r)-tt(l-1))
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.