Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2024-12-10] gwangseok #369 #371

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions BaekJoon/10986/gwangseok.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from collections import defaultdict

N, M = map(int, input().split())
nums = list(map(int, input().split()))

# <풀이2>
cum_sum = 0
value_j_dict = defaultdict(int)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int 추가 풀이를 하셨군요.

for num in nums: # O(N)
cum_sum = (cum_sum + num) % M
value_j_dict[cum_sum] += 1

ans = 0

for key, values in value_j_dict.items(): # O(M)
ans += (values-1) * values // 2 # 0 + ... + k-1
if key == 0:
ans += values

# <풀이1>
# cum_num = [0] * N
# value_j_dict = defaultdict(list)
# for idx, num in enumerate(nums): # O(N)
# cum_num[idx] = (cum_num[idx-1] + num) % M
# value_j_dict[cum_num[idx]].append(idx)

# ans = 0

# for key, values in value_j_dict.items(): # O(M)
# k = len(values)
# ans += (k-1) * k // 2 # 0 + ... + k-1
# if key == 0: # 본인 포함
# ans += k

print(ans)