solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; int n, t; int a[2000 + 5][2000 + 5]; bool c[2000 + 5][2000 + 5]; int dx[] = {0, 0, -1, 1}; int dy[] = {1, -1, 0, 0}; void ini() { memset(a, 0, sizeof(a)); memset(c, 0, sizeof(c)); a[500][500] = n; queue<pair<int, int> > q; q.push(make_pair(500, 500)); while (!q.empty()) { pair<int, int> u = q.front(); q.pop(); c[u.first][u.second] = 0; int k = a[u.first][u.second] / 4; for (int i = 0; i < 4; i++) { int x = u.first + dx[i]; int y = u.second + dy[i]; a[x][y] += k; if (!c[x][y] && a[x][y] > 3) { q.push(make_pair(x, y)); c[x][y] = 1; } } a[u.first][u.second] %= 4; } } int main() { cin >> n >> t; ini(); while (t--) { long long x, y; cin >> x >> y; if (abs(x) > 500 || abs(y) > 500) cout << 0 << endl; else cout << a[x + 500][y + 500] << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int SIZE = 256; int M[SIZE * 2][SIZE * 2]; void add(int y, int x) { M[y][x]++; if (M[y][x] == 4) { M[y][x] = 0; add(y + 1, x); add(y - 1, x); add(y, x + 1); add(y, x - 1); } } int main() { int n, t, x, y; scanf("%d%d", &n, &t); for (int i = 0; i < n; i++) { add(SIZE, SIZE); } for (int i = 0; i < t; i++) { scanf("%d%d", &x, &y); if (x + SIZE >= 2 * SIZE || x + SIZE < 0 || y + SIZE >= 2 * SIZE || y + SIZE < 0) { cout << 0 << endl; } else { cout << M[y + SIZE][x + SIZE] << endl; } } return 0; }
8
CPP
from sys import * f = lambda: map(int, stdin.readline().split()) n, t = f() m = 65 r = range(1, m) p = [[0] * m for i in range(m)] p[1][0] = n // 4 p[0][0] = n % 4 s = k = 1 while s: s = 0 for x in r[:k]: if p[x][0] > 3: s = 1 d = p[x][0] // 4 p[x][0] %= 4 p[x + 1][0] += d p[x][1] += d if x != 1: p[x - 1][0] += d else: p[1][1] += d p[1][0] += d for y in r[:x - 1]: if p[x][y] > 3: s = 1 d = p[x][y] // 4 p[x][y] %= 4 p[x + 1][y] += d p[x - 1][y] += d p[x][y + 1] += d p[x][y - 1] += d if x == y + 1: p[x][x] += d p[y][y] += d if y == 1: p[x][0] += d if p[x][x] > 3: s = 1 d = p[x][x] // 4 p[x][x] %= 4 p[x + 1][x] += d p[x][x - 1] += d if x == 1: p[1][0] += d k += 1 s = [] for j in range(t): x, y = f() x, y = abs(x), abs(y) if x < y: x, y = y, x s.append(p[x][y] if x < m else 0) stdout.write('\n'.join(map(str, s)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int c[400][400]; const int F = 200; int que[1000000][2]; int mm[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; int main() { int i, j, f, n, k, p, q, r, x, y, dx, dy, t; scanf("%d", &n); for (k = 0; k < n; k++) { c[F][F]++; if (c[F][F] < 4) continue; p = 0; q = -1; que[0][0] = F; que[0][1] = F; while (q != p) { r = p; while (q != r) { x = que[++q][0]; y = que[q][1]; c[x][y] -= 4; for (i = 0; i < 4; i++) { dx = x + mm[i][0]; dy = y + mm[i][1]; c[dx][dy]++; if (c[dx][dy] == 4) { que[++p][0] = dx; que[p][1] = dy; } } } } } scanf("%d", &t); for (k = 0; k < t; k++) { scanf("%d %d", &x, &y); if (x > 190 || y > 190 || x < -190 || y < -190) { puts("0"); } else { printf("%d\n", c[x + F][y + F]); } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int locations[1005][1005]; int s[1000005]; int di[] = {-1, 1, 0, 0}; int dj[] = {0, 0, -1, 1}; inline void add(int x, int y, int v) { locations[500 + x][500 + y] += v; } inline int get(int x, int y) { return locations[500 + x][500 + y]; } int main(int argc, char *argv[]) { int n, t; scanf("%d %d", &n, &t); int stop = 0; add(0, 0, n); if (n >= 4) { s[stop++] = 0; s[stop++] = 0; } while (stop) { int y = s[--stop], x = s[--stop]; int value = get(x, y) / 4; add(x, y, -value * 4); for (int i = 0; i < 4; i++) { int v = get(x + di[i], y + dj[i]); if (v + value >= 4 && v < 4) { s[stop++] = x + di[i]; s[stop++] = y + dj[i]; } add(x + di[i], y + dj[i], value); } } while (t--) { int x, y; scanf("%d %d", &x, &y); if (fabs(x) > 500 || fabs(y) >= 500) puts("0"); else printf("%d\n", get(x, y)); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, t; int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; int mp[3002][3002]; bool vis[3002][3002]; void bfs(int x, int y) { queue<pair<int, int> > q; q.push(make_pair(x, y)); mp[x][y] = n; while (!q.empty()) { pair<int, int> now = q.front(); q.pop(); x = now.first; y = now.second; vis[x][y] = 0; int val = mp[x][y] / 4; mp[x][y] %= 4; for (register int i = 0; i < 4; i++) { int tx = x + dir[i][0]; int ty = y + dir[i][1]; mp[tx][ty] += val; if (mp[tx][ty] >= 4 && !vis[tx][ty]) { q.push(make_pair(tx, ty)); vis[tx][ty] = 1; } } } } int main() { scanf("%d%d", &n, &t); bfs(1000, 1000); for (register int i = 1; i <= t; i++) { int x, y; scanf("%d%d", &x, &y); x += 1000; y += 1000; if (x < 0 || y < 0 || x > 3001 || y > 3001) { printf("0\n"); } else { printf("%d\n", mp[x][y]); } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; template <class T> inline T min(T &a, T &b) { return a < b ? a : b; } template <class T> inline T max(T &a, T &b) { return a > b ? a : b; } template <class T> void read(T &x) { char ch; while ((ch = getchar()) && !isdigit(ch)) ; x = ch - '0'; while ((ch = getchar()) && isdigit(ch)) x = x * 10 + ch - '0'; } long long Pow(long long a, long long b, long long Mod) { long long ans = 1; a %= Mod; for (; b; b >>= 1) ans = b & 1 ? (ans * a % Mod) : ans, a = a * a % Mod; return ans; } const int U[4] = {0, 0, 1, -1}; const int V[4] = {1, -1, 0, 0}; queue<pair<int, int> > Q; int num[300][300], n, t, x, y; int main() { scanf("%d%d", &n, &t); num[100][100] = n; Q.push(make_pair(100, 100)); while (Q.size()) { int x = Q.front().first, y = Q.front().second; Q.pop(); if (num[x][y] < 4) continue; for (int i = 0; i <= 3; i++) { num[x + U[i]][y + V[i]] += num[x][y] / 4; if (num[x + U[i]][y + V[i]] >= 4) Q.push(make_pair(x + U[i], y + V[i])); } num[x][y] %= 4; } for (int i = 1; i <= t; i++) { scanf("%d%d", &x, &y); x += 100; y += 100; if (x >= 0 && x <= 200 && y >= 0 && y <= 200) printf("%d\n", num[x][y]); else puts("0"); } }
8
CPP
#include <bits/stdc++.h> using namespace std; struct pos { int x, y; }; int dx[] = {-1, 0, 1, 0, 0}; int dy[] = {0, 1, 0, -1, 0}; int n, m, w, size, x, y, itr, mx; int v[1000][1000], u[1000][1000]; pos q[2][1000 * 1000]; int main() { scanf("%d%d", &n, &m); v[500][500] = n; if (n >= 4) { q[0][0] = (pos){500, 500}; size = 1; } while (size) { ++itr; int t = 0; for (int j = (0); j < (size); j++) { x = q[w][j].x; y = q[w][j].y; if (u[x][y] == itr) continue; u[x][y] = itr; v[x][y] -= 4; for (int k = (0); k < (4); k++) v[x + dx[k]][y + dy[k]]++; } ++itr; for (int j = (0); j < (size); j++) { x = q[w][j].x; y = q[w][j].y; for (int k = (0); k < (5); k++) { if (v[x + dx[k]][y + dy[k]] >= 4 && u[x + dx[k]][y + dy[k]] != itr) { q[w ^ 1][t++] = (pos){x + dx[k], y + dy[k]}; u[x + dx[k]][y + dy[k]] = itr; mx = max(mx, x + dx[k]); } } } size = t; w ^= 1; } while (m--) { scanf("%d%d", &x, &y); x += 500, y += 500; if (x < 0 || y < 0 || x >= 1000 || y >= 1000) puts("0"); else printf("%d\n", v[x][y]); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; struct point { int x; int y; }; bool operator<(const point &a, const point &b) { return (a.x < b.x); } int compare(const void *a, const void *b) { return (*(int *)a - *(int *)b); } int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } int a[200][200]; int main() { int n, t, i, j, x, y; cin >> n >> t; a[100][100] = n; while (1) { int flag = 0; for (i = 0; i < 200; i++) for (j = 0; j < 200; j++) if (a[i][j] >= 4) { if (i > 0) a[i - 1][j] += (a[i][j] / 4); if (i < 199) a[i + 1][j] += (a[i][j] / 4); if (j > 0) a[i][j - 1] += (a[i][j] / 4); if (j < 199) a[i][j + 1] += (a[i][j] / 4); flag = 1; a[i][j] %= 4; } if (!flag) break; } for (i = 0; i < t; i++) { cin >> x >> y; if (x < 100 && x >= -100 && y < 100 && y >= -100) cout << a[x + 100][y + 100] << endl; else cout << 0 << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; long long int modpow(long long int a, long long int n, long long int temp) { long long int res = 1; while (n > 0) { res = (res * res) % temp; if (n & 1) res = (res * a) % temp; n /= 2; } return res; } long long int gcd(long long int a, long long int b) { if (a == 0) return (b); else return (gcd(b % a, a)); } void f(int a[][1001], int x, int y) { if (a[x][y] >= 4) { int t = a[x][y] / 4; a[x][y + 1] += t; a[x + 1][y] += t; a[x - 1][y] += t; a[x][y - 1] += t; a[x][y] = a[x][y] % 4; f(a, x + 1, y); f(a, x - 1, y); f(a, x, y + 1); f(a, x, y - 1); } } int main() { int a[1001][1001] = {0}; int n, q, x, y; cin >> n >> q; a[500][500] = n; f(a, 500, 500); while (q--) { cin >> x >> y; x += 500; y += 500; if (x > 1000 || y > 1000 || x < 0 || y < 0) cout << 0 << endl; else cout << a[x][y] << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int a, b, x, y; int v[1005][1005]; void tp(int m, int n, int k) { v[m][n] += k; if (v[m][n] < 4) return; v[m][n] -= 4; tp(m, n, 0); tp(m + 1, n, 1); tp(m - 1, n, 1); tp(m, n + 1, 1); tp(m, n - 1, 1); return; } int main() { scanf("%d %d", &a, &b); tp(500, 500, a); for (int i = 0; i < b; i++) { scanf("%d %d", &x, &y); if (x > 500 || y > 500 || x < -500 || y < -500) { printf("0\n"); continue; } printf("%d\n", v[x + 500][y + 500]); } }
8
CPP
#include <bits/stdc++.h> using namespace std; template <typename T1> inline void deb(const T1 &); template <typename T1, typename T2> inline void deb(const T1 &, const T2 &); template <typename T1, typename T2, typename T3> inline void deb(const T1 &, const T2 &, const T3 &); template <typename T1, typename T2, typename T3, typename T4> inline void deb(const T1 &, const T2 &, const T3 &, const T4 &); template <typename T1, typename T2, typename T3, typename T4, typename T5> inline void deb(const T1 &, const T2 &, const T3 &, const T4 &, const T5 &); const int NMax = 1011; const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; inline int p(int a) { return a + 505; } int mat[NMax][NMax]; int N, K; queue<pair<int, int> > Q[2]; int main() { cin >> N >> K; mat[p(0)][p(0)] = N; Q[0].push(make_pair(p(0), p(0))); for (int i = 0; Q[i & 1].size(); i++) { for (; !Q[i & 1].empty(); Q[i & 1].pop()) { pair<int, int> ret = Q[i & 1].front(); int rem = mat[ret.first][ret.second] / 4; if (rem) { mat[ret.first][ret.second] -= 4 * rem; for (int k = 0; k < 4; k++) { pair<int, int> acm = ret; acm.first += dx[k]; acm.second += dy[k]; mat[acm.first][acm.second] += rem; if (mat[acm.first][acm.second] >= 4) { Q[!(i & 1)].push(acm); } } } } } for (int i = 1; i <= K; i++) { int X, Y; cin >> X >> Y; if (0 <= p(X) && p(X) <= NMax && 0 <= p(Y) && p(Y) <= NMax) cout << mat[p(X)][p(Y)]; else cout << 0; cout << '\n'; } } template <typename T1> inline void deb(const T1 &V1) {} template <typename T1, typename T2> inline void deb(const T1 &V1, const T2 &V2) {} template <typename T1, typename T2, typename T3> inline void deb(const T1 &V1, const T2 &V2, const T3 &V3) {} template <typename T1, typename T2, typename T3, typename T4> inline void deb(const T1 &V1, const T2 &V2, const T3 &V3, const T4 &V4) {} template <typename T1, typename T2, typename T3, typename T4, typename T5> inline void deb(const T1 &V1, const T2 &V2, const T3 &V3, const T4 &V4, const T5 &V5) {}
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int n, q; cin >> n >> q; int tab[181][181], tab2[181][181]; for (int i = 0; i < 181; i++) { for (int j = 0; j < 181; j++) tab[i][j] = tab2[i][j] = 0; } tab[90][90] = n; bool cont = 1; while (cont) { cont = 0; for (int i = 0; i < 181; i++) { for (int j = 0; j < 181; j++) tab2[i][j] = tab[i][j]; } for (int i = 0; i < 181; i++) { for (int j = 0; j < 181; j++) { if (tab[i][j] < 4) continue; cont = 1; tab2[i + 1][j] += tab[i][j] / 4; tab2[i - 1][j] += tab[i][j] / 4; tab2[i][j + 1] += tab[i][j] / 4; tab2[i][j - 1] += tab[i][j] / 4; tab2[i][j] = tab[i][j] % 4; } } for (int i = 0; i < 181; i++) { for (int j = 0; j < 181; j++) tab[i][j] = tab2[i][j]; } } int a, b; for (int i = 0; i < q; i++) { cin >> a >> b; if (a < -90 || b < -90 || a > 90 || b > 90) { cout << 0 << "\n"; continue; } cout << tab[a + 90][b + 90] << "\n"; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int Get() { char c; while (c = getchar(), (c < '0' || c > '9') && (c != '-')) ; bool Flag = (c == '-'); if (Flag) c = getchar(); int X = 0; while (c >= '0' && c <= '9') { X = X * 10 + c - 48; c = getchar(); } return Flag ? -X : X; } void Output(int X) { if (X < 0) { putchar('-'); X = -X; } int Len = 0, Data[10]; while (X) { Data[Len++] = X % 10; X /= 10; } if (!Len) Data[Len++] = 0; while (Len--) putchar(Data[Len] + 48); putchar('\n'); } const int S = 80; int main() { static int DP[2][S * 2 + 1][S * 2 + 1]; memset(DP, 0, sizeof(DP)); DP[0][S][S] = Get(); static int Ans[S * 2 + 1][S * 2 + 1]; for (int i = 1; i <= 10000; i++) { memset(DP[i & 1], 0, sizeof(DP[i & 1])); for (int j = 0; j <= S * 2; j++) for (int k = 0; k <= S * 2; k++) { int T = DP[~i & 1][j][k] / 4; if (T) { DP[i & 1][j + 1][k] += T; DP[i & 1][j - 1][k] += T; DP[i & 1][j][k + 1] += T; DP[i & 1][j][k - 1] += T; } DP[i & 1][j][k] += DP[~i & 1][j][k] % 4; } bool Find = false; for (int j = 0; !Find && j <= S * 2; j++) for (int k = 0; !Find && k <= S * 2; k++) if (DP[0][j][k] != DP[1][j][k]) Find = true; if (!Find) { for (int j = 0; j <= S * 2; j++) for (int k = 0; k <= S * 2; k++) Ans[j][k] = DP[i & 1][j][k]; break; } } int M = Get(); while (M--) { int X = Get(), Y = Get(); if (X < -S || X > S || Y < -S || Y > S) Output(0); else Output(Ans[X + S][Y + S]); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> inline T Abs(T a) { return a > 0 ? a : -a; } template <typename T> inline T sqr(T a) { return a * a; } template <typename T> inline void relaxMin(T &a, T b) { if (b < a) a = b; } template <typename T> inline void relaxMax(T &a, T b) { if (b > a) a = b; } const int INF = (int)1E9; const long double EPS = 1E-6; const long double PI = 3.1415926535897932384626433832795; int f[65 * 2 + 3][65 * 2 + 3]; int f2[65 * 2 + 3][65 * 2 + 3]; int &get(int x, int y) { return f[x + 65][y + 65]; } int &get2(int x, int y) { return f2[x + 65][y + 65]; } void print() { const int k = 10; for (int i = -k; i <= k; i++) { for (int j = -k; j <= k; j++) printf("%3d ", get(i, j)); cout << endl; } cout << endl << endl; } int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; bool valid(int a, int b) { return a >= -65 && a <= 65 && b >= -65 && b <= 65; } void solve() { int n, q; scanf("%d%d", &n, &q); get(0, 0) = n; for (;;) { memset(f2, 0, sizeof f2); ; bool ok = false; for (int x = -65; x <= 65; x++) for (int y = -65; y <= 65; y++) { if (get(x, y) / 4) { ok = true; } if (get(x, y)) for (int d(0), _bb(4); d < _bb; ++d) get2(x + dx[d], y + dy[d]) += get(x, y) / 4; get2(x, y) += get(x, y) % 4; } memcpy(f, f2, sizeof(f)); if (!ok) { break; } } for (int i(0), _bb(q); i < _bb; ++i) { int a, b; scanf("%d%d", &a, &b); if (valid(a, b)) cout << get(a, b) << endl; else cout << 0 << endl; } } int main() { srand(0xA1B2C3D4); solve(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 30005, M = 500; struct coord { int x, y; }; int dx[10] = {0, 1, -1, 0}; int dy[10] = {1, 0, 0, -1}; coord q[5][2 * N]; int top[5]; int T, te, mn, x, nm, cr, k, n, d[2 * M + 5][2 * M + 5], mark[2 * M + 5][2 * M + 5]; coord c, e; int main() { scanf("%d", &n); d[M][M] = n; if (n >= 4) { c.x = M; c.y = M; top[mn]++; q[mn][top[mn]] = c; } while (top[mn] != 0) { nm = 1 - mn; cr++; top[nm] = 0; x = top[mn]; while (x != 0) { c = q[mn][x]; x--; for (k = 0; k < 4; ++k) { e.x = c.x + dx[k]; e.y = c.y + dy[k]; d[e.x][e.y]++; } e.x = c.x; e.y = c.y; d[e.x][e.y] -= 4; } while (top[mn] != 0) { c = q[mn][top[mn]]; top[mn]--; for (k = 0; k < 4; ++k) { e.x = c.x + dx[k]; e.y = c.y + dy[k]; if (d[e.x][e.y] >= 4 && mark[e.x][e.y] != cr) { top[nm]++; q[nm][top[nm]] = e; mark[e.x][e.y] = cr; } } e.x = c.x; e.y = c.y; if (d[e.x][e.y] >= 4 && mark[e.x][e.y] != cr) { top[nm]++; q[nm][top[nm]] = e; mark[e.x][e.y] = cr; } } mn = nm; } scanf("%d", &T); for (te = 1; te <= T; ++te) { scanf("%d%d", &c.x, &c.y); if (c.x >= -M && c.x <= M && c.y >= -M && c.y <= M) { printf("%d\n", d[c.x + M][c.y + M]); } else { printf("0\n"); } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; struct node { int x, y; } e[110000]; int a[1100][1100], b, c, d, f, g, h, i, j, k, m, n; int use[1100][1100]; void add(int x, int y, int v) { if (x > d) d = x; if (!use[x][y]) { m++; e[m].x = x; e[m].y = y; use[x][y] = 1; } a[x][y] += v; } void deal() { m = 0; scanf("%d%d", &n, &h); a[500][500] = n; m++; e[1].x = 500; e[1].y = 500; use[500][500] = 1; f = 1; while (f) { f = 0; for (i = 1; i <= m; i++) if (a[e[i].x][e[i].y] >= 4) { b = a[e[i].x][e[i].y] / 4; add(e[i].x + 1, e[i].y, b); add(e[i].x - 1, e[i].y, b); add(e[i].x, e[i].y + 1, b); add(e[i].x, e[i].y - 1, b); a[e[i].x][e[i].y] %= 4; f = 1; } } for (i = 1; i <= h; i++) { scanf("%d%d", &c, &d); if (c + 500 < 0 || c + 500 > 1000 || d + 500 < 0 || d + 500 > 1000) printf("%d\n", 0); else printf("%d\n", a[500 + c][500 + d]); } } int main() { deal(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int d = 200; const int X[4] = {-1, 1, 0, 0}; const int Y[4] = {0, 0, 1, -1}; int n, t, x, y, tx, ty, dk; int a[2 * d + 10][2 * d + 10]; bool use[2 * d + 10][2 * d + 10]; queue<pair<int, int> > q; int main() { cin >> n >> t; a[d][d] = n; q.push(make_pair(d, d)); use[d][d] = 1; while (!q.empty()) { tx = q.front().first, ty = q.front().second; q.pop(); use[tx][ty] = 0; if (a[tx][ty] < 4) continue; dk = a[tx][ty] / 4; a[tx][ty] -= 4 * dk; for (int i = 0; i < 4; i++) { a[tx + X[i]][ty + Y[i]] += dk; if (a[tx + X[i]][ty + Y[i]] >= 4 && use[tx + X[i]][ty + Y[i]] == 0) { q.push(make_pair(tx + X[i], ty + Y[i])); use[tx + X[i]][ty + Y[i]] == 1; } } } for (int i = 0; i < t; i++) { scanf("%d%d", &x, &y); x += d, y += d; if (x <= 0 || y <= 0 || x >= 2 * d || y >= 2 * d) printf("%d\n", 0); else printf("%d\n", a[x][y]); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; void solve() { int n, t; scanf("%d%d", &n, &t); vector<vector<int>> m(400, vector<int>(400)); queue<pair<int, int>> q; q.push(pair<int, int>(0, 0)); m[200][200] = n; while (q.size()) { pair<int, int> pos = q.front(); q.pop(); int x = pos.first; int y = pos.second; int v = m[200 + x][200 + y]; if (v < 4) continue; m[200 + x][200 + y] = v % 4; v /= 4; int z = (m[200 + x + 1][200 + y] += v); if (z >= 4 && z - v < 4) q.push(pair<int, int>(x + 1, y)); z = (m[200 + x][200 + y + 1] += v); if (z >= 4 && z - v < 4) q.push(pair<int, int>(x, y + 1)); z = (m[200 + x - 1][200 + y] += v); if (z >= 4 && z - v < 4) q.push(pair<int, int>(x - 1, y)); z = m[200 + x][200 + y - 1] += v; if (z >= 4 && z - v < 4) q.push(pair<int, int>(x, y - 1)); } for (int i = 0; i < t; ++i) { int x, y; scanf("%d%d", &x, &y); if (x + 200 >= 0 && x + 200 < 400 && y + 200 >= 0 && y + 200 <= 400) { printf("%d\n", m[x + 200][y + 200]); } else { printf("0\n"); } } } int main() { solve(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxk = 140; typedef int mang[2][maxk][maxk]; int n, q, v = 0; mang a; void loang() { int dung = 1; do { v = 1 - v; dung = 0; for (int i = 1; i <= maxk - 2; i++) for (int j = 1; j <= maxk - 2; j++) { a[v][i][j] = a[1 - v][i + 1][j] / 4 + a[1 - v][i - 1][j] / 4 + a[1 - v][i][j + 1] / 4 + a[1 - v][i][j - 1] / 4 + a[1 - v][i][j] % 4; int t = a[v][i][j] / 4; dung = t || dung; } } while (dung); } int main() { scanf("%d", &n); a[0][maxk / 2][maxk / 2] = n; loang(); scanf("%d", &q); int x, y; for (int i = 0; i < q; i++) { scanf("%d%d", &x, &y); if (x > -maxk / 2 && x < maxk / 2 && y > -maxk / 2 && y < maxk / 2) printf("%d\n", a[v][x + maxk / 2][y + maxk / 2]); else printf("0\n"); } }
8
CPP
#include <bits/stdc++.h> int grid[500][500]; void remplissage(int m, int n, int k) { grid[m][n] += k; if (grid[m][n] < 4) return; grid[m][n] -= 4; remplissage(m, n, 0); remplissage(m + 1, n, 1); remplissage(m - 1, n, 1); remplissage(m, n + 1, 1); remplissage(m, n - 1, 1); return; } int main(int argc, char *argv[]) { int n(0); int t(0); scanf("%d%d", &n, &t); remplissage(250, 250, n); int x(0); int y(0); for (int i = 0; i < t; i++) { scanf("%d%d", &x, &y); if (x > 250 || y > 250 || x < -250 || y < -250) printf("%d\n", 0); else printf("%d\n", grid[250 + x][250 + y]); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; inline int in() { int x; scanf("%d", &x); return x; } const long double EPS = 1e-12; const int INF = 1e9 + 10; const long long LINF = 1000ll * 1000 * 1000 * 1000 * 1000 * 1000 + 100; const int MN = 4e2 + 10; int arr[MN][MN]; int x[4] = {1, 0, -1, 0}; int y[4] = {0, 1, 0, -1}; void dfs(int r, int c) { if (arr[r][c] < 4) return; int d = arr[r][c] / 4; arr[r][c] %= 4; for (int i = 0; i < 4; ++i) { int t1 = r + x[i], t2 = c + y[i]; arr[t1][t2] += d; dfs(t1, t2); } } int main() { ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, q; cin >> n >> q; arr[200][200] = n; dfs(200, 200); int r, c; while (q--) { cin >> r >> c; if (max(r, c) > 200 || min(r, c) < -200) cout << 0 << '\n'; else cout << arr[r + 200][c + 200] << '\n'; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } void PLAY() { cout << fixed << setprecision(10); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int res[1005][1005]; int main() { PLAY(); int n, t; cin >> n >> t; queue<pair<int, int>> qu; qu.push({0, 0}); res[300][300] = n; while ((int)qu.size()) { int curx = qu.front().first; int cury = qu.front().second; qu.pop(); if (res[curx + 300][cury + 300] < 4) continue; for (int k = 0; k < 4; k++) { int tox = dx[k] + curx; int toy = dy[k] + cury; res[tox + 300][toy + 300] += res[curx + 300][cury + 300] / 4; if (res[tox + 300][toy + 300] >= 4) qu.push({tox, toy}); } res[curx + 300][cury + 300] %= 4; } while (t--) { int x, y; cin >> x >> y; if (abs(x) >= 300 || abs(y) >= 300) cout << 0 << endl; else cout << res[x + 300][y + 300] << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int a, b, x, y; int v[1005][1005]; void tp(int m, int n, int k) { v[m][n] += k; if (v[m][n] < 4) return; v[m][n] -= 4; tp(m, n, 0); tp(m + 1, n, 1); tp(m - 1, n, 1); tp(m, n + 1, 1); tp(m, n - 1, 1); return; } int main() { scanf("%d %d", &a, &b); tp(500, 500, a); for (int j = 0; j < b; j++) { scanf("%d %d", &x, &y); if (x > 500 || y > 500 || x < -500 || y < -500) { printf("0\n"); continue; } printf("%d\n", v[x + 500][y + 500]); } }
8
CPP
#include <bits/stdc++.h> int v[1000][1000]; int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, -1, 1}; void put(int x, int y, int n) { v[x][y] += n; if (v[x][y] >= 4) { int t = v[x][y] / 4; v[x][y] -= t * 4; for (int i = 0; i < 4; i++) put(x + dx[i], y + dy[i], t); } } int main() { memset(v, 0, 0); int n, k; scanf("%d%d", &n, &k); put(500, 500, n); for (int i = 0; i < k; i++) { int x, y; scanf("%d%d", &x, &y); x += 500; y += 500; if (x < 0 || y < 0 || x > 999 || y > 999) printf("0\n"); else printf("%d\n", v[x][y]); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int v[1005][1005] = {0}; void dfs(int a, int b, int k) { v[a][b] += k; if (v[a][b] < 4) return; v[a][b] -= 4; dfs(a, b, 0); dfs(a + 1, b, 1); dfs(a, b + 1, 1); dfs(a - 1, b, 1); dfs(a, b - 1, 1); } int main() { int n, m; cin >> n >> m; dfs(500, 500, n); while (m--) { int a, b; cin >> a >> b; if (a > 500 || a < -500 || b > 500 || b < -500) printf("%d\n", 0); else printf("%d\n", v[a + 500][b + 500]); } return 0; }
8
CPP
#include <bits/stdc++.h> #pragma GCC optimize(3) using namespace std; const int maxn = (int)1e5 + 5; const int mod = (int)1e9 + 7; int n, m, k, l, s, t, r; int q[2000][2000]; int ok[2000][2000]; vector<pair<int, int> > now, tmp; int main() { cin >> n >> t; q[1000][1000] = n; if (n >= 4) now.push_back({1000, 1000}); int u = 0; while (now.size() != 0) { u++; tmp.clear(); for (auto i : now) { if (q[i.first][i.second] < 4) continue; q[i.first][i.second] -= 4; q[i.first - 1][i.second]++; q[i.first + 1][i.second]++; q[i.first][i.second + 1]++; q[i.first][i.second - 1]++; if (q[i.first][i.second] >= 4 && ok[i.first][i.second] != u) tmp.push_back(i), ok[i.first][i.second] = u; if (q[i.first - 1][i.second] >= 4 && ok[i.first - 1][i.second] != u) tmp.push_back({i.first - 1, i.second}), ok[i.first - 1][i.second] = u; if (q[i.first][i.second + 1] >= 4 && ok[i.first][i.second + 1] != u) tmp.push_back({i.first, i.second + 1}), ok[i.first][i.second + 1] = u; if (q[i.first + 1][i.second] >= 4 && ok[i.first + 1][i.second] != u) tmp.push_back({i.first + 1, i.second}), ok[i.first + 1][i.second] = u; if (q[i.first][i.second - 1] >= 4 && ok[i.first][i.second - 1] != u) tmp.push_back({i.first, i.second - 1}), ok[i.first][i.second - 1] = u; } now = tmp; } while (t--) { cin >> l >> r; l += 1000; r += 1000; if (l < 0 || r < 0 || l >= 2000 || r >= 2000) cout << 0 << endl; else cout << q[l][r] << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int M = 100; const int mod = 1e9 + 7; const double eps = 1e-6; const double Pi = 3 * acos(0.0); inline int max(int a, int b) { return a > b ? a : b; } inline int min(int a, int b) { return a < b ? a : b; } inline int bit(int x) { return 1 << x; } inline double dabs(double x) { return x > 0 ? x : (-x); } inline int iabs(int x) { return x > 0 ? x : (-x); } inline int lowbit(int x) { return x & (-x); } template <class T> inline void checkmin(T &a, T b) { if (a == -1 || b < a) a = b; } int ma[M * 2][M * 2]; void dfs(int x, int y) { if (ma[x][y] >= 4) { int t = ma[x][y] / 4; ma[x][y] %= 4; ma[x + 1][y] += t; dfs(x + 1, y); ma[x - 1][y] += t; dfs(x - 1, y); ma[x][y + 1] += t; dfs(x, y + 1); ma[x][y - 1] += t; dfs(x, y - 1); } } int main() { int n, t, x, y; cin >> n >> t; ma[M][M] = n; dfs(M, M); while (t--) { cin >> x >> y; if (x >= M || x <= -M || y >= M || y <= -M) cout << 0 << endl; else cout << ma[x + M][y + M] << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, t; int a[200][200], off = 100; int main() { cin >> n >> t; a[off][off] = n; while (true) { bool seen = false; for (int i = 1; i < 200 - 1; i++) { for (int j = 1; j < 200 - 1; j++) { if (a[i][j] >= 4) { seen = true; int flow = a[i][j] / 4; a[i][j] %= 4; a[i - 1][j] += flow; a[i + 1][j] += flow; a[i][j - 1] += flow; a[i][j + 1] += flow; } } } if (!seen) break; } for (int i = 0; i < (t); i++) { int first, second; cin >> first >> second; if (0 <= first + off && first + off < 200 && 0 < second + off && second + off < 200) { cout << a[first + off][second + off] << endl; } else { cout << 0 << endl; } } }
8
CPP
#include <bits/stdc++.h> using namespace std; const double PI = 3.14159265358979323; const double eps = 1e-12; const int inf = ((int)1e9) + 7; const int MAX_N = 2000000; int arr[660][660]; void rec(int i, int j) { if (arr[i][j] == 3) { arr[i][j] = 0; rec(i - 1, j); rec(i + 1, j); rec(i, j + 1); rec(i, j - 1); } else arr[i][j]++; } int main() { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) rec(330, 330); for (int i = 0; i < k; i++) { int x, y; cin >> x >> y; x += 330, y += 330; if (x < 0 || y < 0 || x > 650 || y > 650) { cout << 0 << endl; continue; } cout << arr[x][y] << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:268435456") int main() { int n; cin >> n; int mp[201][201]; for (int i = 0; i < 200; i++) for (int j = 0; j < 200; j++) mp[i][j] = 0; mp[0][0] = n; bool ch = 0; do { ch = 0; for (int i = 0; i < 150; i++) for (int j = 0; j < 150; j++) { if (mp[i][j] >= 4) { mp[i + 1][j] += mp[i][j] / 4; mp[i][j + 1] += mp[i][j] / 4; if (i != 0) mp[i - 1][j] += mp[i][j] / 4; if (j != 0) mp[i][j - 1] += mp[i][j] / 4; if (i == 1) mp[i - 1][j] += mp[i][j] / 4; if (j == 1) mp[i][j - 1] += mp[i][j] / 4; ch = 1; mp[i][j] %= 4; } } } while (ch); int t; cin >> t; for (int i = 0; i < t; i++) { int a, b; cin >> a >> b; if ((abs(a) > 150) || (abs(b) > 150)) cout << 0 << endl; else cout << mp[abs(a)][abs(b)] << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 131; const int ox = 65; const int oy = 65; int a[MAXN][MAXN], b[MAXN][MAXN]; int dx[] = {0, 0, -1, 1}; int dy[] = {1, -1, 0, 0}; void init() { memset(b, 0, sizeof(b)); int p = MAXN - 1; bool ok = false; for (int i = 1; i <= p; i++) for (int j = 1; j <= p; j++) if (a[i][j] > 3) { for (int k = 0; k < 4; k++) b[i + dx[k]][j + dy[k]] += a[i][j] / 4; b[i][j] -= a[i][j] / 4 * 4; } for (int i = 1; i <= p; i++) for (int j = 1; j <= p; j++) { a[i][j] += b[i][j]; if (a[i][j] > 3) ok = true; } if (ok) init(); } int main() { int n, m, x, y; while (cin >> n >> m) { a[ox][oy] = n; init(); while (m--) { scanf("%d%d", &x, &y); x += ox; y += oy; if (x >= MAXN || y >= MAXN || x < 0 || y < 0) puts("0"); else printf("%d\n", a[x][y]); } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, t; int T[5000][5000]; int used[5000][5000]; queue<pair<int, int> > Q; void solve() { T[2500][2500] = n; if (n >= 4) Q.push(pair<int, int>(2500, 2500)); while (!Q.empty()) { pair<int, int> p = Q.front(); Q.pop(); int x = p.first, y = p.second; int i = T[x][y] / 4; T[x][y - 1] += i; T[x][y + 1] += i; T[x - 1][y] += i; T[x + 1][y] += i; T[x][y] %= 4; if (T[x][y - 1] >= 4 && used[x][y - 1] == 0) { used[x][y - 1] = 1; Q.push(pair<int, int>(x, y - 1)); } if (T[x][y + 1] >= 4 && used[x][y + 1] == 0) { used[x][y + 1] = 1; Q.push(pair<int, int>(x, y + 1)); } if (T[x - 1][y] >= 4 && used[x - 1][y] == 0) { used[x - 1][y] = 1; Q.push(pair<int, int>(x - 1, y)); } if (T[x + 1][y] >= 4 && used[x + 1][y] == 0) { used[x + 1][y] = 1; Q.push(pair<int, int>(x + 1, y)); } used[x][y] = 0; } } void write_output(int x, int y) { if (x > 1000 || y > 1000 || x < -1000 || y < -1000) printf("0\n"); else printf("%d\n", T[x + 2500][y + 2500]); } void read_input() { scanf("%d%d", &n, &t); solve(); int x, y; for (int i = 0; i < t; i++) { scanf("%d%d", &x, &y); write_output(x, y); } } int main() { read_input(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int cnt[1111][1111]; bool inq[1111][1111]; int n, t; queue<pair<int, int> > q; const int kFx[4] = {-1, 1, 0, 0}; const int kFy[4] = {0, 0, -1, 1}; int main() { scanf("%d%d", &n, &t); cnt[555][555] = n; if (n >= 4) { q.push(make_pair(555, 555)); inq[555][555] = true; } while (q.size()) { pair<int, int> u = q.front(); q.pop(); int _x = u.first; int _y = u.second; inq[_x][_y] = false; int z = cnt[_x][_y]; cnt[_x][_y] = z & 3; for (int d = 0; d < 4; ++d) { int x = _x + kFx[d]; int y = _y + kFy[d]; pair<int, int> v = make_pair(x, y); cnt[x][y] += z >> 2; if (!inq[x][y] && cnt[x][y] >= 4) q.push(v), inq[x][y] = true; } } for (int x, y; t--;) { scanf("%d%d", &x, &y); x += 555; y += 555; if (x < 0 || x >= 1111 || y < 0 || y >= 1111) puts("0"); else printf("%d\n", cnt[x][y]); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; template <class T> void chmax(T& a, const T& b) { a = max(a, b); } template <class T> void chmin(T& a, const T& b) { a = min(a, b); } template <class T> void uniq(T& c) { sort(c.begin(), c.end()); c.erase(unique(c.begin(), c.end()), c.end()); } template <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); } template <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; } template <typename T> void print_container(ostream& os, const T& c) { const char* _s = " "; if (!c.empty()) { __typeof__(c.begin()) last = --c.end(); for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it) { os << *it; if (it != last) cout << _s; } } } template <typename T> ostream& operator<<(ostream& os, const vector<T>& c) { print_container(os, c); return os; } template <typename T> ostream& operator<<(ostream& os, const set<T>& c) { print_container(os, c); return os; } template <typename T> ostream& operator<<(ostream& os, const multiset<T>& c) { print_container(os, c); return os; } template <typename T> ostream& operator<<(ostream& os, const deque<T>& c) { print_container(os, c); return os; } template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& c) { print_container(os, c); return os; } template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << "( " << p.first << ", " << p.second << " )"; return os; } template <class T> void print(T a, int n, const string& deli = " ", int br = 1) { for (int i = 0; i < n; ++i) { cout << a[i]; if (i + 1 != n) cout << deli; } while (br--) cout << endl; } template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; } template <class T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; } template <class T> void input(T* a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; } void fix_pre(int n) { cout.setf(ios::fixed, ios::floatfield); cout.precision(10); } void fast_io() { cin.tie(0); ios::sync_with_stdio(false); } bool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; } bool in_seg(int n, int l, int r) { return l <= n && n <= r; } const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; const double PI = acos(-1.0); int o = 100; int a[200][200]; int& get(int x, int y) { return a[y + o][x + o]; } void simulate(int n) { memset(a, 0, sizeof(a)); get(0, 0) = n; for (;;) { vector<pair<pair<int, int>, int> > add; for (int y = -70; y <= 70; ++y) { for (int x = -70; x <= 70; ++x) { int p = get(x, y) / 4; get(x, y) %= 4; if (p > 0) add.push_back(make_pair(pair<int, int>(x, y), p)); } } if (add.empty()) break; for (__typeof__((add).begin()) it = (add).begin(); it != (add).end(); ++it) { for (int i = 0; i < (int)(4); ++i) get(it->first.first + dx[i], it->first.second + dy[i]) += it->second; } } } int main() { fast_io(); int n, q; cin >> n >> q; simulate(n); while (q--) { int x, y; cin >> x >> y; x = abs(x), y = abs(y); int res; if (max(x, y) < 70) res = get(x, y); else res = 0; cout << res << endl; } }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MAX = 100; struct Mat { int ma[MAX][MAX]; void clear() { memset(ma, 0, sizeof(ma)); } } res[MAX]; int pow4[] = {1, 4, 16, 64, 256, 1024, 4096, 16384, 65536}; int mxsize; int FourBit(int val) { for (int i = 0;; i++) if (pow4[i] > val) return i - 1; } void Expand(int x, int y, int val, Mat &a) { int tp = val / 4; if (x == 1) a.ma[x - 1][y] += tp * 2; else if (x > 1) a.ma[x - 1][y] += tp; if (y == 1) a.ma[x][y - 1] += tp * 2; else if (y > 1) a.ma[x][y - 1] += tp; if (x + 1 == mxsize) mxsize++; a.ma[x + 1][y] += tp; if (y + 1 == mxsize) mxsize++; a.ma[x][y + 1] += tp; a.ma[x][y] += val % 4; } Mat Update(Mat m) { Mat ans[2]; int a = 0, b = 1, flag = 1, tp; ans[0] = m; while (flag) { ans[b].clear(); flag = 0; for (int i = 0; i < mxsize; i++) { for (int j = 0; j < mxsize; j++) { if ((tp = ans[a].ma[i][j]) >= 4) { flag = 1; Expand(i, j, tp, ans[b]); } else { ans[b].ma[i][j] += tp; } } } a ^= 1; b ^= 1; } return ans[a]; } Mat AddMat(Mat a, Mat b) { Mat c; c.clear(); for (int i = 0; i < mxsize; i++) for (int j = 0; j < mxsize; j++) c.ma[i][j] = a.ma[i][j] + b.ma[i][j]; return c; } void GetPow() { res[0].clear(); res[0].ma[0][0] = 1; mxsize = 1; for (int i = 1; i <= 15; i++) { res[i] = Update(AddMat(res[i - 1], res[i - 1])); } } int main() { int n, m; GetPow(); while (scanf("%d%d", &n, &m) != EOF) { Mat ans; ans.clear(); for (int i = 0, st = n; st; st >>= 1, i++) if (st & 1) ans = AddMat(ans, res[i]); ans = Update(ans); for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); if (x < 0) x = -x; if (y < 0) y = -y; if (x >= MAX || y >= MAX) printf("0\n"); else printf("%d\n", ans.ma[x][y]); } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int x, y, T; int F[140][140]; void f(int x, int y) { if (F[x][y] < 4) return; int g = F[x][y] / 4; F[x][y] %= 4; F[x + 1][y] += g; f(x + 1, y); F[x - 1][y] += g; f(x - 1, y); F[x][y + 1] += g; f(x, y + 1); F[x][y - 1] += g; f(x, y - 1); } int main() { cin >> F[70][70] >> T; f(70, 70); while (T--) { cin >> x >> y; if (x < -70 || x >= 70 || y < -70 || y >= 70) cout << "0\n"; else cout << F[x + 70][y + 70] << "\n"; } }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { static int B[1000][1000] = {0}, b[1000][1000] = {0}; const int S = 500; int n; scanf("%d", &n); queue<pair<int, int> > Q; B[S][S] += n; Q.push(make_pair(S, S)); b[S][S] = 1; while (!Q.empty()) { pair<int, int> p = Q.front(); Q.pop(); int x = p.first, y = p.second; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; for (int v = 0; v < 4; v++) { int X = x + dx[v], Y = y + dy[v]; B[X][Y] += B[x][y] / 4; if (B[X][Y] >= 4 && !b[X][Y]) { Q.push(make_pair(X, Y)); b[X][Y] = 1; } } B[x][y] %= 4; b[x][y] = 0; } int t; scanf("%d", &t); for (int i = 0; i < t; i++) { int x, y; scanf("%d%d", &x, &y); printf("%d\n", x <= -S || S <= x || y <= -S || S <= y ? 0 : B[x + S][y + S]); } return 0; }
8
CPP
/* * Package: StandardCodeLibrary.Core * */ #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <utility> #include <vector> #include <list> #include <string> #include <stack> #include <queue> #include <deque> #include <set> #include <map> #include <algorithm> #include <functional> #include <numeric> #include <bitset> #include <complex> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <ctime> #include <climits> #if __GNUC__>=4 and __GNUC_MINOR__>=6 #include <ext/rope> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/tag_and_trait.hpp> #endif #ifdef __GXX_EXPERIMENTAL_CXX0X__ #define typeof decltype #endif using namespace std; #define lp for(;;) #define repf(i,a,b) for (int i=(a);i<(b);++i) #define rep(i,n) repf(i,0,n) #define ft(i,a,b) for (int i=(a);i<=(b);++i) #define fdt(i,a,b) for (int i=(a);i>=(b);--i) #define feach(e,s) for (typeof((s).begin()) e=(s).begin();e!=(s).end();++e) #define fsubset(subset,set) for (int subset=(set)&((set)-1);subset;subset=(subset-1)&(set)) #define forin(i,charset) for (cstr i=(charset);*i;i++) #define whl while #define rtn return #define fl(x,y) memset((x),char(y),sizeof(x)) #define clr(x) fl(x,char(0)) #define cpy(x,y) memcpy(x,y,sizeof(x)) #define pb push_back #define mp make_pair #define ins insert #define ers erase #define lb lower_bound #define ub upper_bound #define rnk order_of_key #define sel find_by_order #define x first #define y second #define sz(x) (int((x).size())) #define all(x) (x).begin(),(x).end() #define srt(x) sort(all(x)) #define uniq(x) srt(x),(x).erase(unique(all(x)),(x).end()) #define rev(x) reverse(all(x)) #define vec vector #define pr pair #define que queue #define prq priority_queue #define itr iterator #define sf scanf #define pf printf #define pdb(prcs,x) (cout<<setprecision(prcs)<<fixed<<(sgn(x)?(x):0)) #ifdef DEBUG #define prt(x) cerr<<#x"="<<(x)<<endl #define asrtWA(s) do if(!(s))do{cerr<<"assert("#s")"<<endl;}whl(0);whl(0) #define asrtTLE(s) do if(!(s))do{cerr<<"assert("#s")"<<endl;}whl(0);whl(0) #define asrtMLE(s) do if(!(s))do{cerr<<"assert("#s")"<<endl;}whl(0);whl(0) #define asrtOLE(s) do if(!(s))do{cerr<<"assert("#s")"<<endl;}whl(0);whl(0) #define asrtRE(s) do if(!(s))do{cerr<<"assert("#s")"<<endl;}whl(0);whl(0) #define runtime() cerr<<"Used: "<<db(clock())/CLOCKS_PER_SEC<<"s"<<endl #define input(in) do{}whl(0) #define output(out) do{}whl(0) #else #define endl (char('\n')) #define prt(x) (cerr) #define asrtWA(s) do if(!(s))exit(0);whl(0) #define asrtTLE(s) do if(!(s))whl(1);whl(0) #define asrtMLE(s) do if(!(s))whl(new int);whl(0) #define asrtOLE(s) do if(!(s))whl(1)puts("OLE");whl(0) #define asrtRE(s) do if(!(s))*(int*)0=0;whl(0) #define runtime() (cerr) #define input(in) freopen(in,"r",stdin) #define output(out) freopen(out,"w",stdout) #endif typedef long long int lli; typedef double db; typedef const char* cstr; typedef string str; typedef vec<int> vi; typedef vec<vi> vvi; typedef vec<bool> vb; typedef vec<vb> vvb; typedef vec<str> vs; typedef pr<int,int> pii; typedef pr<lli,lli> pll; typedef pr<db,db> pdd; typedef map<int,int> mii; typedef map<str,int> msi; typedef map<char,int> mci; typedef set<int> si; typedef set<str> ss; typedef que<int> qi; typedef vec<pii> vpii; typedef vec<pdd> vpdd; #if __GNUC__>=4 and __GNUC_MINOR__>=6 using __gnu_cxx::rope; #endif #if __GNUC__>=4 and __GNUC_MINOR__>=7 template<typename key,typename value>class ext_map:public __gnu_pbds::tree<key,value,less<key>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>{}; template<typename key>class ext_set:public __gnu_pbds::tree<key,__gnu_pbds::null_type,less<key>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>{}; #elif __GNUC__>=4 and __GNUC_MINOR__>=6 template<typename key,typename value>class ext_map:public __gnu_pbds::tree<key,value,less<key>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>{}; template<typename key>class ext_set:public __gnu_pbds::tree<key,__gnu_pbds::null_mapped_type,less<key>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>{}; #endif int oo=(~0u)>>1; lli ooll=(~0ull)>>1; db inf=1e+10; db eps=1e-10; db gam=0.5772156649015328606; db pi=acos(-1.0); int dx[]={1,0,-1,0,1,-1,-1,1,0}; int dy[]={0,1,0,-1,1,1,-1,-1,0}; int MOD=1000000007; template<typename type>inline bool cmax(type& a,const type& b){rtn a<b?a=b,true:false;} template<typename type>inline bool cmin(type& a,const type& b){rtn b<a?a=b,true:false;} template<typename type>inline type sqr(const type& x){rtn x*x;} inline int sgn(const db& x){rtn (x>+eps)-(x<-eps);} inline int dbcmp(const db& a,const db& b){rtn sgn(a-b);} template<typename istream,typename first_type,typename second_type>inline istream& operator>>(istream& cin,pr<first_type,second_type>& x){rtn cin>>x.x>>x.y;} template<typename ostream,typename first_type,typename second_type>inline ostream& operator<<(ostream& cout,const pr<first_type,second_type>& x){rtn cout<<x.x<<" "<<x.y;} template<typename istream,typename type>inline istream& operator>>(istream& cin,vec<type>& x){rep(i,sz(x))cin>>x[i];rtn cin;} template<typename ostream,typename type>inline ostream& operator<<(ostream& cout,const vec<type>& x){rep(i,sz(x))cout<<x[i]<<(i+1==sz(x)?"":" ");rtn cout;} template<typename type>inline pr<type,type> operator-(const pr<type,type>& x){rtn mp(-x.x,-x.y);} template<typename type>inline pr<type,type> operator+(const pr<type,type>& a,const pr<type,type>& b){rtn mp(a.x+b.x,a.y+b.y);} template<typename type>inline pr<type,type> operator-(const pr<type,type>& a,const pr<type,type>& b){rtn mp(a.x-b.x,a.y-b.y);} template<typename type>inline pr<type,type> operator*(const pr<type,type>& a,const type& b){rtn mp(a.x*b,a.y*b);} template<typename type>inline pr<type,type> operator/(const pr<type,type>& a,const type& b){rtn mp(a.x/b,a.y/b);} template<typename type>inline pr<type,type>& operator-=(pr<type,type>& a,const pr<type,type>& b){rtn a=a-b;} template<typename type>inline pr<type,type>& operator+=(pr<type,type>& a,const pr<type,type>& b){rtn a=a+b;} template<typename type>inline pr<type,type>& operator*=(pr<type,type>& a,const type& b){rtn a=a*b;} template<typename type>inline pr<type,type>& operator/=(pr<type,type>& a,const type& b){rtn a=a/b;} template<typename type>inline type cross(const pr<type,type>& a,const pr<type,type>& b){rtn a.x*b.y-a.y*b.x;} template<typename type>inline type dot(const pr<type,type>& a,const pr<type,type>& b){rtn a.x*b.x+a.y*b.y;} template<typename type>inline type gcd(type a,type b){if(b)whl((a%=b)&&(b%=a));rtn a+b;} template<typename type>inline type lcm(type a,type b){rtn a*b/gcd(a,b);} template<typename type>inline void bit_inc(vec<type>& st,int x,type inc){whl(x<sz(st))st[x]+=inc,x|=x+1;} template<typename type>inline type bit_sum(const vec<type>& st,int x){type s=0;whl(x>=0)s+=st[x],x=(x&(x+1))-1;rtn s;} template<typename type>inline type bit_kth(const vec<type>& st,int k){int x=0,y=0,z=0;whl((1<<(++y))<=sz(st));fdt(i,y-1,0){if((x+=1<<i)>sz(st)||z+st[x-1]>k)x-=1<<i;else z+=st[x-1];}rtn x;} inline void make_set(vi& st){rep(i,sz(st))st[i]=i;} inline int find_set(vi& st,int x){int y=x,z;whl(y!=st[y])y=st[y];whl(x!=st[x])z=st[x],st[x]=y,x=z;rtn y;} inline bool union_set(vi& st,int a,int b){a=find_set(st,a),b=find_set(st,b);rtn a!=b?st[a]=b,true:false;} template<typename type>inline void merge(type& a,type& b){if(sz(a)<sz(b))swap(a,b);whl(sz(b))a.ins(*b.begin()),b.ers(b.begin());} struct Initializer{Initializer(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}~Initializer(){runtime();}}initializer; int md[201][201]; #define m(x,y) md[x+100][y+100] int main() { int n; cin>>n; m(0,0)=n; bool still=false; whl(!still) { still=true; ft(x,-100,100) ft(y,-100,100) { int get=m(x,y)/4; if (get) { rep(d,4) m(x+dx[d],y+dy[d])+=get; m(x,y)-=get*4; still=false; } } } int t; cin>>t; rep(i,t) { int x,y; cin>>x>>y; if (max(abs(x),abs(y))<=100) cout<<m(x,y)<<endl; else cout<<0<<endl; } }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; pair<int, int> operator+(pair<int, int> p1, pair<int, int> p2) { pair<int, int> ans = {p1.first + p2.first, p1.second + p2.second}; return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, t; cin >> n >> t; vector<vector<int>> grid(500, vector<int>(500)); vector<pair<int, int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; grid[250][250] = n; queue<pair<int, int>> q; q.push({0, 0}); while (q.size()) { auto val = q.front(); q.pop(); if (grid[250 + val.first][250 + val.second] < 4) continue; int addRem = grid[250 + val.first][250 + val.second] - (grid[250 + val.first][250 + val.second] % 4); grid[250 + val.first][250 + val.second] %= 4; for (auto p : dirs) { auto p2 = p + val; grid[p2.first + 250][p2.second + 250] += addRem / 4; if (grid[p2.first + 250][p2.second + 250] >= 4) { q.push(p2); } } } while (t--) { int x, y; cin >> x >> y; int nx = x + 250, ny = y + 250; if (nx >= 0 && nx < 500 && ny >= 0 && ny <= 500) { cout << grid[250 + x][250 + y]; } else { cout << 0; } cout << '\n'; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 131; const int ox = 65; const int oy = 65; int a[MAXN][MAXN], b[MAXN][MAXN]; int dx[] = {0, 0, -1, 1}; int dy[] = {1, -1, 0, 0}; void init() { memset(b, 0, sizeof(b)); int p = MAXN - 1; bool ok = false; for (int i = 1; i <= p; i++) for (int j = 1; j <= p; j++) if (a[i][j] > 3) { for (int k = 0; k < 4; k++) b[i + dx[k]][j + dy[k]] += a[i][j] / 4; b[i][j] -= a[i][j] / 4 * 4; } for (int i = 1; i <= p; i++) for (int j = 1; j <= p; j++) { a[i][j] += b[i][j]; if (a[i][j] > 3) ok = true; } if (ok) init(); } int main() { int n, m, x, y; while (cin >> n >> m) { a[ox][oy] = n; init(); while (m--) { scanf("%d%d", &x, &y); x += ox; y += oy; if (x >= MAXN || y >= MAXN || x < 0 || y < 0) puts("0"); else printf("%d\n", a[x][y]); } } return 0; }
8
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") const int inf = 1e9; const double eps = 1e-8; const double pi = acos(-1.0); template <class T> inline bool rd(T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; while (c != '-' && (c < '0' || c > '9')) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1; } template <class T> inline void pt(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) pt(x / 10); putchar(x % 10 + '0'); } using namespace std; const int N = 1000; const int C = N / 2; int step[4][2] = {0, 1, 0, -1, 1, 0, -1, 0}; int a[N][N]; void work(int x, int y) { if (a[x][y] < 4) return; int tmp = a[x][y] / 4; a[x][y] %= 4; for (int i = 0; i < 4; i++) { a[x + step[i][0]][y + step[i][1]] += tmp; work(x + step[i][0], y + step[i][1]); } work(x, y); } void put(int mp[][N]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { pt(mp[i][j]); if (i == C && j == C) putchar('*'); else putchar(' '); } puts(""); } } int main() { int n; while (cin >> n) { if (!n) { rd(n); while (n--) { int x, y; rd(x), rd(y); puts("0"); } continue; } memset(a, 0, sizeof a); a[C][C] = n; work(C, C); int t; rd(t); while (t--) { int x, y; rd(x); rd(y); if (abs(x) >= N / 2 || abs(y) >= N / 2) puts("0"); else pt(a[x + C][y + C]), puts(""); } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int arr[150][150]; void solve(int x, int y) { if (arr[x][y] < 3) arr[x][y]++; else { arr[x][y] = 0; solve(x + 1, y); solve(x - 1, y); solve(x, y + 1); solve(x, y - 1); } } int main() { int n, t, x, y; cin >> n >> t; for (int i = 0; i < n; i++) solve(65, 65); for (int i = 0; i < t; i++) { cin >> x >> y; if (x < (-65) || x > 65 || y < (-65) || y > 65) cout << 0 << endl; else cout << arr[x + 65][y + 65] << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int A[222][222]; bool u[222][222]; vector<pair<int, int> > V; int main(void) { int n, t; cin >> n >> t; A[111][111] = n; u[111][111] = 1; V.push_back(make_pair(111, 111)); bool ok; while (1) { vector<pair<int, int> > ops; vector<pair<int, int> > V2; ok = false; memset(u, 0, sizeof(u)); for (vector<pair<int, int> >::const_iterator it = V.begin(); it != V.end(); ++it) { int x = it->first, y = it->second; if (A[x][y] >= 4) { ops.push_back(make_pair(x, y)); if (!u[x][y]) V2.push_back(make_pair(x, y)); if (!u[x - 1][y]) V2.push_back(make_pair(x - 1, y)); if (!u[x][y - 1]) V2.push_back(make_pair(x, y - 1)); if (!u[x + 1][y]) V2.push_back(make_pair(x + 1, y)); if (!u[x][y + 1]) V2.push_back(make_pair(x, y + 1)); u[x][y] = 1; u[x - 1][y] = 1; u[x][y - 1] = 1; u[x + 1][y] = 1; u[x][y + 1] = 1; ok = true; } } V = V2; for (int i = 0; i < (int)ops.size(); i++) { int x = ops[i].first, y = ops[i].second; int nn = A[x][y] / 4; A[x][y] -= 4 * nn; A[x - 1][y] += nn; A[x][y - 1] += nn; A[x + 1][y] += nn; A[x][y + 1] += nn; } if (!ok) break; } cerr << "Done." << endl; for (int i = 0; i < t; i++) { int x, y; cin >> x >> y; if (abs(x) > 111 || abs(y) > 111) { cout << "0" << endl; } else { cout << A[111 + x][111 + y] << endl; } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MAX = 2000; int matrix[MAX][MAX]; void dfs(int x, int y, int num) { matrix[x][y] += num; if (matrix[x][y] < 4) return; int p = matrix[x][y] / 4; matrix[x][y] %= 4; dfs(x + 1, y, p); dfs(x - 1, y, p); dfs(x, y + 1, p); dfs(x, y - 1, p); } int main() { ios::sync_with_stdio(false); int n, t; int x[50002], y[50002]; cin >> n >> t; for (int i = 1; i <= t; i++) cin >> x[i] >> y[i]; dfs(MAX / 2, MAX / 2, n); for (int i = 1; i <= t; i++) { int x1 = x[i] + MAX / 2; int y1 = y[i] + MAX / 2; if (x1 < MAX && x1 > 0 && y1 < MAX && y1 > 0) cout << matrix[x1][y1] << endl; else cout << 0 << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3; int long long n, t, ans[2000][2000]; queue<pair<int, int>> qu; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> t; ans[1000][1000] = n; qu.push({0, 0}); while (qu.size() > 0) { auto a = qu.front(); qu.pop(); int x = a.first + 1000, y = a.second + 1000; if (ans[x][y] < 4) { continue; } for (int i = -1; i <= 1; i++) for (int j = -1; j <= 1; j++) if (abs(i - j) == 1) { ans[x + i][y + j] += ans[x][y] / 4; if (ans[x + i][j + y] >= 4) qu.push({x + i - 1000, y + j - 1000}); } ans[x][y] %= 4; } while (t--) { int long long a, b; cin >> a >> b; if (a + 1e3 < 0 || a + 1e3 > 2e3 || b + 1e3 < 0 || b + 1e3 > 2e3) { cout << 0 << endl; continue; } cout << ans[a + 1000][b + 1000] << endl; } }
8
CPP
#include <bits/stdc++.h> using namespace std; int matrix[1000][1000]; int ultim_luat[1000][1000]; pair<short int, short int> luat[2][100000]; int c[2]; int main() { ios_base ::sync_with_stdio(0); int n; cin >> n; matrix[500][500] = n; luat[0][0] = {500, 500}; c[0] = 1; for (int q(0), j(1); c[q]; q = 1 - q, j++) { c[1 - q] = 0; for (int i(0); i < c[q]; i++) { int x(luat[q][i].first), y(luat[q][i].second); ultim_luat[x][y] = 0; int div(matrix[x][y] / 4); matrix[x][y] -= 4 * div; if (div == 0) continue; for (int l(-1); l <= 1; l++) { for (int p(-1); p <= 1; p++) { if (l != 0 && p != 0) continue; if (l == p) continue; matrix[x + l][y + p] += div; if (matrix[x + l][y + p] >= 4 && !ultim_luat[x + l][y + p]) { ultim_luat[x + l][y + p] = 1; luat[1 - q][c[1 - q]++] = {x + l, y + p}; } } } } } int t, a, b; cin >> t; while (t--) { cin >> a >> b; a += 500, b += 500; if (a > 1000 || a < 0 || b > 1000 || b < 0) cout << "0\n"; else cout << matrix[a][b] << '\n'; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e3, M = 5e4 + 100; long long n, t, ax[M], ay[M], b[N][N]; void dfs(int xx, int yy, int k) { b[xx][yy] += k; if (b[xx][yy] < 4) return; int p = b[xx][yy] / 4; b[xx][yy] %= 4; dfs(xx - 1, yy, p); dfs(xx + 1, yy, p); dfs(xx, yy - 1, p); dfs(xx, yy + 1, p); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> t; for (int i = 0; i < t; i++) cin >> ax[i] >> ay[i]; dfs(N / 2, N / 2, n); for (int i = 0; i < t; i++) { int x = ax[i] + N / 2; int y = ay[i] + N / 2; if ((x > 0) && (y > 0) && (x < N) && (y < N)) cout << b[x][y] << endl; else cout << 0 << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> long long a[202][202]; using namespace std; int main() { long long n, t, x, y, i, j, q; cin >> n >> t; q = 1; for (i = 1; i <= 201; i++) { for (j = 1; j <= 201; j++) a[i][j] = 0; } a[101][101] = n; while (q != 0) { for (i = 1; i <= 201; i++) { for (j = 1; j <= 201; j++) { if (a[i][j] >= 4) { int y = a[i][j] / 4; a[i][j] = a[i][j] % 4; a[i - 1][j] += y; a[i + 1][j] += y; a[i][j - 1] += y; a[i][j + 1] += y; } } } q = 0; for (i = 1; i <= 201; i++) { for (j = 1; j <= 201; j++) { if (a[i][j] >= 4) { q = 1; } } } } for (i = 0; i < t; i++) { cin >> x >> y; x = fabs(x) + 101; y = fabs(y) + 101; if (x > 201 || y > 201) cout << 0 << endl; else cout << a[x][y] << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char buf[1 << 21], *p1 = buf, *p2 = buf; template <class T> void read(T &x) { x = 0; int c = getchar(); int flag = 0; while (c < '0' || c > '9') flag |= (c == '-'), c = getchar(); while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); if (flag) x = -x; } template <class T> T _max(T a, T b) { return b < a ? a : b; } template <class T> T _min(T a, T b) { return a < b ? a : b; } template <class T> bool checkmax(T &a, T b) { return a < b ? a = b, 1 : 0; } template <class T> bool checkmin(T &a, T b) { return b < a ? a = b, 1 : 0; } const int B = 1000; int n, t; int a[2005][2005]; void init() { read(n); read(t); } void run(int x, int y) { ++a[x][y]; if (a[x][y] == 4) { a[x][y] = 0; run(x, y + 1); run(x, y - 1); run(x - 1, y); run(x + 1, y); } } void solve() { for (int i = 1; i <= n; ++i) run(B, B); int x, y; while (t--) { read(x); read(y); if (abs(x) > 1000 || abs(y) > 1000) { printf("0\n"); continue; } printf("%d\n", a[B + x][B + y]); } } int main() { init(); solve(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int ans[222][222]; int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; struct P { int x, y; }; int inque[222][222]; queue<P> que; void bfs() { int i, j, k; while (!que.empty()) { P p = que.front(); que.pop(); int t = ans[p.x][p.y] / 4; ans[p.x][p.y] -= t * 4; if (t <= 0) { inque[p.x][p.y] = 0; continue; } for (i = 0; i < 4; i++) { int x = p.x + dir[i][0]; int y = p.y + dir[i][1]; if (x < 0 || y < 0) continue; if ((x == 0 || y == 0) && (p.x != 0 && p.y != 0)) { if (x == 0) { ans[x][y] += 2 * t; } if (y == 0) { ans[x][y] += 2 * t; } if (x == 0 && y == 0) ans[x][y] -= 2 * t; } else if (x == 0 && y == 0) { ans[x][y] += 2 * t; } else ans[x][y] += t; if (!inque[x][y]) { P p1; p1.x = x; p1.y = y; que.push(p1); inque[x][y] = 1; } } inque[p.x][p.y] = 0; } } int main() { int n, t; cin >> n >> t; int i; P p1; p1.x = 0; p1.y = 0; ans[0][0] = n; inque[0][0] = 1; que.push(p1); bfs(); for (i = 1; i <= t; i++) { long long x, y; cin >> x >> y; if (x < 0) x = -x; if (y < 0) y = -y; if (x >= 222 || y >= 222) { cout << "0" << endl; continue; } cout << ans[x][y] << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007, dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; const double eps = 1e-8; void read(int &k) { k = 0; char x = getchar(); while (x < '0' || x > '9') x = getchar(); while (x >= '0' && x <= '9') { k = k * 10 - 48 + x; x = getchar(); } } int N, q, a[210][210], b[210][210], x, y; int main() { scanf("%d%d", &N, &q); a[70][70] = N; int dead = 1; while (dead) { dead = 0; memset(b, 0, sizeof b); for (int i = 1; i < 140; i++) for (int j = 1; j < 140; j++) if (a[i][j] >= 4) { for (int dir = 0; dir < 4; dir++) b[i + dx[dir]][j + dy[dir]] += a[i][j] / 4; a[i][j] %= 4; dead = 1; } for (int i = 1; i < 140; i++) for (int j = 1; j < 140; j++) b[i][j] += a[i][j]; memcpy(a, b, sizeof a); } for (; q--;) { scanf("%d%d", &x, &y); if (70 + x > 0 && 70 + x < 140 && 70 + y > 0 && 70 + y < 140) printf("%d\n", a[70 + x][70 + y]); else printf("%d\n", 0); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int in() { int tmp; scanf("%d", &tmp); return tmp; } int &in(int &x) { return x = in(); } template <typename T, typename... Args> void in(T &x, Args &...args) { in(x); if (sizeof...(args)) in(args...); } template <class T, class L> T &smax(T &x, L y) { if (y > x) x = y; return x; } template <class T, class L> T &smin(T &x, L y) { if (y < x) x = y; return x; } namespace std { template <> struct hash<pair<int, int> > { size_t operator()(pair<int, int> const &x) const { return hash<long long>()(((long long)x.first << 32) ^ x.second); } }; } // namespace std const int maxn = 1e3 + 17; int x, y, a, b, c, m, ans, xs[] = {-1, 1, 0, 0}, ys[] = {0, 0, -1, 1}; int mp(int x) { return x < 0 ? -2 * x : 2 * x + 1; } int ant[maxn][maxn]; queue<pair<int, int> > q; int main() { in(ant[mp(0)][mp(0)]); q.push({0, 0}); while (!q.empty()) { int x = q.front().first, y = q.front().second; q.pop(); if (ant[mp(x)][mp(y)] < 4) continue; for (int i = 0; i < 4; i++) { ant[mp(x + xs[i])][mp(y + ys[i])] += ant[mp(x)][mp(y)] / 4; if (ant[mp(x + xs[i])][mp(y + ys[i])] >= 4) q.push({x + xs[i], y + ys[i]}); } ant[mp(x)][mp(y)] %= 4; } int t = in(); while (t--) { int x = in(), y = in(); if (mp(x) >= maxn || mp(y) >= maxn) printf("0\n"); else printf("%d\n", ant[mp(x)][mp(y)]); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, t; int ans[2 * 1000][2 * 1000]; int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; void dfs(int x, int y) { if (ans[x][y] < 4) return; int t = ans[x][y] / 4; ans[x][y] %= 4; for (int i = 0; i < 4; i++) { int x2 = x + dir[i][0]; int y2 = y + dir[i][1]; ans[x2][y2] += t; dfs(x2, y2); } } int main() { scanf("%d%d", &n, &t); ans[1000][1000] = n; dfs(1000, 1000); for (int i = 0; i < t; i++) { int x, y; scanf("%d%d", &x, &y); x += 1000; y += 1000; if (x < 0 || x >= 2 * 1000 || y < 0 || y >= 2 * 1000) printf("0\n"); else printf("%d\n", ans[x][y]); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, t; int ans[2 * 1000][2 * 1000]; int directs[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; void dfs(int x, int y) { if (ans[x][y] < 4) return; int t = ans[x][y] / 4; ans[x][y] %= 4; for (int i = 0; i < 4; i++) { int x2 = x + directs[i][0]; int y2 = y + directs[i][1]; ans[x2][y2] += t; dfs(x2, y2); } } int main() { scanf("%d %d", &n, &t); ans[1000][1000] = n; dfs(1000, 1000); for (int i = 0; i < t; i++) { int temp1, temp2; scanf("%d %d", &temp1, &temp2); temp1 += 1000; temp2 += 1000; if (temp1 < 0 || temp1 >= 2 * 1000 || temp2 < 0 || temp2 >= 2 * 1000) printf("0\n"); else printf("%d\n", ans[temp1][temp2]); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long double pi = acos(-1.0); const long double eps = 1e-9; int n, t; short m[210][210]; short b[210][210]; int mx = 0; int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, 1, 0, -1}; int main() { cin >> n >> t; long long s = 0; m[100][100] = n; while (1) { int f = 0; memset(b, 0, sizeof b); ; for (int i = 0; i < 201; ++i) { for (int j = 0; j < 201; ++j) { if (m[i][j] >= 4) { f = 1; for (int k = 0; k < 4; ++k) { int xx = i + dx[k]; int yy = j + dy[k]; b[xx][yy] += m[i][j] / 4; } } b[i][j] += m[i][j] % 4; } } if (!f) { break; } memcpy(m, b, sizeof m); ; } for (int i = 0; i < t; ++i) { int t1, t2; scanf("%d%d", &t1, &t2); if (max(t1, t2) > 100 || min(t1, t2) < -100) { cout << "0" << endl; } else { cout << m[100 + t1][100 + t2] << endl; } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int f[4][2] = {0, -1, 0, 1, -1, 0, 1, 0}; struct node { int x, y; }; int k[2000][2000]; bool used[2000][2000]; queue<node> q; void bfs(int n) { node p; p.x = 1000; p.y = 1000; while (!q.empty()) q.pop(); q.push(p); memset(k, 0, sizeof(k)); memset(used, 0, sizeof(used)); k[1000][1000] = n; while (!q.empty()) { node tmp = q.front(); q.pop(); used[tmp.x][tmp.y] = 0; for (int i = 0; i < 4; i++) { node nt; nt.x = tmp.x + f[i][0]; nt.y = tmp.y + f[i][1]; k[nt.x][nt.y] += k[tmp.x][tmp.y] / 4; if (k[nt.x][nt.y] >= 4 && !used[nt.x][nt.y]) { used[nt.x][nt.y] = 1; q.push(nt); } } k[tmp.x][tmp.y] %= 4; } return; } int main() { int n, m; while (cin >> n >> m) { bfs(n); for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x += 1000; y += 1000; if (x < 0 || x > 2000 || y < 0 || y > 2000) cout << 0 << endl; else cout << k[x][y] % 4 << endl; } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int a[201][201] = {0}; int n, m; int main(int argc, char *argv[]) { int x = 100; int y = 100; int ok = 0; cin >> n >> m; a[x][y] = n; do { ok = 0; int max = y; int min = x; for (int i = x; i <= y; i++) for (int j = x; j <= y; j++) if (a[i][j] >= 4) { ok = 1; int numofant = a[i][j]; int mods = numofant % 4; int temp = (numofant - mods) / 4; a[i][j - 1] += temp; a[i][j + 1] += temp; a[i - 1][j] += temp; a[i + 1][j] += temp; a[i][j] = mods; } if (a[x - 1][100] >= 4) { x--; y++; } } while (ok == 1); long long u, v; int k[50005]; for (int i = 0; i < m; i++) { cin >> u >> v; u += 100; v += 100; if (u > 200 || u < 0 || v > 200 || v < 0) k[i] = 0; else k[i] = a[u][v]; } for (int i = 0; i < m; i++) cout << k[i] << endl; return EXIT_SUCCESS; }
8
CPP
#include <bits/stdc++.h> int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; int n; int t[256][256]; int main() { int i, j, k; bool fl; scanf("%d", &n); memset(t, 0, sizeof(t)); t[128][128] = n; int it = 0; while (1) { it++; fl = 0; for (i = 1; i < 255; i++) for (j = 1; j < 255; j++) { if (t[i][j] >= 4) { fl = 1; int ad = t[i][j] / 4; for (k = 0; k < 4; k++) { t[i + dx[k]][j + dy[k]] += ad; } t[i][j] &= 3; } } if (!fl) break; } int q; scanf("%d", &q); for (i = 0; i < q; i++) { scanf("%d %d", &j, &k); if (k + 128 >= 256 || j + 128 >= 256 || k + 128 < 1 || j + 128 < 1) { printf("0\n"); } else printf("%d\n", t[j + 128][k + 128]); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int d[1001][1001] = {0}, p[1001][1001] = {0}; int main() { int n, m; cin >> n >> m; d[500][500] = n; int q = 0; int w = 0; while (1) { bool any = false; for (int i = 500 - q; i <= 500 + q; ++i) { for (int j = 500 - w; j <= 500 + w; ++j) { if (d[i][j] >= 4) any = true; else continue; p[i + 1][j] += d[i][j] / 4; p[i][j + 1] += d[i][j] / 4; p[i][j - 1] += d[i][j] / 4; p[i - 1][j] += d[i][j] / 4; q = max(q, i + 1 - 500); w = max(w, j + 1 - 500); q = max(q, 500 - i + 1); w = max(w, 500 - j + 1); d[i][j] %= 4; } } for (int i = 500 - q; i <= 500 + q; ++i) { for (int j = 500 - w; j <= 500 + w; ++j) { d[i][j] += p[i][j]; p[i][j] = 0; } } if (!any) break; } for (int i = 0; i < m; ++i) { int x, y; cin >> x >> y; if (x < -500 || x > 500 || y < -500 || y > 500) cout << "0\n"; else cout << d[x + 500][y + 500] << "\n"; } }
8
CPP
#include <bits/stdc++.h> int grid[101][101]; int ukxb[101][101]; int x, y, m = 1; void Translate() { if (x < 0) x = -x; if (y < 0) y = -y; } bool Propagate() { int peak = 0; for (y = 0; y <= 0 + m; y++) for (x = 0; x <= 0 + m; x++) { int ukxn = grid[y][x] % 4; ukxn += grid[y][x > 0 ? x - 1 : 1] / 4; ukxn += grid[y > 0 ? y - 1 : 1][x] / 4; ukxn += grid[y][x + 1] / 4; ukxn += grid[y + 1][x] / 4; ukxb[y][x] = ukxn; if (ukxn > peak) peak = ukxn; if (ukxn && x - 0 >= m) m = x - 0 + 1; } for (y = 0; y < 0 + m; y++) for (x = 0; x < 0 + m; x++) { grid[y][x] = ukxb[y][x]; } return peak >= 4; } int main() { int q, n; scanf("%d %d", &n, &q); x = y = 0; Translate(); grid[y][x] = n; while (Propagate()) ; for (; q > 0; q--) { scanf("%d %d", &x, &y); Translate(); printf("%d\n", y > 175 || x > 175 ? 0 : grid[y][x]); } if (0) { for (y = 0; y < 101; y++) { for (x = 0; x < 101; x++) { if (0) { printf("%d", grid[y][x]); } } if (0) { putchar(10); } } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; bool vis[203][203]; int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; int a[203][203]; void dfs(int x, int y) { if (a[x][y] >= 4) { int cnt = a[x][y] / 4; a[x][y] -= 4 * cnt; for (int k = 0; k < 4; k++) { int fx = x + dir[k][0]; int fy = y + dir[k][1]; a[fx][fy] += cnt; dfs(fx, fy); } } } int n, t; int main() { int i, j; scanf("%d%d", &n, &t); a[100][100] = n; dfs(100, 100); for (i = 0; i <= 200; i++) for (j = 0; j <= 200; j++) if (a[i][j] >= 4) { dfs(i, j); } while (t--) { int x, y; scanf("%d%d", &x, &y); if (x < 100 && x > -100 && y < 100 && y > -100) printf("%d\n", a[x + 100][y + 100]); else printf("0\n"); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int a[205][205]; void dfs(int x, int y, int n) { a[x][y] += n; if (a[x][y] < 4) return; a[x][y] -= 4; if (a[x][y] > 3) dfs(x, y, 0); dfs(x - 1, y, 1); dfs(x + 1, y, 1); dfs(x, y + 1, 1); dfs(x, y - 1, 1); } int main() { cin.sync_with_stdio(false); cout.sync_with_stdio(false); int n, t; cin >> n >> t; int zero = 100; dfs(100, 100, n); while (t--) { int x, y; cin >> x >> y; if (x > 100 || x < -100 || y > 100 || y < -100) cout << 0 << endl; else cout << a[x + zero][y + zero] << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } const int LM = 5005; const long long mod = 1000000007; int moveX[] = {0, 0, -1, 1}; int moveY[] = {-1, 1, 0, 0}; int ants[LM][LM]; class problem { void dfs(int x, int y) { if (ants[x][y] < 4) return; int add = ants[x][y] / 4; ants[x][y] -= add * 4; for (int i = 0; i < 4; i += 1) { int newX = x + moveX[i], newY = y + moveY[i]; ants[newX][newY] += add; dfs(newX, newY); } } public: void init() { int N, T; scanf("%d %d", &N, &T); ants[2500][2500] = N; dfs(2500, 2500); for (int i = 0; i < T; i += 1) { int x, y; scanf("%d %d", &x, &y); x += 2500, y += 2500; if (x < 0 || x >= LM || y < 0 || y >= LM) printf("0\n"); else printf("%d\n", ants[x][y]); } } }; int main() { problem obj; obj.init(); return 0; }
8
CPP
#include <bits/stdc++.h> int dh[4] = {-1, 0, 1, 0}, dw[4] = {0, 1, 0, -1}; int cnt[2 * 70][2 * 70]; void doit(int a, int b) { int i, j, k, c = cnt[a][b] / 4; for (k = 0; k < 4; k++) { i = a + dh[k]; j = b + dw[k]; cnt[i][j] += c; } cnt[a][b] -= 4 * c; } int main() { int i, j, n, t, a, b; bool bf = 1; scanf("%d%d", &n, &t); cnt[70][70] = n; while (bf) { bf = 0; for (i = 1; i < 2 * 70; i++) for (j = 1; j < 2 * 70; j++) if (cnt[i][j] >= 4) { doit(i, j); bf = 1; } } while (t--) { scanf("%d%d", &a, &b); if (a + 70 < 1 || a + 70 >= 2 * 70 || b + 70 < 1 || b + 70 >= 2 * 70) puts("0"); else printf("%d\n", cnt[a + 70][b + 70]); } }
8
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> inline string tostring(const T &x) { ostringstream os; os << x; return os.str(); } inline int toint(const string &s) { istringstream is(s); int x; is >> x; return x; } inline int todecimal(string s) { int a = 0; for (int i = 0; i < s.size(); i++) a = 2 * a + (s[i] - '0'); return a; } inline string tobinary(int a) { string s; while (a != 0) { s = (char)(a % 2 + '0') + s; a >>= 1; } return s; } template <typename T> inline T sqr(T x) { return x * x; } template <typename T> T gcd(T a, T b) { return (b == 0) ? abs(a) : gcd(b, a % b); } inline int isvowel(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return 1; return 0; } inline int isprime(int a) { for (int i = 2; i * i <= a; i++) if (!(a % i)) return 0; return 1; } inline void inp(int &n) { n = 0; int ch = getchar(); int sign = 1; while (ch < '0' || ch > '9') { if (ch == '-') sign = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') n = (n << 3) + (n << 1) + ch - '0', ch = getchar(); n = n * sign; } const int dx4[4] = {0, 0, 1, -1}; const int dy4[4] = {1, -1, 0, 0}; const int dx8[8] = {0, 0, 1, 1, 1, -1, -1, -1}; const int dy8[8] = {1, -1, 0, 1, -1, 1, 0, -1}; class node { public: int x, y, z; node(int a, int b, int c) { x = a; y = b; z = c; } }; bool operator<(const node &a, const node &b) { return a.x > b.x; } bool compare(const node &a, const node &b) { return a.x < b.x; } const int maxn = 66; int grid[maxn][maxn]; int n; void solve() { memset((grid), (0), sizeof(grid)); grid[0][0] = n; int moving = 1; while (moving) { moving = 0; for (int i = 0; i < maxn; i++) { for (int j = 0; j < maxn; j++) { int x = grid[i][j] / 4; grid[i + 1][j] += x; grid[i][j + 1] += x; if (i) grid[i - 1][j] += x * (i == 1 ? 2 : 1); if (j) grid[i][j - 1] += x * (j == 1 ? 2 : 1); grid[i][j] %= 4; if (x) moving = 1; } } } int q = 0; ; cin >> q; int x, y; for (int i = 0; i < q; i++) { cin >> x >> y; if (x < 0) x = -x; if (y < 0) y = -y; if (x >= maxn || y >= maxn) cout << 0 << endl; else cout << grid[x][y] << endl; } } void input() { cin >> n; } int main() { int testcase = 1; for (int i = 0; i < testcase; i++) { input(); solve(); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int a[610][610], i, j, n, q, x, y; void dfs(int x, int y) { if (a[x][y] >= 4) { int d = a[x][y] / 4; a[x - 1][y] += d; a[x + 1][y] += d; a[x][y - 1] += d; a[x][y + 1] += d; a[x][y] -= 4 * d; dfs(x - 1, y); dfs(x + 1, y); dfs(x, y - 1); dfs(x, y + 1); } } int main() { scanf("%d%d", &n, &q); a[300][300] = n; dfs(300, 300); for (i = 1; i <= q; ++i) { scanf("%d%d", &x, &y); if (abs(x) + abs(y) <= 300) printf("%d\n", a[300 + x][300 + y]); else printf("0\n"); } }
8
CPP
#include <bits/stdc++.h> using namespace std; void Get(int &T) { char C; bool F = 0; for (; C = getchar(), C < '0' || C > '9';) if (C == '-') F = 1; for (T = C - '0'; C = getchar(), C >= '0' && C <= '9'; T = T * 10 + C - '0') ; F && (T = -T); } int F[200][200]; int Need[200][200]; int N; void Init() { Get(N); } int M; void Work() { F[100][100] = N; for (int Again = 1; Again;) { Again = 0; for (int i = 35; i <= 165; i++) for (int j = 35; j <= 165; j++) { Need[i][j] = F[i][j] / 4; F[i][j] %= 4; } for (int i = 35; i <= 165; i++) for (int j = 35; j <= 165; j++) { if (Need[i][j]) Again = 1; F[i - 1][j] += Need[i][j]; F[i + 1][j] += Need[i][j]; F[i][j - 1] += Need[i][j]; F[i][j + 1] += Need[i][j]; Need[i][j] = 0; } } Get(M); for (int k = 1, X, Y; k <= M; k++) { Get(X); Get(Y); X += 100; Y += 100; if (X < 35 || X > 165 || Y < 35 || Y > 165) puts("0"); else printf("%d\n", F[X][Y]); } } int main() { Init(); Work(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; struct node { int x, y; }; int s[210][210]; int dx[] = {1, 0, 0, -1}; int dy[] = {0, 1, -1, 0}; int main() { int i, j, k, m, n, p, q; while (~scanf("%d%d", &n, &m)) { memset(s, 0, sizeof(s)); s[100][100] = n; vector<node> ss; node ret; ret.x = 100; ret.y = 100; ss.push_back(ret); for (i = 0; i < ss.size(); i++) { int x = ss[i].x; int y = ss[i].y; int j = s[x][y] / 4; s[x][y] %= 4; for (k = 0; k < 4; k++) { int nx = x + dx[k]; int ny = y + dy[k]; s[nx][ny] += j; if (s[nx][ny] - j < 4 && s[nx][ny] >= 4) { ret.x = nx; ret.y = ny; ss.push_back(ret); } } } while (m--) { scanf("%d%d", &p, &q); if (abs(p) >= 100 || abs(q) >= 100) { puts("0"); continue; } printf("%d\n", s[p + 100][q + 100]); } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int G[200][200]; void dfs(int x, int y) { if (G[x][y] < 4) return; int d = G[x][y] / 4; G[x][y] %= 4; G[x + 1][y] += d; dfs(x + 1, y); G[x - 1][y] += d; dfs(x - 1, y); G[x][y + 1] += d; dfs(x, y + 1); G[x][y - 1] += d; dfs(x, y - 1); } int main() { int n, t; cin >> n >> t; G[100][100] = n; dfs(100, 100); while (t--) { int x, y; cin >> x >> y; x += 100, y += 100; if (x < 0 || x >= 200 || y < 0 || y >= 200) cout << 0 << endl; else cout << G[x][y] << endl; } }
8
CPP
#include <bits/stdc++.h> using namespace std; struct c_istream { c_istream operator>>(int &n) { scanf("%i", &n); return *this; } c_istream operator>>(char &c) { scanf("%c", &c); return *this; } c_istream operator>>(double &d) { scanf("%lf", &d); return *this; } }; struct c_ostream { c_ostream operator<<(int n) { printf("%d", n); return *this; } c_ostream operator<<(char c) { printf("%c", c); return *this; } c_ostream operator<<(double d) { printf("%lf", d); return *this; } c_ostream operator<<(const char *s) { printf("%s", s); return *this; } }; int arr[200][200]; void fill(int x, int y) { if (arr[x][y] < 4) return; int cnt = arr[x][y] / 4; arr[x][y] %= 4; arr[x + 1][y] += cnt; arr[x - 1][y] += cnt; arr[x][y + 1] += cnt; arr[x][y - 1] += cnt; fill(x + 1, y); fill(x - 1, y); fill(x, y + 1); fill(x, y - 1); } int main() { int n, t; cin >> n >> t; arr[100][100] = n; fill(100, 100); for (int i = 0; i < t; i++) { int x, y; cin >> x >> y; if (x >= -100 && x < 100 && y >= -100 && y < 100) cout << arr[x + 100][y + 100] << '\n'; else cout << 0 << '\n'; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int ar[150][150]; int main() { int n, t; scanf("%d", &n); scanf("%d", &t); int f = 1; int br = 70, bc = 70; ar[br][bc] = n; int c = 0, iter = 0; int maxx, maxy; maxx = -1; maxy = -1; while (f) { c = 0; iter++; for (int(i) = (0); (i) < (141); ++(i)) { for (int(j) = (0); (j) < (141); ++(j)) { if (ar[i][j] >= 4) { c++; maxx = max(maxx, i); maxy = max(maxy, j); } int first = ar[i][j] % 4; if (i + 1 < 140) { ar[i + 1][j] += ar[i][j] / 4; } if (i - 1 >= 0) { ar[i - 1][j] += ar[i][j] / 4; } if (j + 1 < 140) { ar[i][j + 1] += ar[i][j] / 4; } if (j - 1 >= 0) { ar[i][j - 1] += ar[i][j] / 4; } ar[i][j] = ar[i][j] % 4; } } if (c == 0) break; } while (t--) { int first, second; scanf("%d", &first); scanf("%d", &second); if (first + 70 > 140 || first + 70 < 0 || second + 70 > 140 || second + 70 < 0) { printf("0\n"); continue; } printf("%d\n", ar[first + 70][second + 70]); } }
8
CPP
#include <bits/stdc++.h> using namespace std; const int fx[4] = {1, -1, 0, 0}, fy[4] = {0, 0, -1, 1}; int n, t; int a[80 * 2 + 1][80 * 2 + 1], b[80 * 2 + 1][80 * 2 + 1]; int main() { scanf("%d%d", &n, &t); a[80][80] = n; while (1) { int ok = 0; for (int i = 0; i <= 2 * 80; ++i) for (int j = 0; j <= 2 * 80; ++j) { if (a[i][j] >= 4) { for (int k = 0; k <= 3; ++k) { int x = i + fx[k], y = j + fy[k]; if (x > -1 && y > -1 && x <= 2 * 80 && y <= 2 * 80) b[x][y] += a[i][j] / 4; } ok = 1; } b[i][j] += a[i][j] % 4; } for (int i = 0; i <= 2 * 80; ++i) for (int j = 0; j <= 2 * 80; ++j) { a[i][j] = b[i][j]; b[i][j] = 0; } if (!ok) break; } while (t--) { int x, y; scanf("%d%d", &x, &y); if (abs(x) > 80 || abs(y) > 80) printf("%d\n", 0); else printf("%d\n", a[x + 80][y + 80]); } }
8
CPP
#include <bits/stdc++.h> using namespace std; int num[2005][2005]; int main() { int flat, n, q, x, y, i, j, r; cin >> n >> q; num[1000][1000] = n; r = 0; while (1) { flat = 1; for (i = 1000 - r; i <= 1000 + r; i++) for (j = 1000 - r; j <= 1000 + r; j++) { if (num[i][j] >= 4) { flat = 0; int temp = num[i][j] / 4; num[i][j] = num[i][j] - 4 * temp; num[i - 1][j] += temp; num[i + 1][j] += temp; num[i][j - 1] += temp; num[i][j + 1] += temp; if (abs(i - 1000) == r || abs(j - 1000) == r) r += 1; } } if (flat) break; } while (q--) { cin >> x >> y; if (abs(x) > r || abs(y) > r) cout << 0 << endl; else cout << num[x + 1000][y + 1000] << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> ostream& operator<<(ostream& o, const vector<T>& v); int di[] = {-1, 1, 0, 0}; int dj[] = {0, 0, -1, 1}; struct Board { int n, ants; vector<vector<int> > B, prev; Board(int ants_, int n_) { n = n_; ants = ants_; B = vector<vector<int> >(n, vector<int>(n, 0)); prev = vector<vector<int> >(n, vector<int>(n, 0)); B[0][0] = ants; } bool advance() { prev.swap(B); for (int i = 0; i < (n); i++) for (int j = 0; j < (n); j++) B[i][j] = 0; for (int i = 0; i < (n); i++) { for (int j = 0; j < (n); j++) { if (prev[i][j] >= 4) { for (int k = 0; k < (4); k++) { int i2 = i + di[k]; int j2 = j + dj[k]; if (i2 < 0 || j2 < 0) continue; B[i2][j2]++; if (i != 0 && j != 0 && (i2 == 0 || j2 == 0)) { B[i2][j2]++; } else if (i2 == 0 && j2 == 0) { B[i2][j2]++; } } B[i][j] += prev[i][j] - 4; } else { B[i][j] += prev[i][j]; } } } return B != prev; } void print() { for (int j = 0; j < (n); j++) printf("--"); printf("\n"); for (int i = 0; i < (n); i++) { bool has = false; for (int j = 0; j < (n); j++) { if (B[i][j]) has = true; } if (!has) continue; for (int j = 0; j < (n); j++) { if (B[i][j]) printf("%d ", B[i][j]); else printf(" "); } printf("\n"); } printf("\n"); } int MX() { int res = 0; for (int i = 0; i < (n); i++) { for (int j = 0; j < (n); j++) { if (B[i][j] == 0) continue; res = max(res, max(i, j)); } } return res; } }; int main() { int ants; scanf("%d", &ants); Board board(ants, 65); while (true) { if (!board.advance()) { break; } } int q; scanf("%d", &q); while (q--) { int x, y; scanf("%d %d", &x, &y); x = abs(x); y = abs(y); int res; if (x >= board.n || y >= board.n) { res = 0; } else { res = board.B[x][y]; } printf("%d\n", res); } } template <typename T> ostream& operator<<(ostream& o, const vector<T>& v) { o << "["; bool first = true; for (__typeof((v).begin()) x = (v).begin(); x != (v).end(); x++) { if (!first) { o << ", "; } o << *x; first = false; } o << "]"; return o; }
8
CPP
#include <bits/stdc++.h> using namespace std; int a[150][150], b[150][150]; int dir[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; int main() { int n, t; while (scanf("%d %d", &n, &t) != EOF) { memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); a[75][75] = n; int tag = 1; while (tag) { tag = 0; memset(b, 0, sizeof(b)); for (int i = 0; i < 150; i++) { for (int j = 0; j < 150; j++) { if (a[i][j] >= 4) { tag = 1; for (int k = 0; k < 4; k++) { b[i + dir[k][0]][j + dir[k][1]] += a[i][j] / 4; } b[i][j] += a[i][j] % 4; } else { b[i][j] += a[i][j]; } } } if (tag) memcpy(a, b, sizeof(b)); } while (t--) { int x, y; scanf("%d %d", &x, &y); x += 75; y += 75; if (x < 150 && y < 150 && x >= 0 && y >= 0) { printf("%d\n", a[x][y]); } else { puts("0"); } } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fll; const int OFF = 150, MAXC = 2 * OFF + 10; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int q[MAXC][MAXC]; bool inqueue[MAXC][MAXC]; int main() { int n, t; scanf("%d%d", &n, &t); q[OFF][OFF] = n; queue<pair<int, int> > fila; fila.push(pair<int, int>(0, 0)); inqueue[OFF][OFF] = 1; while (!fila.empty()) { pair<int, int> cur = fila.front(); fila.pop(); int x = cur.first + OFF, y = cur.second + OFF; inqueue[x][y] = 0; int add = q[x][y] / 4; q[x][y] -= 4 * add; if (add) { for (int a = 0; a < 4; ++a) { int xx = x + dx[a], yy = y + dy[a]; q[xx][yy] += add; if (q[xx][yy] >= 4 && !inqueue[xx][yy]) { fila.push(pair<int, int>(xx - OFF, yy - OFF)); inqueue[xx][yy] = 1; } } } } while (t--) { int x, y; scanf("%d%d", &x, &y); if (abs(x) > OFF || abs(y) > OFF) printf("0\n"); else printf("%d\n", q[x + OFF][y + OFF]); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7, N = 3005; long long vis[N]; vector<long long> arr[N]; map<long long, long long> m; long long a[N], col[N]; long long dx[] = {-1, 0, 1, 0}; long long dy[] = {0, -1, 0, 1}; long long tot[N][N]; long long fk = 1500; signed main() { long long n, t; cin >> n >> t; tot[fk][fk] = n; queue<pair<long long, long long>> q; q.push({fk, fk}); while (!q.empty()) { auto node = q.front(); q.pop(); long long x = node.first; long long y = node.second; long long nis = tot[x][y]; tot[x][y] = nis % 4; if (nis < 4) continue; for (long long i = 0; i < 4; i++) { long long nx = x + dx[i], ny = y + dy[i]; tot[nx][ny] += nis / 4; if (tot[nx][ny] >= 4) q.push({nx, ny}); } } while (t--) { long long x, y; cin >> x >> y; if (x + fk >= N || y + fk >= N || x + fk < 0 || y + fk < 0) cout << "0\n"; else cout << tot[x + fk][y + fk] << "\n"; } }
8
CPP
#include <bits/stdc++.h> using namespace std; int n; int t; int v[1004][1004]; int xc[5] = {0, 0, 0, -1, 1}; int yc[5] = {0, -1, 1, 0, 0}; void dfs(int x, int y, int k) { v[x][y] += k; if (v[x][y] < 4) return; v[x][y] -= 4; dfs(x, y, 0); dfs(x - 1, y, 1); dfs(x + 1, y, 1); dfs(x, y - 1, 1); dfs(x, y + 1, 1); } int main() { cin >> n >> t; memset(v, 0, sizeof v); dfs(500, 500, n); while (t--) { int x; int y; cin >> x >> y; if (x < (-500) || x > 500 || y < (-500) || y > 500) { cout << "0\n"; continue; } cout << v[x + 500][y + 500] << "\n"; } return 0; }
8
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:200000000") using namespace std; int main() { char a[400][400] = {0}, b[400][400] = {0}; int g = 1; int t; cin >> t; if (t > 100) a[200][200] = 100; else a[200][200] = t; t -= a[200][200]; b[200][200] = a[200][200]; bool ok = true; while (ok) { ok = false; for (int i = 201 - g; (i) < int(200 + g); (i)++) for (int j = 201 - g; (j) < int(200 + g); (j)++) { if (a[i][j] >= 4) { ok = true; b[i][j] -= 4; b[i][j + 1]++; b[i][j - 1]++; b[i - 1][j]++; b[i + 1][j]++; } } if (b[200][200 + g] > 0) g++; if (b[200][200] < 10) { int c; if (t > 100) c = 100; else c = t; b[200][200] += c; t -= c; } for (int i = 201 - g; (i) < int(200 + g); (i)++) for (int j = 201 - g; (j) < int(200 + g); (j)++) a[i][j] = b[i][j]; } cin >> t; int x, y; for (int i = 0; (i) < int(t); (i)++) { cin >> x >> y; if ((abs(x) >= 200) || (abs(y) >= 200)) cout << 0 << endl; else cout << (int)b[200 + y][200 + x] << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int mmax = 5000; typedef struct pnt { int x; int y; } pnt; queue<pnt> q[2]; int grap[mmax][mmax]; int main() { int n, t, origin = mmax >> 1; int slct = 0; pnt tmp, ttmp; while (!q[0].empty()) q[0].pop(); while (!q[1].empty()) q[1].pop(); cin >> n >> t; grap[origin][origin] = n; if (n >= 4) { tmp.x = origin; tmp.y = origin; q[slct].push(tmp); grap[tmp.x][tmp.y] -= 4; } while (!q[slct].empty()) { while (!q[slct].empty()) { tmp = q[slct].front(); q[slct].pop(); if (grap[tmp.x][tmp.y] >= 4) { grap[tmp.x][tmp.y] -= 4; q[slct ^ 1].push(tmp); } if (++grap[tmp.x - 1][tmp.y] >= 4) { grap[tmp.x - 1][tmp.y] -= 4; ttmp.x = tmp.x - 1; ttmp.y = tmp.y; q[slct ^ 1].push(ttmp); } if (++grap[tmp.x + 1][tmp.y] >= 4) { grap[tmp.x + 1][tmp.y] -= 4; ttmp.x = tmp.x + 1; ttmp.y = tmp.y; q[slct ^ 1].push(ttmp); } if (++grap[tmp.x][tmp.y + 1] >= 4) { grap[tmp.x][tmp.y + 1] -= 4; ttmp.x = tmp.x; ttmp.y = tmp.y + 1; q[slct ^ 1].push(ttmp); } if (++grap[tmp.x][tmp.y - 1] >= 4) { grap[tmp.x][tmp.y - 1] -= 4; ttmp.x = tmp.x; ttmp.y = tmp.y - 1; q[slct ^ 1].push(ttmp); } } slct ^= 1; } int tx, ty; for (int i = 0; i < t; i++) { cin >> tx >> ty; if ((tx + origin >= 0) && (tx + origin < mmax) && (ty + origin >= 0) && (ty + origin < mmax)) cout << grap[origin + tx][origin + ty] << endl; else cout << "0" << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; int const N = 5000 + 5, C = N / 2; int n, t, a[N][N]; inline bool in(int x, int y) { x += C, y += C; return (0 <= x && x < N && 0 <= y && y < N); } void dfs(int x, int y, int val) { a[C + x][C + y] += val; if (a[C + x][C + y] >= 4) { int tmp = a[C + x][C + y] / 4; a[C + x][C + y] %= 4; dfs(x + 1, y, tmp); dfs(x, y + 1, tmp); dfs(x - 1, y, tmp); dfs(x, y - 1, tmp); } } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; dfs(0, 0, n); cin >> t; while (t--) { int x, y; cin >> x >> y; if (!in(x, y)) cout << "0\n"; else cout << a[C + x][C + y] << '\n'; } }
8
CPP
#include <bits/stdc++.h> using namespace std; int g_array[140][140] = {0}; int g_center = 70; int main(int argc, const char* argv[]) { std::ios_base::sync_with_stdio(0); int n = 0; int t = 0; cin >> n >> t; g_array[g_center][g_center] = n; int minc = g_center - 1; int maxc = g_center + 1; while (true) { bool end = true; for (int x = minc; x < maxc; ++x) { for (int y = minc; y < maxc; ++y) { int current = g_array[x][y]; if (current < 4) continue; end = false; int mod = current % 4; int quarter = (current - mod) / 4; g_array[x + 1][y] += quarter; g_array[x - 1][y] += quarter; g_array[x][y + 1] += quarter; g_array[x][y - 1] += quarter; g_array[x][y] = mod; } } if (minc > 1) { --minc; } if (maxc < 140) { ++maxc; } if (end) { break; } } std::stringstream outStream; for (int i = 0; i < t; ++i) { int64_t a = 0; int64_t b = 0; cin >> a >> b; if (abs(a) + g_center >= 140 || abs(b) + g_center >= 140) { outStream << 0 << endl; continue; } outStream << g_array[a + g_center][b + g_center] << endl; } cout << outStream.str(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 500 + 3; const int p = 200; int a[2][N][N], n, Q, cur = 0, dir[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}}; bool vis[N][N]; bool in(int x, int y) { return 0 <= x && x <= 400 && 0 <= y && y <= 400; } struct note { int x, y, v; note() {} note(int _x, int _y, int _v) { x = _x; y = _y; v = _v; } }; int main() { scanf("%d%d", &n, &Q); memset(a, 0, sizeof a); a[cur][p][p] = n; queue<note> q; queue<pair<int, int> > qq; if (n >= 4) qq.push(make_pair(p, p)); while (1) { bool f = 0; while (!qq.empty()) { int i = qq.front().first, j = qq.front().second; qq.pop(); f = 1; a[cur ^ 1][i][j] = a[cur][i][j] % 4; q.push(note(i, j, a[cur][i][j] / 4)); a[cur][i][j] %= 4; vis[i][j] = 0; } if (!f) break; cur ^= 1; while (!q.empty()) { note u = q.front(); q.pop(); int nx, ny; for (int i = 0; i < 4; i++) { nx = u.x + dir[i][0]; ny = u.y + dir[i][1]; if (!in(nx, ny)) continue; a[cur][nx][ny] += u.v; a[cur ^ 1][nx][ny] = a[cur][nx][ny]; if (a[cur][nx][ny] >= 4) { if (!vis[nx][ny]) { vis[nx][ny] = 1; qq.push(make_pair(nx, ny)); } } } } } for (int i = 0; i < Q; i++) { int x, y; scanf("%d%d", &x, &y); x += p, y += p; if (!in(x, y)) printf("0\n"); else printf("%d\n", a[cur][x][y]); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int LEN = 131; int ants[2][LEN][LEN]; int main() { int n, t; cin >> n >> t; memset(ants, 0, sizeof(ants)); ants[0][LEN / 2][LEN / 2] = n; int d = 1; int s = 0; while (true) { bool update = false; memset(ants[d], 0, sizeof(ants[d])); for (int y = 0; y < LEN; y++) { for (int x = 0; x < LEN; x++) { if (ants[s][y][x] >= 4) { update = true; int add = ants[s][y][x] / 4; int rem = ants[s][y][x] % 4; ants[d][y][x + 1] += add; ants[d][y][x - 1] += add; ants[d][y + 1][x] += add; ants[d][y - 1][x] += add; ants[d][y][x] += rem; } else { ants[d][y][x] += ants[s][y][x]; } } } if (!update) break; swap(s, d); } int MAX_ABS = LEN / 2; for (int i = 0; i < t; i++) { int x, y; cin >> x >> y; if (abs(x) > MAX_ABS || abs(y) > MAX_ABS) { printf("%d\n", 0); } else { x += LEN / 2; y += LEN / 2; printf("%d\n", ants[s][y][x]); } } }
8
CPP
#include <bits/stdc++.h> using namespace std; int a[200][200], ans, n; bool flag = true; int main() { ios::sync_with_stdio(false); cin >> a[80][80] >> n; while (flag) { flag = false; for (int i = 1; i < 160; i++) for (int j = 1; j < 160; j++) if (a[i][j] >= 4) { flag = true; int k = a[i][j] / 4; a[i + 1][j] += k, a[i - 1][j] += k; a[i][j + 1] += k, a[i][j - 1] += k; a[i][j] -= 4 * k; } } for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; if (-80 < x && x < 80 && -80 < y && y < 80) cout << a[x + 80][y + 80] << '\n'; else cout << 0 << '\n'; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, t; int arr[2][4000][4000]; int ind0 = 0; int ind1 = 1; int a(int z, int x, int y) { return arr[z][x + 2000][y + 2000]; } void add(int z, int x, int y, int cnt) { arr[z][x + 2000][y + 2000] += cnt; } void clear(int z, int x, int y) { arr[z][x + 2000][y + 2000] = 0; } void ost(int z, int x, int y) { arr[z][x + 2000][y + 2000] &= 3; } void fun() { scanf("%d%d", &n, &t); add(0, 0, 0, n); int minim = 0; bool bo = true; while (bo) { bo = false; for (int i = -minim; i <= minim; ++i) { for (int j = -minim; j <= minim; ++j) { clear(ind1, i, j); } } for (int i = -minim; i <= minim; ++i) { for (int j = -minim; j <= minim; ++j) { if (a(ind0, i, j) >= 4) { bo = true; minim = max(minim, i + 1); int z = a(ind0, i, j) >> 2; add(ind1, i + 1, j, z); add(ind1, i - 1, j, z); add(ind1, i, j + 1, z); add(ind1, i, j - 1, z); ost(ind0, i, j); } add(ind1, i, j, a(ind0, i, j)); } } if (ind0 == 0) { ind0 = 1; ind1 = 0; } else { ind0 = 0; ind1 = 1; } } for (; t; --t) { int x, y; scanf("%d%d", &x, &y); if (abs(x) > 1000 || abs(y) > 1000) printf("0\n"); else printf("%d\n", a(ind0, x, y)); } } int main() { fun(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int ar[600][600][2] = {}; int n, q; cin >> n >> q; ar[300][300][0] = n; int l = 1; int i = 0; while (l) { l = 0; for (int x = 233; x < 367; x++) for (int y = 233; y < 367; y++) ar[x][y][!i] = ar[x][y][i]; for (int x = 233; x < 367; x++) for (int y = 233; y < 367; y++) if (ar[x][y][i] >= 4) { ar[x][y][!i] -= 4; ar[x + 1][y][!i]++; ar[x - 1][y][!i]++; ar[x][y + 1][!i]++; ar[x][y - 1][!i]++; l++; } i = !i; } for (int o = 0; o < q; o++) { int x, y; cin >> x >> y; if (x >= 100 || x <= -100 || y >= 100 || y <= -100) cout << 0 << "\n"; else cout << ar[x + 300][y + 300][i] << "\n"; } }
8
CPP
#include <bits/stdc++.h> const int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1}; struct Ac { int x, y; } q[60 * 60 * 1000 + 5]; int g[60 + 5][60 + 5], Maxl = 0; bool vst[60 + 5][60 + 5]; void Bfs() { scanf("%d", &g[0][0]); int head = 0, tail = 0; if (g[0][0] >= 4) { q[1].x = 0; q[1].y = 0; tail++; } while (head < tail) { head++; int t = g[q[head].x][q[head].y] / 4; g[q[head].x][q[head].y] %= 4; vst[q[head].x][q[head].y] = false; for (int i = 0; i < 4; i++) { int x0 = q[head].x + dx[i]; int y0 = q[head].y + dy[i]; if (Maxl < x0) Maxl = x0; if (Maxl < y0) Maxl = y0; if (x0 >= 0 && y0 >= 0) { if ((!x0 && dx[i]) || (!y0 && dy[i])) g[x0][y0] += t << 1; else g[x0][y0] += t; if (!vst[x0][y0] && g[x0][y0] >= 4) { q[++tail].x = x0; q[tail].y = y0; vst[x0][y0] = true; } } } } int m; scanf("%d", &m); for (int i = 1; i <= m; i++) { int x0, y0; scanf("%d %d", &x0, &y0); if (x0 < 0) x0 = -x0; if (y0 < 0) y0 = -y0; if (x0 > Maxl || y0 > Maxl) printf("0\n"); else printf("%d\n", g[x0][y0]); } return; } int main() { Bfs(); getchar(); getchar(); return 0; }
8
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; } template <typename T> int SIZE(const T &t) { return t.size(); } string to_string(const string s, int x1 = 0, int x2 = 1e9) { return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c) { return string({c}); } template <size_t N> string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) { string t = ""; for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1); __iii__ <= __jjj__; ++__iii__) { t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A(&v), int x1 = 0, int x2 = 1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A(&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if (l_v_l_v_l == 0) res += '\n'; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2 - x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if (e != l) { if (rnk > 1) { res += '\n'; t_a_b_s = l_v_l_v_l; }; } else { t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if (l_v_l_v_l == 0) res += '\n'; return res; } void dbgm() { ; } template <typename Heads, typename... Tails> void dbgm(Heads H, Tails... T) { cout << to_string(H) << " | "; dbgm(T...); } const long long MOD = (int)1e9 + 7; const long long INF = 2e18 + 5; struct hashLL { size_t operator()(long long x) const { x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9); x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb); x = x ^ (x >> 31); return x; } }; long long inv(long long a, long long b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; } long long gcd(long long a, long long b, long long &x, long long &y) { if (a == 0) { x = 0; y = 1; return b; } long long x1, y1; long long d = gcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } long long distsq(long long x1, long long y1, long long x2, long long y2) { return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1); } long long n, m, t, qx, qy; const int MX = 100; const int offset = 100; long long arr[2 * MX][2 * MX]; int dx[4] = {-1, 0, 0, 1}; int dy[4] = {0, -1, 1, 0}; long long mx = -1, my = -1; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> t; deque<pair<long long, long long>> frontier; set<pair<long long, long long>> added; set<pair<long long, long long>> processed; memset(arr, 0, sizeof(arr)); arr[0 + offset][0 + offset] = n; if (n >= 4) frontier.push_back(make_pair(0, 0)); while (!frontier.empty()) { long long sz = frontier.size(); for (int z = 0; z < (sz); z++) { auto pr = frontier.front(); frontier.pop_front(); long long x = pr.first; long long y = pr.second; long long curr = arr[x + offset][y + offset]; long long each = curr / 4; for (int i = 0; i < (4); i++) { long long nx = x + dx[i]; long long ny = y + dy[i]; auto np = make_pair(nx, ny); arr[nx + offset][ny + offset] += each; if (arr[nx + offset][ny + offset] - each < 4 && arr[nx + offset][ny + offset] >= 4) frontier.push_back(np); } arr[x + offset][y + offset] -= each * 4; } } for (int i = 0; i < (t); i++) { cin >> qx >> qy; if (abs(qx) <= 80 && abs(qy) <= 80) cout << arr[qx + offset][qy + offset] << '\n'; else cout << "0\n"; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; pair<int, int> d[] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; int n, t; int cnt[5005][5005]; int offs = 2505; pair<int, int> operator+(pair<int, int> a, pair<int, int> b) { return {a.first + b.first, a.second + b.second}; } int main(void) { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n >> t; cnt[offs][offs] = n; queue<pair<int, int> > todo; if (n >= 4) todo.push({0, 0}); int mx = 0; while (!todo.empty()) { auto x = todo.front(); todo.pop(); mx = max(mx, x.first); if (cnt[x.first + offs][x.second + offs] >= 4) { int add = cnt[x.first + offs][x.second + offs] / 4; cnt[x.first + offs][x.second + offs] %= 4; for (int i = 0; i < 4; ++i) { auto y = x + d[i]; cnt[y.first + offs][y.second + offs] += add; if (cnt[y.first + offs][y.second + offs] >= 4 && cnt[y.first + offs][y.second + offs] - add < 4) todo.push(y); } } } while (t--) { int x, y; cin >> x >> y; x += offs; y += offs; if (x < 0 || x > 5000 || y < 0 || y > 5000) cout << 0 << '\n'; else cout << (cnt[x][y]) << '\n'; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; bool In[140][140]; int g, x, y, T; int F[140][140]; queue<pair<int, int>> Q; int main() { cin >> F[70][70] >> T; Q.push(make_pair(70, 70)); while (!Q.empty()) { pair<int, int> p = Q.front(); Q.pop(); x = p.first; y = p.second; In[x][y] = false; g = F[x][y] / 4; F[x][y] %= 4; F[x + 1][y] += g; if (F[x + 1][y] >= 4 && !In[x + 1][y]) In[x + 1][y] = true, Q.push(make_pair(x + 1, y)); F[x - 1][y] += g; if (F[x - 1][y] >= 4 && !In[x - 1][y]) In[x - 1][y] = true, Q.push(make_pair(x - 1, y)); F[x][y + 1] += g; if (F[x][y + 1] >= 4 && !In[x][y + 1]) In[x][y + 1] = true, Q.push(make_pair(x, y + 1)); F[x][y - 1] += g; if (F[x][y - 1] >= 4 && !In[x][y - 1]) In[x][y - 1] = true, Q.push(make_pair(x, y - 1)); } while (T--) { cin >> x >> y; if (x < -70 || x >= 70 || y < -70 || y >= 70) cout << "0\n"; else cout << F[x + 70][y + 70] << "\n"; } }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 340; int cnt[340][340]; int main() { int n, t; cin >> n >> t; cnt[170][170] = n; int m = maxn; bool fin = false; while (!fin) { bool flag = false; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { if (cnt[i][j] >= 4) { int x = cnt[i][j] / 4; cnt[i - 1][j] += x, cnt[i + 1][j] += x, cnt[i][j - 1] += x, cnt[i][j + 1] += x; cnt[i][j] %= 4; flag = true; } } } if (!flag) fin = true; } for (int i = 0; i < t; i++) { int x, y; cin >> x >> y; if (x + 170 >= 0 && x + 170 < 340 && y + 170 >= 0 && y + 170 < 340) cout << cnt[x + 170][y + 170] << endl; else cout << "0" << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, t, i, ii, jj, p4; int M[2 * 500 + 5][2 * 500 + 5]; queue<pair<int, int> > q; void slv() { q.push(make_pair(500, 500)); while (!q.empty()) { ii = q.front().first; jj = q.front().second; q.pop(); p4 = M[ii][jj] / 4; if (p4 == 0) continue; M[ii - 1][jj] += p4; M[ii + 1][jj] += p4; M[ii][jj - 1] += p4; M[ii][jj + 1] += p4; M[ii][jj] -= p4 * 4; q.push(make_pair(ii - 1, jj)); q.push(make_pair(ii + 1, jj)); q.push(make_pair(ii, jj + 1)); q.push(make_pair(ii, jj - 1)); } } int main() { scanf("%d%d", &n, &t); M[500][500] = n; slv(); while (t--) { scanf("%d%d", &ii, &jj); if (ii < -500 || ii > 500 || jj < -500 || jj > 500) printf("0\n"); else printf("%d\n", M[ii + 500][jj + 500]); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { const int N = 64; int a[2 * N + 1][2 * N + 1] = {{0}}; int n, t; scanf("%d %d", &n, &t); a[N][N] = n; bool finished = false; while (!finished) { bool all_under = true; for (int i = 0; i < 2 * N + 1 && all_under; i++) { for (int j = 0; j < 2 * N + 1; j++) { if (a[i][j] >= 4) { all_under = false; break; } } } if (all_under) { finished = true; break; } int b[2 * N + 1][2 * N + 1] = {{0}}; for (int i = 0; i < 2 * N + 1; i++) { for (int j = 0; j < 2 * N + 1; j++) { int temp = a[i][j] / 4; if (a[i][j] >= 4) { b[i - 1][j] += temp; b[i + 1][j] += temp; b[i][j - 1] += temp; b[i][j + 1] += temp; a[i][j] -= 4 * temp; } } } for (int i = 0; i < 2 * N + 1; i++) { for (int j = 0; j < 2 * N + 1; j++) { a[i][j] += b[i][j]; } } } int x, y; while (t--) { scanf("%d %d", &x, &y); if (x < -N || x > N || y < -N || y > N) { printf("0\n"); } else { printf("%d\n", a[x + N][y + N]); } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:255000000") bool firstout = 1; template <class T> T &minn(T &a, T b) { if (b < a) a = b; return a; } template <class T> T &maxx(T &a, T b) { if (a < b) a = b; return a; } int &madd(int &a, int b) { a += b; if (a >= 1000000007) a -= 1000000007; return a; } int &msub(int &a, int b) { a -= b; if (a < 0) a += 1000000007; return a; } int &mmult(int &a, int b) { return a = (long long)a * b % 1000000007; } int mdiv(long long a, long long b, long long m) { a = (a % m + m) % m; b = (b % m + m) % m; if (a % b == 0) return a / b; return (a + m * mdiv(-a, m, b)) / b; } int DX[4] = {-1, 0, 0, 1}; int DY[4] = {0, -1, 1, 0}; int n, m, q; int A[1012][1012]; bool B[1012][1012]; int lq, rq; short QX[(1012 * 1012 * 20)]; short QY[(1012 * 1012 * 20)]; bool valid(int i, int j) { return 0 <= i && i < 1012 && 0 <= j && j < (1012 * 1012 * 20); } int main() { int i, j, k; char c; int a, d; int ts; for (ts = 1; scanf("%" "d", &(n)) > 0; ++ts) { for (i = (0); i < (1012); ++i) for (j = (0); j < (1012); ++j) A[i][j] = B[i][j] = 0; A[1012 / 2][1012 / 2] = n; B[1012 / 2][1012 / 2] = 1; lq = rq = 0; QX[rq] = 1012 / 2; QY[rq++] = 1012 / 2; for (; lq < rq; ++lq) { i = QX[lq]; j = QY[lq]; B[i][j] = 0; if (A[i][j] < 4) continue; for (k = (0); k < (4); ++k) { int ii = i + DX[k]; int jj = j + DY[k]; A[ii][jj] += A[i][j] / 4; if (!B[ii][jj]) { B[ii][jj] = 1; QX[rq] = ii; QY[rq++] = jj; } } A[i][j] %= 4; } scanf( "%" "d", &(q)); for (; q--;) { scanf( "%" "d", &(i)); scanf( "%" "d", &(j)); i += 1012 / 2; j += 1012 / 2; if (!valid(i, j)) printf( "%" "d", (0)); else printf( "%" "d", (A[i][j])); printf("\n"), firstout = 1; } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int cnt[1004][1004]; int vis[1004][1004]; vector<pair<int, int> > v; queue<pair<int, int> > pq; int main() { int i, n, t, x, y, cx, cy, cnum, nnum, j, cn = 0, nx, ny, k; int cenx = 500, ceny = 500; cin >> n >> t; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; cnt[cenx][ceny] = n; while (1) { int ch = 0; for (i = -70; i <= 70; i++) for (j = -70; j <= 70; j++) { cx = cenx + i; cy = ceny + j; if (cnt[cx][cy] >= 4) { int add = (cnt[cx][cy] >> 2); ch = 1; for (k = 0; k < 4; k++) { nx = cx + dx[k]; ny = cy + dy[k]; cnt[nx][ny] += add; } cnt[cx][cy] %= 4; } } if (ch == 0) break; } while (t--) { cin >> x >> y; x += cenx; y += ceny; if (x <= 0 || y <= 0 || x >= 1000 || y >= 1000) cout << 0 << endl; else cout << cnt[x][y] << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, t; int sl[2 * 100 + 20][2 * 100 + 20]; void Loang(int x, int y, int val) { sl[x][y] += val; int p = sl[x][y]; sl[x][y] %= 4; if (p < 4) return; Loang(x + 1, y, p / 4); Loang(x - 1, y, p / 4); Loang(x, y + 1, p / 4); Loang(x, y - 1, p / 4); } int main() { scanf("%d%d", &n, &t); Loang(100, 100, n); for (int i = 1; i <= t; i++) { int x, y; scanf("%d%d", &x, &y); if (abs(x) > 100 || abs(y) > 100) cout << 0 << endl; else cout << sl[x + 100][y + 100] << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; int c[1000][1000]; bool vis[1000][1000]; struct XX { int x; int y; }; queue<XX> q; int main() { int n, Q; int i, j, k; int x, y, v; XX now; while (scanf("%d%d", &n, &Q) != EOF) { memset(c, 0, sizeof(c)); memset(vis, false, sizeof(vis)); q.push({500, 500}); vis[500][500] = true; c[500][500] = n; while (!q.empty()) { now = q.front(); q.pop(); v = c[now.x][now.y] >> 2; for (i = 0; i < 4; i++) { x = now.x + dx[i]; y = now.y + dy[i]; c[x][y] += v; if (c[x][y] > 3 && !vis[x][y]) { q.push({x, y}); vis[x][y] = true; } } c[now.x][now.y] %= 4; vis[now.x][now.y] = false; } while (Q--) { scanf("%d%d", &x, &y); x = abs(x); y = abs(y); if (x > 500 || y > 500) puts("0"); else printf("%d\n", c[x + 500][y + 500]); } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int Maxn = 5000 + 7, Maxx = 2500 + 7; int n, m, ans[Maxn][Maxn]; void find(int x, int y, int t) { ans[x][y] += t; if ((ans[x][y] / 4) == 0) return; int tmp = ans[x][y] / 4; ans[x][y] %= 4; find(x + 1, y, tmp); find(x, y + 1, tmp); find(x - 1, y, tmp); find(x, y - 1, tmp); } int main() { cin >> n >> m; find(Maxx, Maxx, n); for (int i = 0, a, b; i < m; i++) { cin >> a >> b; if (a + Maxx > 5000 || a + Maxx < 0 || b + Maxx > 5000 || b + Maxx < 0) cout << 0 << endl; else cout << ans[a + Maxx][b + Maxx] << endl; } }
8
CPP