Submission #1321383

#TimeUsernameProblemLanguageResultExecution timeMemory
1321383nathako9nSpiderman (COCI20_spiderman)C++20
70 / 70
64 ms10120 KiB
#include <iostream>
#include <vector>

using namespace std;

const int MAXH = 1000001;
int freq[MAXH];
int answers[MAXH];

int main() {
    // Optimization for fast I/O
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int N, K;
    if (!(cin >> N >> K)) return 0;

    vector<int> h(N);
    for (int i = 0; i < N; ++i) {
        cin >> h[i];
        freq[h[i]]++;
    }

    // We iterate through every possible height 'hj' that could be a divisor
    // Only hj > K can result in a remainder of K
    for (int hj = K + 1; hj < MAXH; ++hj) {
        if (freq[hj] == 0) continue;

        // Jump from hi to hj: hi % hj == K  =>  hi = m*hj + K
        // We iterate through multiples of hj
        for (int hi = K; hi < MAXH; hi += hj) {
            if (freq[hi] > 0) {
                // All skyscrapers of height 'hi' can jump to all 
                // skyscrapers of height 'hj'
                answers[hi] += freq[hj];
            }
        }
    }

    // Output the results
    for (int i = 0; i < N; ++i) {
        int res = answers[h[i]];
        
        // If K == 0, hi % hi == 0, so the skyscraper counted itself.
        // We must subtract 1 because Peter jumps to *other* skyscrapers.
        if (K == 0) {
            res--;
        }
        
        cout << res << (i == N - 1 ? "" : " ");
    }
    cout << endl;

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...