Skip to content

Added Tutorial LOJ-1234 #456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions 1234/en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# LOJ 1234 - Harmonic Number

## Solution
We won't be able to iterate from 1 to n for each test-case independently, since it will not fit the time limit. What we can do is iterate once from `n=1` to `1e8` (max) and compute `H(n)` for each of them using `H(n) = H(n-1) + 1/n`. Whenever we come across a `n` that is queried in the input, we will save the value for output. After the iteration has been completed, we will output all the queries together.

### Complexity
- Time Complexity: `O(MAX(N))`.
- Memory Complexity: `O(T)`.

## C++ Code

```cpp
#include <bits/stdc++.h>

using namespace std;


int main() {

// For fast I/O
ios_base::sync_with_stdio(false);
cin.tie(nullptr);

const int MAXN = 1e8;

int t;
cin >> t;

// 1-based indexing
vector <pair <int, int>> N(t+1);
for(int i = 1; i <= t; ++i) {
cin >> N[i].first;
// Saving corresponding index of the test case
N[i].second = i;
}
sort(N.begin(), N.end());

vector <double> ans(t+1);
double now = 0;
int idx = 1;
for(int i = 1; i <= MAXN; ++i) {
now += 1.0 / i;
// Could be duplicates, that's why using 'while' loop
while (idx < (int)N.size() && N[idx].first == i) {
ans[N[idx++].second] = now;
}
}

for(int ts = 1; ts <= t; ++ts) {
cout << fixed << setprecision(10) << "Case " << ts << ": " << ans[ts] << '\n';
}

return 0;
}
```