Task: | Sum of ones |
Sender: | aalto2024e_002 |
Submission time: | 2024-09-30 17:16:29 +0300 |
Language: | C++ (C++17) |
Status: | READY |
Result: | TIME LIMIT EXCEEDED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.00 s | details |
#2 | ACCEPTED | 0.00 s | details |
#3 | ACCEPTED | 0.00 s | details |
#4 | ACCEPTED | 0.00 s | details |
#5 | ACCEPTED | 0.00 s | details |
#6 | ACCEPTED | 0.00 s | details |
#7 | ACCEPTED | 0.00 s | details |
#8 | ACCEPTED | 0.00 s | details |
#9 | ACCEPTED | 0.00 s | details |
#10 | ACCEPTED | 0.00 s | details |
#11 | TIME LIMIT EXCEEDED | -- | details |
#12 | TIME LIMIT EXCEEDED | -- | details |
#13 | TIME LIMIT EXCEEDED | -- | details |
#14 | TIME LIMIT EXCEEDED | -- | details |
#15 | TIME LIMIT EXCEEDED | -- | details |
#16 | TIME LIMIT EXCEEDED | -- | details |
#17 | TIME LIMIT EXCEEDED | -- | details |
#18 | TIME LIMIT EXCEEDED | -- | details |
#19 | TIME LIMIT EXCEEDED | -- | details |
#20 | TIME LIMIT EXCEEDED | -- | details |
#21 | TIME LIMIT EXCEEDED | -- | details |
Code
#include <bits/stdc++.h> #define REP(i, a, b) for (int i = a; i < b; i++) // Type Aliases for 1D and 2D vectors with initialization #define vll(n, val) vector<long long>(n, val) // 1D vector of long longs with size n, initialized to val #define ll long long #define vvi(n, m, val) vector<vector<int>>(n, vector<int>(m, val)) // 2D vector of ints (n x m), initialized to val #define vvll(n, m, val) vector<vector<long long>>(n, vector<long long>(m, val)) // 2D vector of long longs (n x m), initialized to val using namespace std; void print_vector(vector<int> &x) { for (int v : x) { cout << v << " "; } cout << "\n"; } void print_matrix(vector<vector<int>> &matrix) { cout << "\n" << "----------------" << "\n"; for (vector<int> row : matrix) { print_vector(row); } cout << "\n" << "----------------" << "\n"; } int one_count(int n) { int result = 0; while (n > 0) { result += n % 2; n /= 2; } return result; } int highest_set_bit(ll n) { int exponent = 0; while (n > 1) { exponent++; n /= 2; } return exponent; } ll sum_ones(ll n) { if (n == 0) { return 0; } int no_bits = highest_set_bit(n); ll lp2 = 1 << no_bits; // next lowest power of 2 ll ones_until_lp2 = no_bits * (1 << (no_bits - 1)); ll r = n - lp2; ll ones_from_highest_bit = r + 1; return ones_until_lp2 + ones_from_highest_bit + sum_ones(r); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll n; cin >> n; cout << sum_ones(n) << "\n"; }
Test details
Test 1
Verdict: ACCEPTED
input |
---|
1 |
correct output |
---|
1 |
user output |
---|
1 |
Test 2
Verdict: ACCEPTED
input |
---|
2 |
correct output |
---|
2 |
user output |
---|
2 |
Test 3
Verdict: ACCEPTED
input |
---|
3 |
correct output |
---|
4 |
user output |
---|
4 |
Test 4
Verdict: ACCEPTED
input |
---|
4 |
correct output |
---|
5 |
user output |
---|
5 |
Test 5
Verdict: ACCEPTED
input |
---|
5 |
correct output |
---|
7 |
user output |
---|
7 |
Test 6
Verdict: ACCEPTED
input |
---|
6 |
correct output |
---|
9 |
user output |
---|
9 |
Test 7
Verdict: ACCEPTED
input |
---|
7 |
correct output |
---|
12 |
user output |
---|
12 |
Test 8
Verdict: ACCEPTED
input |
---|
8 |
correct output |
---|
13 |
user output |
---|
13 |
Test 9
Verdict: ACCEPTED
input |
---|
9 |
correct output |
---|
15 |
user output |
---|
15 |
Test 10
Verdict: ACCEPTED
input |
---|
10 |
correct output |
---|
17 |
user output |
---|
17 |
Test 11
Verdict: TIME LIMIT EXCEEDED
input |
---|
303021765044187 |
correct output |
---|
7250167017244884 |
user output |
---|
(empty) |
Test 12
Verdict: TIME LIMIT EXCEEDED
input |
---|
390977392667778 |
correct output |
---|
9400236781929604 |
user output |
---|
(empty) |
Test 13
Verdict: TIME LIMIT EXCEEDED
input |
---|
670904313808571 |
correct output |
---|
16399391652009372 |
user output |
---|
(empty) |
Test 14
Verdict: TIME LIMIT EXCEEDED
input |
---|
704275111916256 |
correct output |
---|
17252936460583174 |
user output |
---|
(empty) |
Test 15
Verdict: TIME LIMIT EXCEEDED
input |
---|
799807335176164 |
correct output |
---|
19646099779305746 |
user output |
---|
(empty) |
Test 16
Verdict: TIME LIMIT EXCEEDED
input |
---|
852012055430877 |
correct output |
---|
21005540865890493 |
user output |
---|
(empty) |
Test 17
Verdict: TIME LIMIT EXCEEDED
input |
---|
901305628563213 |
correct output |
---|
22232965316761961 |
user output |
---|
(empty) |
Test 18
Verdict: TIME LIMIT EXCEEDED
input |
---|
917427461591619 |
correct output |
---|
22650112032503064 |
user output |
---|
(empty) |
Test 19
Verdict: TIME LIMIT EXCEEDED
input |
---|
981787468874797 |
correct output |
---|
24322274256024775 |
user output |
---|
(empty) |
Test 20
Verdict: TIME LIMIT EXCEEDED
input |
---|
1000000000000000 |
correct output |
---|
24784747400675348 |
user output |
---|
(empty) |
Test 21
Verdict: TIME LIMIT EXCEEDED
input |
---|
989898989898989 |
correct output |
---|
24531282955144033 |
user output |
---|
(empty) |