Skip to content

Commit 999bada

Browse files
committed
feat: add solutions to lc problem: No.0793
No.0793.Preimage Size of Factorial Zeroes Function
1 parent b66ec4d commit 999bada

File tree

6 files changed

+305
-0
lines changed

6 files changed

+305
-0
lines changed

solution/0700-0799/0793.Preimage Size of Factorial Zeroes Function/README.md

+117
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,139 @@
5050

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

53+
**方法一:二分查找**
54+
55+
定义 $f(x)$ 为 $x!$ 末尾零的个数,那么
56+
57+
$$
58+
f(x)=
59+
\begin{cases}
60+
0, x=0\\
61+
x/5+f(x/5), x>0
62+
\end{cases}
63+
$$
64+
65+
定义 $g(k)$ 表示 $x!$ 末尾为零的个数为 $k$ 的最小的 $x$ 值,那么题目等价于求解 $g(k+1)-g(k)$。
66+
67+
由于 $g(k)$ 是单调递增的,因此可以使用二分查找求解 $g(k)$。
68+
69+
同时,由于 $f(x)=x/5+f(x/5) \ge x/5$,因此 $f(5k)\ge k$。所以,求解 $g(k)$ 时,二分的右边界可以取 $5k$。
70+
71+
时间复杂度 $O(log^2k)$,其中 $k$ 为题目给定的整数。二分查找 $g(k)$ 的时间复杂度为 $O(logk)$,计算 $f(x)$ 的时间复杂度为 $O(logx)$,因此总时间复杂度为 $O(log^2k)$。
72+
5373
<!-- tabs:start -->
5474

5575
### **Python3**
5676

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

5979
```python
80+
class Solution:
81+
def preimageSizeFZF(self, k: int) -> int:
82+
def f(x):
83+
if x == 0:
84+
return 0
85+
return x // 5 + f(x // 5)
86+
87+
def g(k):
88+
return bisect_left(range(5 * k), k, key=f)
6089

90+
return g(k + 1) - g(k)
6191
```
6292

6393
### **Java**
6494

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

6797
```java
98+
class Solution {
99+
public int preimageSizeFZF(int k) {
100+
return g(k + 1) - g(k);
101+
}
102+
103+
private int g(int k) {
104+
long left = 0, right = 5 * k;
105+
while (left < right) {
106+
long mid = (left + right) >> 1;
107+
if (f(mid) >= k) {
108+
right = mid;
109+
} else {
110+
left = mid + 1;
111+
}
112+
}
113+
return (int) left;
114+
}
115+
116+
private int f(long x) {
117+
if (x == 0) {
118+
return 0;
119+
}
120+
return (int) (x / 5) + f(x / 5);
121+
}
122+
}
123+
```
124+
125+
### **C++**
126+
127+
```cpp
128+
class Solution {
129+
public:
130+
int preimageSizeFZF(int k) {
131+
return g(k + 1) - g(k);
132+
}
133+
134+
int g(int k) {
135+
long long left = 0, right = 1ll * 5 * k;
136+
while (left < right) {
137+
long long mid = (left + right) >> 1;
138+
if (f(mid) >= k) {
139+
right = mid;
140+
} else {
141+
left = mid + 1;
142+
}
143+
}
144+
return (int) left;
145+
}
146+
147+
int f(long x) {
148+
int res = 0;
149+
while (x) {
150+
x /= 5;
151+
res += x;
152+
}
153+
return res;
154+
}
155+
};
156+
```
68157
158+
### **Go**
159+
160+
```go
161+
func preimageSizeFZF(k int) int {
162+
f := func(x int) int {
163+
res := 0
164+
for x != 0 {
165+
x /= 5
166+
res += x
167+
}
168+
return res
169+
}
170+
171+
g := func(k int) int {
172+
left, right := 0, k*5
173+
for left < right {
174+
mid := (left + right) >> 1
175+
if f(mid) >= k {
176+
right = mid
177+
} else {
178+
left = mid + 1
179+
}
180+
}
181+
return left
182+
}
183+
184+
return g(k+1) - g(k)
185+
}
69186
```
70187

71188
### **...**

solution/0700-0799/0793.Preimage Size of Factorial Zeroes Function/README_EN.md

+99
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,117 @@
4545

4646
## Solutions
4747

48+
Binary search.
49+
4850
<!-- tabs:start -->
4951

5052
### **Python3**
5153

