#include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; struct TestCase { size_t n; vector<int> numbers; }; TestCase read_test_case() { TestCase tc; cin >> tc.n; tc.numbers.resize(tc.n); for (auto& n : tc.numbers) cin >> n; return tc; } void solve_test_case(TestCase tc); int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); solve_test_case(read_test_case()); } void solve_test_case(TestCase tc) { map<int, int> counts; for (auto n : tc.numbers) counts[n]++; vector<int> muls(2 * tc.n + 1, 0); for (const auto& [k, v] : counts) muls[v] += 1; for (size_t i = 2; i <= 2 * tc.n; i++) muls[i] += muls[i - 1]; for (size_t k = 1; k <= tc.n; k++) { int given = 0; for (size_t m = k; m <= tc.n + k; m += k) { int cnt = muls[m - 1] - muls[m - k - 1]; given += cnt * (m - k); } cout << given << " "; } cout << "\n"; }
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 44 45 46 47 48 49 50 | #include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; struct TestCase { size_t n; vector<int> numbers; }; TestCase read_test_case() { TestCase tc; cin >> tc.n; tc.numbers.resize(tc.n); for (auto& n : tc.numbers) cin >> n; return tc; } void solve_test_case(TestCase tc); int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); solve_test_case(read_test_case()); } void solve_test_case(TestCase tc) { map<int, int> counts; for (auto n : tc.numbers) counts[n]++; vector<int> muls(2 * tc.n + 1, 0); for (const auto& [k, v] : counts) muls[v] += 1; for (size_t i = 2; i <= 2 * tc.n; i++) muls[i] += muls[i - 1]; for (size_t k = 1; k <= tc.n; k++) { int given = 0; for (size_t m = k; m <= tc.n + k; m += k) { int cnt = muls[m - 1] - muls[m - k - 1]; given += cnt * (m - k); } cout << given << " "; } cout << "\n"; } |