From 8ae888d746d5a8f17bac799b66f97fd47ea0ded4 Mon Sep 17 00:00:00 2001 From: Gwangseok Han <66068653+c0natus@users.noreply.github.com> Date: Wed, 11 Dec 2024 13:50:23 +0900 Subject: [PATCH 1/2] Create 10986 gwangseok.py --- BaekJoon/10986/gwangseok.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 BaekJoon/10986/gwangseok.py diff --git a/BaekJoon/10986/gwangseok.py b/BaekJoon/10986/gwangseok.py new file mode 100644 index 0000000..7ce6b95 --- /dev/null +++ b/BaekJoon/10986/gwangseok.py @@ -0,0 +1,20 @@ +from collections import defaultdict + +N, M = map(int, input().split()) +nums = list(map(int, input().split())) + +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) From 151f36867054f55b7c9be3bd0effdea8b9b0f65e Mon Sep 17 00:00:00 2001 From: Gwangseok Han <66068653+c0natus@users.noreply.github.com> Date: Tue, 17 Dec 2024 13:36:08 +0900 Subject: [PATCH 2/2] Update 10986 gwangseok.py --- BaekJoon/10986/gwangseok.py | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/BaekJoon/10986/gwangseok.py b/BaekJoon/10986/gwangseok.py index 7ce6b95..32f7046 100644 --- a/BaekJoon/10986/gwangseok.py +++ b/BaekJoon/10986/gwangseok.py @@ -3,18 +3,33 @@ N, M = map(int, input().split()) nums = list(map(int, input().split())) -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) +# <풀이2> +cum_sum = 0 +value_j_dict = defaultdict(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) - k = len(values) - ans += (k-1) * k // 2 # 0 + ... + k-1 - if key == 0: # 본인 포함 - ans += k + 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)