Skip to content

Commit 0812c25

Browse files
committed
feat: add solutions to lc problem: No.2198
No.2198.Number of Single Divisor Triplets
1 parent 607fe0a commit 0812c25

File tree

6 files changed

+399
-2
lines changed

6 files changed

+399
-2
lines changed

solution/2100-2199/2198.Number of Single Divisor Triplets/README.md

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,150 @@ Note that (0, 1, 2) is not a single divisor triplet because nums[0] + nums[1] +
6262
<!-- 这里可写当前语言的特殊实现逻辑 -->
6363

6464
```python
65-
65+
class Solution:
66+
def singleDivisorTriplet(self, nums: List[int]) -> int:
67+
def check(a, b, c):
68+
s = a + b + c
69+
return sum(s % x == 0 for x in [a, b, c]) == 1
70+
71+
counter = Counter(nums)
72+
ans = 0
73+
for a, cnt1 in counter.items():
74+
for b, cnt2 in counter.items():
75+
for c, cnt3 in counter.items():
76+
if check(a, b, c):
77+
if a == b:
78+
ans += cnt1 * (cnt1 - 1) * cnt3
79+
elif a == c:
80+
ans += cnt1 * (cnt1 - 1) * cnt2
81+
elif b == c:
82+
ans += cnt1 * cnt2 * (cnt2 - 1)
83+
else:
84+
ans += cnt1 * cnt2 * cnt3
85+
return ans
6686
```
6787

6888
### **Java**
6989

7090
<!-- 这里可写当前语言的特殊实现逻辑 -->
7191

7292
```java
93+
class Solution {
94+
public long singleDivisorTriplet(int[] nums) {
95+
int[] counter = new int[101];
96+
for (int x : nums) {
97+
++counter[x];
98+
}
99+
long ans = 0;
100+
for (int i = 1; i <= 100; ++i) {
101+
for (int j = 1; j <= 100; ++j) {
102+
for (int k = 1; k <= 100; ++k) {
103+
int cnt1 = counter[i], cnt2 = counter[j], cnt3 = counter[k];
104+
int s = i + j + k;
105+
int cnt = 0;
106+
if (s % i == 0) {
107+
++cnt;
108+
}
109+
if (s % j == 0) {
110+
++cnt;
111+
}
112+
if (s % k == 0) {
113+
++cnt;
114+
}
115+
if (cnt != 1) {
116+
continue;
117+
}
118+
if (i == j) {
119+
ans += (long) cnt1 * (cnt1 - 1) * cnt3;
120+
} else if (i == k) {
121+
ans += (long) cnt1 * (cnt1 - 1) * cnt2;
122+
} else if (j == k) {
123+
ans += (long) cnt1 * cnt2 * (cnt2 - 1);
124+
} else {
125+
ans += (long) cnt1 * cnt2 * cnt3;
126+
}
127+
}
128+
}
129+
}
130+
return ans;
131+
}
132+
}
133+
```
134+
135+
### **C++**
136+
137+
```cpp
138+
class Solution {
139+
public:
140+
long long singleDivisorTriplet(vector<int>& nums) {
141+
vector<int> counter(101);
142+
for (int& x : nums) ++counter[x];
143+
long long ans = 0;
144+
for (int i = 1; i <= 100; ++i)
145+
{
146+
for (int j = 1; j <= 100; ++j)
147+
{
148+
for (int k = 1; k <= 100; ++k)
149+
{
150+
int cnt1 = counter[i], cnt2 = counter[j], cnt3 = counter[k];
151+
int s = i + j + k;
152+
int cnt = (s % i == 0) + (s % j == 0) + (s % k == 0);
153+
if (cnt != 1) continue;
154+
if (i == j) ans += 1ll * cnt1 * (cnt1 - 1) * cnt3;
155+
else if (i == k) ans += 1ll * cnt1 * (cnt1 - 1) * cnt2;
156+
else if (j == k) ans += 1ll * cnt1 * cnt2 * (cnt2 - 1);
157+
else ans += 1ll * cnt1 * cnt2 * cnt3;
158+
}
159+
}
160+
}
161+
return ans;
162+
}
163+
};
164+
```
73165
166+
### **Go**
167+
168+
```go
169+
func singleDivisorTriplet(nums []int) int64 {
170+
counter := make([]int, 101)
171+
for _, x := range nums {
172+
counter[x]++
173+
}
174+
var ans int64
175+
check := func(a, b, c int) bool {
176+
s := a + b + c
177+
cnt := 0
178+
if s%a == 0 {
179+
cnt++
180+
}
181+
if s%b == 0 {
182+
cnt++
183+
}
184+
if s%c == 0 {
185+
cnt++
186+
}
187+
return cnt == 1
188+
}
189+
for i := 1; i <= 100; i++ {
190+
for j := 1; j <= 100; j++ {
191+
for k := 1; k <= 100; k++ {
192+
if check(i, j, k) {
193+
cnt1, cnt2, cnt3 := counter[i], counter[j], counter[k]
194+
if i == j {
195+
ans += int64(cnt1 * (cnt1 - 1) * cnt3)
196+
} else if i == k {
197+
ans += int64(cnt1 * (cnt1 - 1) * cnt2)
198+
} else if j == k {
199+
ans += int64(cnt1 * cnt2 * (cnt2 - 1))
200+
} else {
201+
ans += int64(cnt1 * cnt2 * cnt3)
202+
}
203+
}
204+
}
205+
}
206+
}
207+
return ans
208+
}
74209
```
75210

