From 8fa1490a34cf7af0a621e60bd7fa31961dd09cd8 Mon Sep 17 00:00:00 2001 From: Bruno Freitas Tissei <bft15@inf.ufpr.br> Date: Sun, 13 Oct 2019 15:04:52 -0300 Subject: [PATCH] Add SBC14 Signed-off-by: Bruno Freitas Tissei <bft15@inf.ufpr.br> --- algorithms/math/legendre.cpp | 2 +- algorithms/structure/disjoint_set.cpp | 2 +- contests/SBC14/A.cpp | 29 +++++++++ contests/SBC14/B.cpp | 36 +++++++++++ contests/SBC14/C.cpp | 49 +++++++++++++++ contests/SBC14/F.cpp | 62 +++++++++++++++++++ contests/SBC14/G.cpp | 84 +++++++++++++++++++++++++ contests/SBC14/H.cpp | 40 ++++++++++++ contests/SBC14/I.cpp | 89 +++++++++++++++++++++++++++ contests/SBC14/K.cpp | 68 ++++++++++++++++++++ 10 files changed, 459 insertions(+), 2 deletions(-) create mode 100644 contests/SBC14/A.cpp create mode 100644 contests/SBC14/B.cpp create mode 100644 contests/SBC14/C.cpp create mode 100644 contests/SBC14/F.cpp create mode 100644 contests/SBC14/G.cpp create mode 100644 contests/SBC14/H.cpp create mode 100644 contests/SBC14/I.cpp create mode 100644 contests/SBC14/K.cpp diff --git a/algorithms/math/legendre.cpp b/algorithms/math/legendre.cpp index 7be0e3c..229c642 100644 --- a/algorithms/math/legendre.cpp +++ b/algorithms/math/legendre.cpp @@ -2,7 +2,7 @@ /// /// Description: /// Given an interger $n$ and a prime number $p$, find the largest power of -/// $p$ ($x$) such that $n!$ is divisible by $k^x$. \par +/// $p$ ($x$) such that $n!$ is divisible by $p^x$. \par /// Writing $n!$ as $1 \cdot 2 \cdot 3 ... (n-1) \cdot n$ shows that every /// $k$-th element of the product is divisible by $k$ (i.e. adds $1$ to the /// answer), the number of such elements is $\lfloor\frac{n}{k}\rfloor$. The diff --git a/algorithms/structure/disjoint_set.cpp b/algorithms/structure/disjoint_set.cpp index 7ce7b10..67a7594 100644 --- a/algorithms/structure/disjoint_set.cpp +++ b/algorithms/structure/disjoint_set.cpp @@ -8,7 +8,7 @@ struct DisjointSet { int N; - vector<int> rnk, par; + vector<int> rnk, par, siz; DisjointSet(int N) : N(N), rnk(N), par(N), siz(N) diff --git a/contests/SBC14/A.cpp b/contests/SBC14/A.cpp new file mode 100644 index 0000000..159d22c --- /dev/null +++ b/contests/SBC14/A.cpp @@ -0,0 +1,29 @@ +#include <bits/stdc++.h> + +#define EPS 1e-6 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define llinf 0x3f3f3f3f3f3f3f3f + +#define fi first +#define se second +#define pb push_back +#define ende '\n' + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define mset(x, y) memset(&x, (y), sizeof(x)) + +using namespace std; + +using ll = long long; +using ii = pair<int,int>; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int x, y; cin >> x >> y; + cout << ((y - 1) / (y - x)) + 1 << ende; + return 0; +} diff --git a/contests/SBC14/B.cpp b/contests/SBC14/B.cpp new file mode 100644 index 0000000..98e0d00 --- /dev/null +++ b/contests/SBC14/B.cpp @@ -0,0 +1,36 @@ +#include <bits/stdc++.h> + +#define EPS 1e-6 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define llinf 0x3f3f3f3f3f3f3f3f + +#define fi first +#define se second +#define pb push_back +#define ende '\n' + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define mset(x, y) memset(&x, (y), sizeof(x)) + +using namespace std; + +using ll = long long; +using ii = pair<int,int>; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + ll n; cin >> n; + ll num = 2 % (n + 1); + int ans = 1; + while (true) { + if (num == 1) + return cout << ans << ende, 0; + num = (num * 2) % (n + 1); + ans++; + } + return 0; +} diff --git a/contests/SBC14/C.cpp b/contests/SBC14/C.cpp new file mode 100644 index 0000000..57cf893 --- /dev/null +++ b/contests/SBC14/C.cpp @@ -0,0 +1,49 @@ +#include <bits/stdc++.h> + +#define EPS 1e-6 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define llinf 0x3f3f3f3f3f3f3f3f + +#define fi first +#define se second +#define pb push_back +#define ende '\n' + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define mset(x, y) memset(&x, (y), sizeof(x)) + +using namespace std; + +using ll = long long; +using ii = pair<int,int>; + +struct plane { ll a, b, c, d; }; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int m, n; cin >> m >> n; + vector<plane> p(m); + for (int i = 0; i < m; ++i) { + ll a, b, c, d; cin >> a >> b >> c >> d; + p[i] = {a, b, c, d}; + } + + map<string,int> M; + for (int i = 0; i < n; ++i) { + string st; + ll a, b, c; cin >> a >> b >> c; + for (int j = 0; j < m; ++j) + st.pb((p[j].a*a + p[j].b*b + p[j].c*c > p[j].d) + '0'); + M[st] += 1; + } + + int ans = 0; + for (auto &i : M) + ans = max(ans, i.se); + cout << ans << ende; + return 0; +} diff --git a/contests/SBC14/F.cpp b/contests/SBC14/F.cpp new file mode 100644 index 0000000..af8af4a --- /dev/null +++ b/contests/SBC14/F.cpp @@ -0,0 +1,62 @@ +#include <bits/stdc++.h> + +#define EPS 1e-6 +#define MOD 10000 +#define inf 0x3f3f3f3f +#define llinf 0x3f3f3f3f3f3f3f3f + +#define fi first +#define se second +#define pb push_back +#define ende '\n' + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define mset(x, y) memset(&x, (y), sizeof(x)) + +using namespace std; + +using ll = long long; +using ii = pair<int,int>; + +ll mat[110*110], ans[110*110], aux[110*110]; + +void mult(ll *a, ll *b, ll *res, int n) { + for (int i = 0; i < n; ++i) + for (int j = 0; j < n; ++j) { + aux[i*n+j] = 0; + for (int k = 0; k < n; ++k) + aux[i*n+j] = (aux[i*n+j] + a[i*n+k] * b[k*n+j]) % MOD; + } + + memcpy(res, aux, sizeof aux); +} + +void bin_exp(ll e, int n) { + mset(ans, 0); + for (int i = 0; i < n; ++i) + ans[i*n+i] = 1; + + while (e) { + if (e & 1) mult(ans, mat, ans, n); + e >>= 1; + mult(mat, mat, mat, n); + } +} + +int main() { + int n; ll l; + while (scanf("%d %lld", &n, &l) != EOF) { + int s, t, x; cin >> s >> t; s--, t--; + mset(mat, 0); + for (int i = 0; i < n; ++i) + for (int j = 0; j < 4; ++j) { + scanf("%d", &x); x--; + mat[i*n+x]++; + } + + bin_exp(l, n); + printf("%lld\n", ans[s*n + t] % MOD); + } + return 0; +} diff --git a/contests/SBC14/G.cpp b/contests/SBC14/G.cpp new file mode 100644 index 0000000..b38b7ba --- /dev/null +++ b/contests/SBC14/G.cpp @@ -0,0 +1,84 @@ +#include <bits/stdc++.h> + +#define EPS 1e-6 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define llinf 0x3f3f3f3f3f3f3f3f + +#define fi first +#define se second +#define pb push_back +#define ende '\n' + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define mset(x, y) memset(&x, (y), sizeof(x)) + +using namespace std; + +using ll = long long; +using ii = pair<int,int>; +using pii = pair<ii,ii>; + +struct elem { + int x, y, l, u, d; +}; + +ii get(char x) { + if (x >= 'a' && x <= 'j') + return {1 << (x - 'a'), 0}; + return {0, 1 << (x - 'A')}; +} + +map<pii,int> foi; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + vector<int> dx = {-1, 1, 0, 0}; + vector<int> dy = {0, 0, 1, -1}; + + int n; cin >> n; + vector<string> v(n); + for (auto &i : v) + cin >> i; + + queue<elem> Q; + ii val = get(v[0][0]); + Q.push({0, 0, val.fi, val.se, 1}); + foi[pii(ii(0,0),ii(val.fi,val.fi))] = 1; + int ans = inf; + + while (!Q.empty()) { + elem u = Q.front(); Q.pop(); + if (u.x == n - 1 && u.y == n - 1) + ans = min(ans, u.d); + + for (int i = 0; i < 4; ++i) { + int x = u.x + dx[i]; + int y = u.y + dy[i]; + + if (x >= 0 && x < n && y >= 0 && y < n) { + val = get(v[x][y]); + if (val.fi) { + if (!(u.u & val.fi)) + if (!foi[pii(ii(x,y),ii(val.fi|u.l,u.u))]) { + Q.push({x, y, val.fi | u.l, u.u, u.d + 1}); + foi[pii(ii(x,y),ii(val.fi|u.l,u.u))] = 1; + } + } else { + if (!(u.l & val.se)) + if (!foi[pii(ii(x,y),ii(u.l,val.se | u.u))]) { + Q.push({x, y, u.l, val.se | u.u, u.d + 1}); + foi[pii(ii(x,y),ii(u.l,val.se|u.u))] = 1; + } + } + } + } + } + + if (ans == inf) cout << -1 << ende; + else cout << ans << ende; + return 0; +} diff --git a/contests/SBC14/H.cpp b/contests/SBC14/H.cpp new file mode 100644 index 0000000..6972a43 --- /dev/null +++ b/contests/SBC14/H.cpp @@ -0,0 +1,40 @@ +#include <bits/stdc++.h> + +#define EPS 1e-6 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define llinf 0x3f3f3f3f3f3f3f3f + +#define fi first +#define se second +#define pb push_back +#define ende '\n' + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define mset(x, y) memset(&x, (y), sizeof(x)) + +using namespace std; + +using ll = long long; +using ii = pair<int,int>; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int ans = 0; + int n, m; cin >> n >> m; + for (int i = 0; i < n; ++i) { + int cnt = 0; + for (int j = 0; j < m; ++j) { + int x; cin >> x; + cnt += (x != 0); + } + if (cnt == m) + ans++; + } + + cout << ans << ende; + return 0; +} diff --git a/contests/SBC14/I.cpp b/contests/SBC14/I.cpp new file mode 100644 index 0000000..bbc0aff --- /dev/null +++ b/contests/SBC14/I.cpp @@ -0,0 +1,89 @@ +#include <bits/stdc++.h> + +#define EPS 1e-6 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define llinf 0x3f3f3f3f3f3f3f3f + +#define fi first +#define se second +#define pb push_back +#define ende '\n' + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define mset(x, y) memset(&x, (y), sizeof(x)) + +using namespace std; + +using ll = long long; +using ii = pair<int,int>; + +vector<int> sieve(int n) { + vector<int> primes; + vector<int> is_prime(n + 1, 1); + + for (int p = 2; p*p <= n; ++p) + if (is_prime[p]) + for (int i = p*p; i <= n; i += p) + is_prime[i] = false; + + for (int p = 2; p <= n; ++p) + if (is_prime[p]) + primes.pb(p); + + return primes; +} + +struct ans_t { ll x, y, d; }; + +ans_t ext_gcd(ll a, ll b) { + if (a == 0) return {0, 1, b}; + ans_t e = ext_gcd(b % a, a); + return {e.y - (b/a)*e.x, e.x, e.d}; +} + +int extended_euclidean(int a, int m) { + ans_t g = ext_gcd(a, m); + return (g.x % m + m) % m; +} + +ll bin_exp(ll a, ll e, ll M = MOD) { + ll x = 1; + while (e) { + if (e & 1) x = (x * a) % M; + e >>= 1; + a = (a * a) % M; + } + return x % M; +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + ll n, e, c; cin >> n >> e >> c; + vector<int> primes = sieve((int) sqrt(n)+1); + + for (int i = 0; i < primes.size() && primes[i]*primes[i] <= n; ++i) { + if (n % primes[i] == 0) { + ll p = primes[i]; + ll q = n / p; + + if (p % 2 == 0 || q % 2 == 0) + continue; + + ll phi = (p - 1) * (q - 1); + if (e >= phi || __gcd(phi, e) != 1) + continue; + + ll d = extended_euclidean(e, phi); + ll m = bin_exp(c, d, n); + + if (bin_exp(m, e, n) == c && m > 0) + return cout << m << ende, 0; + } + } + + return 0; +} diff --git a/contests/SBC14/K.cpp b/contests/SBC14/K.cpp new file mode 100644 index 0000000..007b9e7 --- /dev/null +++ b/contests/SBC14/K.cpp @@ -0,0 +1,68 @@ +#include <bits/stdc++.h> + +#define EPS 1e-6 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define llinf 0x3f3f3f3f3f3f3f3f + +#define fi first +#define se second +#define pb push_back +#define ende '\n' + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define mset(x, y) memset(&x, (y), sizeof(x)) + +using namespace std; + +using ll = long long; +using ii = pair<int,int>; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + double c; + int n; cin >> c >> n; + + vector<double> v(n); + for (auto &i : v) cin >> i; + + double l = v[0] + EPS, r = v[1]; + for (int i = 0; i < 30; ++i) { + double m = (l + r) / 2.0; + double cc = m + (c / n); + + int k = 1; + int whi = 0; + for (int j = 1; j < n; j = k) { + while (k < n && v[k] < cc) k++; + if (k == n) + if (v[0] + c < cc) + k++; + + if (k - j > 1) { + whi = 1; + break; + } + + if (k - j == 0) { + whi = 2; + break; + } + + cc += (c / n); + } + + if (whi == 0) { + return cout << "S" << ende, 0; + } else if (whi == 1) + r = m; + else + l = m; + } + + cout << "N" << ende; + return 0; +} -- GitLab