Skip to content

Commit 986f20a

Browse files
committed
feat: add solutions to lc problem: No.1175
No.1175.Prime Arrangements
1 parent 786f248 commit 986f20a

File tree

11 files changed

+417
-26
lines changed

11 files changed

+417
-26
lines changed

solution/0100-0199/0172.Factorial Trailing Zeroes/README.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,16 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54-
题目实际上是求 1~n 中有多少个 5 的因数。
54+
**方法一:数学**
5555

56-
我们以 130 为例来分析:
56+
题目实际上是求 $[1,n]$ 中有多少个 $5$ 的因数。
5757

58-
1. 第 1 次除以 5,得到 26,表示存在 26 个包含因数 5 的数;
59-
1. 第 2 次除以 5,得到 5,表示存在 5 个包含因数 5² 的数;
60-
1. 第 3 次除以 5,得到 1,表示存在 1 个包含因数 5³ 的数;
61-
1. 累加得到从 1~n 中所有 5 的因数的个数。
58+
我们以 $130$ 为例来分析:
59+
60+
1. 第 $1$ 次除以 $5$,得到 $26$,表示存在 $26$ 个包含因数 $5$ 的数;
61+
1. 第 $2$ 次除以 $5$,得到 $5$,表示存在 $5$ 个包含因数 $5^2$ 的数;
62+
1. 第 $3$ 次除以 $5$,得到 $1$,表示存在 $1$ 个包含因数 $5^3$ 的数;
63+
1. 累加得到从 $[1,n]$ 中所有 $5$ 的因数的个数。
6264

6365
<!-- tabs:start -->
6466

solution/0200-0299/0263.Ugly Number/README.md

+19-6
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,9 @@ class Solution:
6363
def isUgly(self, n: int) -> bool:
6464
if n < 1:
6565
return False
66-
while n % 2 == 0:
67-
n //= 2
68-
while n % 3 == 0:
69-
n //= 3
70-
while n % 5 == 0:
71-
n //= 5
66+
for x in [2, 3, 5]:
67+
while n % x == 0:
68+
n //= x
7269
return n == 1
7370
```
7471

@@ -137,6 +134,22 @@ var isUgly = function (n) {
137134
};
138135
```
139136

137+
### **Go**
138+
139+
```go
140+
func isUgly(n int) bool {
141+
if n < 1 {
142+
return false
143+
}
144+
for _, x := range []int{2, 3, 5} {
145+
for n%x == 0 {
146+
n /= x
147+
}
148+
}
149+
return n == 1
150+
}
151+
```
152+
140153
### **...**
141154

142155
```

solution/0200-0299/0263.Ugly Number/README_EN.md

+19-6
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,9 @@ class Solution:
5151
def isUgly(self, n: int) -> bool:
5252
if n < 1:
5353
return False
54-
while n % 2 == 0:
55-
n //= 2
56-
while n % 3 == 0:
57-
n //= 3
58-
while n % 5 == 0:
59-
n //= 5
54+
for x in [2, 3, 5]:
55+
while n % x == 0:
56+
n //= x
6057
return n == 1
6158
```
6259

