Tổng dãy số

View as PDF

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

Please read the guidelines before commenting.



  • 0
    hanhpmst  commented on May 9, 2025, 2:07 p.m.

    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)


  • -2
    baohoa200  commented on April 28, 2025, 12:18 p.m.

    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))


  • -2
    NahImNotBtBro  commented on April 22, 2025, 4:11 p.m.

    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))


    • -3
      thaicuwin  commented on April 23, 2025, 2:27 a.m.

      code này dùng CHATGPT


  • -1
    ngominhnhat  commented on April 21, 2025, 10:58 a.m.

    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))


  • -7
    dopdopyesyes2k14  commented on April 13, 2025, 1:57 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -5
    dopdopyesyes2k14  commented on April 13, 2025, 1:54 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -8
    hainam  commented on April 8, 2025, 1:47 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -9
    quangvinhthhd5c  commented on April 4, 2025, 11:14 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -12
    phong_2014  commented on March 31, 2025, 7:36 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -9
    nhatnam  commented on March 31, 2025, 7:34 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -9
    tom_tom_teve  commented on March 30, 2025, 1:45 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.