You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include<iomanip>
#include<iostream>usingnamespacestd;intmain() {
int n, m;
while (cin >> n >> m) {
int a, tot = 0;
for (int i = 0; i < n*m; i++) {
cin >> a;
tot += a;
}
cout << fixed << setprecision(9) << (longdouble)(tot) / (n*m) << endl;
}
return0;
}
Problem B : Schedule
Solution
#include<algorithm>
#include<iostream>
#include<vector>usingnamespacestd;intmain() {
int N, W;
while (cin >> N >> W) {
int c;
vector<vector<int>> sch;
for (c = 4; c <= W; c++) {
sch.clear();
vector<int> cur(c, 1);
for (int i = c/2; i < c; i++) cur[i] = 2;
do {
sch.push_back(cur);
next_permutation(cur.begin(), cur.end());
} while (cur[0] == 1);
if (sch.size() >= N) break;
}
if (c > W) { cout << "infinity" << endl; continue; }
cout << c << endl;
for (int i = 0; i < W; i++) {
for (int j = 0; j < N; j++) cout << sch[j][i%c];
cout << endl;
}
}
}
Problem H : Jet Lag
Solution
#include<iostream>
#include<vector>usingnamespacestd;intmain() {
int N;
while (cin >> N) {
vector<int64_t> B(N+1), E(N+1);
for (int i = 1; i <= N; i++) cin >> B[i] >> E[i];
vector<int64_t> S, T;
for (int i = N, j = i; i > 0;) {
if (j == 0) goto fail;
int64_t t = B[j] - (E[i]-B[j]+1)/2;
if (E[j-1] > t) { j--; continue; }
if (E[j-1] >= t-1) {
S.push_back(E[j-1]);
T.push_back(B[j] - (E[j-1] == t-1 && E[i]-B[j]==1));
} else {
S.push_back(t);
T.push_back(B[j]);
S.push_back(E[j-1]);
T.push_back((E[j-1]+t)/2);
}
i = --j;
}
cout << S.size() << endl;
for (int i = S.size()-1; i >= 0; i--) cout << S[i] << '' << T[i] << endl;
continue;
fail:
cout << "impossible" << endl;
}
}
Problem A : Riddle of the Sphinx
Solution
#include<iostream>usingnamespacestd;intmain()
{
int a, b, c, d, e;
cout << "1 0 0" << endl;
cin >> a;
cout << "0 1 0" << endl;
cin >> b;
cout << "0 0 1" << endl;
cin >> c;
cout << "1 1 1" << endl;
cin >> d;
cout << "1 2 3" << endl;
cin >> e;
if (a + b + c == d)
cout << a << '' << b << '' << c << endl;
elseif (a + 2 * b + 3 * c == e)
cout << a << '' << b << '' << c << endl;
elseif ((d - b - c) + 2 * b + 3 * c == e)
cout << d - b - c << '' << b << '' << c << endl;
elseif (a + 2 * (d - c - a) + 3 * c == e)
cout << a << '' << d - c - a << '' << c << endl;
else
cout << a << '' << b << '' << d - a - b << endl;
}
Problem G : Turning Red
Solution
#include<algorithm>
#include<functional>
#include<iostream>
#include<vector>usingnamespacestd;intmain() {
int L, B;
while (cin >> L >> B) {
vector<vector<int>> lb(L), bl(B);
vector<int> ls(L);
for (int i = 0; i < L; i++) {
char ch;
cin >> ch;
ls[i] = string("RGB").find(ch);
}
for (int i = 0; i < B; i++) {
int K;
cin >> K;
bl[i].resize(K);
for (auto& x : bl[i]) {
cin >> x; x--;
lb[x].push_back(i);
}
}
int ret = 0;
vector<int> push(B, -1);
for (int j = 0; j < L; j++) if (lb[j].empty() && ls[j] != 0) goto fail;
for (int i = 0; i < B; i++) if (push[i] == -1 && bl[i].size()) {
int best = 1e9;
function<int(int,int,int)> rec = [&](int i, int p, int cookie) -> int {
if (push[i] >= cookie) return push[i] == cookie+p ? 0 : 1e9;
push[i] = cookie+p;
int ret = p;
for (auto j : bl[i]) if (lb[j].size() == 2) {
int k = lb[j][0] ^ lb[j][1] ^ i; // Ooh, I'm so clever.
ret += rec(k, (12-ls[j]-p)%3, cookie);
if (ret >= 1e9) return1e9;
} else {
if ((ls[j] + p) % 3 != 0) return1e9;
}
return ret;
};
for (int p = 0; p < 3; p++) best = min(best, rec(i, p, p*3));
if (best == 1e9) goto fail;
ret += best;
}
cout << ret << endl;
continue;
fail:
cout << "impossible" << endl;
}
}
Problem F : Tilting Tiles
Solution
#include<algorithm>
#include<cstring>
#include<iostream>
#include<string>
#include<vector>usingnamespacestd;template<typename T> constexpr T Gcd(const T& a, const T& b) { return b != 0 ? Gcd(b, a%b) : a < 0 ? -a : a; }
voidtilt(int dir, vector<vector<int>>& g) {
int X = g[0].size(), Y = g.size();
if (dir&1) swap(X, Y);
auto get = [&](int x, int y) {
return dir == 0 ? &g[y][x] : dir == 1 ? &g[x][y] : dir == 2 ? &g[y][X-1-x] : &g[X-1-x][y];
};
for (int y = 0; y < Y; y++) {
int x2 = 0;
for (int x = 0; x < X; x++) if (*get(x, y)) {
*get(x2++, y) = *get(x, y);
}
for (; x2 < X; x2++) *get(x2, y) = 0;
}
}
pair<int64_t, int64_t> match(const string& s, const string& t) {
// Who needs hashing/string algorithms?int64_t r, m;
for (r = 0; r <= s.size(); r++) {
if (r == s.size()) return {0, 0};
if (memcmp(&s[r], &t[0], s.size()-r) == 0 && memcmp(&s[0], &t[s.size()-r], r) == 0) break;
}
for (m = r ? r : 1; m < s.size(); m++) {
if (s.size() % m == 0 && memcmp(&s[0], &s[m], s.size()-m) == 0) break;
}
return {r, m};
}
intmain() {
int Y, X;
while (cin >> Y >> X) {
vector<vector<int>> g(Y, vector<int>(X)), g2 = g;
char ch;
for (auto& row : g ) for (auto& v : row) { cin >> ch; if (ch != '.') v = ch-'a'+1; }
for (auto& row : g2) for (auto& v : row) { cin >> ch; if (ch != '.') v = ch-'a'+1; }
for (int sd = 0; sd < 4; sd++)
for (int dd = 1; dd < 4; dd += 2) {
auto tg = g;
for (int i = 0, d = sd; i <= 6; i++, d = (d+dd)%4) {
if (tg == g2) goto pass;
if (i >= 2) {
auto ng = tg;
for (int y = 0; y < Y; y++)
for (int x = 0; x < X; x++) {
if (!!g2[y][x] != !!ng[y][x]) goto nomatch;
if (ng[y][x]) ng[y][x] = y*X+x+1;
}
for (int j = 0; j < 4; j++) tilt((d+j)%4, ng);
vector<int64_t> residues, mods;
for (int y = 0; y < Y; y++)
for (int x = 0; x < X; x++) if (ng[y][x]) {
string s, t;
for (int* ptr = &ng[y][x]; *ptr;) {
int x2 = (*ptr-1)%X, y2 = (*ptr-1)/X;
*ptr = 0;
s += tg[y2][x2]; t += g2[y2][x2];
ptr = &ng[y2][x2];
}
auto [residue, mod] = match(s, t);
if (mod == 0) goto nomatch;
if (mod == 1) continue;
for (int i = 0; i < mods.size(); i++) {
int64_t g = Gcd(mod, mods[i]);
if (residues[i] % g != residue % g) goto nomatch;
}
//cerr << "Adding r=" << residue << " m=" << mod << endl;
residues.push_back(residue); mods.push_back(mod);
}
goto pass;
}
nomatch:;
tilt(d, tg);
}
}
fail:
cout << "no" << endl;
continue;
pass:
cout << "yes" << endl;
}
}