-
Notifications
You must be signed in to change notification settings - Fork 0
/
CHEFHACK.cpp
executable file
·132 lines (129 loc) · 5.04 KB
/
CHEFHACK.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <cmath>
#define PREPRIME 10000000
using namespace std;
int main(){
vector<long long int> prime(PREPRIME, 1);
vector<long long int> primePass;
long long int count = 1;
for(long long int i=2; i<=3162; i++){
if(prime[i] > 0){
prime[i] = count;
count++;
primePass.push_back(i);
for(long long int j=i+i; j<PREPRIME; j+=i)
prime[j] = 0;
}
}
for(long long int i=3163; i<PREPRIME; i++){
if(prime[i] > 0){
prime[i] = count;
count++;
}
}
int T;
scanf("%d", &T);
prime[0] = 0;
prime[1] = 0;
while(T--){
int N;
scanf("%d",&N);
vector< vector<long long int> > server(N, vector<long long int>(N));
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
scanf("%lld",&server[i][j]);
}
}
long long int unsuccess = 0;
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
if(server[i][j] == -1)
continue;
else if(prime[server[i][j]] >= 1){
unsuccess = unsuccess + prime[server[i][j]] - 1;
continue;
}
else if(server[i][j]%2 == 0){
unsuccess = unsuccess + server[i][j]/2;
queue<int> hackX;
queue<int> hackY;
hackX.push(i);
hackY.push(j);
while(!hackX.empty()){
if(hackY.front() > 0 && server[hackX.front()][hackY.front()-1] != -1 && server[hackX.front()][hackY.front()-1]%2 == 0){
if(server[hackX.front()][hackY.front()-1] != 2){
hackX.push(hackX.front());
hackY.push(hackY.front()-1);
}
}
if(hackX.front() < N-1 && server[hackX.front()+1][hackY.front()] != -1 && server[hackX.front()+1][hackY.front()]%2 == 0){
if(server[hackX.front()+1][hackY.front()] != 2){
hackX.push(hackX.front()+1);
hackY.push(hackY.front());
}
}
if(hackY.front() < N-1 && server[hackX.front()][hackY.front()+1] != -1 && server[hackX.front()][hackY.front()+1]%2 == 0){
if(server[hackX.front()][hackY.front()+1] != 2){
hackX.push(hackX.front());
hackY.push(hackY.front()+1);
}
}
if(hackX.front() > 0 && server[hackX.front()-1][hackY.front()] != -1 && server[hackX.front()-1][hackY.front()]%2 == 0){
if(server[hackX.front()-1][hackY.front()] != 2){
hackX.push(hackX.front()-1);
hackY.push(hackY.front());
}
}
server[hackX.front()][hackY.front()] = -1;
hackX.pop();
hackY.pop();
}
continue;
}
else if(server[i][j]%2 == 1){
unsuccess = unsuccess + (server[i][j]+1)/2 + 1;
queue<int> hackX;
queue<int> hackY;
hackX.push(i);
hackY.push(j);
while(!hackX.empty()){
if(hackY.front() > 0 && server[hackX.front()][hackY.front()-1] != -1 && server[hackX.front()][hackY.front()-1]%2 == 1){
if(prime[server[hackX.front()][hackY.front()-1]] == 0){
hackX.push(hackX.front());
hackY.push(hackY.front()-1);
}
}
if(hackX.front() < N-1 && server[hackX.front()+1][hackY.front()] != -1 && server[hackX.front()+1][hackY.front()]%2 == 1){
if(prime[server[hackX.front()+1][hackY.front()]] == 0){
hackX.push(hackX.front()+1);
hackY.push(hackY.front());
}
}
if(hackY.front() < N-1 && server[hackX.front()][hackY.front()+1] != -1 && server[hackX.front()][hackY.front()+1]%2 == 1){
if(prime[server[hackX.front()][hackY.front()+1]] == 0){
hackX.push(hackX.front());
hackY.push(hackY.front()+1);
}
}
if(hackX.front() > 0 && server[hackX.front()-1][hackY.front()] != -1 && server[hackX.front()-1][hackY.front()]%2 == 1){
if(prime[server[hackX.front()-1][hackY.front()]] == 0){
hackX.push(hackX.front()-1);
hackY.push(hackY.front());
}
}
server[hackX.front()][hackY.front()] = -1;
hackX.pop();
hackY.pop();
}
continue;
}
}
}
cout<<unsuccess<<"\n";
}
return 0;
}