Skip to content

Commit

Permalink
new: ACPC 2022
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-besbes committed Nov 27, 2024
1 parent 79ff178 commit fed02cf
Show file tree
Hide file tree
Showing 7 changed files with 374 additions and 0 deletions.
90 changes: 90 additions & 0 deletions solutions/ACM ICPC/ACPC/ACPC 2022/A.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll

const int N = 3e6 + 100;
const int mod = 1e9 + 7;

int binpow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) {
res *= a;
res %= mod;
}
b /= 2;
a *= a;
a %= mod;
}
return res;
}
int inv(int n) { return binpow(n, mod - 2) % mod; }
int fact[N];
int nck(int n, int k) {
return (fact[n] * inv((fact[k] * fact[n - k]) % mod) % mod) % mod;
}

void solve() {
int n, s, g;
cin >> n >> s >> g;
if (s % g != 0 || s / g < n) {
cout << 0 << endl;
return;
}
int t = s / g;

if (t % n != 0) {
int x = t % n;
cout << nck(n, x) << endl;
return;
}
if (t / n == 1) {
cout << 1 << endl;
return;
}
if (n == 2 && ((t / n) & 1)) {
cout << 2 << endl;
return;
}
int res = 0;
for (int n1 = 1; n1 <= (n - (t / n) % 2) / 2; n1++) {
res +=
fact[n] * inv((fact[n1] * fact[n1] % mod * fact[n - 2 * n1]) % mod);
res %= mod;
}
cout << res << endl;
}

/*
6
2 6 15
1 15 5
2 15 2
2 16 2
2 6 1
1 15 6
0
0
0
2
2
0
*/

signed main() {
ios::sync_with_stdio(false);
cin.tie(0);

fact[0] = 1;
for (int i = 1; i < N; i++) {
fact[i] = fact[i - 1] * i;
fact[i] %= mod;
}

int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
}
102 changes: 102 additions & 0 deletions solutions/ACM ICPC/ACPC/ACPC 2022/B.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
typedef long long ll;

struct P {
ll x, y;
P operator+(P p) const { return P{x + p.x, y + p.y}; }
P operator-(P p) const { return P{x - p.x, y - p.y}; }
ll dot(P p) const { return x * p.x + y * p.y; }
ll cross(P p) const { return x * p.y - y * p.x; }
bool between(P a, P b, P c, P d, bool strict) {
auto s1 = (d - c).cross(*this - c);
auto s2 = (a - b).cross(*this - b);
if (strict) return s1 && s2 && ((s1 < 0) ^ (s2 < 0));
return (s1 == 0) || (s2 == 0) || ((s1 < 0) ^ (s2 < 0));
}
};

const ll mod = 1e9 + 7;

void solve() {
int n;
cin >> n;
vector<P> p(n);
vector<ll> keys;

auto key = [](P &p) {
auto [x, y] = p;
return (x + 1e6) + (y + 1e6) * mod;
};

for (auto &i : p) {
cin >> i.x >> i.y;
keys.push_back(key(i));
}

sort(all(p), [&](auto x, auto y) { return key(x) <= key(y); });
sort(all(keys));

auto cnt = [&](int a, int b, int c, int d) {
int ans = 0;
for (int i = 0; i < n; i++) {
if (i == a || i == b || i == c || i == d) continue;
if (p[i].between(p[a], p[b], p[c], p[d], true)) {
auto s1 = (p[i] - p[b]).cross(p[c] - p[b]);
auto s2 = (p[d] - p[b]).cross(p[c] - p[b]);
if (s1 && s2 && ((s1 < 0) ^ (s2 < 0))) {
// cout << a << " " << b << " " << c << " " << d << " " << i <<
// endl;
ans++;
}
}
}
return ans;
};

int res = 0;
for (int k = 0; k < n; k++) {
for (int i = k + 1; i < n; i++) {
for (int j = i + 1; j < n; j++) {
auto a = p[k], b = p[i], c = p[j];
if ((a - b).dot(c - b) != 0 || (a - b).cross(c - b) == 0) continue;

/**
* A ---------- D
* | |
* | |
* B ---------- C
*/

auto d = a + (c - b); // OD = OB + BD = OB + BA + BC = OA + BC
assert((b - a).dot(d - a) == 0);
auto it = lower_bound(all(keys), key(d));
if (it == keys.end() || *it != key(d)) continue;
int idx = it - keys.begin();

// cout << "(" << a.x << "," << a.y << ") (" << b.x << "," << b.y
// << ") (" << c.x << "," << c.y << ") => (" << d.x << "," <<
// d.y
// << ")" << endl;
// cout << k << " " << i << " " << j << " " << idx << endl;

res += cnt(k, i, j, idx);
res += cnt(j, idx, k, i);
}
}
}

cout << res << endl;
}

