forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
146 lines (129 loc) · 4.05 KB
/
C.cpp
File metadata and controls
146 lines (129 loc) · 4.05 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(x) ((int) (x).size())
using namespace std;
int maxVal, n, targetAdd;
ll target;
char op;
pair<int,int> a[111];
bool good[11][11], visited[11][11];
int qu[111], qv[111], first, last;
int res = 0;
const int di[4] = {-1,1,0,0};
const int dj[4] = {0,0,-1,1};
bool usedRow[11][11], usedCol[11][11];
void attemptAdd(int i, int cur) {
if (i > n) {
++res;
return ;
}
int u = qu[i], v = qv[i];
if (i == n) {
int need = targetAdd - cur;
if (need > 0 && need <= maxVal
&& !usedRow[u][need]
&& !usedCol[v][need]) {
++res;
}
return ;
}
FOR(val,1,maxVal) {
if (!usedRow[u][val] && !usedCol[v][val]) {
if (cur + val > targetAdd) continue;
int left = n - i;
if (cur + val + left > targetAdd) continue;
if (cur + val + left*9 < targetAdd) continue;
usedRow[u][val] = true;
usedCol[v][val] = true;
attemptAdd(i+1, val + cur);
usedRow[u][val] = false;
usedCol[v][val] = false;
}
}
}
void attemptMult(int i, ll cur) {
if (i > n) {
if (cur == target) ++res;
return ;
}
int u = qu[i], v = qv[i];
if (i == n) {
if (target % cur) return ;
int need = target / cur;
if (need > 0 && need <= maxVal
&& !usedRow[u][need]
&& !usedCol[v][need]) {
++res;
}
return ;
}
FOR(val,1,maxVal) {
if (!usedRow[u][val] && !usedCol[v][val]) {
if (target % (cur * val)) continue;
usedRow[u][val] = true;
usedCol[v][val] = true;
attemptMult(i+1, (op == '+') ? (val + cur) : (val * cur));
usedRow[u][val] = false;
usedCol[v][val] = false;
}
}
}
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
while (cin >> maxVal >> n >> target >> op) {
memset(good, 0, sizeof good);
FOR(i,1,n) {
cin >> a[i].first >> a[i].second;
good[a[i].first][a[i].second] = true;
}
random_shuffle(a+1, a+n+1);
if (op == '-') {
int res = 0;
FOR(i,1,maxVal) FOR(j,1,maxVal)
if (i != j)
if (i - j == target) res += 2;
cout << res << endl;
continue;
}
if (op == '/') {
int res = 0;
FOR(i,1,maxVal) FOR(j,1,maxVal)
if (i != j)
if (i % j == 0 && i / j == target) res += 2;
cout << res << endl;
continue;
}
first = last = 1;
qu[1] = a[1].first;
qv[1] = a[1].second;
memset(visited, 0, sizeof visited);
visited[a[1].first][a[1].second] = 1;
while (first <= last) {
int u = qu[first], v = qv[first]; ++first;
REP(dir,4) {
int uu = u + di[dir], vv = v + dj[dir];
if (uu < 1 || uu > maxVal || vv < 1 || vv > maxVal) continue;
if (!good[uu][vv]) continue;
if (!visited[uu][vv]) {
visited[uu][vv] = 1;
++last;
qu[last] = uu; qv[last] = vv;
}
}
}
res = 0;
if (op == '+') {
targetAdd = target;
attemptAdd(1, 0);
}
if (op == '*') attemptMult(1, 1LL);
cout << res << endl;
}
}