fork download
  1. #include <queue>
  2. #include <iostream>
  3. using namespace std;
  4. int dir[4] = { 0, 1, 0, -1 };
  5. long long n; int m, q, a[700009]; bool use[200009][7], vis[200009][7];
  6. void dfs(int x, int y, int l) {
  7. vis[x][y] = true;
  8. for (int i = 0; i < 4; i++) {
  9. int tx = x + dir[i], ty = y + dir[i ^ 1];
  10. if (0 <= tx && tx < l && 0 <= ty && ty < 7 && !use[tx][ty] && !vis[tx][ty]) {
  11. dfs(tx, ty, l);
  12. }
  13. }
  14. }
  15. int solve(int l) {
  16. for (int i = 0; i < m / 7 * 2; i++) {
  17. for (int j = 0; j < 7; j++) vis[i][j] = false;
  18. }
  19. int ret = 0;
  20. for (int i = 0; i < l; i++) {
  21. for (int j = 0; j < 7; j++) {
  22. if (!use[i][j] && !vis[i][j]) dfs(i, j, l), ret++;
  23. }
  24. }
  25. return ret;
  26. }
  27. int main() {
  28. cin >> n >> m >> q;
  29. for (int i = 0; i < q; i++) cin >> a[i];
  30. if (m % 7 != 0) {
  31. for (int i = 0; i < q * 6; i++) a[i + q] = a[i] + m;
  32. m *= 7; q *= 7;
  33. }
  34. for (int i = 0; i < q; i++) use[a[i] / 7][a[i] % 7] = use[(m + a[i]) / 7][a[i] % 7] = true;
  35. long long rep = n * 7 / m;
  36. int r1 = solve(m / 7), r2 = solve(m / 7 * 2);
  37. cout << (2 * r1 - r2) + (r2 - r1) * rep << endl;
  38. return 0;
  39. }
Success #stdin #stdout 0s 21536KB
stdin
10 14 8
5 6 7 8 9 10 11 12
stdout
10