signed main() {
ios::sync_with_stdio(false);
cin.tie(0);

int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
}
24 changes: 24 additions & 0 deletions solutions/ACM ICPC/ACPC/ACPC 2022/G.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll

void solve() {
int z, o;
cin >> z >> o;
if (o & 1) {
cout << "Second" << endl;
} else
cout << "First" << endl;
}

signed main() {
ios::sync_with_stdio(false);
cin.tie(0);

int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
}
20 changes: 20 additions & 0 deletions solutions/ACM ICPC/ACPC/ACPC 2022/I.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <bits/stdc++.h>
using namespace std;

void solve() {
int n;
cin >> n;
if (n == 2022 || n == 2023) {
cout << "Egypt" << endl;
} else {
cout << "Elsewhere" << endl;
}
}

int main() {
int tt = 1;
// cin >> tt ;
while (tt--) {
solve();
}
}
40 changes: 40 additions & 0 deletions solutions/ACM ICPC/ACPC/ACPC 2022/J.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

void solve() {
int n;
cin >> n;
vector<string> s;
vector<vector<int>> v;

int maxi = 0;
for (int i = 0; i < n; i++) {
string cur;
int val;
cin >> cur >> val;
if (!s.empty() && s.back() == cur) {
v.back().push_back(val);
maxi = max(maxi, (int)v.back().size());
} else {
s.push_back(cur);
v.push_back({val});
maxi = max(maxi, (int)v.back().size());
}
}

for (int i = 0; i < maxi; i++) {
for (int j = 0; j < s.size(); j++)
if (i < v[j].size()) cout << s[j] << " " << v[j][i] << "\n";
}
}

signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
}
65 changes: 65 additions & 0 deletions solutions/ACM ICPC/ACPC/ACPC 2022/K.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) x.begin(), x.end()
#define int ll

const int N = 1e6 + 1;

pair<int, int> good(int x) {
int l = 0;
int r = 1e6 + 100;
int ans = 0;
while (l <= r) {
int m = (l + r) / 2;
int sum = (m * (m + 1)) / 2;
if (sum >= x) {
r = m - 1;
ans = m;
} else {
l = m + 1;
}
}
return {(x - (ans * (ans - 1)) / 2), ans};
}

void solve() {
int n, k;
cin >> n >> k;
auto [nb, m] = good(k);
vector<int> ans(n);
for (int i = 0; i < n - m; i++) ans[i] = i + 1;
for (int i = n - m, j = n; i < n; i++, j--) ans[i] = j;

// for (auto i : ans) cout << i << " ";
// cout << endl;
// cout << n - m - 1 << " " << k << endl ;

// if (n - m - 1 == -1) {
// for (auto i : ans) cout << i << " ";
// cout << endl;
// return;
// }

int idx = n - 1;
while (idx >= 0 && nb--) {
swap(ans[n - m - 1], ans[idx]);
idx--;
}

// // cout << idx << " " << k << endl;
for (auto i : ans) cout << i << " ";
cout << endl;
}

signed main() {
// freopen("a.txt", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);

int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
}
33 changes: 33 additions & 0 deletions solutions/ACM ICPC/ACPC/ACPC 2022/L.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

void solve() {
int n, m;
cin >> n >> m;
set<pair<int, int>> s;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
s.insert({x, y});
}
for (int i = 0; i < n - 1; i++) {
if (s.count({i + 1, i + 2})) {
cout << 0 << " ";
} else {
cout << 1 << " ";
}
}
cout << 0;
cout << endl;
}

signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt = 1;
// cin >> tt;
while (tt--) {
solve();
}
}

0 comments on commit fed02cf

Please sign in to comment.