From b6243b73787c7321975e89aa8b9502d9d083a96b Mon Sep 17 00:00:00 2001 From: Frederick Chen Date: Fri, 28 Feb 2025 16:06:22 +0000 Subject: [PATCH] Upload codes: contests --- AtCoder/abc390/A_12435.cpp | 34 +++++++++ AtCoder/abc390/B_Geometric_Sequence.cpp | 33 ++++++++ .../abc390/C_Paint_to_make_a_rectangle.cpp | 60 +++++++++++++++ AtCoder/abc391/A_Lucky_Direction.cpp | 30 ++++++++ AtCoder/abc391/B_Seek_Grid.cpp | 48 ++++++++++++ AtCoder/abc391/C_Pigeonhole_Query.cpp | 32 ++++++++ .../A_The_Play_Never_Ends.cpp | 28 +++++++ .../B_Perfecto.cpp | 76 +++++++++++++++++++ .../C_Trapmigiano_Reggiano.cpp | 64 ++++++++++++++++ Luogu/P_1015_NOIP_1999_普及组_回文数.cpp | 58 ++++++++++++++ Luogu/P_10446_64_位整数乘法.cpp | 34 +++++++++ Luogu/P_1060_NOIP_2006_普及组_开心的金明.cpp | 27 +++++++ Luogu/P_1135_奇怪的电梯.cpp | 49 ++++++++++++ README.md | 1 + 14 files changed, 574 insertions(+) create mode 100644 AtCoder/abc390/A_12435.cpp create mode 100644 AtCoder/abc390/B_Geometric_Sequence.cpp create mode 100644 AtCoder/abc390/C_Paint_to_make_a_rectangle.cpp create mode 100644 AtCoder/abc391/A_Lucky_Direction.cpp create mode 100644 AtCoder/abc391/B_Seek_Grid.cpp create mode 100644 AtCoder/abc391/C_Pigeonhole_Query.cpp create mode 100644 CodeForces/Codeforces Round 1007 (Div. 2)/A_The_Play_Never_Ends.cpp create mode 100644 CodeForces/Codeforces Round 1007 (Div. 2)/B_Perfecto.cpp create mode 100644 CodeForces/Codeforces Round 1007 (Div. 2)/C_Trapmigiano_Reggiano.cpp create mode 100644 Luogu/P_1015_NOIP_1999_普及组_回文数.cpp create mode 100644 Luogu/P_10446_64_位整数乘法.cpp create mode 100644 Luogu/P_1060_NOIP_2006_普及组_开心的金明.cpp create mode 100644 Luogu/P_1135_奇怪的电梯.cpp diff --git a/AtCoder/abc390/A_12435.cpp b/AtCoder/abc390/A_12435.cpp new file mode 100644 index 0000000..4ccbd90 --- /dev/null +++ b/AtCoder/abc390/A_12435.cpp @@ -0,0 +1,34 @@ +#include +#define lo long long +#define INF INT_MAX +#define LLM LONG_LONG_MAX + +using namespace std; +const int N = 1e7 + 10; +/* + toothless. #17 + @fredcss_dev +*/ + +int a[N], b[N]; + +signed main() +{ + for (int i = 1; i <= 5; i++) { + cin >> a[i]; + b[i] = a[i]; + } + if (a[1] == 1 && a[2] == 2 && a[3] == 3 && a[4] == 4 && a[5] == 5) { + puts("No"); + return 0; + } + sort(b + 1, b + 6); + int cnt = 0, r = 0; + for (int i = 1; i <= 5; i++) { + if (a[i] != b[i]) cnt++; + if (a[i] != b[i] && i != 1 && a[i - 1] != b[i - 1]) r = 1; + } + if (cnt == 2 && r == 1) puts("Yes"); + else puts("No"); + return 0; +} \ No newline at end of file diff --git a/AtCoder/abc390/B_Geometric_Sequence.cpp b/AtCoder/abc390/B_Geometric_Sequence.cpp new file mode 100644 index 0000000..fe24970 --- /dev/null +++ b/AtCoder/abc390/B_Geometric_Sequence.cpp @@ -0,0 +1,33 @@ +#include +#define lo long long +#define INF INT_MAX +#define LLM LONG_LONG_MAX + +using namespace std; +const lo N = 1e7 + 10; +/* + toothless. #17 + @fredcss_dev +*/ + +lo n; +lo a[N], b; +bool flag = 0; + +signed main() +{ + cin >> n; + for (int i = 1; i <= n; i++) { + cin >> a[i]; + } + for (int i = 3; i <= n; i++) + { + if (a[i] * a[1] != a[i - 1] * a[2]) + { + puts("No"); + return 0; + } + } + puts("Yes"); + return 0; +} \ No newline at end of file diff --git a/AtCoder/abc390/C_Paint_to_make_a_rectangle.cpp b/AtCoder/abc390/C_Paint_to_make_a_rectangle.cpp new file mode 100644 index 0000000..cc4ad05 --- /dev/null +++ b/AtCoder/abc390/C_Paint_to_make_a_rectangle.cpp @@ -0,0 +1,60 @@ +#include +#define lo long long +#define INF INT_MAX +#define LLM LONG_LONG_MAX + +using namespace std; +const int N = 1e7 + 10; +/* + toothless. #17 + @fredcss_dev +*/ + +bool f(int h, int w, vector& g) +{ + int top = h, b = -1, l = w, r = -1; + for (int i = 0; i < h; i++) { + for (int j = 0; j < w; j++) { + if (g[i][j] == '#') { + top = min(top, i); + b = max(b, i); + l = min(l, j); + r = max(r, j); + } + } + } + if (b == -1) return true; + for (int i = top; i <= b; i++) { + for (int j = l; j <= r; j++) { + if (g[i][j] == '.') return false; + } + } + for (int i = 0; i < h; i++) { + for (int j = 0; j < w; j++) { + if (i < top || i > b || j < l || j > r) { + if (g[i][j] == '#') return false; + } + } + } + return true; +} + +signed main() +{ + int h, w; + cin >> h >> w; + if (h == 1 && w == 1) { + puts("Yes"); + return 0; + } + vector g(h); + for (int i = 0; i < h; i++) { + cin >> g[i]; + } + if (f(h, w, g)) { + cout << "Yes" << endl; + } else { + cout << "No" << endl; + } + return 0; +} \ No newline at end of file diff --git a/AtCoder/abc391/A_Lucky_Direction.cpp b/AtCoder/abc391/A_Lucky_Direction.cpp new file mode 100644 index 0000000..c264b1e --- /dev/null +++ b/AtCoder/abc391/A_Lucky_Direction.cpp @@ -0,0 +1,30 @@ +#include +#define lo long long +#define INF INT_MAX +#define LLM LONG_LONG_MAX + +using namespace std; +const int N = 1e7 + 10; +/* + toothless. #17 + @fredcss_dev +*/ + +map mp; + +string n; + +signed main() +{ + mp["N"] = "S"; + mp["E"] = "W"; + mp["W"] = "E"; + mp["S"] = "N"; + mp["NE"] = "SW"; + mp["NW"] = "SE"; + mp["SE"] = "NW"; + mp["SW"] = "NE"; + cin >> n; + cout << mp[n]; + return 0; +} \ No newline at end of file diff --git a/AtCoder/abc391/B_Seek_Grid.cpp b/AtCoder/abc391/B_Seek_Grid.cpp new file mode 100644 index 0000000..f66753e --- /dev/null +++ b/AtCoder/abc391/B_Seek_Grid.cpp @@ -0,0 +1,48 @@ +#include +#define lo long long +#define INF INT_MAX +#define LLM LONG_LONG_MAX + +using namespace std; +const int N = 1e7 + 10; +/* + toothless. #17 + @fredcss_dev +*/ + +signed main() { + int N, M; + cin >> N >> M; + + vector > S(N, vector(N)); + vector > T(M, vector(M)); + for (int i = 0; i < N; ++i) { + for (int j = 0; j < N; ++j) { + cin >> S[i][j]; + } + } + for (int i = 0; i < M; ++i) { + for (int j = 0; j < M; ++j) { + cin >> T[i][j]; + } + } + for (int a = 0; a <= N - M; ++a) { + for (int b = 0; b <= N - M; ++b) { + bool match = true; + for (int i = 0; i < M; ++i) { + for (int j = 0; j < M; ++j) { + if (S[a + i][b + j] != T[i][j]) { + match = false; + break; + } + } + if (!match) break; + } + if (match) { + cout << a + 1 << " " << b + 1 << endl; + } + } + } + + return 0; +} \ No newline at end of file diff --git a/AtCoder/abc391/C_Pigeonhole_Query.cpp b/AtCoder/abc391/C_Pigeonhole_Query.cpp new file mode 100644 index 0000000..cf6fd51 --- /dev/null +++ b/AtCoder/abc391/C_Pigeonhole_Query.cpp @@ -0,0 +1,32 @@ +#include +#include +using namespace std; + +int n, q, mp[10005], g[10005], cnt = 0, a, b, c; + +int main() +{ + cin >> n >> q; + for (int i = 0; i <= n; i++) + { + mp[i] = 1; + g[i] = i; + } + while (q--) + { + cin >> a; + if (a == 1) + { + cin >> b >> c; + if (mp[g[b]] == 2) + cnt--; + if (mp[c] == 1) + cnt++; + mp[g[b]]--; + mp[c]++; + g[b] = c; + } + else if (a == 2) + cout << cnt << endl; + return 0; + } \ No newline at end of file diff --git a/CodeForces/Codeforces Round 1007 (Div. 2)/A_The_Play_Never_Ends.cpp b/CodeForces/Codeforces Round 1007 (Div. 2)/A_The_Play_Never_Ends.cpp new file mode 100644 index 0000000..1e265b9 --- /dev/null +++ b/CodeForces/Codeforces Round 1007 (Div. 2)/A_The_Play_Never_Ends.cpp @@ -0,0 +1,28 @@ +#include +#define lo long long +#define INF INT_MAX +#define LLM LONG_LONG_MAX +#define endl "\n" + +using namespace std; +const int N = 1e7 + 10; +/* + toothless. #17 + @fredcss_dev +*/ + +int t, k; + +bool wczffl_503(int k) { + return (k % 3 == 1); +} + +signed main() { + cin >> t; + while (t--){ + cin >> k; + if (wczffl_503(k)) cout << "YES" << endl; + else cout << "NO" << endl; + } + return 0; +} \ No newline at end of file diff --git a/CodeForces/Codeforces Round 1007 (Div. 2)/B_Perfecto.cpp b/CodeForces/Codeforces Round 1007 (Div. 2)/B_Perfecto.cpp new file mode 100644 index 0000000..46425a2 --- /dev/null +++ b/CodeForces/Codeforces Round 1007 (Div. 2)/B_Perfecto.cpp @@ -0,0 +1,76 @@ +#include +#define lo long long +#define INF INT_MAX +#define LLM LONG_LONG_MAX +#define endl "\n" + +using namespace std; +const int N = 1e7 + 10; +/* + toothless. #17 + @fredcss_dev +*/ + +bool f(lo x) { + if (x < 0) return false; + lo root = sqrtl(x); + return root * root == x; +} + +void wczffl_503() { + int n; + cin >> n; + lo total = 1LL * n * (n + 1) / 2; + if (f(total)) { + cout << "-1\n"; + return; + } + vector s(n + 1); + for (int i = 1; i <= n; ++i) { + s[i] = (2LL * n - i + 1) * i / 2; + } + vector jhd(n + 1, false); + for (int i = 1; i <= n - 1; ++i) { + if (f(s[i])) { + jhd[i] = true; + } + } + bool bad = false; + for (int i = 1; i <= n; ++i) { + lo now = s[i]; + if (i <= n - 1 && jhd[i]) { + now--; + } + if (f(now)) { + bad = true; + break; + } + } + if (bad) { + cout << "-1\n"; + return; + } + vector p(n); + for (int i = 0; i < n; ++i) { + p[i] = n - i; + } + for (int i = 1; i <= n - 1; ++i) { + if (jhd[i]) { + swap(p[i - 1], p[i]); + } + } + for (int x : p) { + cout << x << ' '; + } + cout << '\n'; +} + +signed main() +{ + int t; + cin >> t; + while (t--) { + wczffl_503(); + } + return 0; +} \ No newline at end of file diff --git a/CodeForces/Codeforces Round 1007 (Div. 2)/C_Trapmigiano_Reggiano.cpp b/CodeForces/Codeforces Round 1007 (Div. 2)/C_Trapmigiano_Reggiano.cpp new file mode 100644 index 0000000..6aaf089 --- /dev/null +++ b/CodeForces/Codeforces Round 1007 (Div. 2)/C_Trapmigiano_Reggiano.cpp @@ -0,0 +1,64 @@ +#include +#define lo long long +#define INF INT_MAX +#define LLM LONG_LONG_MAX +#define endl "\n" + +using namespace std; +const int N = 1e7 + 10; +/* + toothless. #17 + @fredcss_dev +*/ + +vector f(int root, const vector>& adj) { + vector post_order; + stack> s; + s.push({root, -1, false}); + + while (!s.empty()) { + auto [node, parent, visited] = s.top(); + s.pop(); + if (visited) { + post_order.push_back(node); + } else { + s.push({node, parent, true}); + for (int neighbor : adj[node]) { + if (neighbor != parent) { + s.push({neighbor, node, false}); + } + } + } + } + return post_order; +} + +void wczffl_503() { + int t; + cin >> t; + while (t--) { + int n, st, en; + cin >> n >> st >> en; + vector> adj(n + 1); + for (int i = 0; i < n - 1; ++i) { + int u, v; + cin >> u >> v; + adj[u].push_back(v); + adj[v].push_back(u); + } + + vector p = f(en, adj); + if (p.back() != en) { + p.push_back(en); + } + for (int x : p) { + cout << x << ' '; + } + cout << '\n'; + } +} + +int main() { + wczffl_503(); + return 0; +} \ No newline at end of file diff --git a/Luogu/P_1015_NOIP_1999_普及组_回文数.cpp b/Luogu/P_1015_NOIP_1999_普及组_回文数.cpp new file mode 100644 index 0000000..d9a92d2 --- /dev/null +++ b/Luogu/P_1015_NOIP_1999_普及组_回文数.cpp @@ -0,0 +1,58 @@ +#include +#define int long long +#define endl "\n" +#define IMX LONG_LONG_MAX +#define IMN LONG_LONG_MIN +#define debug$ if (dev) +using namespace std; + +/* toothless. #17 */ + +const int N = 1e7 + 10; +const int M = 2e3 + 5; +const bool dev = false; + +char s[M]; +int a[M], top; +bool check() +{ + int t = top / 2; + for (int i = 0; i <= t; ++i) + if (a[i] != a[top - i]) + return true; + return false; +} + +signed main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + int n, step = 0; + cin >> n >> s; + top = strlen(s) - 1; + for (int i = 0; i <= top; ++i) + if (s[i] >= '0' && s[i] <= '9') + a[top - i] = s[i] - '0'; + else + a[top - i] = s[i] - 55; + while (check()) { + if (step > 30) { + cout << "Impossible!"; + return 0; + } + for (int i = 0; i <= top; ++i) + if (top - i >= i) + a[i] += a[top - i]; + else + a[i] = a[top - i]; + for (int i = 0; i <= top; ++i) + if (a[i] >= n) + a[i + 1]++, a[i] -= n; + if (a[top + 1]) + top++; + step++; + } + cout << "STEP=" << step; + return 0; +} diff --git a/Luogu/P_10446_64_位整数乘法.cpp b/Luogu/P_10446_64_位整数乘法.cpp new file mode 100644 index 0000000..c004086 --- /dev/null +++ b/Luogu/P_10446_64_位整数乘法.cpp @@ -0,0 +1,34 @@ +#include +#define lo long long +#define IMX LONG_LONG_MAX +#define IMN LONG_LONG_MIN + +using namespace std; +const int N = 1e7 + 10; +/* + toothless. #17 + @fredcss_dev +*/ + +lo qpow(lo a, lo b, lo p) { + lo result = 0; + while (b) { + if (b & 1) { + result += a; + result %= p; + } + b >>= 1; + a += a; + a %= p; + } + return result; +} + +lo a, b, p; + +signed main() +{ + cin >> a >> b >> p; + cout << qpow(a, b, p); + return 0; +} \ No newline at end of file diff --git a/Luogu/P_1060_NOIP_2006_普及组_开心的金明.cpp b/Luogu/P_1060_NOIP_2006_普及组_开心的金明.cpp new file mode 100644 index 0000000..8a85d50 --- /dev/null +++ b/Luogu/P_1060_NOIP_2006_普及组_开心的金明.cpp @@ -0,0 +1,27 @@ +#include +#define lo long long +#define IMX LONG_LONG_MAX +#define IMN LONG_LONG_MIN + +using namespace std; +const int N = 1e7 + 10; +/* + toothless. #17 + @fredcss_dev + <2025/1/21> +*/ + +int m, n, d[N], w, v; + +signed main() +{ + cin >> m >> n; + for(int i = 1; i <= n; i++) { + cin >> v >> w; + for(int j = m; j >= v; j--){ + d[j] = max(d[j], d[j - v] + w * v); + } + } + printf("%d",d[m]); + return 0; +} \ No newline at end of file diff --git a/Luogu/P_1135_奇怪的电梯.cpp b/Luogu/P_1135_奇怪的电梯.cpp new file mode 100644 index 0000000..2498013 --- /dev/null +++ b/Luogu/P_1135_奇怪的电梯.cpp @@ -0,0 +1,49 @@ +#include +#define int long long +#define endl "\n" +#define IMX LONG_LONG_MAX +#define IMN LONG_LONG_MIN +#define debug$ if(dev) +using namespace std; + +/* toothless. #17 */ + +const int N = 1e7 + 10; +const int M = 2e3 + 5; +const bool dev = false; + +int n, k; +int a[N]; +int ans=0; + +bool isPrime(int x) +{ + if (x == 1) + return false; + for (int i = 2; i * i <= x; i++) + if (x % i == 0) + return false; + return true; +} + +void dfs(int x, int sum, int cnt) +{ + if (x == k) { + if (isPrime(sum)) + ans++; + return; + } + for (int i = cnt; i < n; i++) { + dfs(x + 1, sum + a[i], i + 1); + } +} + +signed main() +{ + cin >> n; + for (int i = 1; i <= n; i++) + cin >> a[i]; + dfs(0, 0, 0); + cout << ans << endl; + return 0; +}` \ No newline at end of file diff --git a/README.md b/README.md index 58d8ff7..e1fa7a2 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Here's my personal solutions to algorithm problems. | Name | Link | Account | :---: | :---: | :---: | +| AtCoder | atcoder.jp | [wczffL_4ever](https://atcoder.jp/users/wczffL_4ever) | | Codeforces | codeforces.com | [Fengyi_Chen](https://codeforces.com/profile/Fengyi_Chen) | | FZOI | 内部 OJ | 内部 OJ | | Luogu | luogu.com.cn | [BunDragon126](https://www.luogu.com.cn/user/927203) |