This commit is contained in:
2024-06-01 23:50:25 +02:00
parent ca126c7a0a
commit 5f6b389ee3
8 changed files with 188 additions and 1 deletions

View File

@@ -0,0 +1,19 @@
// Bit Strings
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
ll MOD = 1e9 + 7;
int main() {
int n; cin >> n;
ll ans = 1;
for (int i{}; i < n; i++) {
ans *= 2;
ans %= MOD;
}
ans %= MOD;
cout << ans;
return 0;
}

View File

@@ -0,0 +1,23 @@
// Coin Piles
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int t; cin >> t;
for (int T{}; T < t; T++) {
ll a, b; cin >> a >> b;
if (a < b) swap(a, b);
if ((a + b) % 3 != 0) {
cout << "NO" << endl;
continue;
} else if (a > 2*b) {
cout << "NO" << endl;
continue;
}
cout << "YES" << endl;
}
return 0;
}

View File

@@ -0,0 +1,32 @@
// Gray Code
#include<bits/stdc++.h>
using namespace std;
vector<string> gray_code(int size, bool print) {
if (size == 1) {
if (print) {
cout << "0" << '\n' << "1" << endl;
}
return {"0", "1"};
}
auto prev_gray_code = gray_code(size - 1, false);
vector<string> output;
for (auto &s: prev_gray_code) {
if (print) cout << "0"+s << endl;
output.push_back("0"+s);
}
std::reverse(prev_gray_code.begin(), prev_gray_code.end());
for (auto &s: prev_gray_code) {
if (print) cout << "1"+s << endl;
output.push_back("1"+s);
}
return output;
}
int main() {
int n; cin >> n;
gray_code(n, true);
return 0;
}

View File

@@ -0,0 +1,31 @@
// Palindrome Reorder
#include<bits/stdc++.h>
using namespace std;
int main() {
map<char, int> char_to_count;
string s; cin >> s;
for (auto c: s) char_to_count[c]++;
string forward;
char middle = ' ';
for (auto &[ch, c]: char_to_count) {
if (c & 1) {
if (middle != ' ') {
cout << "NO SOLUTION" << endl;
return 0;
}
middle = ch;
forward.append(string((c - 1) / 2, ch));
} else {
forward.append(string(c/2, ch));
}
}
cout << forward;
if (middle != ' ') cout << middle;
std::reverse(forward.begin(), forward.end());
cout << forward << endl;
return 0;
}

View File

@@ -0,0 +1,38 @@
// Tower of Hanoi
#include <bits/stdc++.h>
using namespace std;
void hanoi(deque<int> &current, deque<int> &destination, deque<int> &tmp, int n, vector<pair<int, int>> &remember, tuple<int, int, int> stack_names) {
if (n == 1) {
auto top = current.back();
current.pop_back();
destination.push_back(top);
remember.push_back({get<0>(stack_names), get<1>(stack_names)});
return;
}
hanoi(current, tmp, destination, n - 1, remember, {get<0>(stack_names), get<2>(stack_names), get<1>(stack_names)});
auto top = current.back();
current.pop_back();
destination.push_back(top);
remember.push_back({get<0>(stack_names), get<1>(stack_names)});
hanoi(tmp, destination, current, n - 1, remember, {get<2>(stack_names), get<1>(stack_names), get<0>(stack_names)});
return ;
};
int main() {
int size;
cin >> size;
deque<int> start;
deque<int> s2;
deque<int> s3;
for (int i = size; i > 0; i--) {
start.push_front(i);
}
vector<pair<int, int>> r; //c, t, d
hanoi(start, s2, s3, size, r, {1, 3, 2});
cout << r.size() << endl;
for (auto &[a, b]: r) cout << a << " " << b << endl;
cout << endl;
return 0;
}

View File

@@ -0,0 +1,16 @@
// Trailing Zeros
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int count = 0;
ll n; cin >> n;
for (int i = 1; i <= 15; i++) {
count += n / pow(5, i);
}
cout << count;
return 0;
}

View File

@@ -0,0 +1,28 @@
// Two Sets
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll n; cin >> n;
if ((n * (n+1) / 2) & 1) {
cout << "NO" << endl;
return 0;
}
cout << "YES" << endl;
ll target_sum = n * (n+1) / 4;
vector<ll> first;
vector<ll> second;
for (ll i = n; i > 0; i--) {
if (i <= target_sum) {first.push_back(i); target_sum -= i;}
else second.push_back(i);
}
cout << first.size() << endl;
for (auto e : first) cout << e << " ";
cout << '\n' << second.size() << '\n';
for (auto e: second) cout << e << " ";
cout << endl;
return 0;
}