Skip to content

Commit

Permalink
Codeforces Gym: 2002-2003 Winter Petrozavodsk Camp, Andrew Stankevich…
Browse files Browse the repository at this point in the history
… Contest 1 (ASC 1)
  • Loading branch information
Macesuted committed Jan 10, 2025
1 parent 8b1ba5a commit 75ed33a
Show file tree
Hide file tree
Showing 7 changed files with 534 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Codeforces Gym/100199A.py
Original file line number Diff line number Diff line change
@@ -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))
121 changes: 121 additions & 0 deletions Codeforces Gym/100199B.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**
* @file 100199B.cpp
* @author Macesuted ([email protected])
* @date 2025-01-10
*
* @copyright Copyright (c) 2025
*
*/

#include <bits/stdc++.h>
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<vector<Edge>> graph;
vector<vector<Edge>::iterator> cur;
vector<int> 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<int> 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;
}
70 changes: 70 additions & 0 deletions Codeforces Gym/100199C.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @file 100199C.cpp
* @author Macesuted ([email protected])
* @date 2025-01-10
*
* @copyright Copyright (c) 2025
*
*/

#include <bits/stdc++.h>
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<int> que;
for (int i = 1; i <= n; i++)
if (!deg[i]) que.push(i);

vector<int> 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;
}
49 changes: 49 additions & 0 deletions Codeforces Gym/100199D.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @file 100199D.cpp
* @author Macesuted ([email protected])
* @date 2025-01-10
*
* @copyright Copyright (c) 2025
*
*/

#include <bits/stdc++.h>
using namespace std;

#ifndef LOCAL
#define endl '\n'
#endif

bool mem1;

void solve(void) {
int n, m;
cin >> n >> m;

vector<int> 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;
}
109 changes: 109 additions & 0 deletions Codeforces Gym/100199E.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* @file 100199E.cpp
* @author Macesuted ([email protected])
* @date 2025-01-10
*
* @copyright Copyright (c) 2025
*
*/

#include <bits/stdc++.h>
using namespace std;

#ifndef LOCAL
#define endl '\n'
#endif

bool mem1;

int mod;

class Matrix {
private:
vector<vector<int>> mat;
int n, m;

public:
vector<int>& 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<int>(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;
}
Loading

0 comments on commit 75ed33a

Please sign in to comment.