#include <algorithm> #include <iostream> #include <unordered_map> #include <vector> using namespace std; int main() { cin.tie(0)->sync_with_stdio(false); int n, k; cin >> n; unordered_map<int, int> freq; for (int i = 0; i < n; i++) { cin >> k; freq[k]++; } vector<int> counts; transform(freq.begin(), freq.end(), back_inserter(counts), [](auto &kv) { return kv.second; }); sort(counts.begin(), counts.end()); int l = counts.size(); vector<int> prefix(n + 1, 0); for (int i = 1; i <= n; ++i) { prefix[i] = prefix[i - 1]; while (prefix[i] < l && counts[prefix[i]] < i) prefix[i]++; } vector<int> totals(n + 1, n); for (int i = 2; i <= n; ++i) { int q = 0; for (int j = i; j <= n; j += i) q += (l - prefix[j]) * i; totals[i] = q; } for (int i = 1; i <= n; ++i) { cout << totals[i] << " "; } cout << endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include <algorithm> #include <iostream> #include <unordered_map> #include <vector> using namespace std; int main() { cin.tie(0)->sync_with_stdio(false); int n, k; cin >> n; unordered_map<int, int> freq; for (int i = 0; i < n; i++) { cin >> k; freq[k]++; } vector<int> counts; transform(freq.begin(), freq.end(), back_inserter(counts), [](auto &kv) { return kv.second; }); sort(counts.begin(), counts.end()); int l = counts.size(); vector<int> prefix(n + 1, 0); for (int i = 1; i <= n; ++i) { prefix[i] = prefix[i - 1]; while (prefix[i] < l && counts[prefix[i]] < i) prefix[i]++; } vector<int> totals(n + 1, n); for (int i = 2; i <= n; ++i) { int q = 0; for (int j = i; j <= n; j += i) q += (l - prefix[j]) * i; totals[i] = q; } for (int i = 1; i <= n; ++i) { cout << totals[i] << " "; } cout << endl; return 0; } |