mirror of
https://github.com/calofmijuck/blog.git
synced 2025-12-06 14:53:50 +00:00
feat: breaking change (unstable) (#198)
* [PUBLISHER] upload files #175 * PUSH NOTE : 3. Symmetric Key Encryption.md * PUSH NOTE : 03. Symmetric Key Cryptography (2).md * DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md * DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-19-symmetric-key-encryption.md * [PUBLISHER] upload files #177 * PUSH NOTE : 3. Symmetric Key Encryption.md * PUSH NOTE : 03. Symmetric Key Cryptography (2).md * DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md * DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-19-symmetric-key-encryptio.md * [PUBLISHER] upload files #178 * PUSH NOTE : 3. Symmetric Key Encryption.md * PUSH NOTE : 03. Symmetric Key Cryptography (2).md * DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md * [PUBLISHER] upload files #179 * PUSH NOTE : 3. Symmetric Key Encryption.md * PUSH NOTE : 03. Symmetric Key Cryptography (2).md * DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md * [PUBLISHER] upload files #180 * PUSH NOTE : 3. Symmetric Key Encryption.md * PUSH NOTE : 03. Symmetric Key Cryptography (2).md * DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md * [PUBLISHER] upload files #181 * PUSH NOTE : 3. Symmetric Key Encryption.md * PUSH NOTE : 03. Symmetric Key Cryptography (2).md * DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md * [PUBLISHER] upload files #182 * PUSH NOTE : 3. Symmetric Key Encryption.md * PUSH NOTE : 03. Symmetric Key Cryptography (2).md * [PUBLISHER] upload files #183 * PUSH NOTE : 3. Symmetric Key Encryption.md * PUSH NOTE : 03. Symmetric Key Cryptography (2).md * DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md * [PUBLISHER] upload files #184 * PUSH NOTE : 3. Symmetric Key Encryption.md * PUSH NOTE : 03. Symmetric Key Cryptography (2).md * DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md * [PUBLISHER] upload files #185 * PUSH NOTE : 3. Symmetric Key Encryption.md * PUSH NOTE : 03. Symmetric Key Cryptography (2).md * DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md * [PUBLISHER] upload files #186 * PUSH NOTE : 3. Symmetric Key Encryption.md * PUSH NOTE : 03. Symmetric Key Cryptography (2).md * [PUBLISHER] upload files #187 * PUSH NOTE : 3. Symmetric Key Encryption.md * PUSH NOTE : 14. Secure Multiparty Computation.md * DELETE FILE : _posts/Lecture Notes/Modern Cryptography/2023-09-19-symmetric-key-encryption.md * DELETE FILE : _posts/lecture-notes/modern-cryptography/2023-09-18-symmetric-key-cryptography-2.md * [PUBLISHER] upload files #188 * PUSH NOTE : 3. Symmetric Key Encryption.md * PUSH NOTE : 14. Secure Multiparty Computation.md * DELETE FILE : _posts/Lecture Notes/Modern Cryptography/2023-09-19-symmetric-key-encryption.md * chore: remove files * [PUBLISHER] upload files #197 * PUSH NOTE : 수학 공부에 대한 고찰.md * PUSH NOTE : 09. Lp Functions.md * PUSH ATTACHMENT : mt-09.png * PUSH NOTE : 08. Comparison with the Riemann Integral.md * PUSH ATTACHMENT : mt-08.png * PUSH NOTE : 04. Measurable Functions.md * PUSH ATTACHMENT : mt-04.png * PUSH NOTE : 06. Convergence Theorems.md * PUSH ATTACHMENT : mt-06.png * PUSH NOTE : 07. Dominated Convergence Theorem.md * PUSH ATTACHMENT : mt-07.png * PUSH NOTE : 05. Lebesgue Integration.md * PUSH ATTACHMENT : mt-05.png * PUSH NOTE : 03. Measure Spaces.md * PUSH ATTACHMENT : mt-03.png * PUSH NOTE : 02. Construction of Measure.md * PUSH ATTACHMENT : mt-02.png * PUSH NOTE : 01. Algebra of Sets and Set Functions.md * PUSH ATTACHMENT : mt-01.png * PUSH NOTE : Rules of Inference with Coq.md * PUSH NOTE : 블로그 이주 이야기.md * PUSH NOTE : Secure IAM on AWS with Multi-Account Strategy.md * PUSH ATTACHMENT : separation-by-product.png * PUSH NOTE : You and Your Research, Richard Hamming.md * PUSH NOTE : 10. Digital Signatures.md * PUSH ATTACHMENT : mc-10-dsig-security.png * PUSH ATTACHMENT : mc-10-schnorr-identification.png * PUSH NOTE : 9. Public Key Encryption.md * PUSH ATTACHMENT : mc-09-ss-pke.png * PUSH NOTE : 8. Number Theory.md * PUSH NOTE : 7. Key Exchange.md * PUSH ATTACHMENT : mc-07-dhke.png * PUSH ATTACHMENT : mc-07-dhke-mitm.png * PUSH ATTACHMENT : mc-07-merkle-puzzles.png * PUSH NOTE : 6. Hash Functions.md * PUSH ATTACHMENT : mc-06-merkle-damgard.png * PUSH ATTACHMENT : mc-06-davies-meyer.png * PUSH ATTACHMENT : mc-06-hmac.png * PUSH NOTE : 5. CCA-Security and Authenticated Encryption.md * PUSH ATTACHMENT : mc-05-ci.png * PUSH ATTACHMENT : mc-05-etm-mte.png * PUSH NOTE : 1. OTP, Stream Ciphers and PRGs.md * PUSH ATTACHMENT : mc-01-prg-game.png * PUSH ATTACHMENT : mc-01-ss.png * PUSH NOTE : 4. Message Authentication Codes.md * PUSH ATTACHMENT : mc-04-mac.png * PUSH ATTACHMENT : mc-04-mac-security.png * PUSH ATTACHMENT : mc-04-cbc-mac.png * PUSH ATTACHMENT : mc-04-ecbc-mac.png * PUSH NOTE : 3. Symmetric Key Encryption.md * PUSH ATTACHMENT : is-03-ecb-encryption.png * PUSH ATTACHMENT : is-03-cbc-encryption.png * PUSH ATTACHMENT : is-03-ctr-encryption.png * PUSH NOTE : 2. PRFs, PRPs and Block Ciphers.md * PUSH ATTACHMENT : mc-02-block-cipher.png * PUSH ATTACHMENT : mc-02-feistel-network.png * PUSH ATTACHMENT : mc-02-des-round.png * PUSH ATTACHMENT : mc-02-DES.png * PUSH ATTACHMENT : mc-02-aes-128.png * PUSH ATTACHMENT : mc-02-2des-mitm.png * PUSH NOTE : 18. Bootstrapping & CKKS.md * PUSH NOTE : 17. BGV Scheme.md * PUSH NOTE : 16. The GMW Protocol.md * PUSH ATTACHMENT : mc-16-beaver-triple.png * PUSH NOTE : 15. Garbled Circuits.md * PUSH NOTE : 14. Secure Multiparty Computation.md * PUSH NOTE : 13. Sigma Protocols.md * PUSH ATTACHMENT : mc-13-sigma-protocol.png * PUSH ATTACHMENT : mc-13-okamoto.png * PUSH ATTACHMENT : mc-13-chaum-pedersen.png * PUSH ATTACHMENT : mc-13-gq-protocol.png * PUSH NOTE : 12. Zero-Knowledge Proofs (Introduction).md * PUSH ATTACHMENT : mc-12-id-protocol.png * PUSH NOTE : 11. Advanced Topics.md * PUSH NOTE : 0. Introduction.md * PUSH NOTE : 02. Symmetric Key Cryptography (1).md * PUSH NOTE : 09. Transport Layer Security.md * PUSH ATTACHMENT : is-09-tls-handshake.png * PUSH NOTE : 08. Public Key Infrastructure.md * PUSH ATTACHMENT : is-08-certificate-validation.png * PUSH NOTE : 07. Public Key Cryptography.md * PUSH NOTE : 06. RSA and ElGamal Encryption.md * PUSH NOTE : 05. Modular Arithmetic (2).md * PUSH NOTE : 03. Symmetric Key Cryptography (2).md * PUSH ATTACHMENT : is-03-feistel-function.png * PUSH ATTACHMENT : is-03-cfb-encryption.png * PUSH ATTACHMENT : is-03-ofb-encryption.png * PUSH NOTE : 04. Modular Arithmetic (1).md * PUSH NOTE : 01. Security Introduction.md * PUSH ATTACHMENT : is-01-cryptosystem.png * PUSH NOTE : Search Time in Hash Tables.md * PUSH NOTE : 랜덤 PS일지 (1).md * chore: rearrange articles * feat: fix paths * feat: fix all broken links * feat: title font to palatino
This commit is contained in:
114
_posts/algorithms/boj/2023-11-02-random-ps-1.md
Normal file
114
_posts/algorithms/boj/2023-11-02-random-ps-1.md
Normal file
@@ -0,0 +1,114 @@
|
||||
---
|
||||
share: true
|
||||
toc: true
|
||||
math: true
|
||||
categories:
|
||||
- Algorithms
|
||||
- BOJ
|
||||
path: _posts/algorithms/boj
|
||||
tags:
|
||||
- algorithms
|
||||
- boj
|
||||
title: 랜덤 PS일지 (1)
|
||||
date: 2023-11-02
|
||||
github_title: 2023-11-02-random-ps-1
|
||||
---
|
||||
|
||||
중간고사 끝난 것을 기념으로! 근데 첫 문제는 시험 전날에 공부하기 싫어서 잡았다.
|
||||
|
||||
## 30323번
|
||||
|
||||
- [BOJ 30323](https://www.acmicpc.net/problem/30323): Exponentiation
|
||||
|
||||
주어진 $\alpha = x + x^{-1}$와 $\beta$에 대하여 $x^\beta + x^{-\beta} \pmod m$을 구하면 된다.
|
||||
|
||||
먼 옛날 곱셈 공식의 변형
|
||||
|
||||
$$
|
||||
x^2 + \frac{1}{x^2} = \left( x + \frac{1}{x} \right)^2 - 2
|
||||
$$
|
||||
|
||||
을 공부하던 기억을 떠올리며...
|
||||
|
||||
$\beta$가 짝수인 경우에는 쉽게 처리할 수 있다.
|
||||
|
||||
$$
|
||||
x^\beta + x^{-\beta} = (x^{\beta/2} + x^{-\beta/2})^2 - 2.
|
||||
$$
|
||||
|
||||
홀수인 경우에는[^1] 지수의 크기를 절반으로 줄이는 것이 핵심일테니,
|
||||
|
||||
$$
|
||||
x^\beta + x^{-\beta} = \left( x^{\frac{\beta+1}{2}} + x^{-\frac{\beta+1}{2}}\right)\left( x^{\frac{\beta-1}{2}} + x^{-\frac{\beta-1}{2}}\right) - (x + x^{-1})
|
||||
$$
|
||||
|
||||
를 생각하면 된다. 같은 지수에 대해서 여러 번 계산을 할 수도 있으므로, 결과를 저장해뒀다가 꺼내서 쓰도록 하자.
|
||||
|
||||
## 12728, 12925번
|
||||
|
||||
- [BOJ 12728](https://www.acmicpc.net/problem/12728): $n$제곱 계산
|
||||
- [BOJ 12925](https://www.acmicpc.net/problem/12925): Numbers
|
||||
|
||||
두 문제가 같은 문제이다. $(3 + \sqrt{5})^n$의 정수부분의 마지막 $3$자리를 구하면 된다.
|
||||
|
||||
중학교 시절 에이급 수학에서 $(3 + 2\sqrt{2})^5$의 정수부분을 구하라는 문제를 봤었는데 이 때 사용했던 아이디어가 켤레무리수를 생각하는 것이었다. 비슷한 아이디어를 2017학년도 서울대학교 공과대학 수시 일반 심층 면접에서도 $(2 + \sqrt{5})^n$이 나와 사용했었다. 그리고...
|
||||
|
||||
> **정리.** $\alpha = 3 + \sqrt{5}$, $\beta = 3 - \sqrt{5}$ 일 때, $\alpha^n + \beta^n \in \mathbb{N}$ for all $n \in \mathbb{N}$.[^2]
|
||||
|
||||
여기서 핵심은 $0 < \beta < 1$ 임을 이용하는 것이다. 따라서, $\alpha^n$의 정수부분은 $\alpha^n + \beta^n - 1$이 된다. 이제 $\alpha^n + \beta^n$만 구하면 된다. 근과 계수의 관계를 이용하면 수열 $s _ n = \alpha^n + \beta^n$에 대한 귀납적 정의를 얻을 수 있다.
|
||||
|
||||
$n$이 크기 때문에 계산은 행렬을 빠르게 거듭제곱하는 형태를 사용하면 된다.
|
||||
|
||||
## 4215번
|
||||
|
||||
- [BOJ 4215](https://www.acmicpc.net/problem/4215): To Add or to Multiply
|
||||
|
||||
우선 주어진 범위 내에서 가능한 곱셈의 횟수는 최대 $29$번이다. ($2^{30} > 10^9$) 그러므로 모든 $m$에 대해 조사할 시간은 충분하다.
|
||||
|
||||
곱셈을 $g$번 한다고 가정하고, 이 때 $0, \dots, g$ 번째 덧셈 횟수를 $h _ i$라 하자. 그렇다면 입력이 $x$일 때, 우리가 생성한 프로그램의 최종 값은
|
||||
|
||||
$$
|
||||
x \cdot m^g + a \sum _ {i=0}^g m^{g-i} h _ i
|
||||
$$
|
||||
|
||||
가 된다. 입력 범위 $[p, q]$를 $[r, s]$로 보내야 하므로, 부등식을 세워보면
|
||||
|
||||
$$
|
||||
\tag{1}
|
||||
a\sum _ {i=0}^g m^{g-i} h _ i \in \left[ r - p \cdot m^g, s - q\cdot m^g \right]
|
||||
$$
|
||||
|
||||
여야 함을 알 수 있다. 그러면 이제 프로그램의 길이를 최소화 해야하는데, 길이가 $\sum _ {i=0}^g h _ i + g$ 이므로 $h _ i$의 합을 최소화하는 방향으로 생각해본다. 좌변의 식을 조금 풀어서 써보면
|
||||
|
||||
$$
|
||||
a\sum _ {i=0}^g m^{g-i} h _ i =(am^g)h _ 0 + (am^{g-1})h _ 1 + \cdots + (a)h _ g
|
||||
$$
|
||||
|
||||
이므로 $\sum h _ i$를 최소화 하려면 $h _ 0$부터 $h _ g$까지 순서대로 잡아주면 된다. 이 때 $(1)$의 범위를 만족해야 하므로, 범위를 만족하는 최소의 $h _ 0$가 잡히면 바로 끝. 만약 불가능하다면 $r - p\cdot m^g$를 넘지 않는 최대의 $h _ 0$를 잡고 이번에는 구간 양 끝에서 $am^gh _ 0$를 뺀 뒤 같은 방법으로 $h _ 1$을 잡으면 된다.
|
||||
|
||||
모든 가능한 프로그램의 후보를 얻었다면, 가장 짧은 것을 찾고 사전 순으로 제일 먼저 오는 것을 찾으면 된다. 사전 순 정렬의 경우 귀납적으로 생각하면 쉽게 구현할 수 있다. 앞에서부터 연산의 종류와 횟수를 비교하면 된다.
|
||||
|
||||
## 13174번
|
||||
|
||||
- [BOJ 13174](https://www.acmicpc.net/problem/13174): 괄호
|
||||
|
||||
어차피 palindrome이니 절반을 정해주면 나머지는 자동으로 결정된다. 그러므로 길이 $n$인 괄호 문자열의 임의의 prefix에 대해 `)`의 개수는 `(`의 개수를 넘을 수 없다.
|
||||
|
||||
이는 [Catalan's triangle](https://en.wikipedia.org/wiki/Catalan%27s_triangle)의 응용이다. $i$개의 `(`와 $n-i$개의 `)`로 길이 $n$인 괄호 문자열을 구성하고, $k$개의 색으로 칠한다고 했으니 정답은
|
||||
|
||||
$$
|
||||
\sum _ {i=\lceil n/2\rceil}^n C(i, n-i)\cdot k^i
|
||||
$$
|
||||
|
||||
이다. 색칠하는 방법의 수가 $k^i$인 이유는 각 `)`가 짝이 되는 `(`와 색이 같아야 하므로 `(`의 색만 정하면 되기 때문이다.
|
||||
|
||||
계산에는
|
||||
|
||||
$$
|
||||
C(n, k) = \frac{n-k+1}{n+1} {n+k \choose k}
|
||||
$$
|
||||
|
||||
를 사용하면 된다.
|
||||
|
||||
[^1]: 원래 빠른 거듭제곱을 할 때는 $a^n = a \cdot (a^2)^{(n-1)/2}$ 으로 했던 것 같은데 이 경우에는 잘 안되므로...
|
||||
[^2]: 증명은 귀납법. 이항정리를 써도 좋고, 수열의 귀납적 정의를 사용해도 좋다.
|
||||
@@ -0,0 +1,114 @@
|
||||
---
|
||||
share: true
|
||||
toc: true
|
||||
math: true
|
||||
categories:
|
||||
- Algorithms
|
||||
- Data Structures
|
||||
path: _posts/algorithms/data-structures
|
||||
tags:
|
||||
- algorithms
|
||||
- data-structures
|
||||
title: Search Time in Hash Tables
|
||||
date: 2024-04-12
|
||||
github_title: 2024-04-12-search-time-hash-tables
|
||||
---
|
||||
|
||||
Here are the expected time complexities of the search operation in hash tables.
|
||||
|
||||
## Assumptions
|
||||
|
||||
- Let $m$ be the number of buckets in the hash table.
|
||||
- Let $n$ be the number of entries currently in the hash table.
|
||||
- Let $\alpha = n/m$ be the *load factor*.
|
||||
- Elements are uniformly hashed to each bucket of the hash table.
|
||||
|
||||
These results imply that the `search` operation takes almost constant time.
|
||||
|
||||
## Hashing with Chaining
|
||||
|
||||
### Unsuccessful Search
|
||||
|
||||
> **Theorem.** The expected time complexity of an unsuccessful search in a hash table using chaining is $\alpha$.
|
||||
|
||||
*Proof*. Observe that since elements are hashed uniformly into each bucket, the expected number of elements in each bucket is the same for all buckets. By linearity of expectation, their sum should equal $n$. So each bucket has $\alpha = n/m$ expected number of elements. Thus, on an unsuccessful search, we search at most $\alpha$ elements.
|
||||
|
||||
### Successful Search
|
||||
|
||||
> **Theorem.** The expected time complexity of a successful search in a hash table using chaining is $1 + \frac{\alpha}{2} - \frac{\alpha}{2n}$.
|
||||
|
||||
*Proof*. Suppose we are looking for the element $x$. The number of elements to search is determined by the number of elements inserted after $x$, whose hash collided with $x$.
|
||||
|
||||
The probability of collision is $\frac{1}{m}$, since the hash function is uniform by assumption. If $x$ was inserted as the $i$-th element, the number of elements to search equals
|
||||
|
||||
$$
|
||||
1 + \sum _ {j = i + 1}^n \frac{1}{m}.
|
||||
$$
|
||||
|
||||
The additional $1$ comes from searching $x$ itself. Averaging over all $i$ gives the final result.
|
||||
|
||||
$$
|
||||
\begin{aligned}
|
||||
\frac{1}{n}\sum _ {i=1}^n \paren{1 + \sum _ {j=i+1}^n \frac{1}{m}} &= 1 + \frac{1}{mn} \sum _ {i=1}^n \sum _ {j=i+1}^n 1 \\
|
||||
&= 1 + \frac{1}{mn}\paren{n^2 - \frac{n(n+1)}{2}} \\
|
||||
&= 1 + \frac{n(n-1)}{2mn} \\
|
||||
&= 1+ \frac{(n-1)\alpha}{2n} = 1+ \frac{\alpha}{2} - \frac{\alpha}{2n}.
|
||||
\end{aligned}
|
||||
$$
|
||||
|
||||
## Hashing with Open Addressing
|
||||
|
||||
For open addressing, we first assume that $\alpha < 1$. The case $\alpha = 1$ will be handled separately. Also, we assume no deletion.
|
||||
|
||||
### Unsuccessful Search
|
||||
|
||||
> **Theorem.** The expected time complexity of an unsuccessful search in a hash table using open addressing is $\frac{1}{1-\alpha}$.
|
||||
|
||||
*Proof*. Let the random variable $X$ be the number of probes made in an unsuccessful search. We want to find $\bf{E}[X]$, so we use the identity
|
||||
|
||||
$$
|
||||
\bf{E}[X] = \sum _ {i \geq 1} \Pr[X \geq i].
|
||||
$$
|
||||
|
||||
We want to find a bound for $\Pr[X \geq i]$. For $X \geq i$ to happen, $i - 1$ probes must fail, i.e., it must probe to an occupied bucket. On the $j$-th probe, there are $m - j + 1$ buckets left to be probed, and $n - j + 1$ elements not probed yet. Thus the $j$-th probe fails with probability $\frac{n - j + 1}{m - j + 1} < \frac{n}{m}$. Therefore,
|
||||
|
||||
$$
|
||||
\begin{aligned}
|
||||
\Pr[X \geq i] &= \frac{n}{m} \cdot \frac{n - 1}{m - 1} \cdot \cdots \cdot \frac{n - (i - 2)}{m - (i - 2)} \\
|
||||
&\leq \paren{\frac{n}{m}}^{i-1} = \alpha^{i - 1}.
|
||||
\end{aligned}
|
||||
$$
|
||||
|
||||
Now we have
|
||||
|
||||
$$
|
||||
\bf{E}[X] = \sum _ {i \geq 1} \Pr[X \geq i] \leq \sum _ {i\geq 1} \alpha^{i-1} = \frac{1}{1 - \alpha}.
|
||||
$$
|
||||
|
||||
### Successful Search
|
||||
|
||||
> **Theorem.** The expected time complexity of a successful search in a hash table using open addressing is $\frac{1}{\alpha} \log \frac{1}{1- \alpha}$.
|
||||
|
||||
*Proof*. On a successful search, the sequence of probes is exactly the same as the sequence of probes when that element was inserted.
|
||||
|
||||
Suppose that an element $x$ was the $i$-th inserted element. At the moment of insertion, the load factor is ${} \alpha _ i = (i-1)/m {}$. By the above theorem, the expected number of probes must have been ${} 1/(1 -\alpha _ i) = \frac{m}{m-(i-1)} {}$. Averaging this over all $i$ gives
|
||||
|
||||
$$
|
||||
\begin{aligned}
|
||||
\frac{1}{n} \sum _ {i=1}^n \frac{m}{m - (i - 1)} &= \frac{m}{n} \sum _ {i=0}^{n-1} \frac{1}{m - i} \\
|
||||
&\leq \frac{1}{\alpha} \int _ {m-n}^m \frac{1}{x}\,dx \\
|
||||
&= \frac{1}{\alpha} \log \frac{1}{1-\alpha}.
|
||||
\end{aligned}
|
||||
$$
|
||||
|
||||
### When the Hash Table is Full ($\alpha = 1$)
|
||||
|
||||
First of all, on an unsuccessful search, all $m$ buckets should be probed.
|
||||
|
||||
On a successful search, set $m = n$ on the above argument, then the average number of probes is
|
||||
|
||||
$$
|
||||
\frac{1}{m} \sum _ {i=1}^m \frac{m}{m - (i - 1)} = \sum _ {i=1}^m \frac{1}{i} = H _ m,
|
||||
$$
|
||||
|
||||
where $H _ m$ is the $m$-th harmonic number.
|
||||
Reference in New Issue
Block a user