76211
### **TypeScript**

solution/2100-2199/2198.Number of Single Divisor Triplets/README_EN.md

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,148 @@ Note that (0, 1, 2) is not a single divisor triplet because nums[0] + nums[1] +
5656
### **Python3**
5757

5858
```python
59-
59+
class Solution:
60+
def singleDivisorTriplet(self, nums: List[int]) -> int:
61+
def check(a, b, c):
62+
s = a + b + c
63+
return sum(s % x == 0 for x in [a, b, c]) == 1
64+
65+
counter = Counter(nums)
66+
ans = 0
67+
for a, cnt1 in counter.items():
68+
for b, cnt2 in counter.items():
69+
for c, cnt3 in counter.items():
70+
if check(a, b, c):
71+
if a == b:
72+
ans += cnt1 * (cnt1 - 1) * cnt3
73+
elif a == c:
74+
ans += cnt1 * (cnt1 - 1) * cnt2
75+
elif b == c:
76+
ans += cnt1 * cnt2 * (cnt2 - 1)
77+
else:
78+
ans += cnt1 * cnt2 * cnt3
79+
return ans
6080
```
6181

6282
### **Java**
6383

6484
```java
85+
class Solution {
86+
public long singleDivisorTriplet(int[] nums) {
87+
int[] counter = new int[101];
88+
for (int x : nums) {
89+
++counter[x];
90+
}
91+
long ans = 0;
92+
for (int i = 1; i <= 100; ++i) {
93+
for (int j = 1; j <= 100; ++j) {
94+
for (int k = 1; k <= 100; ++k) {
95+
int cnt1 = counter[i], cnt2 = counter[j], cnt3 = counter[k];
96+
int s = i + j + k;
97+
int cnt = 0;
98+
if (s % i == 0) {
99+
++cnt;
100+
}
101+
if (s % j == 0) {
102+
++cnt;
103+
}
104+
if (s % k == 0) {
105+
++cnt;
106+
}
107+
if (cnt != 1) {
108+
continue;
109+
}
110+
if (i == j) {
111+
ans += (long) cnt1 * (cnt1 - 1) * cnt3;
112+
} else if (i == k) {
113+
ans += (long) cnt1 * (cnt1 - 1) * cnt2;
114+
} else if (j == k) {
115+
ans += (long) cnt1 * cnt2 * (cnt2 - 1);
116+
} else {
117+
ans += (long) cnt1 * cnt2 * cnt3;
118+
}
119+
}
120+
}
121+
}
122+
return ans;
123+
}
124+
}
125+
```
126+
127+
### **C++**
128+
129+
```cpp
130+
class Solution {
131+
public:
132+
long long singleDivisorTriplet(vector<int>& nums) {
133+
vector<int> counter(101);
134+
for (int& x : nums) ++counter[x];
135+
long long ans = 0;
136+
for (int i = 1; i <= 100; ++i)
137+
{
138+
for (int j = 1; j <= 100; ++j)
139+
{
140+
for (int k = 1; k <= 100; ++k)
141+
{
142+
int cnt1 = counter[i], cnt2 = counter[j], cnt3 = counter[k];
143+
int s = i + j + k;
144+
int cnt = (s % i == 0) + (s % j == 0) + (s % k == 0);
145+
if (cnt != 1) continue;
146+
if (i == j) ans += 1ll * cnt1 * (cnt1 - 1) * cnt3;
147+
else if (i == k) ans += 1ll * cnt1 * (cnt1 - 1) * cnt2;
148+
else if (j == k) ans += 1ll * cnt1 * cnt2 * (cnt2 - 1);
149+
else ans += 1ll * cnt1 * cnt2 * cnt3;
150+
}
151+
}
152+
}
153+
return ans;
154+
}
155+
};
156+
```
65157
158+
### **Go**
159+
160+
```go
161+
func singleDivisorTriplet(nums []int) int64 {
162+
counter := make([]int, 101)
163+
for _, x := range nums {
164+
counter[x]++
165+
}
166+
var ans int64
167+
check := func(a, b, c int) bool {
168+
s := a + b + c
169+
cnt := 0
170+
if s%a == 0 {
171+
cnt++
172+
}
173+
if s%b == 0 {
174+
cnt++
175+
}
176+
if s%c == 0 {
177+
cnt++
178+
}
179+
return cnt == 1
180+
}
181+
for i := 1; i <= 100; i++ {
182+
for j := 1; j <= 100; j++ {
183+
for k := 1; k <= 100; k++ {
184+
if check(i, j, k) {
185+
cnt1, cnt2, cnt3 := counter[i], counter[j], counter[k]
186+
if i == j {
187+
ans += int64(cnt1 * (cnt1 - 1) * cnt3)
188+
} else if i == k {
189+
ans += int64(cnt1 * (cnt1 - 1) * cnt2)
190+
} else if j == k {
191+
ans += int64(cnt1 * cnt2 * (cnt2 - 1))
192+
} else {
193+
ans += int64(cnt1 * cnt2 * cnt3)
194+
}
195+
}
196+
}
197+
}
198+
}
199+
return ans
200+
}
66201
```
67202