5254
```python
55+
class Solution:
56+
def preimageSizeFZF(self, k: int) -> int:
57+
def f(x):
58+
if x == 0:
59+
return 0
60+
return x // 5 + f(x // 5)
61+
62+
def g(k):
63+
return bisect_left(range(5 * k), k, key=f)
5364

65+
return g(k + 1) - g(k)
5466
```
5567

5668
### **Java**
5769

5870
```java
71+
class Solution {
72+
public int preimageSizeFZF(int k) {
73+
return g(k + 1) - g(k);
74+
}
75+
76+
private int g(int k) {
77+
long left = 0, right = 5 * k;
78+
while (left < right) {
79+
long mid = (left + right) >> 1;
80+
if (f(mid) >= k) {
81+
right = mid;
82+
} else {
83+
left = mid + 1;
84+
}
85+
}
86+
return (int) left;
87+
}
88+
89+
private int f(long x) {
90+
if (x == 0) {
91+
return 0;
92+
}
93+
return (int) (x / 5) + f(x / 5);
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int preimageSizeFZF(int k) {
104+
return g(k + 1) - g(k);
105+
}
106+
107+
int g(int k) {
108+
long long left = 0, right = 1ll * 5 * k;
109+
while (left < right) {
110+
long long mid = (left + right) >> 1;
111+
if (f(mid) >= k) {
112+
right = mid;
113+
} else {
114+
left = mid + 1;
115+
}
116+
}
117+
return (int) left;
118+
}
119+
120+
int f(long x) {
121+
int res = 0;
122+
while (x) {
123+
x /= 5;
124+
res += x;
125+
}
126+
return res;
127+
}
128+
};
129+
```
59130
131+
### **Go**
132+
133+
```go
134+
func preimageSizeFZF(k int) int {
135+
f := func(x int) int {
136+
res := 0
137+
for x != 0 {
138+
x /= 5
139+
res += x
140+
}
141+
return res
142+
}
143+
144+
g := func(k int) int {
145+
left, right := 0, k*5
146+
for left < right {
147+
mid := (left + right) >> 1
148+
if f(mid) >= k {
149+
right = mid
150+
} else {
151+
left = mid + 1
152+
}
153+
}
154+
return left
155+
}
156+
157+
return g(k+1) - g(k)
158+
}
60159
```
61160

62161
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int preimageSizeFZF(int k) {
4+
return g(k + 1) - g(k);
5+
}
6+
7+
int g(int k) {
8+
long long left = 0, right = 1ll * 5 * k;
9+
while (left < right) {
10+
long long mid = (left + right) >> 1;
11+
if (f(mid) >= k) {
12+
right = mid;
13+
} else {
14+
left = mid + 1;
15+
}
16+
}
17+
return (int) left;
18+
}
19+
20+
int f(long x) {
21+
int res = 0;
22+
while (x) {
23+
x /= 5;
24+
res += x;
25+
}
26+
return res;
27+
}
28+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func preimageSizeFZF(k int) int {
2+
f := func(x int) int {
3+
res := 0
4+
for x != 0 {
5+
x /= 5
6+
res += x
7+
}
8+
return res
9+
}
10+
11+
g := func(k int) int {
12+
left, right := 0, k*5
13+
for left < right {
14+
mid := (left + right) >> 1
15+
if f(mid) >= k {
16+
right = mid
17+
} else {
18+
left = mid + 1
19+
}
20+
}
21+
return left
22+
}
23+
24+
return g(k+1) - g(k)
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int preimageSizeFZF(int k) {
3+
return g(k + 1) - g(k);
4+
}
5+
6+
private int g(int k) {
7+
long left = 0, right = 5 * k;
8+
while (left < right) {
9+
long mid = (left + right) >> 1;
10+
if (f(mid) >= k) {
11+
right = mid;
12+
} else {
13+
left = mid + 1;
14+
}
15+
}
16+
return (int) left;
17+
}
18+
19+
private int f(long x) {
20+
if (x == 0) {
21+
return 0;
22+
}
23+
return (int) (x / 5) + f(x / 5);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def preimageSizeFZF(self, k: int) -> int:
3+
def f(x):
4+
if x == 0:
5+
return 0
6+
return x // 5 + f(x // 5)
7+
8+
def g(k):
9+
return bisect_left(range(5 * k), k, key=f)
10+
11+
return g(k + 1) - g(k)

0 commit comments

Comments
 (0)