From 75ed33a207fa99e0f428b57cecc078b67e197452 Mon Sep 17 00:00:00 2001 From: Macesuted Date: Fri, 10 Jan 2025 19:18:35 +0800 Subject: [PATCH] Codeforces Gym: 2002-2003 Winter Petrozavodsk Camp, Andrew Stankevich Contest 1 (ASC 1) --- Codeforces Gym/100199A.py | 13 ++++ Codeforces Gym/100199B.cpp | 121 +++++++++++++++++++++++++++++++++++++ Codeforces Gym/100199C.cpp | 70 +++++++++++++++++++++ Codeforces Gym/100199D.cpp | 49 +++++++++++++++ Codeforces Gym/100199E.cpp | 109 +++++++++++++++++++++++++++++++++ Codeforces Gym/100199G.cpp | 85 ++++++++++++++++++++++++++ Codeforces Gym/100199H.cpp | 87 ++++++++++++++++++++++++++ 7 files changed, 534 insertions(+) create mode 100644 Codeforces Gym/100199A.py create mode 100644 Codeforces Gym/100199B.cpp create mode 100644 Codeforces Gym/100199C.cpp create mode 100644 Codeforces Gym/100199D.cpp create mode 100644 Codeforces Gym/100199E.cpp create mode 100644 Codeforces Gym/100199G.cpp create mode 100644 Codeforces Gym/100199H.cpp diff --git a/Codeforces Gym/100199A.py b/Codeforces Gym/100199A.py new file mode 100644 index 0000000..305638d --- /dev/null +++ b/Codeforces Gym/100199A.py @@ -0,0 +1,13 @@ +import math; + +fin = open("china.in", "r") +fout = open("china.out", "w") + +n = int(fin.read()) + +m = n // 2 + +while math.gcd(n, m) != 1: + m -= 1 + +fout.write(str(m)) \ No newline at end of file diff --git a/Codeforces Gym/100199B.cpp b/Codeforces Gym/100199B.cpp new file mode 100644 index 0000000..309455b --- /dev/null +++ b/Codeforces Gym/100199B.cpp @@ -0,0 +1,121 @@ +/** + * @file 100199B.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2025-01-10 + * + * @copyright Copyright (c) 2025 + * + */ + +#include +using namespace std; + +#ifndef LOCAL +#define endl '\n' +#endif + +bool mem1; + +#define maxm 40005 + +int cap[maxm]; + +class Dinic { + private: + struct Edge { + int to, cap, rev, id; + }; + + vector> graph; + vector::iterator> cur; + vector dist, preF; + int n, S, T; + + bool bfs(void) { + for (int i = 1; i <= n; i++) dist[i] = INT_MAX, cur[i] = graph[i].begin(); + queue que; + dist[S] = 0, que.push(S); + while (!que.empty()) { + int p = que.front(); + que.pop(); + for (auto i : graph[p]) + if (i.cap > 0 && dist[i.to] == INT_MAX) dist[i.to] = dist[p] + 1, que.push(i.to); + } + return dist[T] != INT_MAX; + } + + int dfs(int p, int rest) { + if (p == T) return rest; + int c, flow = 0; + for (auto i = cur[p]; i != graph[p].end() && rest; i++) { + cur[p] = i; + if (i->cap == 0 || dist[i->to] != dist[p] + 1) continue; + if (!(c = dfs(i->to, min(rest, i->cap)))) dist[i->to] = -1; + flow += c, rest -= c, i->cap -= c, graph[i->to][i->rev].cap += c; + } + return flow; + } + + public: + void resize(int _n) { return graph.resize((n = _n) + 1), cur.resize(n + 1), dist.resize(n + 1), preF.resize(n + 1); } + void addEdge(int from, int to, int cap, int low, int id) { + preF[from] -= low, preF[to] += low, cap -= low, ::cap[id] += low; + return graph[from].push_back(Edge{to, cap, (int)graph[to].size(), id}), + graph[to].push_back(Edge{from, 0, (int)graph[from].size() - 1, 0}); + } + bool maxFlow(int _S, int _T) { + S = _S, T = _T; + + for (int i = 1; i <= n; i++) { + if (i == S || i == T) continue; + if (preF[i] > 0) addEdge(S, i, preF[i], 0, 0); + if (preF[i] < 0) addEdge(i, T, -preF[i], 0, 0); + } + + while (bfs()) dfs(S, INT_MAX); + + for (int i = 1; i <= n; i++) + for (auto e : graph[i]) + if (e.id) cap[e.id] += graph[e.to][e.rev].cap; + + for (auto e : graph[S]) + if (e.cap) return false; + return true; + } +}; + +void solve(void) { + int n, m; + cin >> n >> m; + + Dinic dnc; + dnc.resize(n + 2); + for (int i = 1, x, y, c, l; i <= m; i++) cin >> x >> y >> l >> c, dnc.addEdge(x, y, c, l, i); + + if (!dnc.maxFlow(n + 1, n + 2)) return cout << "NO" << endl, void(); + + cout << "YES" << endl; + for (int i = 1; i <= m; i++) cout << cap[i] << endl; + + return; +} + +bool mem2; + +int main() { +#ifndef LOCAL + freopen("cooling.in", "r", stdin), freopen("cooling.out", "w", stdout); +#endif + ios::sync_with_stdio(false), cin.tie(nullptr); +#ifdef LOCAL + cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl; +#endif + + int _ = 1; + while (_--) solve(); + +#ifdef LOCAL + cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl; +#endif + return 0; +} \ No newline at end of file diff --git a/Codeforces Gym/100199C.cpp b/Codeforces Gym/100199C.cpp new file mode 100644 index 0000000..c454758 --- /dev/null +++ b/Codeforces Gym/100199C.cpp @@ -0,0 +1,70 @@ +/** + * @file 100199C.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2025-01-10 + * + * @copyright Copyright (c) 2025 + * + */ + +#include +using namespace std; + +#ifndef LOCAL +#define endl '\n' +#endif + +bool mem1; + +#define maxn 500005 + +int fa[maxn], deg[maxn]; +bool vis[maxn]; + +void solve(void) { + int n; + cin >> n; + + for (int i = 2; i <= n; i++) cin >> fa[i], deg[fa[i]]++; + + queue que; + for (int i = 1; i <= n; i++) + if (!deg[i]) que.push(i); + + vector ans; + while (!que.empty()) { + int p = que.front(); + que.pop(); + if (p == 1) break; + + if (!vis[p] && !vis[fa[p]]) vis[p] = vis[fa[p]] = true, ans.push_back(p); + if (!--deg[fa[p]]) que.push(fa[p]); + } + + cout << ans.size() * 1000 << endl; + sort(ans.begin(), ans.end()); + for (auto i : ans) cout << i << ' '; + cout << endl; + + return; +} + +bool mem2; + +int main() { +#ifndef LOCAL + freopen("grant.in", "r", stdin), freopen("grant.out", "w", stdout); +#endif + ios::sync_with_stdio(false), cin.tie(nullptr); +#ifdef LOCAL + cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl; +#endif + + int _ = 1; + while (_--) solve(); + +#ifdef LOCAL + cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl; +#endif + return 0; +} \ No newline at end of file diff --git a/Codeforces Gym/100199D.cpp b/Codeforces Gym/100199D.cpp new file mode 100644 index 0000000..b13ccd5 --- /dev/null +++ b/Codeforces Gym/100199D.cpp @@ -0,0 +1,49 @@ +/** + * @file 100199D.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2025-01-10 + * + * @copyright Copyright (c) 2025 + * + */ + +#include +using namespace std; + +#ifndef LOCAL +#define endl '\n' +#endif + +bool mem1; + +void solve(void) { + int n, m; + cin >> n >> m; + + vector cnt(n + 1); + for (int i = 1, x, y; i <= m; i++) cin >> x >> y, cnt[x]++, cnt[y]++; + int64_t ans = 0; + for (int i = 1; i <= n; i++) ans += (int64_t)cnt[i] * cnt[i]; + cout << ans << endl; + return; +} + +bool mem2; + +int main() { +#ifndef LOCAL + freopen("matrix.in", "r", stdin), freopen("matrix.out", "w", stdout); +#endif + ios::sync_with_stdio(false), cin.tie(nullptr); +#ifdef LOCAL + cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl; +#endif + + int _ = 1; + while (_--) solve(); + +#ifdef LOCAL + cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl; +#endif + return 0; +} \ No newline at end of file diff --git a/Codeforces Gym/100199E.cpp b/Codeforces Gym/100199E.cpp new file mode 100644 index 0000000..514e179 --- /dev/null +++ b/Codeforces Gym/100199E.cpp @@ -0,0 +1,109 @@ +/** + * @file 100199E.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2025-01-10 + * + * @copyright Copyright (c) 2025 + * + */ + +#include +using namespace std; + +#ifndef LOCAL +#define endl '\n' +#endif + +bool mem1; + +int mod; + +class Matrix { + private: + vector> mat; + int n, m; + + public: + vector& operator[](size_t i) { return mat[i]; } + int get(size_t i, size_t j) const { return mat[i][j]; } + + void resize(int _n, int _m) { return mat.resize(n = _n, vector(m = _m, 0)); } + + friend Matrix operator*(const Matrix& a, const Matrix& b) { + assert(a.m == b.n); + + Matrix c; + c.resize(a.n, b.m); + for (int i = 0; i < a.n; i++) + for (int k = 0; k < b.m; k++) { + int64_t ans = 0; + for (int j = 0; j < a.m; j++) ans += a.get(i, j) * b.get(j, k); + c.mat[i][k] = ans % mod; + } + + return c; + } +}; + +void solve(void) { + string n; + int m; + cin >> n >> m >> mod; + reverse(n.begin(), n.end()); + + Matrix a, f; + a.resize(1, 1 << m), f.resize(1 << m, 1 << m); + + for (int i = 0; i < (1 << m); i++) a[0][i] = 1; + for (int S = 0; S < (1 << m); S++) + for (int T = 0; T < (1 << m); T++) { + f[S][T] = 1; + for (int j = 1; j < m; j++) + if ((S >> (j - 1) & 1) == (S >> j & 1) && (T >> (j - 1) & 1) == (T >> j & 1) && (S >> j & 1) == (T >> j & 1)) + f[S][T] = 0; + } + + { + int p = 0; + while (n[p] == '0') p++; + n[p]--; + while (p) n[--p] = '9'; + while (n.back() == '0') n.pop_back(); + } + + while (!n.empty()) { + if ((n.front() - '0') & 1) a = a * f; + f = f * f; + for (int i = (int)n.size() - 1; ~i; i--) { + if (i && (n[i] - '0') & 1) n[i - 1] += 10; + n[i] = '0' + ((n[i] - '0') >> 1); + } + while (n.back() == '0') n.pop_back(); + } + + int64_t ans = 0; + for (int i = 0; i < (1 << m); i++) ans += a.get(0, i); + cout << ans % mod << endl; + + return; +} + +bool mem2; + +int main() { +#ifndef LOCAL + freopen("nice.in", "r", stdin), freopen("nice.out", "w", stdout); +#endif + ios::sync_with_stdio(false), cin.tie(nullptr); +#ifdef LOCAL + cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl; +#endif + + int _ = 1; + while (_--) solve(); + +#ifdef LOCAL + cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl; +#endif + return 0; +} \ No newline at end of file diff --git a/Codeforces Gym/100199G.cpp b/Codeforces Gym/100199G.cpp new file mode 100644 index 0000000..3d0fd3b --- /dev/null +++ b/Codeforces Gym/100199G.cpp @@ -0,0 +1,85 @@ +/** + * @file 100199G.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2025-01-10 + * + * @copyright Copyright (c) 2025 + * + */ + +#include +using namespace std; + +#ifndef LOCAL +#define endl '\n' +#endif + +bool mem1; + +#define maxn 100005 + +typedef pair pii; +typedef tuple tiii; + +class FenwickTree { + private: + pii tree[maxn]; + + public: + void insert(int p, pii v) { + for (int i = p; i < maxn; i += i & -i) tree[i] = max(tree[i], v); + return; + } + pii query(int p) { + pii ans = {0, 0}; + for (int i = p; i; i -= i & -i) ans = max(ans, tree[i]); + return ans; + } +} FT; + +tiii a[maxn]; +int b[maxn], pre[maxn]; + +void solve(void) { + int n; + cin >> n; + for (int i = 1; i <= n; i++) cin >> get<0>(a[i]) >> get<1>(a[i]), get<2>(a[i]) = i, b[i] = get<1>(a[i]); + sort(a + 1, a + n + 1), sort(b + 1, b + n + 1); + for (int i = 1; i <= n; i++) get<1>(a[i]) = lower_bound(b + 1, b + n + 1, get<1>(a[i])) - b; + for (int i = 1, j; i <= n; i = j + 1) { + j = i; + while (j < n && get<0>(a[j + 1]) == get<0>(a[j])) j++; + for (int t = j; t >= i; t--) { + auto [v, p] = FT.query(get<1>(a[t]) - 1); + pre[t] = p, FT.insert(get<1>(a[t]), {v + 1, t}); + } + } + auto [v, p] = FT.query(maxn - 1); + cout << v << endl; + vector ans; + while (p) ans.push_back(get<2>(a[p])), p = pre[p]; + sort(ans.begin(), ans.end()); + for (auto x : ans) cout << x << ' '; + cout << endl; + return; +} + +bool mem2; + +int main() { +#ifndef LOCAL + freopen("people.in", "r", stdin), freopen("people.out", "w", stdout); +#endif + ios::sync_with_stdio(false), cin.tie(nullptr); +#ifdef LOCAL + cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl; +#endif + + int _ = 1; + while (_--) solve(); + +#ifdef LOCAL + cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl; +#endif + return 0; +} \ No newline at end of file diff --git a/Codeforces Gym/100199H.cpp b/Codeforces Gym/100199H.cpp new file mode 100644 index 0000000..8707a21 --- /dev/null +++ b/Codeforces Gym/100199H.cpp @@ -0,0 +1,87 @@ +/** + * @file 100199H.cpp + * @author Macesuted (i@macesuted.moe) + * @date 2025-01-10 + * + * @copyright Copyright (c) 2025 + * + */ + +#include +using namespace std; + +#ifndef LOCAL +#define endl '\n' +#endif + +bool mem1; + +#define maxn 105 +#define maxv 100005 + +typedef __int128_t int128_t; + +vector primes; +bool notp[maxv]; +int128_t base[maxn]; + +void solve(void) { + int t, m; + cin >> t >> m; + + for (int i = 2; (int)primes.size() < t; i++) { + if (!notp[i]) primes.push_back(i); + for (auto j = primes.begin(); i * *j < maxv; j++) { + notp[i * *j] = true; + if (i % *j == 0) break; + } + } + + int128_t ans = 1; + for (int i = 1, v; i <= m; i++) { + cin >> v; + + int128_t x = 0; + for (int j = 0; j < t; j++) + while (v % primes[j] == 0) x ^= (int128_t)1 << j, v /= primes[j]; + + for (int j = 0; j < t; j++) { + if (!(x >> j & 1)) continue; + if (!base[j]) { + base[j] = x; + break; + } + x ^= base[j]; + } + if (!x) ans <<= 1; + } + + ans--; + if (!ans) return cout << 0 << endl, void(); + string str; + while (ans) str.push_back('0' + ans % 10), ans /= 10; + reverse(str.begin(), str.end()); + cout << str << endl; + + return; +} + +bool mem2; + +int main() { +#ifndef LOCAL + freopen("rsa.in", "r", stdin), freopen("rsa.out", "w", stdout); +#endif + ios::sync_with_stdio(false), cin.tie(nullptr); +#ifdef LOCAL + cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl; +#endif + + int _ = 1; + while (_--) solve(); + +#ifdef LOCAL + cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl; +#endif + return 0; +} \ No newline at end of file