diff --git a/3_dp/Counting Towers.cpp b/3_dp/Counting Towers.cpp new file mode 100644 index 0000000..e1a1b04 --- /dev/null +++ b/3_dp/Counting Towers.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; +const long long MOD = 1e9 + 7; + +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + const int MAXN = 1e6; + vector dpA(MAXN + 1), dpB(MAXN + 1); + dpA[1] = dpB[1] = 1; + + for (int i = 2; i <= MAXN; i++) { + dpA[i] = (2 * dpA[i - 1] + dpB[i - 1]) % MOD; + dpB[i] = (dpA[i - 1] + 4 * dpB[i - 1]) % MOD; + } + + while (t--) { + int n; + cin >> n; + cout << (dpA[n] + dpB[n]) % MOD << "\n"; + } +} diff --git a/Counting bits.cpp b/Counting bits.cpp new file mode 100644 index 0000000..3140a06 --- /dev/null +++ b/Counting bits.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; + +long long countOnes(long long n) { +long long count = 0; +for (long long i = 0; (1LL << i) <= n; i++) { +long long cycle_len = 1LL << (i + 1); +long long full_cycles = (n + 1) / cycle_len; +count += full_cycles * (1LL << i); + +``` + long long remainder = (n + 1) % cycle_len; + count += max(0LL, remainder - (1LL << i)); +} +return count; +``` + +} + +int main() { +ios::sync_with_stdio(false); +cin.tie(nullptr); + +``` +long long n; +cin >> n; +cout << countOnes(n) << "\n"; +return 0; +``` + +}