68203
### **TypeScript**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
long long singleDivisorTriplet(vector<int>& nums) {
4+
vector<int> counter(101);
5+
for (int& x : nums) ++counter[x];
6+
long long ans = 0;
7+
for (int i = 1; i <= 100; ++i)
8+
{
9+
for (int j = 1; j <= 100; ++j)
10+
{
11+
for (int k = 1; k <= 100; ++k)
12+
{
13+
int cnt1 = counter[i], cnt2 = counter[j], cnt3 = counter[k];
14+
int s = i + j + k;
15+
int cnt = (s % i == 0) + (s % j == 0) + (s % k == 0);
16+
if (cnt != 1) continue;
17+
if (i == j) ans += 1ll * cnt1 * (cnt1 - 1) * cnt3;
18+
else if (i == k) ans += 1ll * cnt1 * (cnt1 - 1) * cnt2;
19+
else if (j == k) ans += 1ll * cnt1 * cnt2 * (cnt2 - 1);
20+
else ans += 1ll * cnt1 * cnt2 * cnt3;
21+
}
22+
}
23+
}
24+
return ans;
25+
}
26+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
func singleDivisorTriplet(nums []int) int64 {
2+
counter := make([]int, 101)
3+
for _, x := range nums {
4+
counter[x]++
5+
}
6+
var ans int64
7+
check := func(a, b, c int) bool {
8+
s := a + b + c
9+
cnt := 0
10+
if s%a == 0 {
11+
cnt++
12+
}
13+
if s%b == 0 {
14+
cnt++
15+
}
16+
if s%c == 0 {
17+
cnt++
18+
}
19+
return cnt == 1
20+
}
21+
for i := 1; i <= 100; i++ {
22+
for j := 1; j <= 100; j++ {
23+
for k := 1; k <= 100; k++ {
24+
if check(i, j, k) {
25+
cnt1, cnt2, cnt3 := counter[i], counter[j], counter[k]
26+
if i == j {
27+
ans += int64(cnt1 * (cnt1 - 1) * cnt3)
28+
} else if i == k {
29+
ans += int64(cnt1 * (cnt1 - 1) * cnt2)
30+
} else if j == k {
31+
ans += int64(cnt1 * cnt2 * (cnt2 - 1))
32+
} else {
33+
ans += int64(cnt1 * cnt2 * cnt3)
34+
}
35+
}
36+
}
37+
}
38+
}
39+
return ans
40+
}

0 commit comments

Comments
 (0)