Skip to content

Commit 1f19428

Browse files
authored
Merge branch 'doocs:main' into main
2 parents 3368e73 + 6c933c5 commit 1f19428

File tree

6 files changed

+46
-9
lines changed

6 files changed

+46
-9
lines changed

.github/workflows/deploy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ jobs:
2121
path: mkdocs
2222
- run: |
2323
mv -f mkdocs/* .
24+
mv solution/CONTEST_README.md docs/contest.md
25+
mv solution/CONTEST_README_EN.md docs-en/contest.md
2426
- name: Configure Git Credentials
2527
run: |
2628
git config user.name github-actions[bot]

.gitignore

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55
.cache
66
/node_modules
77
/solution/result.json
8-
/solution/contest.json
9-
/solution/contest_list.json
10-
/solution/raw.json
11-
/lcof/lcof.json
12-
/lcof/lcof_list.json
13-
/lcci/lcci.json
148
/solution/__pycache__
159
/solution/.env
1610
*.iml

solution/1700-1799/1780.Check if Number is a Sum of Powers of Three/README_EN.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@
4343

4444
## Solutions
4545

46-
### Solution 1
46+
### Solution 1: Mathematical Analysis
47+
48+
We find that if a number $n$ can be expressed as the sum of several "different" powers of three, then in the ternary representation of $n$, each digit can only be $0$ or $1$.
49+
50+
Therefore, we convert $n$ to ternary and then check whether each digit is $0$ or $1$. If not, then $n$ cannot be expressed as the sum of several powers of three, and we directly return `false`; otherwise, $n$ can be expressed as the sum of several powers of three, and we return `true`.
51+
52+
The time complexity is $O(\log_3 n)$, and the space complexity is $O(1)$.
4753

4854
<!-- tabs:start -->
4955

solution/1700-1799/1782.Count Pairs Of Nodes/README_EN.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,15 @@ The answers for each of the queries are as follows:
5454

5555
## Solutions
5656

57-
### Solution 1
57+
### Solution 1: Hash Table + Sorting + Binary Search
58+
59+
From the problem, we know that the number of edges connected to the point pair $(a, b)$ is equal to the "number of edges connected to $a$" plus the "number of edges connected to $b$", minus the number of edges connected to both $a$ and $b$.
60+
61+
Therefore, we can first use the array $cnt$ to count the number of edges connected to each point, and use the hash table $g$ to count the number of each point pair.
62+
63+
Then, for each query $q$, we can enumerate $a$. For each $a$, we can find the first $b$ that satisfies $cnt[a] + cnt[b] > q$ through binary search, add the number to the current query answer, and then subtract some duplicate edges.
64+
65+
The time complexity is $O(q \times (n \times \log n + m))$, and the space complexity is $O(n + m)$. Where $n$ and $m$ are the number of points and edges respectively, and $q$ is the number of queries.
5866

5967
<!-- tabs:start -->
6068

solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/README_EN.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,33 @@
4444

4545
## Solutions
4646

47-
### Solution 1
47+
### Solution 1: Dynamic Programming
48+
49+
Notice that after modifying the array `nums`, the XOR result of any interval of length $k$ is equal to $0$. Therefore, for any $i$, we have:
50+
51+
$$
52+
nums[i] \oplus nums[i+1] \oplus ... \oplus nums[i+k-1] = 0
53+
$$
54+
55+
and
56+
57+
$$
58+
nums[i+1] \oplus nums[i+2] \oplus ... \oplus nums[i+k] = 0
59+
$$
60+
61+
Combining the two equations and the properties of XOR operation, we can get $nums[i] \oplus nums[i+k] = 0$, which means $nums[i]=nums[i+k]$. We find that the elements in the modified array `nums` are cyclic with a period of $k$. The numbers congruent modulo $k$ can only take a fixed value, and the XOR result of the first $k$ numbers must be $0$.
62+
63+
First, we count each group $i$, the number of elements in each group is $size[i]$, and the number of elements with value $v$ in each group is $cnt[i][v]$.
64+
65+
Next, we can use dynamic programming to solve it. Let $f[i][j]$ represent the minimum number of modifications with the XOR sum of the first $i+1$ groups being $j$. Since the value of each group is only related to the value of the previous group, we can use a rolling array to optimize the space complexity.
66+
67+
Redefine $f[j]$ to represent the minimum number of modifications with the XOR sum being $j$ when processing to the current group.
68+
69+
When transitioning states, there are two choices: one is to modify all the numbers in the current group to the same value, then we can choose the one with the smallest previous cost, plus the number of elements $size[i]$ in this group, the cost is $\min{f[0..n]} + size[i]$; the second is to modify all the numbers in the current group to some value $j$ of the current group, enumerate $j$ and the element $v$ of the current group, then the previous cost is $f[j \oplus v]$, the cost is $f[j \oplus v] + size[i] - cnt[i][v]$. Take the minimum value.
70+
71+
The final answer is $f[0]$.
72+
73+
The time complexity is $O(2^{C}\times k + n)$. Where $n$ is the length of the array `nums`, and $C$ is the maximum number of bits in the binary representation of the elements in `nums`, in this problem $C=10$.
4874

4975
<!-- tabs:start -->
5076

solution/contest.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)