Bedao Mini Contest 22 - Đếm đi các bạn ơiiii

View as PDF

Submit solution


Points: 0.10 (partial)
Time limit: 1.0s
Memory limit: 256M
Input: stdin
Output: stdout

Author:
Problem types
Allowed languages
C, C++, Go, Java, Kotlin, Pascal, PyPy, Python, Rust, Scratch

Cho dãy số gồm ~n~ số nguyên dương ~a_1, a_2, \ldots, a_n~. Hãy đếm số cặp (~i~, ~j~) thỏa mãn:

  • ~1 \le i < j \le n~.

  • ~j - i > 5~.

  • ~|a_i - a_j|~ chia hết cho ~23~.

Input

Dòng đầu tiên chứa một số nguyên dương ~n~ (~1 \le n \le 2 \cdot 10^5~).

Dòng thứ hai gồm ~n~ số nguyên dương mô tả dãy số ~a_1, a_2, \ldots, a_n~ (~1 \le a_i \le 10^9~, ~1 \le i \le n~).

Output

In ra một số nguyên duy nhất là số cặp (~i~, ~j~) thỏa mãn yêu cầu đề bài.

Scoring

Subtask Điểm Giới hạn
1 ~40~ ~n \le 1000~
2 ~60~ Không có ràng buộc gì thêm

Sample Input 1

10
1 24 25 4 30 15 3 1 24 2

Sample Output 1

5

Notes

Trong ví dụ, các cặp số (~i~, ~j~) thỏa mãn là:

  1. (~1~, ~8~)

  2. (~1~, ~9~)

  3. (~2~, ~8~)

  4. (~2~, ~9~)

  5. (~3~, ~10~)


Comments

Please read the guidelines before commenting.



  • -1
    dphuc02040608  commented on Sept. 24, 2025, 3:51 a.m.

    include<bits/stdc++.h>

    define ll long long

    define N 1000000

    define buff iosbase::syncwith_stdio(false); cin.tie(0); cout.tie(0);

    using namespace std; ll n; ll a[N]; ll c[25]; int main() { cin>>n; for(int i=1; i<=n; i++) cin>>a[i]; ll kq=0; for(int i=7; i<=n; i++) { c[a[i-6]%23]++; ll x=a[i]%23; kq+=c[x]; } cout<<kq; return 0; }

    *


  • 1
    ngoccaidu2008  commented on Sept. 19, 2025, 5:05 p.m.

    các số |ai-aj| chia hết cho 23 khi ai%23=aj%23

    dùng mảng đếm lưu giá trị vị trí + binary search

    bài này có thể có nhiều cách


    • 0
      vominhmanh10  commented on Nov. 28, 2025, 1:19 p.m. edit 3

      mẫu cách giải hướng trên:
      lưu vào từ điển của dư k list các chỉ số mà a[i] % 23 = k
      duyệt list trong từ điển: với mỗi giá trị idx trong list đã sắp tăng dần, tìm kiếm nhị phân idx2 mà idx2 >= idx - 5
      cộng vào kết quả cnt += (idx2 - 1)
      thực hiện trong tối đa là O(23n log n)

      import sys
      import bisect as bs
      from collections import defaultdict
      input = sys.stdin.readline
      
      n = int(input())
      a = list(map(int, input().split()))
      cnt = defaultdict(list)
      res = 0
      for i in range(n): cnt[a[i] % 23].append(i)
      for b in cnt.values():
          for x in b:
              j = bs.bisect_left(b, x - 5)
              res += j
      print(res)
      

    • 0
      quyennguyen121  commented on Oct. 18, 2025, 1:42 p.m. edited

      .


  • -5
    dtnguyenthehung  commented on April 24, 2025, 11:40 a.m.

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


  • -4
    nongquan  commented on March 27, 2025, 8:27 a.m.

    https://ideone.com/PQw1gp co the sd mang danh dau, fenwick tree, segment tree thay the map


  • -4
    keelythuytrang  commented on Nov. 11, 2024, 12:38 p.m.

    tại sao 1,8 lại thỏa v mn


    • -1
      NG_Bao  commented on Nov. 24, 2024, 11:51 a.m.

      1 - 1 = 0 chia hết cho 23


  • -12
    nguyenttuca  commented on June 13, 2024, 3:10 p.m.

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


  • -7
    anhtuanng04050405  commented on April 2, 2024, 2:28 a.m.

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


  • -2
    ngan_2604  commented on Feb. 7, 2024, 5:08 a.m.

    help!!!!!! ai giải thích giúp tui cái ví dụ được không? tui đọc không có hiểu


    • 0
      nccuongtq2023  commented on March 14, 2024, 1:23 p.m.

      cái ví dụ là trường hợp 1 là vị trí thứ 1 với vị trí thứ 8 có hiệu là |1-24|=23 chia hết cho 3 tương tự với trường hợp 2 3 4 5 cũng vậy


  • 6
    ithero  commented on Dec. 4, 2023, 4:30 p.m.

    bài này có special tests ko z? mình làm đc có 36/40 tests :<


    • -4
      BackOnTrack  commented on Dec. 7, 2023, 3:03 p.m.

      https://ideone.com/uHxq9o

      code tk


  • -162
    tomioka_giyu_long  commented on Dec. 1, 2023, 3:25 a.m.

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


    • -10
      van353735  commented on Aug. 18, 2024, 3:22 a.m.

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