some programs!!!
This commit is contained in:
1 parent
71f1022dea
commit
ca6daa582c
43 files changed
+1452
-381
No files matched your search
@@ -0,0 +1,76 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
long long gcd(long long x, long long y) {
|
||||
if (x < y) swap(x, y);
|
||||
if (x % y == 0) { return y; }
|
||||
return gcd(y, x % y);
|
||||
}
|
||||
|
||||
class Fraction {
|
||||
public:
|
||||
long long numerator = 0, denominator = 1;
|
||||
|
||||
void simplify() {
|
||||
if (numerator == 0) { denominator = 1; }
|
||||
long long g = gcd(numerator, denominator);
|
||||
numerator /= g, denominator /= g;
|
||||
}
|
||||
|
||||
Fraction operator+(const Fraction& other) const {
|
||||
Fraction result{
|
||||
numerator * other.denominator + denominator * other.numerator,
|
||||
denominator * other.denominator
|
||||
};
|
||||
result.simplify();
|
||||
return result;
|
||||
}
|
||||
|
||||
Fraction operator+=(const Fraction& other) {
|
||||
return *this = *this + other;
|
||||
}
|
||||
|
||||
Fraction() = default;
|
||||
Fraction(const long long n, const long long d) : numerator(n), denominator(d) { simplify(); }
|
||||
|
||||
Fraction(const string& s) {
|
||||
stringstream ss(s);
|
||||
long long n, d;
|
||||
char slash;
|
||||
if (ss >> n >> slash >> d && slash == '/') {
|
||||
numerator = n, denominator = d;
|
||||
simplify();
|
||||
} else {
|
||||
throw invalid_argument("String is not a fraction");
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] string to_string() const {
|
||||
if (denominator == 1) {
|
||||
return std::to_string(numerator);
|
||||
}
|
||||
return std::to_string(numerator) + '/' + std::to_string(denominator);
|
||||
}
|
||||
};
|
||||
|
||||
int n;
|
||||
string s;
|
||||
Fraction ans;
|
||||
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> n;
|
||||
for (int _ = 0; _ < n; _++) {
|
||||
// clog << ans.numerator << '/' << ans.denominator << endl;
|
||||
cin >> s;
|
||||
ans += Fraction(s);
|
||||
}
|
||||
|
||||
cout << ans.to_string() << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Number {
|
||||
int value;
|
||||
int index;
|
||||
|
||||
bool operator<(const Number& other) const {
|
||||
return value < other.value;
|
||||
}
|
||||
};
|
||||
|
||||
constexpr int MAX_N = 1001;
|
||||
int n;
|
||||
Number numbers[MAX_N];
|
||||
int answers[MAX_N];
|
||||
|
||||
void get_answers() {
|
||||
sort(numbers, numbers + n);
|
||||
int real_idx = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (i == 0 || numbers[i].value != numbers[i - 1].value) { real_idx = i; }
|
||||
answers[numbers[i].index] = real_idx;
|
||||
}
|
||||
}
|
||||
|
||||
void print_answers() {
|
||||
for (int i = 0; i < n; i++) {
|
||||
cout << answers[i] << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> n;
|
||||
for (int i = 0; i < n; i++) {
|
||||
cin >> numbers[i].value;
|
||||
numbers[i].index = i;
|
||||
}
|
||||
|
||||
get_answers();
|
||||
print_answers();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
def get_count_digits_init(n: int, d: int) -> int:
|
||||
return str(n).count(str(d))
|
||||
|
||||
|
||||
MAX_N = 10001
|
||||
count_digits_init = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] for _ in range(MAX_N)]
|
||||
|
||||
if __name__ == '__main__':
|
||||
for i in range(1, MAX_N):
|
||||
for d in range(10):
|
||||
count_digits_init[i][d] = get_count_digits_init(i, d) + count_digits_init[i - 1][d]
|
||||
# with open('dh_20260909_11_count_digits_init.out', 'w') as f:
|
||||
# f.write(str(count_digits_init).replace(" ", "").replace("]", "}").replace("[", "{"))
|
||||
|
||||
n, d = map(int, input().split())
|
||||
print(count_digits_init[n][d])
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
long long exchange_requirement, empty_bottles_amount, filled_bottles_amount, ans;
|
||||
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> exchange_requirement >> filled_bottles_amount;
|
||||
|
||||
while (true) {
|
||||
ans += filled_bottles_amount;
|
||||
empty_bottles_amount += filled_bottles_amount;
|
||||
filled_bottles_amount = empty_bottles_amount / exchange_requirement;
|
||||
empty_bottles_amount %= exchange_requirement;
|
||||
if (filled_bottles_amount == 0) { break; }
|
||||
}
|
||||
cout << ans << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
constexpr int MAX_N = 151;
|
||||
bool cards_flipped[MAX_N];
|
||||
|
||||
void simulate() {
|
||||
for (int divider=2; divider<MAX_N; divider++) {
|
||||
for (int i=divider; i<MAX_N; i+=divider) {
|
||||
cards_flipped[i] = !cards_flipped[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_ans() {
|
||||
int amount = 0;
|
||||
bool first = true;
|
||||
for (int i = 1; i < MAX_N; i++) {
|
||||
if (!cards_flipped[i]) {
|
||||
if (!first) cout << " ";
|
||||
cout << "N" << i;
|
||||
first = false;
|
||||
amount++;
|
||||
}
|
||||
}
|
||||
cout << endl << amount << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
simulate();
|
||||
print_ans();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
class Coord {
|
||||
public:
|
||||
int x = 0, y = 0;
|
||||
constexpr Coord(int x, int y) : x(x), y(y) {}
|
||||
|
||||
constexpr Coord operator+(const Coord other) const {
|
||||
return {x + other.x, y + other.y};
|
||||
}
|
||||
|
||||
constexpr Coord& operator+=(const Coord other) {
|
||||
x += other.x, y += other.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr bool operator==(const Coord other) const {
|
||||
return x == other.x && y == other.y;
|
||||
}
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const Coord coord) {
|
||||
return os << "(" << coord.x << ", " << coord.y << ")";
|
||||
}
|
||||
};
|
||||
|
||||
constexpr int MAX_N = 10;
|
||||
int w, h, grid[MAX_N][MAX_N];
|
||||
std::string original_str;
|
||||
|
||||
std::vector<int> get_nums(const char c) {
|
||||
if (c == ' ') { return {0, 0}; }
|
||||
return {(c - 'A' + 1) / 10, (c - 'A' + 1) % 10};
|
||||
}
|
||||
|
||||
Coord next_move(const Coord prev_coord, const Coord prev_facing) {
|
||||
Coord new_coord = prev_coord + prev_facing;
|
||||
if (new_coord.x < 0 || new_coord.x >= h ||
|
||||
new_coord.y < 0 || new_coord.y >= w ||
|
||||
grid[new_coord.x][new_coord.y] != -1) {
|
||||
if (prev_facing == Coord{1, 0}) { return {0, -1}; }
|
||||
if (prev_facing == Coord{0, -1}) { return {-1, 0}; }
|
||||
if (prev_facing == Coord{-1, 0}) { return {0, 1}; }
|
||||
if (prev_facing == Coord{0, 1}) { return {1, 0}; }
|
||||
throw std::invalid_argument("Invalid facing direction");
|
||||
}
|
||||
return prev_facing;
|
||||
}
|
||||
|
||||
std::ostream& print_grid(std::ostream& os) {
|
||||
for (int i = 0; i < h; i++) {
|
||||
for (int j = 0; j < w; j++) {
|
||||
os << (grid[i][j] == -1 ? 0 : grid[i][j]);
|
||||
}
|
||||
}
|
||||
os << std::endl;
|
||||
return os;
|
||||
}
|
||||
|
||||
void fill_grid(const std::string& s) {
|
||||
memset(grid, -1, sizeof(grid));
|
||||
Coord coord = {0, 0}, facing = {0, 1};
|
||||
for (const char c : s) {
|
||||
for (const int x : get_nums(c)) {
|
||||
grid[coord.x][coord.y] = x;
|
||||
std::clog << "Placing " << x << " at " << coord << std::endl;
|
||||
print_grid(std::clog);
|
||||
coord += facing = next_move(coord, facing);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
getline(std::cin, original_str);
|
||||
std::stringstream ss(original_str);
|
||||
ss >> h >> w;
|
||||
getline(std::cin, original_str);
|
||||
fill_grid(original_str);
|
||||
|
||||
print_grid(std::cout);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
#include <iostream>
|
||||
|
||||
class CycledLinkedList {
|
||||
public:
|
||||
struct Node {
|
||||
int val = 0;
|
||||
Node* next = nullptr;
|
||||
Node* prev = nullptr;
|
||||
};
|
||||
|
||||
Node* head = nullptr; // They are POINTERS, not OBJECTS.
|
||||
|
||||
void push_back(int val) {
|
||||
if (head == nullptr) {
|
||||
head = new Node({.val = val});
|
||||
head->next = head;
|
||||
head->prev = head;
|
||||
return;
|
||||
}
|
||||
Node* node = new Node({.val = val});
|
||||
head->prev->next = node;
|
||||
node->prev = head->prev;
|
||||
head->prev = node;
|
||||
node->next = head;
|
||||
}
|
||||
|
||||
void remove(Node* node) {
|
||||
node->prev->next = node->next;
|
||||
node->next->prev = node->prev;
|
||||
if (node == head) {
|
||||
head = node->next;
|
||||
}
|
||||
delete node;
|
||||
}
|
||||
|
||||
void init_cycle(int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const CycledLinkedList& list) {
|
||||
for (Node* node = list.head->next; node != list.head; node = node->next) {
|
||||
os << node->val << "<->";
|
||||
}
|
||||
os << list.head->val << std::endl;
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
int n, m, target_order;
|
||||
CycledLinkedList players;
|
||||
|
||||
int target_idx() {
|
||||
for (int i = 0; i < target_order - 1; i++) {
|
||||
for (int j = 0; j < m - 1; j++) {
|
||||
players.head = players.head->next;
|
||||
}
|
||||
players.remove(players.head);
|
||||
}
|
||||
for (int j = 0; j < m - 1; j++) {
|
||||
players.head = players.head->next;
|
||||
}
|
||||
return players.head->val;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
std::cin >> n >> m >> target_order;
|
||||
players.init_cycle(n);
|
||||
// std::clog << players << std::endl;
|
||||
std::cout << target_idx() + 1 << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
constexpr size_t MAX_N = 201;
|
||||
int n, evaluation_value, original_evaluation_value;
|
||||
bool bits[MAX_N], original_bits[MAX_N], target_bits[MAX_N];
|
||||
|
||||
std::ostream& print_bits(const bool* bits_list = bits, std::ostream& os = std::clog) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
os << (bits_list[i] ? 1 : 0);
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
void swap_bits(int i, int j) {
|
||||
if (bits[i] != target_bits[i]) { evaluation_value--; }
|
||||
if (bits[j] != target_bits[j]) { evaluation_value--; }
|
||||
std::swap(bits[i], bits[j]);
|
||||
if (bits[i] != target_bits[i]) { evaluation_value++; }
|
||||
if (bits[j] != target_bits[j]) { evaluation_value++; }
|
||||
}
|
||||
|
||||
int evaluate() {
|
||||
if (evaluation_value == 1) { return 1; }
|
||||
return evaluation_value / 2;
|
||||
}
|
||||
|
||||
bool dfs(int last_swap_pos = 0, int depth = 0, int target_depth = 0) {
|
||||
const int h = evaluate();
|
||||
if (h == 0) { return true; }
|
||||
if (depth + h > target_depth) {
|
||||
return false;
|
||||
}
|
||||
std::clog << "DEPTH=" << depth << "\th=" << h << "\t";
|
||||
print_bits() << "\t";
|
||||
print_bits(target_bits) << std::endl;
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
if (bits[i] == bits[i + 1]) { continue; }
|
||||
if (i == last_swap_pos) { continue; }
|
||||
if (bits[i] == target_bits[i] && bits[i + 1] == target_bits[i + 1]) { continue; }
|
||||
if ((bits[i] == target_bits[i] || bits[i + 1] == target_bits[i + 1]) &&
|
||||
(bits[i + 1] != target_bits[i] && bits[i] != target_bits[i + 1])) { continue; }
|
||||
swap_bits(bits[i], bits[i + 1]);
|
||||
if (dfs(i, depth + 1, target_depth)) { return true; }
|
||||
swap_bits(bits[i], bits[i + 1]);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int ida_star() {
|
||||
for (int target_depth = evaluate(); target_depth <= 2 * n; target_depth++) {
|
||||
memcpy(bits, original_bits, n);
|
||||
evaluation_value = original_evaluation_value;
|
||||
std::clog << "target_depth:" << target_depth << std::endl;
|
||||
if (dfs(-1, 0, target_depth)) {
|
||||
return target_depth;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool init_evaluate() {
|
||||
int original_sum = 0, target_sum = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
original_sum += original_bits[i];
|
||||
target_sum += target_bits[i];
|
||||
original_evaluation_value += original_bits[i] != target_bits[i];
|
||||
}
|
||||
return original_sum == target_sum;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
std::cin >> n;
|
||||
for (int i = 0; i < n; i++) { std::cin >> original_bits[i]; }
|
||||
for (int i = 0; i < n; i++) { std::cin >> target_bits[i]; }
|
||||
if (!init_evaluate()) {
|
||||
std::cout << -1 << std::endl;
|
||||
return 0;
|
||||
}
|
||||
std::cout << ida_star() << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user