forsaken喜欢数论
时间: 2023-12-17 21:28:11 浏览: 138
forsaken_mail_4baidu
根据提供的引用内容,可以看出Forsaken喜欢数论,并且在数论方面有一定的研究。以下是一些与数论相关的Forsaken的例子:
1. 线性筛法求质数
```python
def get_primes(n):
is_prime = [True] * (n + 1)
primes = []
for i in range(2, n + 1):
if is_prime[i]:
primes.append(i)
for j in range(len(primes)):
if i * primes[j] > n:
break
is_prime[i * primes[j]] = False
if i % primes[j] == 0:
break
return primes
```
2. 分组算法
```python
def group_students(students, k):
groups = [[] for _ in range(k)]
for i in range(len(students)):
groups[i % k].append(students[i])
return groups
```
3. 正方形计数
```python
def count_squares(n):
count = 0
for i in range(1, int(n ** 0.5) + 1):
count += (n // i - i + 1)
return count
```
4. 三维数点
```python
def count_3d_points(points):
bit = [0] * (max(points, key=lambda x: max(x)) + 1)
count = 0
for i in range(len(points)):
x, y, z = points[i]
count += i - sum(bit[j] for j in range(x))
for j in range(x, len(bit)):
bit[j] += 1
return count
```
5. 独一无二的最小生成树
```python
def unique_minimum_spanning_tree(n, edges):
edges.sort(key=lambda x: x[2])
parent = list(range(n))
rank = [0] * n
def find(x):
if parent[x] != x:
parent[x] = find(parent[x])
return parent[x]
def union(x, y):
px, py = find(x), find(y)
if px == py:
return False
if rank[px] < rank[py]:
parent[px] = py
elif rank[px] > rank[py]:
parent[py] = px
else:
parent[px] = py
rank[py] += 1
return True
mst = []
for u, v, w in edges:
if union(u, v):
mst.append((u, v, w))
return mst
--相关问题--:
阅读全文