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 3 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
61 changes: 61 additions & 0 deletions 1234/en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# LOJ 1234 - Harmonic Number

## Solution
From the definition of H<sub>n</sub> we perceive: **H<sub>n+1</sub> = H<sub>n</sub> + (1/ n)**

So we need not to calculate **H<sub>n</sub>** using the straighforward formula for every single test case. We go from non-decreasing order of test cases whilst keeping a variable for harmonic sum so far. This way not only we save time but memory as well, because there's no need for storing all the harmonic values beforehand.

## Complexity
- Time Complexity: O($T$).
- Memory Complexity: O($T$).

## Code

### C++

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

using namespace std;


typedef pair <int, int> pii;


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 <pii> 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. / i;
while (idx < (int)N.size() && N[idx].first == i) { // Could be duplicates, that's why using 'while' loop
ans[N[idx++].second] = now;
}
}

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

return 0;
}
```