开始计概A下半学期。
This commit is contained in:
1 parent
0cfdc8499c
commit
38e97654e1
31 files changed
+2144
-110
No files matched your search
@@ -0,0 +1,26 @@
|
||||
#include <iostream>
|
||||
|
||||
constexpr int MAX_N = 31;
|
||||
|
||||
int dp[MAX_N];
|
||||
|
||||
void init_dp() {
|
||||
dp[0] = 1;
|
||||
dp[1] = 1;
|
||||
for (int i = 2; i < MAX_N; i++) {
|
||||
dp[i] = dp[i - 1] + dp[i - 2];
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
init_dp();
|
||||
|
||||
int n;
|
||||
std::cin >> n;
|
||||
std::cout << dp[n] << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
|
||||
long long gcd(long long a, long long b) {
|
||||
if (a < b) { return gcd(b, a); }
|
||||
if (b == 1) { return 1; }
|
||||
if (b == 0) { return a; }
|
||||
return gcd(b, a % b);
|
||||
}
|
||||
|
||||
long long lcm(long long a, long long b) {
|
||||
return a / gcd(a, b) * b;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
char temp_comma;
|
||||
long long a, b;
|
||||
std::cin >> a >> temp_comma >> b;
|
||||
std::clog << gcd(a, b) << std::endl;
|
||||
std::cout << lcm(a, b) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <iostream>
|
||||
|
||||
bool is_palindromic(std::string s) {
|
||||
if (s.size() == 1) { return true; }
|
||||
for (int i = 0; i <= (s.size() - 2) / 2; i++) {
|
||||
if (s[i] != s[s.size() - i - 1]) { return false; }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
std::string s;
|
||||
std::cin >> s;
|
||||
std::cout << (is_palindromic(s) ? "yes" : "no") << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
|
||||
void print_kakutani_process(int n) {
|
||||
while (n != 1) {
|
||||
if (n % 2 == 0) {
|
||||
std::cout << n << "/2=" << (n /= 2) << std::endl;
|
||||
} else {
|
||||
std::cout << n << "*3+1=" << (n *= 3, n += 1) << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
std::cin >> n;
|
||||
print_kakutani_process(n);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
#include <any>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using Grid = std::vector<std::vector<int>>;
|
||||
|
||||
class Vector2 {
|
||||
public:
|
||||
int x, y;
|
||||
constexpr explicit Vector2(const int x = 0, const int y = 0) : x(x), y(y) {}
|
||||
constexpr bool operator==(const Vector2 other) const { return x == other.x && y == other.y; }
|
||||
constexpr Vector2 operator+(const Vector2 other) const { return Vector2(x + other.x, y + other.y); }
|
||||
};
|
||||
|
||||
constexpr Vector2 DIRS[4] = {Vector2(0, 1), Vector2(1, 0), Vector2(0, -1), Vector2(-1, 0)};
|
||||
|
||||
template <typename T>
|
||||
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vector) {
|
||||
for (auto elem : vector) { os << elem << ' '; }
|
||||
return os;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void log_grid(const std::vector<std::vector<T>>& grid, const std::string& prefix = "") {
|
||||
for (auto line : grid) {
|
||||
std::clog << prefix << ' ' << line << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
Grid get_init_grid(int r, int c) {
|
||||
Grid grid = std::vector(r + 2, std::vector(c + 2, 0));
|
||||
std::fill(grid[0].begin(), grid[0].end(), 1);
|
||||
std::fill(grid[grid.size() - 1].begin(), grid[grid.size() - 1].end(), 1);
|
||||
for (int j = 1; j <= r; j++) {
|
||||
grid[j][0] = grid[j][c + 1] = 1;
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
Grid input_grid() {
|
||||
int r, c;
|
||||
std::cin >> r >> c;
|
||||
Grid grid = get_init_grid(r, c);
|
||||
for (int i = 1; i <= r; i++) {
|
||||
for (int j = 1; j <= c; j++) {
|
||||
std::cin >> grid[i][j];
|
||||
}
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
bool verify_reachability(const Grid& grid,
|
||||
const Vector2 end_pos,
|
||||
Vector2 pos,
|
||||
std::vector<std::vector<bool>>& visited) {
|
||||
if (pos == end_pos) { return true; }
|
||||
if (grid[pos.x][pos.y] == 1) { return false; }
|
||||
if (visited[pos.x][pos.y]) { return false; }
|
||||
visited[pos.x][pos.y] = true;
|
||||
// log_grid(visited);
|
||||
for (const auto dir : DIRS) {
|
||||
if (verify_reachability(grid, end_pos, pos + dir, visited)) { return true; }
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool verify_reachability(const Grid& grid) {
|
||||
Vector2 start_pos, end_pos;
|
||||
for (int i = 0; i < grid.size(); i++) {
|
||||
for (int j = 0; j < grid[0].size(); j++) {
|
||||
if (grid[i][j] == 2) { start_pos = Vector2(i, j); }
|
||||
if (grid[i][j] == 3) { end_pos = Vector2(i, j); }
|
||||
}
|
||||
}
|
||||
|
||||
std::vector visited(grid.size(), std::vector(grid[0].size(), false));
|
||||
return verify_reachability(grid, end_pos, start_pos, visited);
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
std::cout << (verify_reachability(input_grid()) ? "Yes" : "No") << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <iostream>
|
||||
|
||||
constexpr int MAX_N = 1e6;
|
||||
|
||||
double dp[MAX_N + 1];
|
||||
|
||||
void init_dp(int x) {
|
||||
dp[0] = 1, dp[1] = x;
|
||||
for (int i = 2; i < MAX_N; i++) {
|
||||
dp[i] = ((2 * i - 1) * x * dp[i - 1] - (i - 1) * dp[i - 2]) / i;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
int n, x;
|
||||
std::cin >> n >> x;
|
||||
init_dp(x);
|
||||
std::cout << dp[n] << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user