Skip to content

Commit c6a95cc

Browse files
authored
Merge pull request #116 from Arham07/Arham07-patch-2
Add solution for Counting Towers #112
2 parents a307e86 + 3ebcabe commit c6a95cc

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

3_dp/Counting Towers.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
const long long MOD = 1e9 + 7;
4+
5+
int main() {
6+
ios::sync_with_stdio(false);
7+
cin.tie(nullptr);
8+
9+
int t;
10+
cin >> t;
11+
const int MAXN = 1e6;
12+
vector<long long> dpA(MAXN + 1), dpB(MAXN + 1);
13+
dpA[1] = dpB[1] = 1;
14+
15+
for (int i = 2; i <= MAXN; i++) {
16+
dpA[i] = (2 * dpA[i - 1] + dpB[i - 1]) % MOD;
17+
dpB[i] = (dpA[i - 1] + 4 * dpB[i - 1]) % MOD;
18+
}
19+
20+
while (t--) {
21+
int n;
22+
cin >> n;
23+
cout << (dpA[n] + dpB[n]) % MOD << "\n";
24+
}
25+
}

Counting bits.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
long long countOnes(long long n) {
5+
long long count = 0;
6+
for (long long i = 0; (1LL << i) <= n; i++) {
7+
long long cycle_len = 1LL << (i + 1);
8+
long long full_cycles = (n + 1) / cycle_len;
9+
count += full_cycles * (1LL << i);
10+
11+
```
12+
long long remainder = (n + 1) % cycle_len;
13+
count += max(0LL, remainder - (1LL << i));
14+
}
15+
return count;
16+
```
17+
18+
}
19+
20+
int main() {
21+
ios::sync_with_stdio(false);
22+
cin.tie(nullptr);
23+
24+
```
25+
long long n;
26+
cin >> n;
27+
cout << countOnes(n) << "\n";
28+
return 0;
29+
```
30+
31+
}

0 commit comments

Comments
 (0)