@@ -123,6 +120,22 @@ var isUgly = function (n) {
123120
};
124121
```
125122

123+
### **Go**
124+
125+
```go
126+
func isUgly(n int) bool {
127+
if n < 1 {
128+
return false
129+
}
130+
for _, x := range []int{2, 3, 5} {
131+
for n%x == 0 {
132+
n /= x
133+
}
134+
}
135+
return n == 1
136+
}
137+
```
138+
126139
### **...**
127140

128141
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func isUgly(n int) bool {
2+
if n < 1 {
3+
return false
4+
}
5+
for _, x := range []int{2, 3, 5} {
6+
for n%x == 0 {
7+
n /= x
8+
}
9+
}
10+
return n == 1
11+
}

solution/0200-0299/0263.Ugly Number/Solution.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ class Solution:
22
def isUgly(self, n: int) -> bool:
33
if n < 1:
44
return False
5-
while n % 2 == 0:
6-
n //= 2
7-
while n % 3 == 0:
8-
n //= 3
9-
while n % 5 == 0:
10-
n //= 5
5+
for x in [2, 3, 5]:
6+
while n % x == 0:
7+
n //= x
118
return n == 1

solution/1100-1199/1175.Prime Arrangements/README.md

+130-1
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,151 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42+
**方法一:数学**
43+
44+
先统计 $[1,n]$ 范围内的质数个数,我们记为 $cnt$。然后求 $cnt$ 以及 $n-cnt$ 阶乘的乘积得到答案,注意取模操作。
45+
46+
这里我们用“埃氏筛”统计质数。
47+
48+
如果 $x$ 是质数,那么大于 $x$ 的 $x$ 的倍数 $2x$,$3x$,… 一定不是质数,因此我们可以从这里入手。
49+
50+
我们设 $primes[i]$ 表示数 $i$ 是不是质数,如果是质数则为 $true$,否则为 $false$。从小到大遍历每个数,如果这个数为质数,则将其所有的倍数都标记为合数(除了该质数本身),即 $false$,这样在运行结束的时候我们即能知道质数的个数。
51+
52+
时间复杂度 $O(nloglogn)$。
53+
4254
<!-- tabs:start -->
4355

4456
### **Python3**
4557

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

4860
```python
49-
61+
class Solution:
62+
def numPrimeArrangements(self, n: int) -> int:
63+
def count(n):
64+
cnt = 0
65+
primes = [True] * (n + 1)
66+
for i in range(2, n + 1):
67+
if primes[i]:
68+
cnt += 1
69+
for j in range(i + i, n + 1, i):
70+
primes[j] = False
71+
return cnt
72+
73+
cnt = count(n)
74+
ans = factorial(cnt) * factorial(n - cnt)
75+
return ans % (10**9 + 7)
5076
```
5177

5278
### **Java**
5379

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

5682
```java
83+
class Solution {
84+
private static final int MOD = (int) 1e9 + 7;
85+
86+
public int numPrimeArrangements(int n) {
87+
int cnt = count(n);
88+
long ans = f(cnt) * f(n - cnt);
89+
return (int) (ans % MOD);
90+
}
91+
92+
private long f(int n) {
93+
long ans = 1;
94+
for (int i = 2; i <= n; ++i) {
95+
ans = (ans * i) % MOD;
96+
}
97+
return ans;
98+
}
99+
100+
private int count(int n) {
101+
int cnt = 0;
102+
boolean[] primes = new boolean[n + 1];
103+
Arrays.fill(primes, true);
104+
for (int i = 2; i <= n; ++i) {
105+
if (primes[i]) {
106+
++cnt;
107+
for (int j = i + i; j <= n; j += i) {
108+
primes[j] = false;
109+
}
110+
}
111+
}
112+
return cnt;
113+
}
114+
}
115+
```
116+
117+
### **C++**
118+
119+
```cpp
120+
using ll = long long;
121+
const int MOD = 1e9 + 7;
122+
123+
class Solution {
124+
public:
125+
int numPrimeArrangements(int n) {
126+
int cnt = count(n);
127+
ll ans = f(cnt) * f(n - cnt);
128+
return (int) (ans % MOD);
129+
}
130+
131+
ll f(int n) {
132+
ll ans = 1;
133+
for (int i = 2; i <= n; ++i) ans = (ans * i) % MOD;
134+
return ans;
135+
}
136+
137+
int count(int n) {
138+
vector<bool> primes(n + 1, true);
139+
int cnt = 0;
140+
for (int i = 2; i <= n; ++i)
141+
{
142+
if (primes[i])
143+
{
144+
++cnt;
145+
for (int j = i + i; j <= n; j += i) primes[j] = false;
146+
}
147+
}
148+
return cnt;
149+
}
150+
};
151+
```
57152
153+
### **Go**
154+
155+
```go
156+
func numPrimeArrangements(n int) int {
157+
count := func(n int) int {
158+
cnt := 0
159+
primes := make([]bool, n+1)
160+
for i := range primes {
161+
primes[i] = true
162+
}
163+
for i := 2; i <= n; i++ {
164+
if primes[i] {
165+
cnt++
166+
for j := i + i; j <= n; j += i {
167+
primes[j] = false
168+
}
169+
}
170+
}
171+
return cnt
172+
}
173+
174+
mod := int(1e9) + 7
175+
f := func(n int) int {
176+
ans := 1
177+
for i := 2; i <= n; i++ {
178+
ans = (ans * i) % mod
179+
}
180+
return ans
181+
}
182+
183+
cnt := count(n)
184+
ans := f(cnt) * f(n-cnt)
185+
return ans % mod
186+
}
58187
```
59188

60189
### **...**

0 commit comments

Comments
 (0)