problem_id
stringlengths 6
6
| language
stringclasses 2
values | original_status
stringclasses 3
values | original_src
stringlengths 19
243k
| changed_src
stringlengths 19
243k
| change
stringclasses 3
values | i1
int64 0
8.44k
| i2
int64 0
8.44k
| j1
int64 0
8.44k
| j2
int64 0
8.44k
| error
stringclasses 270
values | stderr
stringlengths 0
226k
|
---|---|---|---|---|---|---|---|---|---|---|---|
p00828 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==
typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;
// →↑←↓
const int dx[] = {
-1, -1, -1, 0, 0, 1, 1, 1, -1, -1, -1, 0, 0,
0, 1, 1, 1, -1, -1, -1, 0, 0, 0, 1, 1, 1,
};
const int dy[] = {
-1, 0, 1, -1, 1, -1, 0, 1, -1, 0, 1, -1, 0,
1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1,
};
const int dz[] = {
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
const double EPS = 1e-9;
int main() {
while (true) {
int n, m, p;
cin >> n >> m >> p;
if (n == 0)
break;
vector<vector<vector<char>>> F(
n, vector<vector<char>>(n, vector<char>(n, '.')));
bool win = false;
bool black = true;
int turn;
bool end = false;
for (int t = 0; t < p; t++) {
int x, y;
cin >> x >> y;
x--;
y--;
if (end)
continue;
for (int h = 0; h < n; h++) {
cerr << x << " " << y << " " << h << endl;
if (F[x][y][h] == '.') {
if (t % 2 == 0) {
F[x][y][h] = 'b';
} else {
F[x][y][h] = 'w';
}
break;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
for (int l = 0; l < 26; l++) {
if (F[i][j][k] == '.')
continue;
int ddx = dx[l];
int ddy = dy[l];
int ddz = dz[l];
int nx = i;
int ny = j;
int nz = k;
bool ok = true;
for (int o = 0; o < m; o++) {
if (not(0 <= nx and nx < n and 0 <= ny and ny < n and
0 <= nz and nz < n and F[nx][ny][nz] == F[i][j][k])) {
ok = false;
}
nx += ddx;
ny += ddy;
nz += ddz;
}
if (ok) {
turn = t + 1;
win = true;
black = turn % 2;
end = true;
}
}
}
}
}
}
if (win) {
if (black) {
cout << "Black";
} else {
cout << "White";
}
cout << " " << turn << endl;
} else {
cout << "Draw" << endl;
}
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==
typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;
// →↑←↓
const int dx[] = {
-1, -1, -1, 0, 0, 1, 1, 1, -1, -1, -1, 0, 0,
0, 1, 1, 1, -1, -1, -1, 0, 0, 0, 1, 1, 1,
};
const int dy[] = {
-1, 0, 1, -1, 1, -1, 0, 1, -1, 0, 1, -1, 0,
1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1,
};
const int dz[] = {
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
const double EPS = 1e-9;
int main() {
while (true) {
int n, m, p;
cin >> n >> m >> p;
if (n == 0)
break;
vector<vector<vector<char>>> F(
n, vector<vector<char>>(n, vector<char>(n, '.')));
bool win = false;
bool black = true;
int turn;
bool end = false;
for (int t = 0; t < p; t++) {
int x, y;
cin >> x >> y;
x--;
y--;
if (end)
continue;
for (int h = 0; h < n; h++) {
if (F[x][y][h] == '.') {
if (t % 2 == 0) {
F[x][y][h] = 'b';
} else {
F[x][y][h] = 'w';
}
break;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
for (int l = 0; l < 26; l++) {
if (F[i][j][k] == '.')
continue;
int ddx = dx[l];
int ddy = dy[l];
int ddz = dz[l];
int nx = i;
int ny = j;
int nz = k;
bool ok = true;
for (int o = 0; o < m; o++) {
if (not(0 <= nx and nx < n and 0 <= ny and ny < n and
0 <= nz and nz < n and F[nx][ny][nz] == F[i][j][k])) {
ok = false;
}
nx += ddx;
ny += ddy;
nz += ddz;
}
if (ok) {
turn = t + 1;
win = true;
black = turn % 2;
end = true;
}
}
}
}
}
}
if (win) {
if (black) {
cout << "Black";
} else {
cout << "White";
}
cout << " " << turn << endl;
} else {
cout << "Draw" << endl;
}
}
return 0;
} | delete | 65 | 66 | 65 | 65 | 0 | 0 0 0
0 0 0
0 0 1
0 0 0
0 0 1
0 0 2
1 1 0
0 2 0
0 0 0
1 2 0
1 0 0
2 2 0
0 0 0
1 1 0
0 0 0
0 0 1
2 2 0
2 2 0
2 2 1
0 0 0
0 0 1
0 0 2
2 2 0
2 2 1
2 2 2
2 2 0
2 2 1
2 2 2
2 2 3
3 3 0
0 0 0
0 0 1
0 0 2
0 0 3
3 3 0
3 3 1
3 3 0
3 3 1
3 3 2
3 3 0
3 3 1
3 3 2
3 3 3
3 0 0
1 1 0
1 1 1
|
p00828 | C++ | Runtime Error | // Enjoy your stay.
#include <bits/stdc++.h>
#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X) for (auto it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())
using namespace std;
typedef istringstream iss;
typedef long long ll;
typedef pair<ll, ll> pi;
typedef stringstream sst;
typedef vector<ll> vi;
int a[8][8][8], n, m, p;
int y[300], x[300];
int dz[13] = {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1};
int dy[] = {0, 1, 0, 1, 1, 0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, 0, 0, 1, -1, 1, -1, 0, 0, 1, -1, 1, -1};
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
while (cin >> n >> m >> p && n) {
memset(a, -1, sizeof(a));
rep(i, p) {
cin >> y[i] >> x[i];
y[i]--;
x[i]--;
}
rep(i, p) {
int z = 0;
while (a[z][y[i]][x[i]] != -1)
z++;
a[z][y[i]][x[i]] = i % 2;
rep(sz, n) rep(sy, n) rep(sx, n) rep(d, 13) {
int z = sz, y = sy, x = sx, ok = 1;
rep(j, m) {
if (z >= n || y < 0 || y >= n || x < 0 || x >= n ||
a[z][y][x] != i % 2) {
ok = 0;
break;
}
z += dz[d], y += dy[d], x += dx[d];
}
if (ok) {
cout << (i % 2 == 0 ? "Black" : "White") << " " << i + 1 << endl;
goto end;
}
}
}
cout << "Draw" << endl;
end:;
}
} | // Enjoy your stay.
#include <bits/stdc++.h>
#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X) for (auto it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())
using namespace std;
typedef istringstream iss;
typedef long long ll;
typedef pair<ll, ll> pi;
typedef stringstream sst;
typedef vector<ll> vi;
int a[8][8][8], n, m, p;
int y[400], x[400];
int dz[13] = {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1};
int dy[] = {0, 1, 0, 1, 1, 0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, 0, 0, 1, -1, 1, -1, 0, 0, 1, -1, 1, -1};
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
while (cin >> n >> m >> p && n) {
memset(a, -1, sizeof(a));
rep(i, p) {
cin >> y[i] >> x[i];
y[i]--;
x[i]--;
}
rep(i, p) {
int z = 0;
while (a[z][y[i]][x[i]] != -1)
z++;
a[z][y[i]][x[i]] = i % 2;
rep(sz, n) rep(sy, n) rep(sx, n) rep(d, 13) {
int z = sz, y = sy, x = sx, ok = 1;
rep(j, m) {
if (z >= n || y < 0 || y >= n || x < 0 || x >= n ||
a[z][y][x] != i % 2) {
ok = 0;
break;
}
z += dz[d], y += dy[d], x += dx[d];
}
if (ok) {
cout << (i % 2 == 0 ? "Black" : "White") << " " << i + 1 << endl;
goto end;
}
}
}
cout << "Draw" << endl;
end:;
}
} | replace | 27 | 28 | 27 | 28 | 0 | |
p00828 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;
using P = pair<ll, ll>;
using namespace std;
template <class t> void vin(vector<t> &v, int n) {
v.resize(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
}
}
int n, m, p;
int b[10][10][60];
int dx[] = {1, 0, -1, 0};
int dy[] = {1, 0, -1, 0};
int dz[] = {1, 0, -1, 0};
bool ok(int x, int y, int z) {
return 0 <= x and x < n and 0 <= y and y < n and 0 <= z and z < 60;
}
int check(int x, int y, int z, int col, int DX, int DY, int DZ) {
int ret = 0;
if (DX == -1) {
rep(aa, 3) rep(bb, 3) rep(cc, 3) {
if (aa == bb and bb == cc and aa == 1)
continue;
int nx = x + dx[aa], ny = y + dy[bb], nz = z + dz[cc];
int nx2 = x + dx[(aa + 2) % 4], ny2 = y + dy[(bb + 2) % 4],
nz2 = z + dz[(cc + 2) % 4];
int tmp = 1;
if (ok(nx, ny, nz) and b[nx][ny][nz] == col) {
tmp += check(nx, ny, nz, col, aa, bb, cc) + 1;
}
if (ok(nx2, ny2, nz2) and b[nx2][ny2][nz2] == col) {
tmp += check(nx2, ny2, nz2, col, (aa + 2) % 4, (bb + 2) % 4,
(cc + 2) % 4) +
1;
}
ret = max(ret, tmp);
}
} else {
int nx = x + dx[DX], ny = y + dy[DY], nz = z + dz[DZ];
if (!ok(nx, ny, nz))
return 0;
else if (b[nx][ny][nz] == col) {
return check(nx, ny, nz, col, DX, DY, DZ) + 1;
}
}
return ret;
}
int main() {
while (cin >> n >> m >> p and (n or m or p)) {
rep(x, 10) rep(y, 10) rep(z, 60) b[x][y][z] = -1;
bool end = false;
rep(i, p) {
int x, y;
cin >> x >> y;
x--;
y--;
for (int z = 0; z < 10; ++z) {
if (b[x][y][z] == -1) {
b[x][y][z] = i % 2;
int flag = check(x, y, z, i % 2, -1, -1, -1);
cerr << flag << endl;
if (!end and flag >= m) {
cout << (i % 2 ? "White" : "Black") << ' ' << i + 1 << endl;
end = true;
}
break;
}
}
}
if (!end) {
cout << "Draw" << endl;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;
using P = pair<ll, ll>;
using namespace std;
template <class t> void vin(vector<t> &v, int n) {
v.resize(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
}
}
int n, m, p;
int b[10][10][60];
int dx[] = {1, 0, -1, 0};
int dy[] = {1, 0, -1, 0};
int dz[] = {1, 0, -1, 0};
bool ok(int x, int y, int z) {
return 0 <= x and x < n and 0 <= y and y < n and 0 <= z and z < 60;
}
int check(int x, int y, int z, int col, int DX, int DY, int DZ) {
int ret = 0;
if (DX == -1) {
rep(aa, 3) rep(bb, 3) rep(cc, 3) {
if (aa == bb and bb == cc and aa == 1)
continue;
int nx = x + dx[aa], ny = y + dy[bb], nz = z + dz[cc];
int nx2 = x + dx[(aa + 2) % 4], ny2 = y + dy[(bb + 2) % 4],
nz2 = z + dz[(cc + 2) % 4];
int tmp = 1;
if (ok(nx, ny, nz) and b[nx][ny][nz] == col) {
tmp += check(nx, ny, nz, col, aa, bb, cc) + 1;
}
if (ok(nx2, ny2, nz2) and b[nx2][ny2][nz2] == col) {
tmp += check(nx2, ny2, nz2, col, (aa + 2) % 4, (bb + 2) % 4,
(cc + 2) % 4) +
1;
}
ret = max(ret, tmp);
}
} else {
int nx = x + dx[DX], ny = y + dy[DY], nz = z + dz[DZ];
if (!ok(nx, ny, nz))
return 0;
else if (b[nx][ny][nz] == col) {
return check(nx, ny, nz, col, DX, DY, DZ) + 1;
}
}
return ret;
}
int main() {
while (cin >> n >> m >> p and (n or m or p)) {
rep(x, 10) rep(y, 10) rep(z, 60) b[x][y][z] = -1;
bool end = false;
rep(i, p) {
int x, y;
cin >> x >> y;
x--;
y--;
for (int z = 0; z < 10; ++z) {
if (b[x][y][z] == -1) {
b[x][y][z] = i % 2;
int flag = check(x, y, z, i % 2, -1, -1, -1);
if (!end and flag >= m) {
cout << (i % 2 ? "White" : "Black") << ' ' << i + 1 << endl;
end = true;
}
break;
}
}
}
if (!end) {
cout << "Draw" << endl;
}
}
}
| delete | 69 | 70 | 69 | 69 | 0 | 1
1
1
1
1
2
2
2
3
3
1
1
2
2
1
1
2
1
2
2
2
2
2
1
4
|
p00828 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int n, m, p;
int x[100], y[100];
int pegs[10][10][10];
int z_idx[10][10];
int dx[] = {0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int dy[] = {0, -1, -1, -1, -1, -1, -1, 0, 0, 0, 1, 1, 1};
int dz[] = {-1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1};
bool sequence(int x, int y, int z) {
for (int i = 0; i < 13; i++) {
int seq = 1;
int nx = x, ny = y, nz = z;
while (1) {
nx += dx[i], ny += dy[i], nz += dz[i];
if (nx < 0 || n <= nx || ny < 0 || n <= ny || nz < 0 || n <= nz)
break;
if (pegs[nx][ny][nz] != pegs[x][y][z])
break;
seq++;
}
nx = x, ny = y, nz = z;
while (1) {
nx -= dx[i], ny -= dy[i], nz -= dz[i];
if (nx < 0 || n <= nx || ny < 0 || n <= ny || nz < 0 || n <= nz)
break;
if (pegs[nx][ny][nz] != pegs[x][y][z])
break;
seq++;
}
if (seq >= m)
return true;
}
return false;
}
int simulation(int t) {
if (t == p)
return -1;
pegs[x[t]][y[t]][z_idx[x[t]][y[t]]] = t % 2;
if (sequence(x[t], y[t], z_idx[x[t]][y[t]]))
return t;
z_idx[x[t]][y[t]]++;
simulation(t + 1);
}
int main() {
while (cin >> n >> m >> p, n || m || p) {
for (int i = 0; i < p; i++)
cin >> x[i] >> y[i], x[i]--, y[i]--;
memset(pegs, -1, sizeof(pegs));
memset(z_idx, 0, sizeof(z_idx));
int ans = simulation(0);
if (ans == -1)
cout << "Draw" << endl;
if (ans % 2 == 0)
cout << "Black " << ans + 1 << endl;
if (ans % 2 == 1)
cout << "White " << ans + 1 << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int n, m, p;
int x[500], y[500];
int pegs[10][10][10];
int z_idx[10][10];
int dx[] = {0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int dy[] = {0, -1, -1, -1, -1, -1, -1, 0, 0, 0, 1, 1, 1};
int dz[] = {-1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1};
bool sequence(int x, int y, int z) {
for (int i = 0; i < 13; i++) {
int seq = 1;
int nx = x, ny = y, nz = z;
while (1) {
nx += dx[i], ny += dy[i], nz += dz[i];
if (nx < 0 || n <= nx || ny < 0 || n <= ny || nz < 0 || n <= nz)
break;
if (pegs[nx][ny][nz] != pegs[x][y][z])
break;
seq++;
}
nx = x, ny = y, nz = z;
while (1) {
nx -= dx[i], ny -= dy[i], nz -= dz[i];
if (nx < 0 || n <= nx || ny < 0 || n <= ny || nz < 0 || n <= nz)
break;
if (pegs[nx][ny][nz] != pegs[x][y][z])
break;
seq++;
}
if (seq >= m)
return true;
}
return false;
}
int simulation(int t) {
if (t == p)
return -1;
pegs[x[t]][y[t]][z_idx[x[t]][y[t]]] = t % 2;
if (sequence(x[t], y[t], z_idx[x[t]][y[t]]))
return t;
z_idx[x[t]][y[t]]++;
simulation(t + 1);
}
int main() {
while (cin >> n >> m >> p, n || m || p) {
for (int i = 0; i < p; i++)
cin >> x[i] >> y[i], x[i]--, y[i]--;
memset(pegs, -1, sizeof(pegs));
memset(z_idx, 0, sizeof(z_idx));
int ans = simulation(0);
if (ans == -1)
cout << "Draw" << endl;
if (ans % 2 == 0)
cout << "Black " << ans + 1 << endl;
if (ans % 2 == 1)
cout << "White " << ans + 1 << endl;
}
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p00830 | C++ | Runtime Error | #include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define rep(i, j) for (int i = 0; i < j; i++)
#define all(i) i.begin(), i.end()
#define clr(n) memset(n, 0, sizeof(n))
#define ll long long
using namespace std;
#define double long double
vector<string> split(string s) {
vector<string> r;
string n;
s += '/';
rep(i, s.size()) {
if (s[i] == '/') {
r.push_back(n);
n.clear();
} else {
n.push_back(s[i]);
}
}
if (r.back().size() == 0) {
r[r.size() - 1] = "index.html";
}
return r;
}
#include <functional>
int main() {
int n, m;
while (cin >> n >> m && n) {
vector<map<string, int>> mp;
vector<map<string, int>> fs;
vector<int> p;
int count = 0;
function<void(int, vector<string>)> make = [&](int now, vector<string> vs) {
string s = vs[0];
if (vs.size() == 1) {
fs[now][s] = count++;
return;
}
if (mp[now].count(s) == false) {
mp[now][s] = mp.size();
mp.push_back(map<string, int>());
fs.push_back(map<string, int>());
p.push_back(now);
}
vs.erase(vs.begin());
make(mp[now][s], vs);
};
function<int(int, vector<string>, int)> search =
[&](int now, vector<string> vs, int f) {
if (vs[0] == "..") {
vs.erase(vs.begin());
if (p[now] == 0)
return -1;
return search(p[now], vs, f);
}
if (vs[0] == ".") {
vs.erase(vs.begin());
return search(now, vs, f);
}
string s = vs[0];
if (vs.size() == 1) {
if (fs[now].count(s)) {
return fs[now][s];
} else {
return -1;
}
}
if (mp[now].count(s) == false) {
return -1;
}
vs.erase(vs.begin());
return search(mp[now][s], vs, f);
};
mp.push_back(map<string, int>());
fs.push_back(map<string, int>());
p.push_back(-1);
rep(i, n) {
string s;
cin >> s;
auto vs = split(s);
make(0, vs);
}
rep(i, m) {
string a, b;
cin >> a >> b;
int ta = search(0, split(a), 0);
int tb = search(0, split(b), 0);
if (ta == -1) {
a += "/index.html";
ta = search(0, split(a), 0);
}
if (tb == -1) {
b += "/index.html";
tb = search(0, split(b), 0);
}
if (ta == -1 || tb == -1) {
cout << "not found" << endl;
continue;
}
if (ta == tb) {
cout << "yes" << endl;
continue;
}
cout << "no" << endl;
}
}
return 0;
} | #include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define rep(i, j) for (int i = 0; i < j; i++)
#define all(i) i.begin(), i.end()
#define clr(n) memset(n, 0, sizeof(n))
#define ll long long
using namespace std;
#define double long double
vector<string> split(string s) {
vector<string> r;
string n;
s += '/';
rep(i, s.size()) {
if (s[i] == '/') {
r.push_back(n);
n.clear();
} else {
n.push_back(s[i]);
}
}
if (r.back().size() == 0) {
r[r.size() - 1] = "index.html";
}
return r;
}
#include <functional>
int main() {
int n, m;
while (cin >> n >> m && n) {
vector<map<string, int>> mp;
vector<map<string, int>> fs;
vector<int> p;
int count = 0;
function<void(int, vector<string>)> make = [&](int now, vector<string> vs) {
string s = vs[0];
if (vs.size() == 1) {
fs[now][s] = count++;
return;
}
if (mp[now].count(s) == false) {
mp[now][s] = mp.size();
mp.push_back(map<string, int>());
fs.push_back(map<string, int>());
p.push_back(now);
}
vs.erase(vs.begin());
make(mp[now][s], vs);
};
function<int(int, vector<string>, int)> search =
[&](int now, vector<string> vs, int f) {
if (vs.size() == 0)
return -1;
if (vs[0] == "..") {
vs.erase(vs.begin());
if (p[now] == 0)
return -1;
return search(p[now], vs, f);
}
if (vs[0] == ".") {
vs.erase(vs.begin());
return search(now, vs, f);
}
string s = vs[0];
if (vs.size() == 1) {
if (fs[now].count(s)) {
return fs[now][s];
} else {
return -1;
}
}
if (mp[now].count(s) == false) {
return -1;
}
vs.erase(vs.begin());
return search(mp[now][s], vs, f);
};
mp.push_back(map<string, int>());
fs.push_back(map<string, int>());
p.push_back(-1);
rep(i, n) {
string s;
cin >> s;
auto vs = split(s);
make(0, vs);
}
rep(i, m) {
string a, b;
cin >> a >> b;
int ta = search(0, split(a), 0);
int tb = search(0, split(b), 0);
if (ta == -1) {
a += "/index.html";
ta = search(0, split(a), 0);
}
if (tb == -1) {
b += "/index.html";
tb = search(0, split(b), 0);
}
if (ta == -1 || tb == -1) {
cout << "not found" << endl;
continue;
}
if (ta == tb) {
cout << "yes" << endl;
continue;
}
cout << "no" << endl;
}
}
return 0;
} | insert | 67 | 67 | 67 | 69 | 0 | |
p00832 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int dx[] = {1, -1, 0, 0, 0, 0};
const int dy[] = {0, 0, 1, -1, 0, 0};
const int dz[] = {0, 0, 0, 0, 1, -1};
const int side[6][4] = {{2, 4, 5, 3}, {1, 3, 6, 4}, {1, 5, 6, 2},
{1, 2, 6, 5}, {1, 4, 6, 3}, {2, 3, 5, 4}};
const int dice_right[6][6] = {{0, 3, 5, 2, 4, 0}, {4, 0, 1, 6, 0, 3},
{2, 6, 0, 0, 1, 5}, {5, 1, 0, 0, 6, 2},
{3, 0, 6, 1, 0, 4}, {0, 4, 2, 5, 3, 0}};
struct UnionFind {
vector<int> v;
UnionFind(int n) : v(n) {
for (int i = 0; i < n; i++)
v[i] = i;
}
int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
void unite(int x, int y) { v[find(x)] = find(y); }
};
vi ans;
vector<vvi> dices(3, vvi(3, vi(3))); // men tate yoko
bool kakutei() {
bool change = true;
while (change) {
change = false;
REP(i, 3) {
REP(j, 3) {
if (dices[0][i][j] == 0) {
REP(k, 3) {
if (dices[1][k][j] > 0 && dices[2][k][2 - i] > 0) {
dices[0][i][j] =
dice_right[dices[1][k][j] - 1][dices[2][k][2 - i] - 1];
if (dices[0][i][j] == 0)
return false;
change = true;
}
}
}
if (dices[1][i][j] == 0) {
REP(k, 3) {
if (dices[0][k][j] > 0 && dices[2][i][2 - k] > 0) {
dices[1][i][j] =
dice_right[dices[2][i][2 - k] - 1][dices[0][k][j] - 1];
if (dices[1][i][j] == 0)
return false;
change = true;
}
}
}
if (dices[2][i][j] == -1) {
REP(k, 3) {
if (dices[0][2 - j][k] > 0 && dices[1][i][k] > 0) {
dices[2][i][j] =
dice_right[dices[0][2 - j][k] - 1][dices[1][i][k] - 1];
if (dices[2][i][j] == 0)
return false;
change = true;
}
}
}
}
}
}
return true;
}
void dfs() {
vector<vvi> copy_dices = dices;
REP(i, 3) {
REP(j, 3) {
if (dices[0][i][j] == 0) {
REP(k, 6) {
bool clear = true;
REP(l, 3) {
if (dices[1][l][j] > 0)
if (dices[1][l][j] == k + 1 || dices[1][l][j] + k + 1 == 7)
clear = false;
if (dices[2][l][2 - i] > 0)
if (dices[2][l][2 - i] == k + 1 ||
dices[2][l][2 - i] + k + 1 == 7)
clear = false;
}
if (clear) {
dices[0][i][j] = k + 1;
if (!kakutei()) {
dices = copy_dices;
continue;
}
int ex[3] = {0};
REP(l, 3) {
REP(x, 3) {
REP(y, 3) {
if (dices[l][x][y] <= 0)
continue;
int a = dices[l][x][y];
if (a == 4)
a = 3;
if (a == 5)
a = 2;
if (a == 6)
a = 1;
ex[a - 1]++;
if (ex[a - 1] > 9)
clear = false;
}
}
}
if (!clear)
continue;
dfs();
dices = copy_dices;
}
}
return;
}
}
}
REP(i, 3) {
REP(j, 3) {
if (dices[1][i][j] == 0) {
REP(k, 6) {
bool clear = true;
REP(l, 3)
if (dices[0][l][j] == k + 1 || dices[0][l][j] + k + 1 == 7)
clear = false;
if (clear) {
dices[1][i][j] = k + 1;
if (!kakutei()) {
dices = copy_dices;
continue;
}
int ex[3] = {0};
REP(l, 3) {
REP(x, 3) {
REP(y, 3) {
if (dices[l][x][y] <= 0)
continue;
int a = dices[l][x][y];
if (a == 4)
a = 3;
if (a == 5)
a = 2;
if (a == 6)
a = 1;
ex[a - 1]++;
if (ex[a - 1] > 9)
clear = false;
}
}
}
if (!clear)
continue;
dfs();
dices = copy_dices;
}
}
return;
}
}
}
int sum = 0;
REP(i, 3) {
REP(j, 3) { sum += dice_right[dices[0][2 - j][2] - 1][dices[1][i][2] - 1]; }
}
ans.push_back(sum);
}
int main() {
int kurikaesu;
cin >> kurikaesu;
while (kurikaesu--) {
ans.clear();
REP(i, 3) {
REP(j, 3) {
REP(k, 3) { dices[i][j][k] = -1; }
}
}
REP(i, 3) {
REP(j, 3) { cin >> dices[0][i][j]; }
}
REP(i, 3) {
REP(j, 3) { cin >> dices[1][i][j]; }
}
if (kakutei())
dfs();
else
goto emp;
SORT(ans);
ans.erase(unique(ans.begin(), ans.end()), ans.end());
REP(i, ans.size()) {
cout << ans[i];
if (i != ans.size() - 1)
cout << ' ';
}
emp:
if (ans.empty())
cout << 0;
cout << endl;
}
} | #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int dx[] = {1, -1, 0, 0, 0, 0};
const int dy[] = {0, 0, 1, -1, 0, 0};
const int dz[] = {0, 0, 0, 0, 1, -1};
const int side[6][4] = {{2, 4, 5, 3}, {1, 3, 6, 4}, {1, 5, 6, 2},
{1, 2, 6, 5}, {1, 4, 6, 3}, {2, 3, 5, 4}};
const int dice_right[6][6] = {{0, 3, 5, 2, 4, 0}, {4, 0, 1, 6, 0, 3},
{2, 6, 0, 0, 1, 5}, {5, 1, 0, 0, 6, 2},
{3, 0, 6, 1, 0, 4}, {0, 4, 2, 5, 3, 0}};
struct UnionFind {
vector<int> v;
UnionFind(int n) : v(n) {
for (int i = 0; i < n; i++)
v[i] = i;
}
int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
void unite(int x, int y) { v[find(x)] = find(y); }
};
vi ans;
vector<vvi> dices(3, vvi(3, vi(3))); // men tate yoko
bool kakutei() {
bool change = true;
while (change) {
change = false;
REP(i, 3) {
REP(j, 3) {
if (dices[0][i][j] == 0) {
REP(k, 3) {
if (dices[1][k][j] > 0 && dices[2][k][2 - i] > 0) {
dices[0][i][j] =
dice_right[dices[1][k][j] - 1][dices[2][k][2 - i] - 1];
if (dices[0][i][j] == 0)
return false;
change = true;
}
}
}
if (dices[1][i][j] == 0) {
REP(k, 3) {
if (dices[0][k][j] > 0 && dices[2][i][2 - k] > 0) {
dices[1][i][j] =
dice_right[dices[2][i][2 - k] - 1][dices[0][k][j] - 1];
if (dices[1][i][j] == 0)
return false;
change = true;
}
}
}
if (dices[2][i][j] == -1) {
REP(k, 3) {
if (dices[0][2 - j][k] > 0 && dices[1][i][k] > 0) {
dices[2][i][j] =
dice_right[dices[0][2 - j][k] - 1][dices[1][i][k] - 1];
if (dices[2][i][j] == 0)
return false;
change = true;
}
}
}
}
}
}
return true;
}
void dfs() {
vector<vvi> copy_dices = dices;
REP(i, 3) {
REP(j, 3) {
if (dices[0][i][j] == 0) {
REP(k, 6) {
bool clear = true;
REP(l, 3) {
if (dices[1][l][j] > 0)
if (dices[1][l][j] == k + 1 || dices[1][l][j] + k + 1 == 7)
clear = false;
if (dices[2][l][2 - i] > 0)
if (dices[2][l][2 - i] == k + 1 ||
dices[2][l][2 - i] + k + 1 == 7)
clear = false;
}
if (clear) {
dices[0][i][j] = k + 1;
if (!kakutei()) {
dices = copy_dices;
continue;
}
int ex[3] = {0};
REP(l, 3) {
REP(x, 3) {
REP(y, 3) {
if (dices[l][x][y] <= 0)
continue;
int a = dices[l][x][y];
if (a == 4)
a = 3;
if (a == 5)
a = 2;
if (a == 6)
a = 1;
ex[a - 1]++;
if (ex[a - 1] > 9)
clear = false;
}
}
}
if (!clear)
continue;
dfs();
dices = copy_dices;
}
}
return;
}
if (dices[1][i][j] == 0) {
REP(k, 6) {
bool clear = true;
REP(l, 3)
if (dices[0][l][j] == k + 1 || dices[0][l][j] + k + 1 == 7)
clear = false;
if (clear) {
dices[1][i][j] = k + 1;
if (!kakutei()) {
dices = copy_dices;
continue;
}
int ex[3] = {0};
REP(l, 3) {
REP(x, 3) {
REP(y, 3) {
if (dices[l][x][y] <= 0)
continue;
int a = dices[l][x][y];
if (a == 4)
a = 3;
if (a == 5)
a = 2;
if (a == 6)
a = 1;
ex[a - 1]++;
if (ex[a - 1] > 9)
clear = false;
}
}
}
if (!clear)
continue;
dfs();
dices = copy_dices;
}
}
return;
}
}
}
int sum = 0;
REP(i, 3) {
REP(j, 3) { sum += dice_right[dices[0][2 - j][2] - 1][dices[1][i][2] - 1]; }
}
ans.push_back(sum);
}
int main() {
int kurikaesu;
cin >> kurikaesu;
while (kurikaesu--) {
ans.clear();
REP(i, 3) {
REP(j, 3) {
REP(k, 3) { dices[i][j][k] = -1; }
}
}
REP(i, 3) {
REP(j, 3) { cin >> dices[0][i][j]; }
}
REP(i, 3) {
REP(j, 3) { cin >> dices[1][i][j]; }
}
if (kakutei())
dfs();
else
goto emp;
SORT(ans);
ans.erase(unique(ans.begin(), ans.end()), ans.end());
REP(i, ans.size()) {
cout << ans[i];
if (i != ans.size() - 1)
cout << ' ';
}
emp:
if (ans.empty())
cout << 0;
cout << endl;
}
} | delete | 156 | 160 | 156 | 156 | TLE | |
p00833 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
/*--------------------template--------------------*/
typedef long double ld;
const ld PI = acos(-1.0);
bool eq(ld a, ld b) { return abs(a - b) < EPS; }
typedef complex<ld> Point;
typedef vector<Point> Polygon;
namespace std {
bool operator<(const Point &a, const Point &b) {
return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std
struct Line {
Point a, b;
Line(Point p, Point q) : a(p), b(q){};
Line(ld x1, ld y1, ld x2, ld y2) : a(Point(x1, y1)), b(Point(x2, y2)){};
};
struct Circle {
Point p;
ld r;
Circle(Point a, ld b) : p(a), r(b){};
};
ld dot(Point a, Point b) { return real(conj(a) * b); }
ld cross(Point a, Point b) { return imag(conj(a) * b); }
int ccw(Point a, Point b, Point c) {
b -= a;
c -= a;
if (cross(b, c) > EPS)
return 1; // counter cloclwise
if (cross(b, c) < -EPS)
return -1; // cloclwise
if (dot(b, c) < 0)
return 2; // c--a--b on line
if (norm(b) < norm(c))
return -2; // a--b--c on line
return 0; // a--c--b on line
}
bool isis_ll(Line l, Line m) { return abs(cross(l.b - l.a, m.b - m.a)) > EPS; }
bool isis_sp(Line s, Point p) {
if (s.a == p || s.b == p)
return false;
return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a)) < EPS;
}
bool is_cover(Line s, Line t) {
if (isis_ll(s, t))
return false;
if ((s.a == t.a && s.b == t.b) || (s.a == t.b && t.a == s.b))
return true;
int cnt = 0;
if (isis_sp(s, t.a))
cnt++;
if (isis_sp(s, t.b))
cnt++;
if (isis_sp(t, s.a))
cnt++;
if (isis_sp(t, s.b))
cnt++;
return cnt > 0;
}
typedef int Weight;
struct Edge {
int from, to;
Weight cost;
bool operator<(const Edge &e) const { return cost < e.cost; }
bool operator>(const Edge &e) const { return cost > e.cost; }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
void add_edge(Graph &g, int from, int to, Weight cost) {
g[from].push_back(Edge{from, to, cost});
}
int ans;
int cnt;
Matrix g;
vi belong;
void solve(int bit, int col) {
if (bit == (1 << cnt) - 1) {
chmin(ans, col);
return;
}
if (col == ans)
return;
vi white;
REP(i, cnt) {
if (!((bit >> i) & 1)) {
white.push_back(i);
}
}
REP(i, 1 << white.size()) {
vi use;
REP(j, white.size()) {
if ((i >> j) & 1)
use.push_back(white[j]);
}
bool f = false;
REP(j, use.size()) REP(k, j) {
if (g[use[j]][use[k]])
f = true;
}
if (f)
continue;
int nx = bit;
REP(j, use.size()) nx |= 1 << use[j];
solve(nx, col + 1);
}
}
int main() {
cin.sync_with_stdio(false);
cout << fixed << setprecision(10);
int n;
while (cin >> n, n) {
belong.clear();
g.clear();
belong.resize(n);
ans = 10;
cnt = 0;
vector<Polygon> ps(n);
map<string, int> mp;
REP(i, n) {
string s;
cin >> s;
if (!mp.count(s)) {
mp[s] = cnt;
cnt++;
}
belong[i] = mp[s];
int a, b;
while (cin >> a, a >= 0) {
cin >> b;
ps[i].emplace_back(a, b);
}
}
g.resize(cnt, Array(cnt));
REP(i, n) REP(j, i) {
if (belong[i] == belong[j])
continue;
bool f = false;
REP(k, ps[i].size()) REP(l, ps[j].size()) {
Line s1(ps[i][k], ps[i][(k + 1) % ps[i].size()]),
s2(ps[j][l], ps[j][(l + 1) % ps[j].size()]);
if (is_cover(s1, s2))
f = true;
}
if (f) {
g[belong[j]][belong[i]] = 1;
g[belong[i]][belong[j]] = 1;
}
}
solve(0, 0);
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
/*--------------------template--------------------*/
typedef long double ld;
const ld PI = acos(-1.0);
bool eq(ld a, ld b) { return abs(a - b) < EPS; }
typedef complex<ld> Point;
typedef vector<Point> Polygon;
namespace std {
bool operator<(const Point &a, const Point &b) {
return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std
struct Line {
Point a, b;
Line(Point p, Point q) : a(p), b(q){};
Line(ld x1, ld y1, ld x2, ld y2) : a(Point(x1, y1)), b(Point(x2, y2)){};
};
struct Circle {
Point p;
ld r;
Circle(Point a, ld b) : p(a), r(b){};
};
ld dot(Point a, Point b) { return real(conj(a) * b); }
ld cross(Point a, Point b) { return imag(conj(a) * b); }
int ccw(Point a, Point b, Point c) {
b -= a;
c -= a;
if (cross(b, c) > EPS)
return 1; // counter cloclwise
if (cross(b, c) < -EPS)
return -1; // cloclwise
if (dot(b, c) < 0)
return 2; // c--a--b on line
if (norm(b) < norm(c))
return -2; // a--b--c on line
return 0; // a--c--b on line
}
bool isis_ll(Line l, Line m) { return abs(cross(l.b - l.a, m.b - m.a)) > EPS; }
bool isis_sp(Line s, Point p) {
if (s.a == p || s.b == p)
return false;
return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a)) < EPS;
}
bool is_cover(Line s, Line t) {
if (isis_ll(s, t))
return false;
if ((s.a == t.a && s.b == t.b) || (s.a == t.b && t.a == s.b))
return true;
int cnt = 0;
if (isis_sp(s, t.a))
cnt++;
if (isis_sp(s, t.b))
cnt++;
if (isis_sp(t, s.a))
cnt++;
if (isis_sp(t, s.b))
cnt++;
return cnt > 0;
}
typedef int Weight;
struct Edge {
int from, to;
Weight cost;
bool operator<(const Edge &e) const { return cost < e.cost; }
bool operator>(const Edge &e) const { return cost > e.cost; }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
void add_edge(Graph &g, int from, int to, Weight cost) {
g[from].push_back(Edge{from, to, cost});
}
int ans;
int cnt;
Matrix g;
vi belong;
void solve(int bit, int col) {
if (bit == (1 << cnt) - 1) {
chmin(ans, col);
return;
}
if (col == ans)
return;
vi white;
REP(i, cnt) {
if (!((bit >> i) & 1)) {
white.push_back(i);
}
}
REP(i, 1 << white.size()) {
if (!i & 1)
continue;
vi use;
REP(j, white.size()) {
if ((i >> j) & 1)
use.push_back(white[j]);
}
bool f = false;
REP(j, use.size()) REP(k, j) {
if (g[use[j]][use[k]])
f = true;
}
if (f)
continue;
int nx = bit;
REP(j, use.size()) nx |= 1 << use[j];
solve(nx, col + 1);
}
}
int main() {
cin.sync_with_stdio(false);
cout << fixed << setprecision(10);
int n;
while (cin >> n, n) {
belong.clear();
g.clear();
belong.resize(n);
ans = 10;
cnt = 0;
vector<Polygon> ps(n);
map<string, int> mp;
REP(i, n) {
string s;
cin >> s;
if (!mp.count(s)) {
mp[s] = cnt;
cnt++;
}
belong[i] = mp[s];
int a, b;
while (cin >> a, a >= 0) {
cin >> b;
ps[i].emplace_back(a, b);
}
}
g.resize(cnt, Array(cnt));
REP(i, n) REP(j, i) {
if (belong[i] == belong[j])
continue;
bool f = false;
REP(k, ps[i].size()) REP(l, ps[j].size()) {
Line s1(ps[i][k], ps[i][(k + 1) % ps[i].size()]),
s2(ps[j][l], ps[j][(l + 1) % ps[j].size()]);
if (is_cover(s1, s2))
f = true;
}
if (f) {
g[belong[j]][belong[i]] = 1;
g[belong[i]][belong[j]] = 1;
}
}
solve(0, 0);
cout << ans << endl;
}
return 0;
} | insert | 120 | 120 | 120 | 122 | TLE | |
p00833 | C++ | Runtime Error | #include <algorithm>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
const double EPS = 1e-10;
const double INF = 1e+10;
const double PI = acos(-1);
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
inline double ABS(double a) { return max(a, -a); }
struct Pt {
double x, y;
Pt() {}
Pt(double x, double y) : x(x), y(y) {}
Pt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y); }
Pt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y); }
Pt operator*(const Pt &a) const {
return Pt(x * a.x - y * a.y, x * a.y + y * a.x);
}
Pt operator-() const { return Pt(-x, -y); }
Pt operator*(const double &k) const { return Pt(x * k, y * k); }
Pt operator/(const double &k) const { return Pt(x / k, y / k); }
double ABS() const { return sqrt(x * x + y * y); }
double abs2() const { return x * x + y * y; }
double arg() const { return atan2(y, x); }
double dot(const Pt &a) const { return x * a.x + y * a.y; }
double det(const Pt &a) const { return x * a.y - y * a.x; }
};
double tri(const Pt &a, const Pt &b, const Pt &c) { return (b - a).det(c - a); }
int iLL(Pt a, Pt b, Pt c, Pt d) {
if (sig((b - a).det(d - c)))
return 1; // intersect
if (sig((b - a).det(c - a)))
return 0; // parallel
return -1; // correspond
}
char in[50];
vector<pair<Pt, Pt>> p[12];
double x[120];
double y[120];
int g[20][20];
int col[20];
int n;
int solve(int a, int b) {
if (a == n)
return 1;
for (int i = 1; i <= b; i++) {
bool ok = true;
for (int j = 0; j < a; j++)
if (g[a][j] && col[j] == i)
ok = false;
if (ok) {
col[a] = i;
int res = solve(a + 1, b);
if (res)
return 1;
}
}
return 0;
}
int main() {
int a;
while (scanf("%d", &a), a) {
map<string, int> m;
n = 0;
for (int i = 0; i < 12; i++)
p[i].clear();
for (int i = 0; i < 20; i++)
for (int j = 0; j < 20; j++)
g[i][j] = 0;
for (int i = 0; i < a; i++) {
scanf("%s", in);
string tmp = in;
int at;
if (m.count(tmp))
at = m[tmp];
else {
at = n;
m[tmp] = n++;
}
int s = 0;
while (1) {
scanf("%lf", x + s);
if (x[s] < -0.5)
break;
scanf("%lf", y + s);
s++;
}
for (int i = 0; i < s; i++) {
double X = x[i] * cos(1) - y[i] * sin(1);
double Y = x[i] * sin(1) + y[i] * cos(1);
x[i] = X;
y[i] = Y;
}
for (int i = 0; i < s; i++) {
p[at].push_back(
make_pair(Pt(x[i], y[i]), Pt(x[(i + 1) % s], y[(i + 1) % s])));
}
}
for (int i = 0; i < a; i++) {
for (int j = i + 1; j < a; j++) {
bool ok = false;
for (int k = 0; k < p[i].size(); k++) {
if (ok)
break;
for (int l = 0; l < p[j].size(); l++) {
if (ok)
break;
if (iLL(p[i][k].first, p[i][k].second, p[j][l].first,
p[j][l].second) == -1) {
if (max(p[i][k].first.x, p[i][k].second.x) <
EPS + min(p[j][l].first.x, p[j][l].second.x))
continue;
if (min(p[i][k].first.x, p[i][k].second.x) + EPS >
max(p[j][l].first.x, p[j][l].second.x))
continue;
ok = true;
}
}
}
if (ok)
g[i][j] = g[j][i] = 1;
}
}
int ans = n;
for (int i = 1; i < n; i++) {
for (int j = 0; j < 20; j++)
col[j] = 0;
if (solve(0, i)) {
ans = i;
break;
}
}
printf("%d\n", ans);
}
} | #include <algorithm>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
const double EPS = 1e-10;
const double INF = 1e+10;
const double PI = acos(-1);
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
inline double ABS(double a) { return max(a, -a); }
struct Pt {
double x, y;
Pt() {}
Pt(double x, double y) : x(x), y(y) {}
Pt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y); }
Pt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y); }
Pt operator*(const Pt &a) const {
return Pt(x * a.x - y * a.y, x * a.y + y * a.x);
}
Pt operator-() const { return Pt(-x, -y); }
Pt operator*(const double &k) const { return Pt(x * k, y * k); }
Pt operator/(const double &k) const { return Pt(x / k, y / k); }
double ABS() const { return sqrt(x * x + y * y); }
double abs2() const { return x * x + y * y; }
double arg() const { return atan2(y, x); }
double dot(const Pt &a) const { return x * a.x + y * a.y; }
double det(const Pt &a) const { return x * a.y - y * a.x; }
};
double tri(const Pt &a, const Pt &b, const Pt &c) { return (b - a).det(c - a); }
int iLL(Pt a, Pt b, Pt c, Pt d) {
if (sig((b - a).det(d - c)))
return 1; // intersect
if (sig((b - a).det(c - a)))
return 0; // parallel
return -1; // correspond
}
char in[50];
vector<pair<Pt, Pt>> p[12];
double x[120];
double y[120];
int g[20][20];
int col[20];
int n;
int solve(int a, int b) {
if (a == n)
return 1;
for (int i = 1; i <= b; i++) {
bool ok = true;
for (int j = 0; j < a; j++)
if (g[a][j] && col[j] == i)
ok = false;
if (ok) {
col[a] = i;
int res = solve(a + 1, b);
if (res)
return 1;
}
}
return 0;
}
int main() {
int a;
while (scanf("%d", &a), a) {
map<string, int> m;
n = 0;
for (int i = 0; i < 12; i++)
p[i].clear();
for (int i = 0; i < 20; i++)
for (int j = 0; j < 20; j++)
g[i][j] = 0;
for (int i = 0; i < a; i++) {
scanf("%s", in);
string tmp = in;
int at;
if (m.count(tmp))
at = m[tmp];
else {
at = n;
m[tmp] = n++;
}
int s = 0;
while (1) {
scanf("%lf", x + s);
if (x[s] < -0.5)
break;
scanf("%lf", y + s);
s++;
}
for (int i = 0; i < s; i++) {
double X = x[i] * cos(1) - y[i] * sin(1);
double Y = x[i] * sin(1) + y[i] * cos(1);
x[i] = X;
y[i] = Y;
}
for (int i = 0; i < s; i++) {
p[at].push_back(
make_pair(Pt(x[i], y[i]), Pt(x[(i + 1) % s], y[(i + 1) % s])));
}
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
bool ok = false;
for (int k = 0; k < p[i].size(); k++) {
if (ok)
break;
for (int l = 0; l < p[j].size(); l++) {
if (ok)
break;
if (iLL(p[i][k].first, p[i][k].second, p[j][l].first,
p[j][l].second) == -1) {
if (max(p[i][k].first.x, p[i][k].second.x) <
EPS + min(p[j][l].first.x, p[j][l].second.x))
continue;
if (min(p[i][k].first.x, p[i][k].second.x) + EPS >
max(p[j][l].first.x, p[j][l].second.x))
continue;
ok = true;
}
}
}
if (ok)
g[i][j] = g[j][i] = 1;
}
}
int ans = n;
for (int i = 1; i < n; i++) {
for (int j = 0; j < 20; j++)
col[j] = 0;
if (solve(0, i)) {
ans = i;
break;
}
}
printf("%d\n", ans);
}
} | replace | 101 | 103 | 101 | 103 | 0 | |
p00833 | C++ | Runtime Error | // Enjoy your stay.
#include <bits/stdc++.h>
#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X) for (auto it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())
using namespace std;
typedef istringstream iss;
typedef long long ll;
typedef pair<ll, ll> pi;
typedef stringstream sst;
typedef vector<ll> vi;
// geometry library from hos
const double PI = acos(-1);
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
struct Pt {
double x, y;
Pt() {}
Pt(double x, double y) : x(x), y(y) {}
Pt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y); }
Pt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y); }
Pt operator*(const Pt &a) const {
return Pt(x * a.x - y * a.y, x * a.y + y * a.x);
}
Pt operator-() const { return Pt(-x, -y); }
Pt operator*(const double &k) const { return Pt(x * k, y * k); }
Pt operator/(const double &k) const { return Pt(x / k, y / k); }
double abs() const { return sqrt(x * x + y * y); }
double abs2() const { return x * x + y * y; }
double arg() const { return atan2(y, x); }
double dot(const Pt &a) const { return x * a.x + y * a.y; }
double det(const Pt &a) const { return x * a.y - y * a.x; }
bool operator<(const Pt &a) const { return x < a.x || x == a.x && y < a.y; }
bool operator==(const Pt &a) const { return x == a.x && y == a.y; }
bool operator!=(const Pt &a) const { return x != a.x || y != a.y; }
};
ostream &operator<<(ostream &os, const Pt &a) {
os << "(" << a.x << ", " << a.y << ")";
return os;
}
double tri(const Pt &a, const Pt &b, const Pt &c) { return (b - a).det(c - a); }
int iSP(Pt a, Pt b, Pt c) {
int s = sig((b - a).det(c - a));
if (s)
return s; // +1 or -1
if (sig((b - a).dot(c - a)) < 0)
return -2; // c-a-b
if (sig((a - b).dot(c - b)) < 0)
return +2; // a-b-c
return 0;
}
// end of library from hos
int iSP2(Pt a, Pt b, Pt c) {
int s = sig((b - a).det(c - a));
if (s)
return s; // +1 or -1
if (sig((b - a).dot(c - a)) <= 0)
return -2; // c-a-b
if (sig((a - b).dot(c - b)) <= 0)
return +2; // a-b-c
return 0;
}
double area(vector<Pt> p) {
double res = 0;
rep(i, sz(p)) { res += p[i].det(p[(i + 1) % sz(p)]); }
return res / 2;
}
int n;
map<string, vector<vector<Pt>>> M;
string name[11];
int g[11][11];
int ok[1 << 11];
int dp[1 << 11];
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
while (cin >> n && n) {
M.clear();
memset(g, 0, sizeof(g));
rep(i, n) {
string s;
cin >> s;
vector<Pt> p;
int x, y;
while (cin >> x && x != -1) {
cin >> y;
p.pb(Pt(x, y));
}
if (area(p) < 0) {
reverse(p.begin(), p.end());
}
M[s].pb(p);
}
int c = 0;
foreach (it, M) {
name[c++] = it->fir;
}
rep(I, c) rep2(J, I + 1, c) {
auto &v1 = M[name[I]], &v2 = M[name[J]];
rep(i, sz(v1)) rep(j, sz(v2)) rep(ii, sz(v1[i])) rep(jj, sz(v2[j])) {
int res1 = iSP2(v1[i][ii], v1[i][(ii + 1) % sz(v1[i])], v2[j][jj]);
int res2 = iSP2(v1[i][ii], v1[i][(ii + 1) % sz(v1[i])],
v2[j][(jj + 1) % sz(v2[j])]);
if (res1 > res2)
swap(res1, res2);
if ((res1 == -2 || res1 == 0) && (res2 == 0 || res2 == +2)) {
// cout<<name[I]<<" "<<name[J]<<" "<<v1[i][ii]<<" "<<v1[i][(ii+1) %
// sz(v1[i])]<<" "<<v2[j][jj]<<" "<<v2[j][(jj+1) % sz(v2[j])]<<endl;
g[I][J] = g[J][I] = 1;
goto end;
}
}
end:;
}
/*rep(i,c)rep(j,c){
cout<<g[i][j]<<" ";if(j==c-1)cout<<endl;
}*/
rep(i, 1 << c) ok[i] = 1, dp[i] = INF;
dp[0] = 0;
rep(mask, 1 << c) {
rep(i, n) if (mask >> i & 1) rep(j, n) if (mask >> j & 1) {
if (g[i][j])
ok[mask] = 0;
}
}
rep(mask, 1 << c) {
rep(sub, 1 << c) if ((mask & sub) == sub && ok[sub]) {
dp[mask] = min(dp[mask], dp[mask ^ sub] + 1);
}
}
cout << dp[(1 << c) - 1] << endl;
}
} | // Enjoy your stay.
#include <bits/stdc++.h>
#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X) for (auto it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())
using namespace std;
typedef istringstream iss;
typedef long long ll;
typedef pair<ll, ll> pi;
typedef stringstream sst;
typedef vector<ll> vi;
// geometry library from hos
const double PI = acos(-1);
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
struct Pt {
double x, y;
Pt() {}
Pt(double x, double y) : x(x), y(y) {}
Pt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y); }
Pt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y); }
Pt operator*(const Pt &a) const {
return Pt(x * a.x - y * a.y, x * a.y + y * a.x);
}
Pt operator-() const { return Pt(-x, -y); }
Pt operator*(const double &k) const { return Pt(x * k, y * k); }
Pt operator/(const double &k) const { return Pt(x / k, y / k); }
double abs() const { return sqrt(x * x + y * y); }
double abs2() const { return x * x + y * y; }
double arg() const { return atan2(y, x); }
double dot(const Pt &a) const { return x * a.x + y * a.y; }
double det(const Pt &a) const { return x * a.y - y * a.x; }
bool operator<(const Pt &a) const { return x < a.x || x == a.x && y < a.y; }
bool operator==(const Pt &a) const { return x == a.x && y == a.y; }
bool operator!=(const Pt &a) const { return x != a.x || y != a.y; }
};
ostream &operator<<(ostream &os, const Pt &a) {
os << "(" << a.x << ", " << a.y << ")";
return os;
}
double tri(const Pt &a, const Pt &b, const Pt &c) { return (b - a).det(c - a); }
int iSP(Pt a, Pt b, Pt c) {
int s = sig((b - a).det(c - a));
if (s)
return s; // +1 or -1
if (sig((b - a).dot(c - a)) < 0)
return -2; // c-a-b
if (sig((a - b).dot(c - b)) < 0)
return +2; // a-b-c
return 0;
}
// end of library from hos
int iSP2(Pt a, Pt b, Pt c) {
int s = sig((b - a).det(c - a));
if (s)
return s; // +1 or -1
if (sig((b - a).dot(c - a)) <= 0)
return -2; // c-a-b
if (sig((a - b).dot(c - b)) <= 0)
return +2; // a-b-c
return 0;
}
double area(vector<Pt> p) {
double res = 0;
rep(i, sz(p)) { res += p[i].det(p[(i + 1) % sz(p)]); }
return res / 2;
}
int n;
map<string, vector<vector<Pt>>> M;
string name[11];
int g[11][11];
int ok[1 << 11];
int dp[1 << 11];
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
while (cin >> n && n) {
M.clear();
memset(g, 0, sizeof(g));
rep(i, n) {
string s;
cin >> s;
vector<Pt> p;
int x, y;
while (cin >> x && x != -1) {
cin >> y;
p.pb(Pt(x, y));
}
if (area(p) < 0) {
reverse(p.begin(), p.end());
}
M[s].pb(p);
}
int c = 0;
foreach (it, M) {
name[c++] = it->fir;
}
rep(I, c) rep2(J, I + 1, c) {
auto &v1 = M[name[I]], &v2 = M[name[J]];
rep(i, sz(v1)) rep(j, sz(v2)) rep(ii, sz(v1[i])) rep(jj, sz(v2[j])) {
int res1 = iSP2(v1[i][ii], v1[i][(ii + 1) % sz(v1[i])], v2[j][jj]);
int res2 = iSP2(v1[i][ii], v1[i][(ii + 1) % sz(v1[i])],
v2[j][(jj + 1) % sz(v2[j])]);
if (res1 > res2)
swap(res1, res2);
if ((res1 == -2 || res1 == 0) && (res2 == 0 || res2 == +2)) {
// cout<<name[I]<<" "<<name[J]<<" "<<v1[i][ii]<<" "<<v1[i][(ii+1) %
// sz(v1[i])]<<" "<<v2[j][jj]<<" "<<v2[j][(jj+1) % sz(v2[j])]<<endl;
g[I][J] = g[J][I] = 1;
goto end;
}
}
end:;
}
/*rep(i,c)rep(j,c){
cout<<g[i][j]<<" ";if(j==c-1)cout<<endl;
}*/
rep(i, 1 << c) ok[i] = 1, dp[i] = INF;
dp[0] = 0;
rep(mask, 1 << c) {
rep(i, c) if (mask >> i & 1) rep(j, c) if (mask >> j & 1) {
if (g[i][j])
ok[mask] = 0;
}
}
rep(mask, 1 << c) {
rep(sub, 1 << c) if ((mask & sub) == sub && ok[sub]) {
dp[mask] = min(dp[mask], dp[mask ^ sub] + 1);
}
}
cout << dp[(1 << c) - 1] << endl;
}
} | replace | 150 | 151 | 150 | 151 | 0 | |
p00836 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define per(i, n) for (int i = n; i >= 0; i--)
#define ROF(i, s, n) for (int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define PB push_back
#define EACH(i, n) \
for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define CLR(a) memset((a), 0, sizeof(a))
#define NCLR(a) memset((a), -1, sizeof(a))
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
typedef complex<double> Point;
static const double EPS = 1e-10;
static const double PI = acos(-1.0);
static const int mod = 1000000007;
static const int INF = 1 << 29;
static const LL LL_INF = 1ll << 60;
static const int dx[] = {-1, 0, 1, 0, 1, -1, 1, -1};
static const int dy[] = {0, -1, 0, 1, 1, 1, -1, -1};
#ifdef WIN32
#include "gtest/gtest.h"
#pragma comment(lib, "gtest.lib")
#define dump(x) cerr << #x << " = " << (x) << "\n"
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << "\n"
#else
#define dump(x)
#define debug(x)
#endif
int n;
#define severalTestCases
static VI primes(1, 2);
static bool is_prime[10001];
bool input() {
cin >> n;
return n;
}
static int dp[1229][10001][2];
int dfs(int now, int sum, bool check) {
if (!sum) {
return 1;
}
if (now == primes.size() || sum < primes[now]) {
return 0;
}
if (!dp[now][sum][check]) {
return dp[now][sum][check];
}
int res = 0;
if (!check) {
res += dfs(now + 1, sum, false);
}
res += dfs(now + 1, sum - primes[now], true);
return dp[now][sum][check] = res;
}
int solve(int _n) {
static bool check = true;
if (check) {
NCLR(is_prime);
for (int i = 3; i * i <= 10000; i += 2) {
if (is_prime[i]) {
for (int j = i * i; j <= 10000; j += i * 2) {
is_prime[j] = false;
}
}
}
for (int i = 3; i <= 10000; i += 2) {
if (is_prime[i]) {
primes.push_back(i);
}
}
NCLR(dp);
check = false;
}
return dfs(0, n, false);
}
void putsanswer() { cout << solve(n) << "\n"; }
int main(int argc, char **argv) {
// #ifndef WIN32
#ifdef severalTestCases
while (input()) {
#else
input();
putsanswer();
#endif
/*#else
::testing::InitGoogleTest( &argc, argv );
return RUN_ALL_TESTS(); // gtest
#endif*/
#ifdef severalTestCases
putsanswer();
}
#endif
return 0;
}
/*#ifdef WIN32
TEST( testcase, solve ) {
}
#endif*/ | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define per(i, n) for (int i = n; i >= 0; i--)
#define ROF(i, s, n) for (int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define PB push_back
#define EACH(i, n) \
for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define CLR(a) memset((a), 0, sizeof(a))
#define NCLR(a) memset((a), -1, sizeof(a))
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
typedef complex<double> Point;
static const double EPS = 1e-10;
static const double PI = acos(-1.0);
static const int mod = 1000000007;
static const int INF = 1 << 29;
static const LL LL_INF = 1ll << 60;
static const int dx[] = {-1, 0, 1, 0, 1, -1, 1, -1};
static const int dy[] = {0, -1, 0, 1, 1, 1, -1, -1};
#ifdef WIN32
#include "gtest/gtest.h"
#pragma comment(lib, "gtest.lib")
#define dump(x) cerr << #x << " = " << (x) << "\n"
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << "\n"
#else
#define dump(x)
#define debug(x)
#endif
int n;
#define severalTestCases
static VI primes(1, 2);
static bool is_prime[10001];
bool input() {
cin >> n;
return n;
}
static short dp[1229][10001][2];
int dfs(int now, int sum, bool check) {
if (!sum) {
return 1;
}
if (now == primes.size() || sum < primes[now]) {
return 0;
}
if (!dp[now][sum][check]) {
return dp[now][sum][check];
}
int res = 0;
if (!check) {
res += dfs(now + 1, sum, false);
}
res += dfs(now + 1, sum - primes[now], true);
return dp[now][sum][check] = res;
}
int solve(int _n) {
static bool check = true;
if (check) {
NCLR(is_prime);
for (int i = 3; i * i <= 10000; i += 2) {
if (is_prime[i]) {
for (int j = i * i; j <= 10000; j += i * 2) {
is_prime[j] = false;
}
}
}
for (int i = 3; i <= 10000; i += 2) {
if (is_prime[i]) {
primes.push_back(i);
}
}
NCLR(dp);
check = false;
}
return dfs(0, n, false);
}
void putsanswer() { cout << solve(n) << "\n"; }
int main(int argc, char **argv) {
// #ifndef WIN32
#ifdef severalTestCases
while (input()) {
#else
input();
putsanswer();
#endif
/*#else
::testing::InitGoogleTest( &argc, argv );
return RUN_ALL_TESTS(); // gtest
#endif*/
#ifdef severalTestCases
putsanswer();
}
#endif
return 0;
}
/*#ifdef WIN32
TEST( testcase, solve ) {
}
#endif*/ | replace | 61 | 62 | 61 | 62 | MLE | |
p00836 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
bool isPrime[10001];
for (int i = 0; i < 10001; i++)
isPrime[i] = 1;
isPrime[0] = isPrime[1] = 0;
for (int i = 0; i < 10001; i++) {
if (!isPrime[i])
continue;
for (int j = 2; j * i < 10001; j++) {
isPrime[i * j] = 0;
}
}
vector<int> prime;
prime.push_back(0);
for (int i = 0; i < 10001; i++)
if (isPrime[i])
prime.push_back(i);
for (int i = 1; i < prime.size(); i++)
prime[i] += prime[i - 1];
while (cin >> N, N) {
int ans = 0;
int i = 0, j = 0;
while (i < prime.size() || j < prime.size()) {
int sub = prime[j] - prime[i];
if (sub < N)
j++;
else if (sub >= N) {
i++;
if (sub == N)
ans++;
}
}
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
bool isPrime[10001];
for (int i = 0; i < 10001; i++)
isPrime[i] = 1;
isPrime[0] = isPrime[1] = 0;
for (int i = 0; i < 10001; i++) {
if (!isPrime[i])
continue;
for (int j = 2; j * i < 10001; j++) {
isPrime[i * j] = 0;
}
}
vector<int> prime;
prime.push_back(0);
for (int i = 0; i < 10001; i++)
if (isPrime[i])
prime.push_back(i);
for (int i = 1; i < prime.size(); i++)
prime[i] += prime[i - 1];
while (cin >> N, N) {
int ans = 0;
int i = 0, j = 0;
while (i < prime.size() && j < prime.size()) {
int sub = prime[j] - prime[i];
if (sub < N)
j++;
else if (sub >= N) {
i++;
if (sub == N)
ans++;
}
}
cout << ans << endl;
}
return 0;
} | replace | 29 | 30 | 29 | 30 | -11 | |
p00836 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template <class T> using vv = vector<vector<T>>;
typedef deque<int> di;
typedef deque<deque<int>> ddi;
int main() {
vi V;
FOR(i, 2, 10001) { V.pb(i); }
rep(i, V.size()) {
int v = V.at(i);
for (int j = V.size() - 1; j > i; j--) {
if (V.at(j) % v == 0)
V.erase(V.begin() + j);
}
}
while (1) {
int count = 0;
int i = 0;
queue<int> Q;
int sum = 0;
int A;
cin >> A;
if (A == 0) {
break;
}
while (1) {
if (sum == A) {
count += 1;
sum += V[i];
Q.push(V[i++]);
} else if (sum > A) {
if (Q.size() == 1) {
break;
} else {
sum -= Q.front();
Q.pop();
}
} else {
sum += V[i];
Q.push(V[i++]);
}
}
cout << count << endl;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template <class T> using vv = vector<vector<T>>;
typedef deque<int> di;
typedef deque<deque<int>> ddi;
int main() {
vi V;
FOR(i, 2, 10008) { V.pb(i); }
rep(i, V.size()) {
int v = V.at(i);
for (int j = V.size() - 1; j > i; j--) {
if (V.at(j) % v == 0)
V.erase(V.begin() + j);
}
}
while (1) {
int count = 0;
int i = 0;
queue<int> Q;
int sum = 0;
int A;
cin >> A;
if (A == 0) {
break;
}
while (1) {
if (sum == A) {
count += 1;
sum += V[i];
Q.push(V[i++]);
} else if (sum > A) {
if (Q.size() == 1) {
break;
} else {
sum -= Q.front();
Q.pop();
}
} else {
sum += V[i];
Q.push(V[i++]);
}
}
cout << count << endl;
}
return 0;
} | replace | 36 | 37 | 36 | 37 | 0 | |
p00836 | C++ | Time Limit Exceeded | #include <iostream>
#include <vector>
using namespace std;
const int M = 10000;
int main() {
vector<int> primes;
for (int i = 2; i < M; i++) {
bool flag = true;
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) {
flag = false;
break;
}
}
if (flag) {
primes.push_back(i);
}
}
for (int n; cin >> n && n;) {
int sum = 0, front = 0, back = 0, count = 0;
while (true) {
while (sum < n && front < primes.size()) {
sum += primes[front++];
}
while (sum > n && back <= front) {
sum -= primes[back++];
}
if (sum == n) {
count++;
sum -= primes[back++];
}
if (front == back) {
break;
}
}
cout << count << endl;
}
} | #include <iostream>
#include <vector>
using namespace std;
const int M = 20000;
int main() {
vector<int> primes;
for (int i = 2; i < M; i++) {
bool flag = true;
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) {
flag = false;
break;
}
}
if (flag) {
primes.push_back(i);
}
}
for (int n; cin >> n && n;) {
int sum = 0, front = 0, back = 0, count = 0;
while (true) {
while (sum < n && front < primes.size()) {
sum += primes[front++];
}
while (sum > n && back <= front) {
sum -= primes[back++];
}
if (sum == n) {
count++;
sum -= primes[back++];
}
if (front == back) {
break;
}
}
cout << count << endl;
}
} | replace | 4 | 5 | 4 | 5 | TLE | |
p00837 | C++ | Runtime Error | /* 2012-11-23T16:06:54 */
#define DEBUG_ON
#define CONDITION true
using namespace std; /*{{{*/
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>
#define INF (1e9)
static const double PI = acos(-1.0);
static const double EPS = 1e-10;
typedef long long int LL;
typedef unsigned long long int ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef complex<double> P;
#define FOR(i, b, e) for (typeof(e) i = (b); i != (e); i < (e) ? ++i : --i)
#define REP(i, n) FOR(i, 0, n)
#define IFC(c) \
if (c) \
continue;
#define IFB(c) \
if (c) \
break;
#define IFR(c, r) \
if (c) \
return r;
#define OPOVER(_op, _type) inline bool operator _op(const _type &t) const
#define arrsz(a) (sizeof(a) / sizeof(a[0]))
#define F first
#define S second
#define MP(a, b) make_pair(a, b)
#define SZ(a) ((LL)a.size())
#define PB(e) push_back(e)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(c, it) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define BIT(n) (1ULL << (n))
#define BITOF(n, m) ((ULL)(n) >> (m)&1)
#define RANGE(a, b, c) ((a) <= (b) && (b) <= (c))
#define PQ priority_queue
#define SC static_cast
#ifdef DEBUG_ON
#define dprt(fmt, ...) \
if (CONDITION) \
fprintf(stderr, fmt, __VA_ARGS__)
#define darr(a) \
if (CONDITION) \
copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " ")); \
cerr << endl
#define darr_range(a, f, t) \
if (CONDITION) \
copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " ")); \
cerr << endl
#define dvec(v) \
if (CONDITION) \
copy(ALL(v), ostream_iterator<int>(cerr, " ")); \
cerr << endl
#define darr2(a, n, m) \
if (CONDITION) \
FOR(i, 0, (n)) { darr_range((a)[i], 0, (m)); }
#define dvec2(v) \
if (CONDITION) \
FOR(i, 0, SZ(v)) { dvec((v)[i]); }
#define WAIT() \
if (CONDITION) { \
string _wait_; \
cerr << "(hit return to continue)" << endl; \
getline(cin, _wait_); \
}
#define dump(x) \
if (CONDITION) \
cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl;
#define dumpf() \
if (CONDITION) \
cerr << __PRETTY_FUNCTION__ << endl;
#define dumpv(x) \
if (CONDITION) \
cerr << " [L:" << __LINE__ << "] " << #x << " = "; \
REP(q, (x).size()) cerr << (x)[q] << " "; \
cerr << endl;
#define where() \
if (CONDITION) \
cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__ \
<< "]" << endl;
#define show_bits(b, s) \
if (CONDITION) { \
REP(i, s) { \
cerr << BITOF(b, s - 1 - i); \
if (i % 4 == 3) \
cerr << ' '; \
} \
cerr << endl; \
}
#else
#define cerr \
if (0) \
cerr
#define dprt(fmt, ...)
#define darr(a)
#define darr_range(a, f, t)
#define dvec(v)
#define darr2(a, n, m)
#define dvec2(v)
#define WAIT()
#define dump(x)
#define dumpf()
#define dumpv(x)
#define where()
#define show_bits(b, s)
#endif
inline int onbits_count(ULL b) {
int c = 0;
while (b != 0) {
c += (b & 1);
b >>= 1;
}
return c;
}
inline int bits_count(ULL b) {
int c = 0;
while (b != 0) {
++c;
b >>= 1;
}
return c;
}
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
inline double now() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (static_cast<double>(tv.tv_sec) +
static_cast<double>(tv.tv_usec) * 1e-6);
}
inline VS split(string s, char delimiter) {
VS v;
string t;
REP(i, s.length()) {
IFC(s[i] == ' ');
if (s[i] == delimiter)
v.PB(t), t = "";
else
t += s[i];
}
v.PB(t);
return v;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
return s << "(" << d.first << ", " << d.second << ")";
}
/*}}}*/
#define KMAX 51
int main() {
std::ios_base::sync_with_stdio(false);
int m, c, n;
while (cin >> m >> c >> n, m || c || n) {
queue<queue<int>> students;
REP(i, n) {
int k;
cin >> k;
queue<int> books;
REP(j, k) {
int t;
cin >> t;
books.push(t);
}
students.push(books);
}
VI address(100, m); /* place of books */
VVI desks(m, VI());
int cost = 0;
while (!students.empty()) {
queue<int> s = students.front();
students.pop();
int b = s.front();
s.pop();
dprt("Request: %d\n", b);
/* Get the book */
cost += address[b] + 1;
dprt("\tget it from %d\n", address[b]);
if (address[b] != m) {
desks[address[b]].erase(find(ALL(desks[address[b]]), b));
}
/* Store the book */
if (desks[0].size() < c) {
cerr << "\tput it on desk 0" << endl;
desks[0].PB(b);
address[b] = 0;
cost++;
} else {
bool shelf = true;
int tmp;
int i;
for (i = 0; i < m; ++i) {
if (desks[i].size() < c) {
cost += i + 1;
tmp = i;
shelf = false;
break;
}
}
if (shelf) {
cost += m + 1;
tmp = m;
}
dprt("\ttemporary put it on %d\n", tmp);
int lru = desks[0][0];
dprt("\ttake lru(%d) from desk 0\n", lru);
desks[0].erase(desks[0].begin());
cost++;
int lruput;
shelf = true;
FOR(j, 1, m) {
if (desks[j].size() + (j == i) < c) {
desks[j].PB(lru);
address[lru] = j;
lruput = j;
cost += j + 1;
shelf = false;
break;
}
}
if (shelf) {
cost += m + 1;
lruput = m;
address[lru] = m;
}
dprt("\tput lru on %d\n", lruput);
dprt("\tretrive book from tmp(%d)\n", tmp);
cost += tmp + 1;
cerr << "\tput it on desk 0" << endl;
desks[0].PB(b);
cost++;
address[b] = 0;
}
if (s.size() > 0) {
students.push(s);
}
dump(cost);
}
cout << cost << endl;
}
} | /* 2012-11-23T16:06:54 */
#define DEBUG_ON_
#define CONDITION true
using namespace std; /*{{{*/
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>
#define INF (1e9)
static const double PI = acos(-1.0);
static const double EPS = 1e-10;
typedef long long int LL;
typedef unsigned long long int ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef complex<double> P;
#define FOR(i, b, e) for (typeof(e) i = (b); i != (e); i < (e) ? ++i : --i)
#define REP(i, n) FOR(i, 0, n)
#define IFC(c) \
if (c) \
continue;
#define IFB(c) \
if (c) \
break;
#define IFR(c, r) \
if (c) \
return r;
#define OPOVER(_op, _type) inline bool operator _op(const _type &t) const
#define arrsz(a) (sizeof(a) / sizeof(a[0]))
#define F first
#define S second
#define MP(a, b) make_pair(a, b)
#define SZ(a) ((LL)a.size())
#define PB(e) push_back(e)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(c, it) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define BIT(n) (1ULL << (n))
#define BITOF(n, m) ((ULL)(n) >> (m)&1)
#define RANGE(a, b, c) ((a) <= (b) && (b) <= (c))
#define PQ priority_queue
#define SC static_cast
#ifdef DEBUG_ON
#define dprt(fmt, ...) \
if (CONDITION) \
fprintf(stderr, fmt, __VA_ARGS__)
#define darr(a) \
if (CONDITION) \
copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " ")); \
cerr << endl
#define darr_range(a, f, t) \
if (CONDITION) \
copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " ")); \
cerr << endl
#define dvec(v) \
if (CONDITION) \
copy(ALL(v), ostream_iterator<int>(cerr, " ")); \
cerr << endl
#define darr2(a, n, m) \
if (CONDITION) \
FOR(i, 0, (n)) { darr_range((a)[i], 0, (m)); }
#define dvec2(v) \
if (CONDITION) \
FOR(i, 0, SZ(v)) { dvec((v)[i]); }
#define WAIT() \
if (CONDITION) { \
string _wait_; \
cerr << "(hit return to continue)" << endl; \
getline(cin, _wait_); \
}
#define dump(x) \
if (CONDITION) \
cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl;
#define dumpf() \
if (CONDITION) \
cerr << __PRETTY_FUNCTION__ << endl;
#define dumpv(x) \
if (CONDITION) \
cerr << " [L:" << __LINE__ << "] " << #x << " = "; \
REP(q, (x).size()) cerr << (x)[q] << " "; \
cerr << endl;
#define where() \
if (CONDITION) \
cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__ \
<< "]" << endl;
#define show_bits(b, s) \
if (CONDITION) { \
REP(i, s) { \
cerr << BITOF(b, s - 1 - i); \
if (i % 4 == 3) \
cerr << ' '; \
} \
cerr << endl; \
}
#else
#define cerr \
if (0) \
cerr
#define dprt(fmt, ...)
#define darr(a)
#define darr_range(a, f, t)
#define dvec(v)
#define darr2(a, n, m)
#define dvec2(v)
#define WAIT()
#define dump(x)
#define dumpf()
#define dumpv(x)
#define where()
#define show_bits(b, s)
#endif
inline int onbits_count(ULL b) {
int c = 0;
while (b != 0) {
c += (b & 1);
b >>= 1;
}
return c;
}
inline int bits_count(ULL b) {
int c = 0;
while (b != 0) {
++c;
b >>= 1;
}
return c;
}
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
inline double now() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (static_cast<double>(tv.tv_sec) +
static_cast<double>(tv.tv_usec) * 1e-6);
}
inline VS split(string s, char delimiter) {
VS v;
string t;
REP(i, s.length()) {
IFC(s[i] == ' ');
if (s[i] == delimiter)
v.PB(t), t = "";
else
t += s[i];
}
v.PB(t);
return v;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
return s << "(" << d.first << ", " << d.second << ")";
}
/*}}}*/
#define KMAX 51
int main() {
std::ios_base::sync_with_stdio(false);
int m, c, n;
while (cin >> m >> c >> n, m || c || n) {
queue<queue<int>> students;
REP(i, n) {
int k;
cin >> k;
queue<int> books;
REP(j, k) {
int t;
cin >> t;
books.push(t);
}
students.push(books);
}
VI address(100, m); /* place of books */
VVI desks(m, VI());
int cost = 0;
while (!students.empty()) {
queue<int> s = students.front();
students.pop();
int b = s.front();
s.pop();
dprt("Request: %d\n", b);
/* Get the book */
cost += address[b] + 1;
dprt("\tget it from %d\n", address[b]);
if (address[b] != m) {
desks[address[b]].erase(find(ALL(desks[address[b]]), b));
}
/* Store the book */
if (desks[0].size() < c) {
cerr << "\tput it on desk 0" << endl;
desks[0].PB(b);
address[b] = 0;
cost++;
} else {
bool shelf = true;
int tmp;
int i;
for (i = 0; i < m; ++i) {
if (desks[i].size() < c) {
cost += i + 1;
tmp = i;
shelf = false;
break;
}
}
if (shelf) {
cost += m + 1;
tmp = m;
}
dprt("\ttemporary put it on %d\n", tmp);
int lru = desks[0][0];
dprt("\ttake lru(%d) from desk 0\n", lru);
desks[0].erase(desks[0].begin());
cost++;
int lruput;
shelf = true;
FOR(j, 1, m) {
if (desks[j].size() + (j == i) < c) {
desks[j].PB(lru);
address[lru] = j;
lruput = j;
cost += j + 1;
shelf = false;
break;
}
}
if (shelf) {
cost += m + 1;
lruput = m;
address[lru] = m;
}
dprt("\tput lru on %d\n", lruput);
dprt("\tretrive book from tmp(%d)\n", tmp);
cost += tmp + 1;
cerr << "\tput it on desk 0" << endl;
desks[0].PB(b);
cost++;
address[b] = 0;
}
if (s.size() > 0) {
students.push(s);
}
dump(cost);
}
cout << cost << endl;
}
} | replace | 1 | 2 | 1 | 2 | 0 | Request: 50
get it from 2
put it on desk 0
[L214] cost = 4
Request: 50
get it from 2
put it on desk 0
[L214] cost = 4
Request: 60
get it from 2
temporary put it on 1
take lru(50) from desk 0
put lru on 2
retrive book from tmp(1)
put it on desk 0
[L214] cost = 16
Request: 60
get it from 2
put it on desk 0
[L214] cost = 4
Request: 70
get it from 2
temporary put it on 1
take lru(60) from desk 0
put lru on 2
retrive book from tmp(1)
put it on desk 0
[L214] cost = 16
Request: 61
get it from 2
temporary put it on 1
take lru(70) from desk 0
put lru on 2
retrive book from tmp(1)
put it on desk 0
[L214] cost = 28
Request: 60
get it from 4
put it on desk 0
[L214] cost = 6
Request: 70
get it from 4
put it on desk 0
[L214] cost = 12
Request: 80
get it from 4
temporary put it on 1
take lru(60) from desk 0
put lru on 1
retrive book from tmp(1)
put it on desk 0
[L214] cost = 25
Request: 61
get it from 4
temporary put it on 1
take lru(70) from desk 0
put lru on 2
retrive book from tmp(1)
put it on desk 0
[L214] cost = 39
Request: 81
get it from 4
temporary put it on 1
take lru(80) from desk 0
put lru on 2
retrive book from tmp(1)
put it on desk 0
[L214] cost = 53
Request: 62
get it from 4
temporary put it on 1
take lru(61) from desk 0
put lru on 3
retrive book from tmp(1)
put it on desk 0
[L214] cost = 68
Request: 60
get it from 3
put it on desk 0
[L214] cost = 5
Request: 70
get it from 3
temporary put it on 1
take lru(60) from desk 0
put lru on 2
retrive book from tmp(1)
put it on desk 0
[L214] cost = 18
Request: 61
get it from 3
temporary put it on 1
take lru(70) from desk 0
put lru on 3
retrive book from tmp(1)
put it on desk 0
[L214] cost = 32
Request: 60
get it from 2
temporary put it on 1
take lru(61) from desk 0
put lru on 2
retrive book from tmp(1)
put it on desk 0
[L214] cost = 44
Request: 62
get it from 3
temporary put it on 1
take lru(60) from desk 0
put lru on 3
retrive book from tmp(1)
put it on desk 0
[L214] cost = 58
Request: 87
get it from 1
put it on desk 0
[L214] cost = 3
Request: 96
get it from 1
put it on desk 0
[L214] cost = 6
Request: 68
get it from 1
temporary put it on 1
take lru(87) from desk 0
put lru on 1
retrive book from tmp(1)
put it on desk 0
[L214] cost = 16
Request: 3
get it from 1
temporary put it on 1
take lru(96) from desk 0
put lru on 1
retrive book from tmp(1)
put it on desk 0
[L214] cost = 26
Request: 57
get it from 1
temporary put it on 1
take lru(68) from desk 0
put lru on 1
retrive book from tmp(1)
put it on desk 0
[L214] cost = 36
Request: 95
get it from 1
temporary put it on 1
take lru(3) from desk 0
put lru on 1
retrive book from tmp(1)
put it on desk 0
[L214] cost = 46
Request: 71
get it from 1
temporary put it on 1
take lru(57) from desk 0
put lru on 1
retrive book from tmp(1)
put it on desk 0
[L214] cost = 56
Request: 2
get it from 1
temporary put it on 1
take lru(95) from desk 0
put lru on 1
retrive book from tmp(1)
put it on desk 0
[L214] cost = 66
Request: 18
get it from 1
temporary put it on 1
take lru(71) from desk 0
put lru on 1
retrive book from tmp(1)
put it on desk 0
[L214] cost = 76
Request: 2
get it from 0
put it on desk 0
[L214] cost = 78
Request: 35
get it from 1
temporary put it on 1
take lru(18) from desk 0
put lru on 1
retrive book from tmp(1)
put it on desk 0
[L214] cost = 88
Request: 93
get it from 1
temporary put it on 1
take lru(2) from desk 0
put lru on 1
retrive book from tmp(1)
put it on desk 0
[L214] cost = 98
Request: 1
get it from 2
put it on desk 0
[L214] cost = 4
Request: 2
get it from 2
put it on desk 0
[L214] cost = 8
Request: 1
get it from 0
put it on desk 0
[L214] cost = 10
Request: 3
get it from 2
temporary put it on 1
take lru(2) from desk 0
put lru on 1
retrive book from tmp(1)
put it on desk 0
[L214] cost = 21
Request: 1
get it from 0
put it on desk 0
[L214] cost = 23
|
p00837 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int main() {
for (int m, c, n; scanf("%d%d%d", &m, &c, &n), m;) {
queue<int> Q[100];
rep(i, n) {
int sz;
scanf("%d", &sz);
rep(j, sz) {
int a;
scanf("%d", &a);
Q[i].push(a);
}
}
int ans = 0;
deque<int> dai[10];
while (1) {
bool end = true;
rep(i, n) if (!Q[i].empty()) {
end = false;
// 本を取る
int book = Q[i].front();
Q[i].pop();
int pos = m;
rep(j, m) if (count(dai[j].begin(), dai[j].end(), book) > 0) {
pos = j;
break;
}
if (pos < m) {
dai[pos].erase(find(dai[pos].begin(), dai[pos].end(), book));
}
ans += pos + 1;
// 本を置く
if (dai[0].size() < c) {
dai[0].push_back(book);
ans += 1;
} else {
// step 1
int pos2 = m;
for (int j = 1; j < m; j++)
if (dai[j].size() < c) {
pos2 = j;
break;
}
if (pos2 < m) {
dai[pos2].push_back(book);
}
ans += pos2 + 1;
// step 2
int sub = dai[0].front();
dai[0].pop_front();
ans++;
// step 3
int pos3 = m;
for (int j = 1; j < m; j++)
if (dai[j].size() < c) {
pos3 = j;
break;
}
if (pos3 < m) {
dai[pos3].push_back(sub);
}
ans += pos3 + 1;
// step 4
dai[pos2].erase(find(dai[pos2].begin(), dai[pos2].end(), book));
ans += pos2 + 1;
// step 5
dai[0].push_back(book);
ans++;
}
}
if (end)
break;
}
printf("%d\n", ans);
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int main() {
for (int m, c, n; scanf("%d%d%d", &m, &c, &n), m;) {
queue<int> Q[100];
rep(i, n) {
int sz;
scanf("%d", &sz);
rep(j, sz) {
int a;
scanf("%d", &a);
Q[i].push(a);
}
}
int ans = 0;
deque<int> dai[10];
while (1) {
bool end = true;
rep(i, n) if (!Q[i].empty()) {
end = false;
// 本を取る
int book = Q[i].front();
Q[i].pop();
int pos = m;
rep(j, m) if (count(dai[j].begin(), dai[j].end(), book) > 0) {
pos = j;
break;
}
if (pos < m) {
dai[pos].erase(find(dai[pos].begin(), dai[pos].end(), book));
}
ans += pos + 1;
// 本を置く
if (dai[0].size() < c) {
dai[0].push_back(book);
ans += 1;
} else {
// step 1
int pos2 = m;
for (int j = 1; j < m; j++)
if (dai[j].size() < c) {
pos2 = j;
break;
}
if (pos2 < m) {
dai[pos2].push_back(book);
}
ans += pos2 + 1;
// step 2
int sub = dai[0].front();
dai[0].pop_front();
ans++;
// step 3
int pos3 = m;
for (int j = 1; j < m; j++)
if (dai[j].size() < c) {
pos3 = j;
break;
}
if (pos3 < m) {
dai[pos3].push_back(sub);
}
ans += pos3 + 1;
// step 4
if (pos2 < m) {
dai[pos2].erase(find(dai[pos2].begin(), dai[pos2].end(), book));
}
ans += pos2 + 1;
// step 5
dai[0].push_back(book);
ans++;
}
}
if (end)
break;
}
printf("%d\n", ans);
}
return 0;
} | replace | 76 | 77 | 76 | 79 | -11 | |
p00838 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
typedef complex<double> P;
typedef pair<P, P> Edge;
typedef long long ll;
// 28ñųÉàÇé
class Dice {
private:
int yoko;
int tate;
int round;
private:
int face[3][4];
public:
int getFace(int i, int j) const { return face[i][j]; }
Dice(vector<int> &v) {
init();
face[1][1] = v[0];
face[2][1] = v[1];
face[1][2] = v[2];
face[1][0] = v[3];
face[0][1] = v[4];
face[1][3] = v[5];
}
private:
// _CXÌú»
void init() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
face[i][j] = -1;
}
}
yoko = 0;
tate = 0;
round = 0;
}
public:
// _CXÌñ]
void rotate() {
if (round < 4) {
int tmp = face[2][1];
face[2][1] = face[1][2];
face[1][2] = face[0][1];
face[0][1] = face[1][0];
face[1][0] = tmp;
round++;
}
if (round >= 4) {
round = 0;
// àÇÁÄ«½çA¡ÌXbgðñ]³¹é
if (yoko < 4) {
int tmp = face[1][3];
for (int i = 2; i >= 0; i--) {
face[1][i + 1] = face[1][i];
}
face[1][0] = tmp;
yoko++;
}
// àÇÁÄ«½çAcÌXbgðñ]³¹é
if (yoko >= 4) {
if (tate < 4) {
tate++;
int tmp = face[1][3];
face[1][3] = face[0][1];
for (int i = 0; i < 2; i++) {
face[i][1] = face[i + 1][1];
}
face[2][1] = tmp;
}
if (tate >= 4) {
round = 0;
tate = 0;
yoko = 0;
}
}
}
};
};
const int INF = 1000000000;
int n;
vector<Dice> dices;
int minCost;
int msi[1001];
void dfs(int depth) {
if (depth == n) {
int sum = 0;
// ʲÆÉf[^ðûW
for (int i = 0; i < 6; i++) {
memset(msi, 0, sizeof(msi));
int maxNum = 0;
for (int j = 0; j < n; j++) {
if (i == 0) {
msi[dices[j].getFace(1, 1)]++;
maxNum = max(maxNum, msi[dices[j].getFace(1, 1)]);
} else if (i == 1) {
msi[dices[j].getFace(2, 1)]++;
maxNum = max(maxNum, msi[dices[j].getFace(2, 1)]);
} else if (i == 2) {
msi[dices[j].getFace(1, 2)]++;
maxNum = max(maxNum, msi[dices[j].getFace(1, 2)]);
} else if (i == 3) {
msi[dices[j].getFace(1, 0)]++;
maxNum = max(maxNum, msi[dices[j].getFace(1, 0)]);
} else if (i == 4) {
msi[dices[j].getFace(0, 1)]++;
maxNum = max(maxNum, msi[dices[j].getFace(0, 1)]);
} else {
msi[dices[j].getFace(1, 3)]++;
maxNum = max(maxNum, msi[dices[j].getFace(1, 3)]);
}
}
sum += n - maxNum;
}
minCost = min(sum, minCost);
} else {
// ñ]ȵ
dfs(depth + 1);
for (int i = 0; i < 24; i++) {
dices[depth].rotate();
dfs(depth + 1);
}
}
}
int main() {
while (cin >> n && n != 0) {
minCost = INF;
int idx = 0;
map<string, int> idxs;
for (int i = 0; i < n; i++) {
vector<int> v;
for (int j = 0; j < 6; j++) {
string s;
cin >> s;
if (idxs.find(s) == idxs.end())
idxs[s] = idx++;
v.push_back(idxs[s]);
}
dices.push_back(Dice(v));
}
dfs(0);
dices.clear();
cout << minCost << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
typedef complex<double> P;
typedef pair<P, P> Edge;
typedef long long ll;
// 28ñųÉàÇé
class Dice {
private:
int yoko;
int tate;
int round;
private:
int face[3][4];
public:
int getFace(int i, int j) const { return face[i][j]; }
Dice(vector<int> &v) {
init();
face[1][1] = v[0];
face[2][1] = v[1];
face[1][2] = v[2];
face[1][0] = v[3];
face[0][1] = v[4];
face[1][3] = v[5];
}
private:
// _CXÌú»
void init() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
face[i][j] = -1;
}
}
yoko = 0;
tate = 0;
round = 0;
}
public:
// _CXÌñ]
void rotate() {
if (round < 4) {
int tmp = face[2][1];
face[2][1] = face[1][2];
face[1][2] = face[0][1];
face[0][1] = face[1][0];
face[1][0] = tmp;
round++;
}
if (round >= 4) {
round = 0;
// àÇÁÄ«½çA¡ÌXbgðñ]³¹é
if (yoko < 4) {
int tmp = face[1][3];
for (int i = 2; i >= 0; i--) {
face[1][i + 1] = face[1][i];
}
face[1][0] = tmp;
yoko++;
}
// àÇÁÄ«½çAcÌXbgðñ]³¹é
if (yoko >= 4) {
if (tate < 4) {
tate++;
int tmp = face[1][3];
face[1][3] = face[0][1];
for (int i = 0; i < 2; i++) {
face[i][1] = face[i + 1][1];
}
face[2][1] = tmp;
}
if (tate >= 4) {
round = 0;
tate = 0;
yoko = 0;
}
}
}
};
};
const int INF = 1000000000;
int n;
vector<Dice> dices;
int minCost;
int msi[1001];
void dfs(int depth) {
if (depth == n) {
int sum = 0;
// ʲÆÉf[^ðûW
for (int i = 0; i < 6; i++) {
memset(msi, 0, sizeof(msi));
int maxNum = 0;
for (int j = 0; j < n; j++) {
if (i == 0) {
msi[dices[j].getFace(1, 1)]++;
maxNum = max(maxNum, msi[dices[j].getFace(1, 1)]);
} else if (i == 1) {
msi[dices[j].getFace(2, 1)]++;
maxNum = max(maxNum, msi[dices[j].getFace(2, 1)]);
} else if (i == 2) {
msi[dices[j].getFace(1, 2)]++;
maxNum = max(maxNum, msi[dices[j].getFace(1, 2)]);
} else if (i == 3) {
msi[dices[j].getFace(1, 0)]++;
maxNum = max(maxNum, msi[dices[j].getFace(1, 0)]);
} else if (i == 4) {
msi[dices[j].getFace(0, 1)]++;
maxNum = max(maxNum, msi[dices[j].getFace(0, 1)]);
} else {
msi[dices[j].getFace(1, 3)]++;
maxNum = max(maxNum, msi[dices[j].getFace(1, 3)]);
}
}
sum += n - maxNum;
}
minCost = min(sum, minCost);
} else {
// ñ]ȵ
dfs(depth + 1);
for (int i = 0; i < 20; i++) {
dices[depth].rotate();
dfs(depth + 1);
}
}
}
int main() {
while (cin >> n && n != 0) {
minCost = INF;
int idx = 0;
map<string, int> idxs;
for (int i = 0; i < n; i++) {
vector<int> v;
for (int j = 0; j < 6; j++) {
string s;
cin >> s;
if (idxs.find(s) == idxs.end())
idxs[s] = idx++;
v.push_back(idxs[s]);
}
dices.push_back(Dice(v));
}
dfs(0);
dices.clear();
cout << minCost << endl;
}
return 0;
} | replace | 145 | 146 | 145 | 146 | TLE | |
p00839 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int n;
vector<vector<int>> to;
void solve(vector<string> start, map<vector<string>, int> &history) {
queue<vector<string>> q1;
q1.push(start);
history[start] = 0;
for (int a = 1; a <= 3; ++a) {
queue<vector<string>> q2;
while (!q1.empty()) {
vector<string> s = q1.front();
q1.pop();
for (int i = 0; i < n * 2; ++i) {
for (unsigned j = 0; j < to[i].size(); ++j) {
vector<string> t = s;
while (!t[i / 2].empty()) {
char c;
if (i % 2 == 0) {
c = t[i / 2][0];
t[i / 2] = t[i / 2].substr(1);
} else {
c = t[i / 2][t[i / 2].size() - 1];
t[i / 2] = t[i / 2].substr(0, t[i / 2].size() - 1);
}
if (to[i][j] % 2 == 0) {
t[to[i][j] / 2] = c + t[to[i][j] / 2];
} else {
t[to[i][j] / 2] += c;
}
if (history.find(t) == history.end()) {
q2.push(t);
history[t] = a;
}
}
}
}
}
q1 = q2;
}
}
int main() {
for (;;) {
int m;
cin >> n >> m;
if (n == 0)
return 0;
to.resize(2 * n);
for (int i = 0; i < m; ++i) {
string s, t;
cin >> s >> t;
int a = (s[0] - '0') * 2;
int b = (t[0] - '0') * 2;
if (s[1] == 'E')
++a;
if (t[1] == 'E')
++b;
to[a].push_back(b);
to[b].push_back(a);
}
vector<string> s(n), t(n);
for (int i = 0; i < n; ++i) {
cin >> s[i];
if (s[i] == "-")
s[i] = "";
}
for (int i = 0; i < n; ++i) {
cin >> t[i];
if (t[i] == "-")
t[i] = "";
}
map<vector<string>, int> history1, history2;
solve(t, history2);
solve(s, history1);
int ret = INT_MAX;
map<vector<string>, int>::iterator it1, it2;
it1 = history1.begin();
it2 = history2.begin();
while (it1 != history1.end() && it2 != history2.end()) {
if (it1->first == it2->first)
ret = min(ret, it1->second + it2->second);
if (it1->first < it2->first)
++it1;
else
++it2;
}
cout << ret << endl;
}
} | #include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int n;
vector<vector<int>> to;
void solve(vector<string> start, map<vector<string>, int> &history) {
queue<vector<string>> q1;
q1.push(start);
history[start] = 0;
for (int a = 1; a <= 3; ++a) {
queue<vector<string>> q2;
while (!q1.empty()) {
vector<string> s = q1.front();
q1.pop();
for (int i = 0; i < n * 2; ++i) {
for (unsigned j = 0; j < to[i].size(); ++j) {
vector<string> t = s;
while (!t[i / 2].empty()) {
char c;
if (i % 2 == 0) {
c = t[i / 2][0];
t[i / 2] = t[i / 2].substr(1);
} else {
c = t[i / 2][t[i / 2].size() - 1];
t[i / 2] = t[i / 2].substr(0, t[i / 2].size() - 1);
}
if (to[i][j] % 2 == 0) {
t[to[i][j] / 2] = c + t[to[i][j] / 2];
} else {
t[to[i][j] / 2] += c;
}
if (history.find(t) == history.end()) {
q2.push(t);
history[t] = a;
}
}
}
}
}
q1 = q2;
}
}
int main() {
for (;;) {
int m;
cin >> n >> m;
if (n == 0)
return 0;
to.assign(2 * n, vector<int>());
for (int i = 0; i < m; ++i) {
string s, t;
cin >> s >> t;
int a = (s[0] - '0') * 2;
int b = (t[0] - '0') * 2;
if (s[1] == 'E')
++a;
if (t[1] == 'E')
++b;
to[a].push_back(b);
to[b].push_back(a);
}
vector<string> s(n), t(n);
for (int i = 0; i < n; ++i) {
cin >> s[i];
if (s[i] == "-")
s[i] = "";
}
for (int i = 0; i < n; ++i) {
cin >> t[i];
if (t[i] == "-")
t[i] = "";
}
map<vector<string>, int> history1, history2;
solve(t, history2);
solve(s, history1);
int ret = INT_MAX;
map<vector<string>, int>::iterator it1, it2;
it1 = history1.begin();
it2 = history2.begin();
while (it1 != history1.end() && it2 != history2.end()) {
if (it1->first == it2->first)
ret = min(ret, it1->second + it2->second);
if (it1->first < it2->first)
++it1;
else
++it2;
}
cout << ret << endl;
}
} | replace | 71 | 72 | 71 | 72 | TLE | |
p00841 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#define INF 2000000
#define t first
#define x second
using namespace std;
typedef pair<double, int> P;
double b, v, e, f;
int r;
double mk(int x) {
if (x >= r)
return 1.0 / (v - e * (x - r));
return 1.0 / (v - f * (r - x));
}
int main() {
while (1) {
int n, a[110];
cin >> n;
if (n == 0)
break;
for (int i = 1; i <= n; i++)
cin >> a[i];
cin >> b;
cin >> r >> v >> e >> f;
P dp[10100] = {};
for (int i = 1; i <= a[n]; i++)
dp[i] = make_pair(INF, 0);
for (int i = 0; i < n; i++) {
int tx;
double tt;
// No Change
tx = dp[a[i]].x, tt = dp[a[i]].t;
for (int j = a[i] + 1; j <= a[n]; j++) {
double k = mk(tx);
tt += k, tx++;
dp[j] = min(dp[j], make_pair(tt, tx));
}
// Change
tx = 0, tt = dp[a[i]].t + b;
for (int j = a[i] + 1; j <= a[n]; j++) {
double k = mk(tx);
tt += k;
tx++;
dp[j] = min(dp[j], make_pair(tt, tx));
}
}
printf("%.4f\n", dp[a[n]].t);
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <iostream>
#define INF 2000000
#define t first
#define x second
using namespace std;
typedef pair<double, int> P;
double b, v, e, f;
int r;
double mk(int x) {
if (x >= r)
return 1.0 / (v - e * (x - r));
return 1.0 / (v - f * (r - x));
}
int main() {
while (1) {
int n, a[110] = {};
cin >> n;
if (n == 0)
break;
for (int i = 1; i <= n; i++)
cin >> a[i];
cin >> b;
cin >> r >> v >> e >> f;
P dp[10100] = {};
for (int i = 1; i <= a[n]; i++)
dp[i] = make_pair(INF, 0);
for (int i = 0; i < n; i++) {
int tx;
double tt;
// No Change
tx = dp[a[i]].x, tt = dp[a[i]].t;
for (int j = a[i] + 1; j <= a[n]; j++) {
double k = mk(tx);
tt += k, tx++;
dp[j] = min(dp[j], make_pair(tt, tx));
}
// Change
tx = 0, tt = dp[a[i]].t + b;
for (int j = a[i] + 1; j <= a[n]; j++) {
double k = mk(tx);
tt += k;
tx++;
dp[j] = min(dp[j], make_pair(tt, tx));
}
}
printf("%.4f\n", dp[a[n]].t);
}
return 0;
} | replace | 19 | 20 | 19 | 20 | 0 | |
p00842 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int N, M;
int a[55][55];
int b[11111][11111];
vector<int> sw[1111];
void print() {
for (int i = 0; i < N + M; i++) {
for (int j = 0; j < N + M; j++)
cout << b[i][j] << " ";
cout << endl;
}
}
int main() {
while (cin >> N, N) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for (int i = 0; i < 55; i++)
sw[i].clear();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> a[i][j];
b[i][j] = a[i][j];
}
}
M = 0;
sw[0].push_back(0);
for (int i = 0; i < a[0][1] - 2; i++) {
sw[M].push_back(N + M + 1);
sw[M + 1].push_back(N + M);
M++;
}
sw[M].push_back(1);
M++;
for (int i = 0; i < M; i++) {
b[0][N + i] = b[N + i][0] = i + 1;
b[1][N + i] = b[N + i][1] = a[0][1] - i - 1;
for (int j = 0; j < M; j++)
b[N + i][N + j] = b[N + j][N + i] = abs(i - j);
}
for (int i = 2; i < N; i++) {
bool judge = false;
for (int j = 0; j < M; j++) {
bool flag = false;
for (int k = 0; k < i; k++) {
if (b[N + j][k] + 1 != b[k][i])
flag = true;
}
if (flag)
continue;
sw[j].push_back(i);
for (int k = 0; k < M; k++)
b[i][N + k] = b[N + k][i] = b[N + j][N + k] + 1;
judge = true;
break;
}
if (!judge) {
for (int j = 0; j < M; j++) {
bool check = false;
for (int k = 0; k < i - 1; k++) {
if (b[i][k] - b[N + j][k] != b[i][k + 1] - b[N + j][k + 1])
check = true;
}
if (check)
continue;
int m = M;
sw[j].push_back(N + M);
sw[M].push_back(j);
for (int k = 0; k < b[i][0] - b[N + j][0] - 2; k++) {
sw[M].push_back(N + M + 1);
sw[M + 1].push_back(N + M);
M++;
}
sw[M].push_back(i);
M++;
for (int k = m; k < M; k++) {
for (int l = 0; l < i; l++)
b[l][N + k] = b[N + k][l] = b[l][N + j] + k - m + 1;
for (int l = m; l < M; l++)
b[N + k][N + l] = b[N + l][N + k] = abs(k - l);
}
for (int k = m; k < M; k++) {
b[N + j][N + k] = b[N + k][N + j] = k - m + 1;
b[i][N + k] = b[N + k][i] = M - k;
for (int l = 0; l < m; l++)
b[N + l][N + k] = b[N + k][N + l] =
b[N + j][N + l] + b[N + j][N + k];
}
for (int k = 0; k < m; k++)
b[i][N + k] = b[N + k][i] =
b[N + j][N + k] + b[N + j][N + M - 1] + 1;
break;
}
}
}
vector<int> ans;
for (int i = 0; i < M; i++)
ans.push_back(sw[i].size());
sort(ans.begin(), ans.end());
for (int i = 0; i < M; i++)
cout << ans[i] << (i == M - 1 ? '\n' : ' ');
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int N, M;
int a[55][55];
int b[1111][1111];
vector<int> sw[1111];
void print() {
for (int i = 0; i < N + M; i++) {
for (int j = 0; j < N + M; j++)
cout << b[i][j] << " ";
cout << endl;
}
}
int main() {
while (cin >> N, N) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for (int i = 0; i < 55; i++)
sw[i].clear();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> a[i][j];
b[i][j] = a[i][j];
}
}
M = 0;
sw[0].push_back(0);
for (int i = 0; i < a[0][1] - 2; i++) {
sw[M].push_back(N + M + 1);
sw[M + 1].push_back(N + M);
M++;
}
sw[M].push_back(1);
M++;
for (int i = 0; i < M; i++) {
b[0][N + i] = b[N + i][0] = i + 1;
b[1][N + i] = b[N + i][1] = a[0][1] - i - 1;
for (int j = 0; j < M; j++)
b[N + i][N + j] = b[N + j][N + i] = abs(i - j);
}
for (int i = 2; i < N; i++) {
bool judge = false;
for (int j = 0; j < M; j++) {
bool flag = false;
for (int k = 0; k < i; k++) {
if (b[N + j][k] + 1 != b[k][i])
flag = true;
}
if (flag)
continue;
sw[j].push_back(i);
for (int k = 0; k < M; k++)
b[i][N + k] = b[N + k][i] = b[N + j][N + k] + 1;
judge = true;
break;
}
if (!judge) {
for (int j = 0; j < M; j++) {
bool check = false;
for (int k = 0; k < i - 1; k++) {
if (b[i][k] - b[N + j][k] != b[i][k + 1] - b[N + j][k + 1])
check = true;
}
if (check)
continue;
int m = M;
sw[j].push_back(N + M);
sw[M].push_back(j);
for (int k = 0; k < b[i][0] - b[N + j][0] - 2; k++) {
sw[M].push_back(N + M + 1);
sw[M + 1].push_back(N + M);
M++;
}
sw[M].push_back(i);
M++;
for (int k = m; k < M; k++) {
for (int l = 0; l < i; l++)
b[l][N + k] = b[N + k][l] = b[l][N + j] + k - m + 1;
for (int l = m; l < M; l++)
b[N + k][N + l] = b[N + l][N + k] = abs(k - l);
}
for (int k = m; k < M; k++) {
b[N + j][N + k] = b[N + k][N + j] = k - m + 1;
b[i][N + k] = b[N + k][i] = M - k;
for (int l = 0; l < m; l++)
b[N + l][N + k] = b[N + k][N + l] =
b[N + j][N + l] + b[N + j][N + k];
}
for (int k = 0; k < m; k++)
b[i][N + k] = b[N + k][i] =
b[N + j][N + k] + b[N + j][N + M - 1] + 1;
break;
}
}
}
vector<int> ans;
for (int i = 0; i < M; i++)
ans.push_back(sw[i].size());
sort(ans.begin(), ans.end());
for (int i = 0; i < M; i++)
cout << ans[i] << (i == M - 1 ? '\n' : ' ');
}
return 0;
} | replace | 6 | 7 | 6 | 7 | MLE | |
p00848 | C++ | Memory Limit Exceeded | // 37
#include <iostream>
#include <vector>
using namespace std;
int main() {
int cmp[1121] = {};
vector<int> prm;
for (int i = 2; i <= 1120; i++) {
if (!cmp[i]) {
prm.push_back(i);
for (int j = 2; i * j <= 1120; j++) {
cmp[i * j] = true;
}
}
}
static int dp[1121][1000][15];
dp[0][0][0] = 1;
for (int i = 0; i < 1121; i++) {
for (int j = 0; j <= prm.size(); j++) {
for (int l = j + 1; l <= prm.size(); l++) {
for (int k = 0; k < 14; k++) {
if (i + prm[l - 1] <= 1120) {
dp[i + prm[l - 1]][l][k + 1] += dp[i][j][k];
}
}
}
}
}
for (int n, k; cin >> n >> k, n | k;) {
int a = 0;
for (int i = 0; i <= prm.size(); i++) {
a += dp[n][i][k];
}
cout << a << endl;
}
return 0;
} | // 37
#include <iostream>
#include <vector>
using namespace std;
int main() {
int cmp[1121] = {};
vector<int> prm;
for (int i = 2; i <= 1120; i++) {
if (!cmp[i]) {
prm.push_back(i);
for (int j = 2; i * j <= 1120; j++) {
cmp[i * j] = true;
}
}
}
static int dp[1121][200][15];
dp[0][0][0] = 1;
for (int i = 0; i < 1121; i++) {
for (int j = 0; j <= prm.size(); j++) {
for (int l = j + 1; l <= prm.size(); l++) {
for (int k = 0; k < 14; k++) {
if (i + prm[l - 1] <= 1120) {
dp[i + prm[l - 1]][l][k + 1] += dp[i][j][k];
}
}
}
}
}
for (int n, k; cin >> n >> k, n | k;) {
int a = 0;
for (int i = 0; i <= prm.size(); i++) {
a += dp[n][i][k];
}
cout << a << endl;
}
return 0;
} | replace | 17 | 18 | 17 | 18 | MLE | |
p00848 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
vector<int> sieve_of_eratosthenes(int n) {
vector<int> primes(n);
for (int i = 2; i < n; ++i)
primes[i] = i;
for (int i = 2; i * i < n; ++i)
if (primes[i])
for (int j = i * i; j < n; j += i)
primes[j] = 0;
return primes;
}
int dp[15][1121][1121];
int main() {
vector<int> a = sieve_of_eratosthenes(1121);
vector<int> prime;
for (int i = 0; i < a.size(); i++)
if (a[i] != 0)
prime.push_back(a[i]);
int n, k;
while (cin >> n >> k && n != 0) {
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
for (int i = 0; i < k; i++) {
for (int j = 0; j < prime.size(); j++) {
for (int num = 0; num <= n; num++) {
if (dp[i][j][num] == 0)
continue;
for (int next = j; next < prime.size(); next++) {
if (num + prime[next] > n)
break;
dp[i + 1][next + 1][num + prime[next]] += dp[i][j][num];
}
}
}
}
int ans = 0;
for (int i = 0; i <= prime.size(); i++) {
ans += dp[k][i][n];
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
vector<int> sieve_of_eratosthenes(int n) {
vector<int> primes(n);
for (int i = 2; i < n; ++i)
primes[i] = i;
for (int i = 2; i * i < n; ++i)
if (primes[i])
for (int j = i * i; j < n; j += i)
primes[j] = 0;
return primes;
}
int dp[15][200][1121];
int main() {
vector<int> a = sieve_of_eratosthenes(1121);
vector<int> prime;
for (int i = 0; i < a.size(); i++)
if (a[i] != 0)
prime.push_back(a[i]);
int n, k;
while (cin >> n >> k && n != 0) {
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
for (int i = 0; i < k; i++) {
for (int j = 0; j < prime.size(); j++) {
for (int num = 0; num <= n; num++) {
if (dp[i][j][num] == 0)
continue;
for (int next = j; next < prime.size(); next++) {
if (num + prime[next] > n)
break;
dp[i + 1][next + 1][num + prime[next]] += dp[i][j][num];
}
}
}
}
int ans = 0;
for (int i = 0; i <= prime.size(); i++) {
ans += dp[k][i][n];
}
cout << ans << endl;
}
return 0;
} | replace | 17 | 18 | 17 | 18 | MLE | |
p00848 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> pn;
vector<bool> is_p;
void enum_prime() {
is_p.resize(1515, true);
is_p[0] = is_p[1] = false;
for (int i = 2; i <= 1515; i++) {
if (!is_p[i])
continue;
pn.push_back(i);
for (int j = i + i; j <= 1515; j += i)
is_p[j] = false;
}
}
int dp[1515][1515][15];
int solve(int n, int p, int k) {
if (k == 0 && n == 0)
return 1;
if (k < 0 || n < 0 || p < 0)
return 0;
int &ret = dp[n][p][k];
if (~ret)
return ret;
ret = solve(n, p - 1, k);
if (n >= pn[p])
ret += solve(n - pn[p], p - 1, k - 1);
return ret;
}
int main() {
enum_prime();
int n, k;
while (cin >> n >> k, n || k) {
memset(dp, -1, sizeof(dp));
int p = upper_bound(pn.begin(), pn.end(), n) - pn.begin() - 1;
cout << solve(n, p, k) << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> pn;
vector<bool> is_p;
void enum_prime() {
is_p.resize(1515, true);
is_p[0] = is_p[1] = false;
for (int i = 2; i <= 1515; i++) {
if (!is_p[i])
continue;
pn.push_back(i);
for (int j = i + i; j <= 1515; j += i)
is_p[j] = false;
}
}
int dp[1200][300][15];
int solve(int n, int p, int k) {
if (k == 0 && n == 0)
return 1;
if (k < 0 || n < 0 || p < 0)
return 0;
int &ret = dp[n][p][k];
if (~ret)
return ret;
ret = solve(n, p - 1, k);
if (n >= pn[p])
ret += solve(n - pn[p], p - 1, k - 1);
return ret;
}
int main() {
enum_prime();
int n, k;
while (cin >> n >> k, n || k) {
memset(dp, -1, sizeof(dp));
int p = upper_bound(pn.begin(), pn.end(), n) - pn.begin() - 1;
cout << solve(n, p, k) << endl;
}
return 0;
} | replace | 21 | 22 | 21 | 22 | MLE | |
p00848 | C++ | Memory Limit Exceeded | #include <iostream>
#include <vector>
using namespace std;
#define MAX_N 20
#define MAX_K 1355
long long dp[MAX_N + 1][MAX_K][300];
long long sum[MAX_N + 1][MAX_K];
int prime[MAX_K];
int main() {
prime[0] = 1;
prime[1] = 1;
for (int i = 2; i * i < MAX_K; i++) {
for (int j = i * i; j < MAX_K; j += i) {
prime[j] = 1;
}
}
for (int i = 0; i < MAX_K; i++)
prime[i] = 1 - prime[i];
vector<int> p;
p.push_back(0);
for (int i = 0; i < MAX_K; i++) {
if (prime[i] == 1) {
p.push_back(i);
}
}
dp[0][0][0] = 1;
for (int i = 0; i < MAX_N; i++) {
for (int j = 0; j < MAX_K; j++) {
for (int k = 0; k < p.size(); k++) {
for (int l = k + 1; l < p.size(); l++) {
if (j + p[l] >= MAX_K) {
continue;
}
dp[i + 1][j + p[l]][l] += dp[i][j][k];
}
}
}
}
for (int i = 0; i < MAX_N; i++) {
for (int j = 0; j < MAX_K; j++) {
for (int k = 0; k < p.size(); k++) {
sum[i][j] += dp[i][j][k];
}
}
}
int n, k;
while (true) {
cin >> n >> k;
if (n == 0 && k == 0) {
break;
}
cout << sum[k][n] << endl;
}
return 0;
} | #include <iostream>
#include <vector>
using namespace std;
#define MAX_N 20
#define MAX_K 1355
long long dp[MAX_N + 1][MAX_K][220];
long long sum[MAX_N + 1][MAX_K];
int prime[MAX_K];
int main() {
prime[0] = 1;
prime[1] = 1;
for (int i = 2; i * i < MAX_K; i++) {
for (int j = i * i; j < MAX_K; j += i) {
prime[j] = 1;
}
}
for (int i = 0; i < MAX_K; i++)
prime[i] = 1 - prime[i];
vector<int> p;
p.push_back(0);
for (int i = 0; i < MAX_K; i++) {
if (prime[i] == 1) {
p.push_back(i);
}
}
dp[0][0][0] = 1;
for (int i = 0; i < MAX_N; i++) {
for (int j = 0; j < MAX_K; j++) {
for (int k = 0; k < p.size(); k++) {
for (int l = k + 1; l < p.size(); l++) {
if (j + p[l] >= MAX_K) {
continue;
}
dp[i + 1][j + p[l]][l] += dp[i][j][k];
}
}
}
}
for (int i = 0; i < MAX_N; i++) {
for (int j = 0; j < MAX_K; j++) {
for (int k = 0; k < p.size(); k++) {
sum[i][j] += dp[i][j][k];
}
}
}
int n, k;
while (true) {
cin >> n >> k;
if (n == 0 && k == 0) {
break;
}
cout << sum[k][n] << endl;
}
return 0;
} | replace | 5 | 6 | 5 | 6 | MLE | |
p00854 | C++ | Runtime Error | #define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
int u, v;
ll w;
};
ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;
int f(int n, int k) { return n == 1 ? 0 : 1 + (k - 1 + f(n - 1, k)) % (n - 1); }
int main() {
for (;;) {
int n, k, m;
cin >> n >> k >> m;
cout << 1 + (m - 1 + f(n, k)) % n << endl;
}
} | #define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
int u, v;
ll w;
};
ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;
int f(int n, int k) { return n == 1 ? 0 : 1 + (k - 1 + f(n - 1, k)) % (n - 1); }
int main() {
for (;;) {
int n, k, m;
cin >> n >> k >> m;
if (n == 0)
break;
cout << 1 + (m - 1 + f(n, k)) % n << endl;
}
} | insert | 41 | 41 | 41 | 43 | -11 | |
p00854 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
bool isExist[10000];
int n, k, m;
int main() {
while (cin >> n >> k >> m) {
if (!n)
break;
int i;
for (i = 0; i < n; i++) {
isExist[i] = true;
}
int cur = m - 1;
isExist[cur] = false;
for (i = 1; i < n; i++) {
int step = (k - 1) % (n - i) + 1;
while (true) {
if (isExist[cur])
step--;
if (step <= 0)
break;
cur = (cur + 1) % n;
}
isExist[cur] = false;
}
cout << cur + 1 << endl;
}
return 0;
} | #include <iostream>
using namespace std;
bool isExist[10000];
int n, k, m;
int main() {
while (cin >> n >> k >> m) {
if (!n)
break;
int i;
for (i = 0; i < n; i++) {
isExist[i] = true;
}
int cur = m - 1;
isExist[cur] = false;
for (i = 1; i < n; i++) {
int step = (k - 1) % (n - i) + 1;
while (true) {
if (isExist[cur])
step--;
if (step <= 0)
break;
cur++;
if (cur == n)
cur = 0;
}
isExist[cur] = false;
}
cout << cur + 1 << endl;
}
return 0;
} | replace | 25 | 26 | 25 | 28 | TLE | |
p00854 | C++ | Runtime Error | #include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long int ll;
typedef double D; // ??§?¨?????????????double???long double?????????
typedef complex<D> P; // Point
typedef pair<P, P> L; // Line
typedef vector<P> VP;
const int INF = (1 << 30) - 1;
const D EPS = 1e-9;
#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)
int N, M, K;
void solve() {
vector<int> next_stones(N);
for (int i = 0; i < N; i++)
next_stones[i] = (i + 1) % N;
// first-remove
int cur = M - 1;
int pcur = (cur - 1 + N) % N;
int remained_stone = N;
while (remained_stone-- > 1) {
next_stones[pcur] = next_stones[cur];
for (int i = 0; i < K; i++) {
pcur = cur;
cur = next_stones[cur];
}
}
cout << cur + 1 << endl;
}
// g++ -std=c++0x -msse4.2 -O3
// #include <bits/stdc++.h>
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
while (cin >> N >> M >> K, N | M | K)
solve();
return 0;
} | #include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long int ll;
typedef double D; // ??§?¨?????????????double???long double?????????
typedef complex<D> P; // Point
typedef pair<P, P> L; // Line
typedef vector<P> VP;
const int INF = (1 << 30) - 1;
const D EPS = 1e-9;
#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)
int N, M, K;
void solve() {
vector<int> next_stones(N);
for (int i = 0; i < N; i++)
next_stones[i] = (i + 1) % N;
// first-remove
int cur = M - 1;
int pcur = (cur - 1 + N) % N;
int remained_stone = N;
while (remained_stone-- > 1) {
next_stones[pcur] = next_stones[cur];
for (int i = 0; i < K; i++) {
pcur = cur;
cur = next_stones[cur];
}
}
cout << cur + 1 << endl;
}
// g++ -std=c++0x -msse4.2 -O3
// #include <bits/stdc++.h>
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
while (cin >> N >> K >> M, N | K | M)
solve();
return 0;
} | replace | 60 | 61 | 60 | 61 | 0 | |
p00854 | C++ | Time Limit Exceeded | #include <iostream>
#include <list>
using std::cin;
using std::cout;
using std::endl;
using std::list;
int main(void) {
int n, k, m;
cin >> n >> k >> m;
while (n != 0 || k != 0 || m != 0) {
list<int> nums(n, 0);
int num = 1;
for (list<int>::iterator itr = nums.begin(); itr != nums.end(); ++itr) {
*itr = num;
num++;
}
int count_s = n;
int rm = m;
int l;
bool first = true;
for (list<int>::iterator itr = nums.begin(); count_s != 1;) {
if (first) {
for (int i = 0; i < rm - 1; i++) {
++itr;
if (itr == nums.end()) {
itr = nums.begin();
}
}
first = false;
} else {
for (int i = 0; i < rm; i++) {
++itr;
if (itr == nums.end()) {
itr = nums.begin();
}
}
}
itr = nums.erase(itr);
if (count_s == n) {
rm = k;
}
count_s--;
if (count_s != 1) {
--itr;
}
}
cout << *nums.begin() << endl;
cin >> n >> k >> m;
}
return 0;
} | #include <iostream>
#include <list>
using std::cin;
using std::cout;
using std::endl;
using std::list;
int main(void) {
int n, k, m;
cin >> n >> k >> m;
while (n != 0 || k != 0 || m != 0) {
list<int> nums(n, 0);
int num = 1;
for (list<int>::iterator itr = nums.begin(); itr != nums.end(); ++itr) {
*itr = num;
num++;
}
int count_s = n;
int rm = m;
int l;
bool first = true;
for (list<int>::iterator itr = nums.begin(); count_s != 1;) {
if (first) {
for (int i = 0; i < rm - 1; i++) {
++itr;
if (itr == nums.end()) {
itr = nums.begin();
}
}
first = false;
} else {
l = ((k - 1) % count_s) + 1;
for (int i = 0; i < l; i++) {
++itr;
if (itr == nums.end()) {
itr = nums.begin();
}
}
}
itr = nums.erase(itr);
if (count_s == n) {
rm = k;
}
count_s--;
if (count_s != 1) {
--itr;
}
}
cout << *nums.begin() << endl;
cin >> n >> k >> m;
}
return 0;
} | replace | 35 | 36 | 35 | 37 | TLE | |
p00854 | C++ | Runtime Error | #define DEBUG_ON
#define CONDITION true
using namespace std; /*{{{*/
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>
#define INF (1e9)
static const double PI = acos(-1.0);
static const double EPS = 1e-10;
typedef long long int LL;
typedef unsigned long long int ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<char> VC;
typedef vector<VC> VVC;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef complex<double> P;
#define FOR(i, b, e) for (typeof(e) i = (b); i != (e); i < (e) ? ++i : --i)
#define REP(i, n) FOR(i, 0, n)
#define IFC(c) \
if (c) \
continue;
#define IFB(c) \
if (c) \
break;
#define IFR(c, r) \
if (c) \
return r;
#define OPOVER(_op, _type) inline bool operator _op(const _type &t) const
#define arrsz(a) (sizeof(a) / sizeof(a[0]))
#define F first
#define S second
#define MP(a, b) make_pair(a, b)
#define SZ(a) ((LL)a.size())
#define PB(e) push_back(e)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(c, it) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define BIT(n) (assert(n < 64), (1ULL << (n)))
#define BITOF(n, m) (assert(m < 64), ((ULL)(n) >> (m)&1))
#define RANGE(a, b, c) ((a) <= (b) && (b) <= (c))
#define PQ priority_queue
#define SC static_cast
#ifdef DEBUG_ON
#define dprt(fmt, ...) \
if (CONDITION) \
fprintf(stderr, fmt, ##__VA_ARGS__)
#define darr(a) \
if (CONDITION) \
copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " ")); \
cerr << endl
#define darr_range(a, f, t) \
if (CONDITION) \
copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " ")); \
cerr << endl
#define dvec(v) \
if (CONDITION) \
copy(ALL(v), ostream_iterator<int>(cerr, " ")); \
cerr << endl
#define darr2(a, n, m) \
if (CONDITION) \
FOR(i, 0, (n)) { darr_range((a)[i], 0, (m)); }
#define dvec2(v) \
if (CONDITION) \
FOR(i, 0, SZ(v)) { dvec((v)[i]); }
#define WAIT() \
if (CONDITION) { \
string _wait_; \
cerr << "(hit return to continue)" << endl; \
getline(cin, _wait_); \
}
#define dump(x) \
if (CONDITION) \
cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl;
#define dumpf() \
if (CONDITION) \
cerr << __PRETTY_FUNCTION__ << endl;
#define dumpv(x) \
if (CONDITION) \
cerr << " [L:" << __LINE__ << "] " << #x << " = "; \
REP(q, (x).size()) cerr << (x)[q] << " "; \
cerr << endl;
#define where() \
if (CONDITION) \
cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__ \
<< "]" << endl;
#define show_bits(b, s) \
if (CONDITION) { \
REP(i, s) { \
cerr << BITOF(b, s - 1 - i); \
if (i % 4 == 3) \
cerr << ' '; \
} \
cerr << endl; \
}
#else
#define cerr \
if (0) \
cerr
#define dprt(fmt, ...)
#define darr(a)
#define darr_range(a, f, t)
#define dvec(v)
#define darr2(a, n, m)
#define dvec2(v)
#define WAIT()
#define dump(x)
#define dumpf()
#define dumpv(x)
#define where()
#define show_bits(b, s)
#endif
/* Inline functions */
inline int onbits_count(ULL b) {
int c = 0;
while (b != 0) {
c += (b & 1);
b >>= 1;
}
return c;
}
inline int bits_count(ULL b) {
int c = 0;
while (b != 0) {
++c;
b >>= 1;
}
return c;
}
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
inline double now() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (static_cast<double>(tv.tv_sec) +
static_cast<double>(tv.tv_usec) * 1e-6);
}
inline VS split(string s, char delimiter) {
VS v;
string t;
REP(i, s.length()) {
if (s[i] == delimiter)
v.PB(t), t = "";
else
t += s[i];
}
v.PB(t);
return v;
}
/* Tweaks */
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
return s << "(" << d.first << ", " << d.second << ")";
}
/* Frequent stuffs */
int n_dir = 4;
int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0}; /* CSS order */
enum direction { UP, RIGHT, DOWN, LEFT };
// int n_dir = 8;
// int dx[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};
// enum direction {
// UP, UPRIGHT, RIGHT, DOWNRIGHT, DOWN, DOWNLEFT, LEFT, UPLEFT
// }
#define FORDIR(d) REP(d, n_dir)
/*}}}*/
#define MAX 1000000
int main() {
std::ios_base::sync_with_stdio(false);
int n, k, m;
while (cin >> n >> k >> m, n | k | m) {
VI numbers(n);
REP(i, n) { numbers[i] = i + 1; }
VI::iterator it = numbers.begin() + m - 1;
it = numbers.erase(it) - 1;
while (numbers.size() > 1) {
it += k % numbers.size();
if (it >= numbers.end()) {
it -= numbers.size();
}
it = numbers.erase(it) - 1;
}
cout << numbers[0] << endl;
}
}
// vim: foldmethod=marker | #define DEBUG_ON
#define CONDITION true
using namespace std; /*{{{*/
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>
#define INF (1e9)
static const double PI = acos(-1.0);
static const double EPS = 1e-10;
typedef long long int LL;
typedef unsigned long long int ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<char> VC;
typedef vector<VC> VVC;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef complex<double> P;
#define FOR(i, b, e) for (typeof(e) i = (b); i != (e); i < (e) ? ++i : --i)
#define REP(i, n) FOR(i, 0, n)
#define IFC(c) \
if (c) \
continue;
#define IFB(c) \
if (c) \
break;
#define IFR(c, r) \
if (c) \
return r;
#define OPOVER(_op, _type) inline bool operator _op(const _type &t) const
#define arrsz(a) (sizeof(a) / sizeof(a[0]))
#define F first
#define S second
#define MP(a, b) make_pair(a, b)
#define SZ(a) ((LL)a.size())
#define PB(e) push_back(e)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(c, it) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define BIT(n) (assert(n < 64), (1ULL << (n)))
#define BITOF(n, m) (assert(m < 64), ((ULL)(n) >> (m)&1))
#define RANGE(a, b, c) ((a) <= (b) && (b) <= (c))
#define PQ priority_queue
#define SC static_cast
#ifdef DEBUG_ON
#define dprt(fmt, ...) \
if (CONDITION) \
fprintf(stderr, fmt, ##__VA_ARGS__)
#define darr(a) \
if (CONDITION) \
copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " ")); \
cerr << endl
#define darr_range(a, f, t) \
if (CONDITION) \
copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " ")); \
cerr << endl
#define dvec(v) \
if (CONDITION) \
copy(ALL(v), ostream_iterator<int>(cerr, " ")); \
cerr << endl
#define darr2(a, n, m) \
if (CONDITION) \
FOR(i, 0, (n)) { darr_range((a)[i], 0, (m)); }
#define dvec2(v) \
if (CONDITION) \
FOR(i, 0, SZ(v)) { dvec((v)[i]); }
#define WAIT() \
if (CONDITION) { \
string _wait_; \
cerr << "(hit return to continue)" << endl; \
getline(cin, _wait_); \
}
#define dump(x) \
if (CONDITION) \
cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl;
#define dumpf() \
if (CONDITION) \
cerr << __PRETTY_FUNCTION__ << endl;
#define dumpv(x) \
if (CONDITION) \
cerr << " [L:" << __LINE__ << "] " << #x << " = "; \
REP(q, (x).size()) cerr << (x)[q] << " "; \
cerr << endl;
#define where() \
if (CONDITION) \
cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__ \
<< "]" << endl;
#define show_bits(b, s) \
if (CONDITION) { \
REP(i, s) { \
cerr << BITOF(b, s - 1 - i); \
if (i % 4 == 3) \
cerr << ' '; \
} \
cerr << endl; \
}
#else
#define cerr \
if (0) \
cerr
#define dprt(fmt, ...)
#define darr(a)
#define darr_range(a, f, t)
#define dvec(v)
#define darr2(a, n, m)
#define dvec2(v)
#define WAIT()
#define dump(x)
#define dumpf()
#define dumpv(x)
#define where()
#define show_bits(b, s)
#endif
/* Inline functions */
inline int onbits_count(ULL b) {
int c = 0;
while (b != 0) {
c += (b & 1);
b >>= 1;
}
return c;
}
inline int bits_count(ULL b) {
int c = 0;
while (b != 0) {
++c;
b >>= 1;
}
return c;
}
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
inline double now() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (static_cast<double>(tv.tv_sec) +
static_cast<double>(tv.tv_usec) * 1e-6);
}
inline VS split(string s, char delimiter) {
VS v;
string t;
REP(i, s.length()) {
if (s[i] == delimiter)
v.PB(t), t = "";
else
t += s[i];
}
v.PB(t);
return v;
}
/* Tweaks */
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
return s << "(" << d.first << ", " << d.second << ")";
}
/* Frequent stuffs */
int n_dir = 4;
int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0}; /* CSS order */
enum direction { UP, RIGHT, DOWN, LEFT };
// int n_dir = 8;
// int dx[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};
// enum direction {
// UP, UPRIGHT, RIGHT, DOWNRIGHT, DOWN, DOWNLEFT, LEFT, UPLEFT
// }
#define FORDIR(d) REP(d, n_dir)
/*}}}*/
#define MAX 1000000
int main() {
std::ios_base::sync_with_stdio(false);
int n, k, m;
while (cin >> n >> k >> m, n | k | m) {
VI numbers(n);
REP(i, n) { numbers[i] = i + 1; }
VI::iterator it = numbers.begin() + m - 1;
it = numbers.erase(it) - 1;
while (numbers.size() > 1) {
it += k % numbers.size();
if (it >= numbers.end()) {
it -= numbers.size();
}
if (it < numbers.begin()) {
it += numbers.size();
}
it = numbers.erase(it) - 1;
}
cout << numbers[0] << endl;
}
}
// vim: foldmethod=marker | insert | 236 | 236 | 236 | 239 | -11 | |
p00854 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
int n, k, m;
while (scanf("%d%d%d", &n, &k, &m)) {
m--;
vector<int> v(n);
iota(v.begin(), v.end(), 1);
while (v.size() > 1) {
v.erase(v.begin() + m);
m = (m + k - 1) % v.size();
}
cout << v[0] << endl;
}
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
int n, k, m;
while (scanf("%d%d%d", &n, &k, &m), n) {
m--;
vector<int> v(n);
iota(v.begin(), v.end(), 1);
while (v.size() > 1) {
v.erase(v.begin() + m);
m = (m + k - 1) % v.size();
}
cout << v[0] << endl;
}
} | replace | 6 | 7 | 6 | 7 | -11 | |
p00854 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, m;
while (cin >> n >> k >> m, n || k || m) {
int stone[10000];
for (int i = 0; i < n; i++)
stone[i] = (i + 1) % n;
m--;
stone[m - 1] = stone[stone[m - 1]];
while (1) {
if (stone[m] == m)
break;
for (int i = 0; i < k - 1; i++)
m = stone[m];
stone[m] = stone[stone[m]];
}
cout << m + 1 << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, m;
while (cin >> n >> k >> m, n || k || m) {
int stone[10000];
for (int i = 0; i < n; i++)
stone[i] = (i + 1) % n;
m -= 2;
if (m < 0)
m += n;
stone[m] = stone[stone[m]];
while (stone[m] != m) {
for (int i = 0; i < k - 1; i++)
m = stone[m];
stone[m] = stone[stone[m]];
}
cout << m + 1 << endl;
}
return 0;
} | replace | 10 | 15 | 10 | 15 | TLE | |
p00854 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF (int)1e09
using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::list;
using std::make_pair;
using std::map;
using std::max;
using std::min;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set; // insert erase find count
using std::setprecision;
using std::stack;
using std::string;
using std::vector;
template <typename T> using Vec = vector<T>; // c++11
typedef pair<int, int> PII;
typedef pair<int, pair<int, int>> PIII;
int main(void) {
while (1) {
int n;
int k;
int m;
cin >> n >> k >> m;
if (n == 0 && k == 0 && m == 0) {
break;
}
Vec<int> list(n, 1);
int remain = n;
int active;
int now = m % n;
int check;
list[now] = 0;
remain--;
while (remain > 1) {
active = k % remain;
if (active == 0) {
active = k;
}
// cout << "ACTIVE = " << active << endl;
// cout << "HERE" << endl;
// cout << "REM = " << remain << endl;
while (1) {
check = now % n;
// cout << "CHECK = " << check << endl;
if (list[check] == 1) {
active--;
if (active == 0) {
list[check] = 0;
now = check;
// cout << "CHECK_END = " << check << endl;
break;
}
}
now++;
}
remain--;
}
for (int i = 0; i < n; i++) {
if (list[0] == 1) {
cout << n << endl;
break;
}
if (list[i] == 1) {
cout << i << endl;
break;
}
}
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF (int)1e09
using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::list;
using std::make_pair;
using std::map;
using std::max;
using std::min;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set; // insert erase find count
using std::setprecision;
using std::stack;
using std::string;
using std::vector;
template <typename T> using Vec = vector<T>; // c++11
typedef pair<int, int> PII;
typedef pair<int, pair<int, int>> PIII;
int main(void) {
while (1) {
int n;
int k;
int m;
cin >> n >> k >> m;
if (n == 0 && k == 0 && m == 0) {
break;
}
Vec<int> list(n, 1);
int remain = n;
int active;
int now = m % n;
int check;
list[now] = 0;
remain--;
while (remain > 1) {
active = k % remain;
if (active == 0) {
active = k;
}
// cout << "ACTIVE = " << active << endl;
// cout << "HERE" << endl;
// cout << "REM = " << remain << endl;
while (1) {
if (now == n) {
now = 0;
}
check = now;
// cout << "CHECK = " << check << endl;
if (list[check] == 1) {
active--;
if (active == 0) {
list[check] = 0;
now = check;
// cout << "CHECK_END = " << check << endl;
break;
}
}
now++;
}
remain--;
}
for (int i = 0; i < n; i++) {
if (list[0] == 1) {
cout << n << endl;
break;
}
if (list[i] == 1) {
cout << i << endl;
break;
}
}
}
return 0;
} | replace | 68 | 69 | 68 | 72 | TLE | |
p00854 | C++ | Time Limit Exceeded | #include <cstring>
#include <iostream>
using namespace std;
bool x[10000];
int a, b, c, d, e, p;
int main() {
while (true) {
memset(x, true, 10000);
cin >> a >> b >> c;
if (a == 0) {
break;
}
d = a - 1;
x[c - 1] = false;
p = c - 1;
while (d >= 2) {
e = b % d + d;
for (int i = 0; i < e; i++) {
while (true) {
p++;
if (x[p % a] == true) {
break;
}
}
}
x[p % a] = false;
d--;
}
for (int i = 0; i < a; i++) {
if (x[i] == true) {
p = i;
}
}
cout << p + 1 << endl;
}
} | #include <cstring>
#include <iostream>
using namespace std;
bool x[10000];
int a, b, c, d, e, p;
int main() {
while (true) {
memset(x, true, 10000);
cin >> a >> b >> c;
if (a == 0) {
break;
}
d = a - 1;
x[c - 1] = false;
p = c - 1;
while (d >= 2) {
e = b % d;
if (e == 0) {
e = d;
}
for (int i = 0; i < e; i++) {
while (true) {
p++;
if (x[p % a] == true) {
break;
}
}
}
x[p % a] = false;
d--;
}
for (int i = 0; i < a; i++) {
if (x[i] == true) {
p = i;
}
}
cout << p + 1 << endl;
}
} | replace | 16 | 17 | 16 | 20 | TLE | |
p00855 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v) \
cout << #v << ":"; \
rep(i, v.size()) cout << " " << v[i]; \
cout << endl
#define sset(s) \
cout << #s << ":"; \
each(i, s) cout << " " << i; \
cout << endl
using namespace std;
typedef pair<int, int> P;
const int MAX_N = 100005;
int prime[MAX_N];
bool is_prime[MAX_N];
int sieve(int n) {
int p = 0;
for (int i = 0; i <= n; i++) {
is_prime[i] = true;
}
is_prime[0] = is_prime[1] = false;
for (int i = 2; i <= n; i++) {
if (is_prime[i]) {
prime[p++] = i;
for (int j = 2 * i; j <= n; j += i) {
is_prime[j] = false;
}
}
}
return p;
}
int main() {
sieve(1299709);
while (1) {
int n;
cin >> n;
if (n == 0) {
break;
}
if (is_prime[n]) {
cout << "0\n";
continue;
}
int i = 0;
while (prime[i] < n) {
i++;
}
cout << prime[i] - prime[i - 1] << "\n";
}
} | #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v) \
cout << #v << ":"; \
rep(i, v.size()) cout << " " << v[i]; \
cout << endl
#define sset(s) \
cout << #s << ":"; \
each(i, s) cout << " " << i; \
cout << endl
using namespace std;
typedef pair<int, int> P;
const int MAX_N = 1300000;
int prime[MAX_N];
bool is_prime[MAX_N];
int sieve(int n) {
int p = 0;
for (int i = 0; i <= n; i++) {
is_prime[i] = true;
}
is_prime[0] = is_prime[1] = false;
for (int i = 2; i <= n; i++) {
if (is_prime[i]) {
prime[p++] = i;
for (int j = 2 * i; j <= n; j += i) {
is_prime[j] = false;
}
}
}
return p;
}
int main() {
sieve(1299709);
while (1) {
int n;
cin >> n;
if (n == 0) {
break;
}
if (is_prime[n]) {
cout << "0\n";
continue;
}
int i = 0;
while (prime[i] < n) {
i++;
}
cout << prime[i] - prime[i - 1] << "\n";
}
} | replace | 26 | 27 | 26 | 27 | -11 | |
p00855 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==
typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;
// →↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
const double EPS = 1e-9;
const int M = 1299800;
bool isPrime[M];
vector<int> primes;
void sieve() {
for (int i = 2; i < M; i++)
isPrime[i] = true;
for (int i = 2; i * i < M; i++) {
if (!isPrime[i])
continue;
for (int j = i * i; j < M; j += i) {
isPrime[j] = false;
}
}
for (int i = 2; i < M; i++) {
if (isPrime[i])
primes.push_back(i);
}
}
int main() {
sieve();
while (true) {
int n;
cin >> n;
if (isPrime[n]) {
cout << 0 << endl;
} else {
cout << (*upper_bound(all(primes), n)) -
*(upper_bound(all(primes), n) - 1)
<< endl;
}
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==
typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;
// →↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
const double EPS = 1e-9;
const int M = 1299800;
bool isPrime[M];
vector<int> primes;
void sieve() {
for (int i = 2; i < M; i++)
isPrime[i] = true;
for (int i = 2; i * i < M; i++) {
if (!isPrime[i])
continue;
for (int j = i * i; j < M; j += i) {
isPrime[j] = false;
}
}
for (int i = 2; i < M; i++) {
if (isPrime[i])
primes.push_back(i);
}
}
int main() {
sieve();
while (true) {
int n;
cin >> n;
if (n == 0)
break;
if (isPrime[n]) {
cout << 0 << endl;
} else {
cout << (*upper_bound(all(primes), n)) -
*(upper_bound(all(primes), n) - 1)
<< endl;
}
}
return 0;
} | insert | 61 | 61 | 61 | 63 | TLE | |
p00855 | C++ | Runtime Error |
#include <iostream>
#include <vector>
using namespace std;
int pmem[130000];
vector<int> prime;
void init() {
for (int i = 2; i < 1300000; i++) {
if (pmem[i])
continue;
pmem[i] = 1;
prime.push_back(i);
for (int j = i + i; j < 1300000; j += i)
pmem[j] = -1;
}
}
int main() {
loop:
init();
int n;
cin >> n;
if (!n)
return 0;
for (int i = 0; i < prime.size(); i++) {
if (prime[i] == n) {
cout << 0 << endl;
goto loop;
}
if (prime[i] > n) {
cout << prime[i] - prime[i - 1] << endl;
goto loop;
}
}
} |
#include <iostream>
#include <vector>
using namespace std;
int pmem[1300000];
vector<int> prime;
void init() {
for (int i = 2; i < 1300000; i++) {
if (pmem[i])
continue;
pmem[i] = 1;
prime.push_back(i);
for (int j = i + i; j < 1300000; j += i)
pmem[j] = -1;
}
}
int main() {
loop:
init();
int n;
cin >> n;
if (!n)
return 0;
for (int i = 0; i < prime.size(); i++) {
if (prime[i] == n) {
cout << 0 << endl;
goto loop;
}
if (prime[i] > n) {
cout << prime[i] - prime[i - 1] << endl;
goto loop;
}
}
} | replace | 5 | 6 | 5 | 6 | -11 | |
p00855 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define COUNT(i, n) for (int i = 1; i <= n; i++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for (ITER(c) it = (c).begin(); it != (c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
const int INF = 1 << 24;
int main(void) {
const int N = (int)1e6;
bool isP[N];
rep(i, N) isP[i] = true;
for (int i = 2; i * i < N; i++)
if (isP[i])
for (int j = i; i * j < N; j++)
isP[i * j] = false;
for (int n, r, res; cin >> n, n; cout << res << endl) {
r = n;
if (isP[n])
res = 0;
else {
while (!isP[--r])
;
while (!isP[++n])
;
}
res = n - r;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define COUNT(i, n) for (int i = 1; i <= n; i++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for (ITER(c) it = (c).begin(); it != (c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
const int INF = 1 << 24;
int main(void) {
const int N = 1299710;
bool isP[N];
rep(i, N) isP[i] = true;
for (int i = 2; i * i < N; i++)
if (isP[i])
for (int j = i; i * j < N; j++)
isP[i * j] = false;
for (int n, r, res; cin >> n, n; cout << res << endl) {
r = n;
if (isP[n])
res = 0;
else {
while (!isP[--r])
;
while (!isP[++n])
;
}
res = n - r;
}
return 0;
} | replace | 34 | 35 | 34 | 35 | 0 | |
p00855 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <iostream>
using namespace std;
#define MAX_LIST 100000000
int main() {
bool *prime = (bool *)malloc(sizeof(bool) * MAX_LIST);
// bool prime[MAX_LIST];
for (int i = 0; i < MAX_LIST; i++) {
prime[i] = 1;
}
prime[0] = prime[1] = 0;
for (int i = 0; i * i < MAX_LIST; i++) {
if (prime[i] == 1) {
for (int j = i * 2; j < MAX_LIST; j = j + i) {
prime[j] = 0;
}
}
}
int in;
while (cin >> in, in) {
int ans = 0;
int i = 0;
while (prime[in + i] == 0) {
ans++;
i++;
}
i = 0;
while (prime[in - i] == 0) {
ans++;
i++;
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
#define MAX_LIST 10000000
int main() {
bool *prime = (bool *)malloc(sizeof(bool) * MAX_LIST);
// bool prime[MAX_LIST];
for (int i = 0; i < MAX_LIST; i++) {
prime[i] = 1;
}
prime[0] = prime[1] = 0;
for (int i = 0; i * i < MAX_LIST; i++) {
if (prime[i] == 1) {
for (int j = i * 2; j < MAX_LIST; j = j + i) {
prime[j] = 0;
}
}
}
int in;
while (cin >> in, in) {
int ans = 0;
int i = 0;
while (prime[in + i] == 0) {
ans++;
i++;
}
i = 0;
while (prime[in - i] == 0) {
ans++;
i++;
}
cout << ans << endl;
}
return 0;
} | replace | 3 | 4 | 3 | 4 | MLE | |
p00855 | C++ | Runtime Error | #include <iostream>
using namespace std;
const int max_p = 1299709;
bool prime[max_p + 1];
void initial_prime() {
for (int i = 0; i < max_p; i++)
prime[i] = true;
prime[0] = prime[1] = false;
for (int i = 2; i * i < max_p; i++)
if (prime[i])
for (int j = 2 * i; j <= max_p; j += i)
prime[j] = false;
}
int main() {
int k;
initial_prime();
while (cin >> k, k) {
int l, r;
int ans = 0;
l = r = k;
while (!prime[l--])
ans++;
while (!prime[r++])
ans++;
cout << ans << endl;
}
return 0;
} | #include <iostream>
using namespace std;
const int max_p = 1300000;
bool prime[max_p + 1];
void initial_prime() {
for (int i = 0; i < max_p; i++)
prime[i] = true;
prime[0] = prime[1] = false;
for (int i = 2; i * i < max_p; i++)
if (prime[i])
for (int j = 2 * i; j <= max_p; j += i)
prime[j] = false;
}
int main() {
int k;
initial_prime();
while (cin >> k, k) {
int l, r;
int ans = 0;
l = r = k;
while (!prime[l--])
ans++;
while (!prime[r++])
ans++;
cout << ans << endl;
}
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00855 | C++ | Runtime Error | #define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i >= (b); i--)
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define all(a) a.begin(), a.end()
#define len(x) ((int)(x).size())
#define tmax(a, b, c) max((a), max((b), (c)))
#define tmin(a, b, c) min((a), min((b), (c)))
#define debug(x) cerr << #x << " is " << x << endl;
typedef pair<int, int> Pii;
typedef map<int, int> Mii;
typedef vector<int> Vi;
typedef long long ll;
const int inf = 2e9;
const ll ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;
int p[1322501];
int main() {
rep(i, 2, 1150) {
int j = i;
while (j < 1322500) {
j += i;
p[j]++;
}
}
while (1) {
int n;
cin >> n;
if (!n)
break;
if (!p[n])
cout << 0 << endl;
else {
int l = n, r = n;
while (p[l])
l--;
while (p[r])
r++;
cout << r - l << endl;
}
}
return 0;
} | #define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i >= (b); i--)
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define all(a) a.begin(), a.end()
#define len(x) ((int)(x).size())
#define tmax(a, b, c) max((a), max((b), (c)))
#define tmin(a, b, c) min((a), min((b), (c)))
#define debug(x) cerr << #x << " is " << x << endl;
typedef pair<int, int> Pii;
typedef map<int, int> Mii;
typedef vector<int> Vi;
typedef long long ll;
const int inf = 2e9;
const ll ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;
int p[1322501];
int main() {
rep(i, 2, 1150) {
int j = i;
while (j + i < 1322500) {
j += i;
p[j]++;
}
}
while (1) {
int n;
cin >> n;
if (!n)
break;
if (!p[n])
cout << 0 << endl;
else {
int l = n, r = n;
while (p[l])
l--;
while (p[r])
r++;
cout << r - l << endl;
}
}
return 0;
} | replace | 40 | 41 | 40 | 41 | -11 | |
p00855 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX 1000001
bool p[MAX];
int main() {
memset(p, 1, sizeof(p));
p[0] = p[1] = 0;
for (int i = 0; i < MAX; i++) {
if (p[i]) {
for (int j = 2 * i; j < MAX; j += i) {
p[j] = 0;
}
}
}
int n;
while (cin >> n, n) {
int next, prev;
if (p[n] == 1)
cout << 0 << endl;
else {
for (int i = n;; i++) {
if (p[i] == 1) {
next = i;
break;
}
}
for (int i = n;; i--) {
if (p[i] == 1) {
prev = i;
break;
}
}
cout << next - prev << endl;
}
}
return 0;
}
| #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX 10000001
bool p[MAX];
int main() {
memset(p, 1, sizeof(p));
p[0] = p[1] = 0;
for (int i = 0; i < MAX; i++) {
if (p[i]) {
for (int j = 2 * i; j < MAX; j += i) {
p[j] = 0;
}
}
}
int n;
while (cin >> n, n) {
int next, prev;
if (p[n] == 1)
cout << 0 << endl;
else {
for (int i = n;; i++) {
if (p[i] == 1) {
next = i;
break;
}
}
for (int i = n;; i--) {
if (p[i] == 1) {
prev = i;
break;
}
}
cout << next - prev << endl;
}
}
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p00855 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
#define N 1000000
bool prime[N + 1];
int main() {
for (int i = 2; i <= N; ++i)
prime[i] = true;
for (int i = 2; i <= N; ++i)
if (prime[i])
for (int j = 2; i * j <= N; ++j)
prime[i * j] = false;
int k;
while (cin >> k, k) {
int m = k, M = k;
while (!prime[m])
--m;
while (!prime[M])
++M;
cout << M - m << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
#define N 2000000
bool prime[N + 1];
int main() {
for (int i = 2; i <= N; ++i)
prime[i] = true;
for (int i = 2; i <= N; ++i)
if (prime[i])
for (int j = 2; i * j <= N; ++j)
prime[i * j] = false;
int k;
while (cin >> k, k) {
int m = k, M = k;
while (!prime[m])
--m;
while (!prime[M])
++M;
cout << M - m << endl;
}
return 0;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p00855 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
int main() {
vector<bool> prime(1000001, 1);
prime[1] = 0;
for (int i = 2; i < prime.size(); i++) {
if (prime[i] == 1) {
for (int j = i * 2; j < prime.size(); j += i)
prime[j] = 0;
}
}
// for(int i=0;i<prime.size();i++)if(prime[i]==1)cout<<i<<" ";
while (1) {
int n;
cin >> n;
if (n == 0)
break;
int ans = 0;
for (int i = 0; i < prime.size(); i++) {
if (prime[n + i] == 1)
break;
ans++;
}
for (int i = 0; i < prime.size(); i++) {
if (prime[n - i] == 1)
break;
ans++;
}
cout << ans << endl;
}
return 0;
} | #include <iostream>
#include <vector>
using namespace std;
int main() {
vector<bool> prime(1299709, 1);
prime[1] = 0;
for (int i = 2; i < prime.size(); i++) {
if (prime[i] == 1) {
for (int j = i * 2; j < prime.size(); j += i)
prime[j] = 0;
}
}
// for(int i=0;i<prime.size();i++)if(prime[i]==1)cout<<i<<" ";
while (1) {
int n;
cin >> n;
if (n == 0)
break;
int ans = 0;
for (int i = 0; i < prime.size(); i++) {
if (prime[n + i] == 1)
break;
ans++;
}
for (int i = 0; i < prime.size(); i++) {
if (prime[n - i] == 1)
break;
ans++;
}
cout << ans << endl;
}
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p00855 | C++ | Runtime Error | // Prime Gap
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> prime;
int main() {
prime.push_back(2);
for (ll i = 3; i < 1200000; i += 2) {
bool flag = true;
for (ll j = 0; j < prime.size() && i >= prime[j] * prime[j]; j++) {
if (i % prime[j] == 0) {
flag = false;
break;
}
}
if (flag)
prime.push_back(i);
}
while (true) {
int n;
cin >> n;
if (n == 0)
break;
ll to;
for (to = 0; n > prime[to]; to++)
;
if (n == prime[to])
cout << "0" << endl;
else
cout << prime[to] - prime[to - 1] << endl;
}
return 0;
} | // Prime Gap
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> prime;
int main() {
prime.push_back(2);
for (ll i = 3; i < 1300000; i += 2) {
bool flag = true;
for (ll j = 0; j < prime.size() && i >= prime[j] * prime[j]; j++) {
if (i % prime[j] == 0) {
flag = false;
break;
}
}
if (flag)
prime.push_back(i);
}
while (true) {
int n;
cin >> n;
if (n == 0)
break;
ll to;
for (to = 0; n > prime[to]; to++)
;
if (n == prime[to])
cout << "0" << endl;
else
cout << prime[to] - prime[to - 1] << endl;
}
return 0;
} | replace | 10 | 11 | 10 | 11 | 0 | |
p00855 | C++ | Runtime Error | #include <iostream>
#define N 1200000
using namespace std;
int start(int);
int goal(int);
int sosu[N];
int main() {
int i, j, n, s, g;
for (i = 0; i < N; i++)
sosu[i] = 1;
sosu[0] = 0;
sosu[1] = 0;
for (i = 0; i < N / 2; i++) {
if (sosu[i] == 1) {
for (j = i * 2; j < N; j += i) {
sosu[j] = 0;
}
}
}
while (1) {
cin >> n;
if (n == 0)
break;
if (sosu[n] != 1) {
s = start(n);
g = goal(n);
cout << g - s << endl;
} else
cout << 0 << endl;
}
return 0;
}
int start(int n) {
int i, result;
if (n % 2 == 0)
n--;
else
n -= 2;
for (i = n; i >= 2; i -= 2) {
if (sosu[i] == 1) {
result = i;
break;
}
}
return result;
}
int goal(int n) {
int i, result;
if (n % 2 == 0)
n++;
else
n += 2;
for (i = n; i < N; i += 2) {
if (sosu[i] == 1) {
result = i;
break;
}
}
return result;
} | #include <iostream>
#define N 1300000
using namespace std;
int start(int);
int goal(int);
int sosu[N];
int main() {
int i, j, n, s, g;
for (i = 0; i < N; i++)
sosu[i] = 1;
sosu[0] = 0;
sosu[1] = 0;
for (i = 0; i < N / 2; i++) {
if (sosu[i] == 1) {
for (j = i * 2; j < N; j += i) {
sosu[j] = 0;
}
}
}
while (1) {
cin >> n;
if (n == 0)
break;
if (sosu[n] != 1) {
s = start(n);
g = goal(n);
cout << g - s << endl;
} else
cout << 0 << endl;
}
return 0;
}
int start(int n) {
int i, result;
if (n % 2 == 0)
n--;
else
n -= 2;
for (i = n; i >= 2; i -= 2) {
if (sosu[i] == 1) {
result = i;
break;
}
}
return result;
}
int goal(int n) {
int i, result;
if (n % 2 == 0)
n++;
else
n += 2;
for (i = n; i < N; i += 2) {
if (sosu[i] == 1) {
result = i;
break;
}
}
return result;
} | replace | 1 | 2 | 1 | 2 | 0 | |
p00856 | C++ | Runtime Error | #include <iostream>
#include <stdio.h>
#define MAX_N 100
#define MAX_T 100
using namespace std;
double mbg(int N, int T, int L, int B, int Lose[], int Back[]) {
int t;
double p_goal = 0;
double ways[MAX_T + 1][MAX_N] = {{0}};
t = 0;
ways[0][0] = 1.0;
while (t < T) {
for (int i = 0; i < N; ++i) {
for (int j = 1; j <= 6; ++j) {
if ((i + j) <= N) {
for (int k = 0; k < L; ++k) {
if ((i + j) == Lose[k]) {
ways[t + 2][i + j] += ways[t][i] / 6;
goto escape1;
}
}
for (int k = 0; k < B; ++k) {
if ((i + j) == Back[k]) {
ways[t + 1][0] += ways[t][i] / 6;
goto escape1;
}
}
ways[t + 1][i + j] += ways[t][i] / 6;
escape1:;
} else {
for (int k = 0; k < L; ++k) {
if ((2 * N - (i + j)) == Lose[k]) {
ways[t + 2][2 * N - (i + j)] += ways[t][i] / 6;
goto escape2;
}
}
for (int k = 0; k < B; ++k) {
if ((2 * N - (i + j)) == Back[k]) {
ways[t + 1][0] += ways[t][i] / 6;
goto escape2;
}
}
ways[t + 1][2 * N - (i + j)] += ways[t][i] / 6;
escape2:;
}
}
}
t++;
}
for (int i = 1; i <= T; ++i) {
p_goal += ways[i][N];
}
return p_goal;
}
int main() {
int N, T, L, B;
while (1) {
int *Lose, *Back;
Lose = new int[MAX_N];
Back = new int[MAX_N];
cin >> N >> T >> L >> B;
if (N == 0 && T == 0 && L == 0 && B == 0)
break;
for (int i = 0; i < L; ++i)
cin >> Lose[i];
for (int i = 0; i < B; ++i)
cin >> Back[i];
printf("%.6f\n", mbg(N, T, L, B, Lose, Back));
delete[] Lose;
delete[] Back;
}
return 0;
} | #include <iostream>
#include <stdio.h>
#define MAX_N 100
#define MAX_T 100
using namespace std;
double mbg(int N, int T, int L, int B, int Lose[], int Back[]) {
int t;
double p_goal = 0;
double ways[MAX_T + 2][MAX_N + 1] = {{0}};
t = 0;
ways[0][0] = 1.0;
while (t < T) {
for (int i = 0; i < N; ++i) {
for (int j = 1; j <= 6; ++j) {
if ((i + j) <= N) {
for (int k = 0; k < L; ++k) {
if ((i + j) == Lose[k]) {
ways[t + 2][i + j] += ways[t][i] / 6;
goto escape1;
}
}
for (int k = 0; k < B; ++k) {
if ((i + j) == Back[k]) {
ways[t + 1][0] += ways[t][i] / 6;
goto escape1;
}
}
ways[t + 1][i + j] += ways[t][i] / 6;
escape1:;
} else {
for (int k = 0; k < L; ++k) {
if ((2 * N - (i + j)) == Lose[k]) {
ways[t + 2][2 * N - (i + j)] += ways[t][i] / 6;
goto escape2;
}
}
for (int k = 0; k < B; ++k) {
if ((2 * N - (i + j)) == Back[k]) {
ways[t + 1][0] += ways[t][i] / 6;
goto escape2;
}
}
ways[t + 1][2 * N - (i + j)] += ways[t][i] / 6;
escape2:;
}
}
}
t++;
}
for (int i = 1; i <= T; ++i) {
p_goal += ways[i][N];
}
return p_goal;
}
int main() {
int N, T, L, B;
while (1) {
int *Lose, *Back;
Lose = new int[MAX_N];
Back = new int[MAX_N];
cin >> N >> T >> L >> B;
if (N == 0 && T == 0 && L == 0 && B == 0)
break;
for (int i = 0; i < L; ++i)
cin >> Lose[i];
for (int i = 0; i < B; ++i)
cin >> Back[i];
printf("%.6f\n", mbg(N, T, L, B, Lose, Back));
delete[] Lose;
delete[] Back;
}
return 0;
} | replace | 9 | 10 | 9 | 10 | 0 | |
p00856 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
template <class T> void debug(T begin, T end) {
for (T i = begin; i != end; ++i)
cout << *i << " ";
cout << endl;
}
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int main() {
int N, T, L, B;
while (cin >> N >> T >> L >> B && N) {
bool lose[105] = {};
bool back[105] = {};
REP(i, L) {
int k;
cin >> k;
assert(k < 100);
lose[k] = true;
}
REP(i, B) {
int k;
cin >> k;
assert(k < 100);
back[k] = true;
}
double dp[102][101] = {};
dp[0][0] = 1;
REP(t, T + 1) {
REP(x, N) if (dp[t][x] > 0) {
for (int d = 1; d <= 6; d++) {
int next = x + d;
if (next > N)
next = N - (next - N);
if (lose[next]) {
dp[t + 2][next] += dp[t][x] * 1.0 / 6.0;
} else if (back[next]) {
dp[t + 1][0] += dp[t][x] * 1.0 / 6.0;
} else {
dp[t + 1][next] += dp[t][x] * 1.0 / 6.0;
}
}
}
// dp[t + 1][N] += dp[t][N];
}
double ans = 0;
REP(t, T) ans += dp[t + 1][N];
printf("%.10lf\n", ans);
// cout<<dp[T][N]<<endl;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
template <class T> void debug(T begin, T end) {
for (T i = begin; i != end; ++i)
cout << *i << " ";
cout << endl;
}
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int main() {
int N, T, L, B;
while (cin >> N >> T >> L >> B && N) {
bool lose[105] = {};
bool back[105] = {};
REP(i, L) {
int k;
cin >> k;
assert(k < 100);
lose[k] = true;
}
REP(i, B) {
int k;
cin >> k;
assert(k < 100);
back[k] = true;
}
double dp[102][101] = {};
dp[0][0] = 1;
REP(t, T) {
REP(x, N) if (dp[t][x] > 0) {
for (int d = 1; d <= 6; d++) {
int next = x + d;
if (next > N)
next = N - (next - N);
if (lose[next]) {
dp[t + 2][next] += dp[t][x] * 1.0 / 6.0;
} else if (back[next]) {
dp[t + 1][0] += dp[t][x] * 1.0 / 6.0;
} else {
dp[t + 1][next] += dp[t][x] * 1.0 / 6.0;
}
}
}
// dp[t + 1][N] += dp[t][N];
}
double ans = 0;
REP(t, T) ans += dp[t + 1][N];
printf("%.10lf\n", ans);
// cout<<dp[T][N]<<endl;
}
return 0;
} | replace | 52 | 53 | 52 | 53 | 0 | |
p00856 | C++ | Runtime Error | #include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
int N, T, L, B;
int next(int cur, int dice) {
int ret = cur + dice;
if (N < ret)
ret = N - (ret - N);
return ret;
}
int main() {
ios::sync_with_stdio(false);
while (cin >> N >> T >> L >> B && (N || T || L || B)) {
string LB(' ', 101);
int k;
for (int i = 0; i < L; ++i) {
cin >> k;
LB[k] = 'L';
}
for (int i = 0; i < B; ++i) {
cin >> k;
LB[k] = 'B';
}
double p[101][101][2] = {0};
p[0][0][0] = 1.0;
for (int i = 0; i < T; ++i) {
for (int j = 0; j < N; ++j) {
for (int k = 1; k <= 6; ++k) {
int ne = next(j, k);
if (LB[ne] == 'B')
ne = 0;
p[i + 1][ne][LB[ne] == 'L'] += p[i][j][0] / 6.0;
}
p[i + 1][j][0] += p[i][j][1];
}
}
double ans = 0;
for (int i = 0; i <= T; ++i)
ans += p[i][N][1] + p[i][N][0];
cout << setprecision(6) << fixed << ans << endl;
}
return 0;
} | #include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
int N, T, L, B;
int next(int cur, int dice) {
int ret = cur + dice;
if (N < ret)
ret = N - (ret - N);
return ret;
}
int main() {
ios::sync_with_stdio(false);
while (cin >> N >> T >> L >> B && (N || T || L || B)) {
string LB = " "
" ";
int k;
for (int i = 0; i < L; ++i) {
cin >> k;
LB[k] = 'L';
}
for (int i = 0; i < B; ++i) {
cin >> k;
LB[k] = 'B';
}
double p[101][101][2] = {0};
p[0][0][0] = 1.0;
for (int i = 0; i < T; ++i) {
for (int j = 0; j < N; ++j) {
for (int k = 1; k <= 6; ++k) {
int ne = next(j, k);
if (LB[ne] == 'B')
ne = 0;
p[i + 1][ne][LB[ne] == 'L'] += p[i][j][0] / 6.0;
}
p[i + 1][j][0] += p[i][j][1];
}
}
double ans = 0;
for (int i = 0; i <= T; ++i)
ans += p[i][N][1] + p[i][N][0];
cout << setprecision(6) << fixed << ans << endl;
}
return 0;
} | replace | 19 | 20 | 19 | 21 | 0 | |
p00856 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define MAX 101
typedef pair<int, int> pii;
int N, T, L, B;
int arr[MAX];
double solve() {
double dp[MAX][MAX]; // dp[ターン数][どのマスにいるか]
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (int i = 1; i <= T; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < 6; k++) {
if (dp[i - 1][j] == 0)
continue;
int next = j + (k + 1);
if (next > N) {
next = next - 2 * (next - N);
}
switch (arr[next]) {
case 0:
dp[i][next] += dp[i - 1][j] / 6;
break;
case 1:
dp[i + 1][next] += dp[i - 1][j] / 6;
break;
case 2:
dp[i][0] += dp[i - 1][j] / 6;
break;
}
}
}
}
double res = 0;
for (int i = 1; i <= T; i++) {
res += dp[i][N];
}
return res;
}
int main() {
int in;
while (cin >> N >> T >> L >> B, N) {
memset(arr, 0, sizeof(arr));
for (int i = 0; i < L; i++) {
cin >> in;
arr[in] = 1;
}
for (int i = 0; i < B; i++) {
cin >> in;
arr[in] = 2;
}
printf("%.8f\n", solve());
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define MAX 501
typedef pair<int, int> pii;
int N, T, L, B;
int arr[MAX];
double solve() {
double dp[MAX][MAX]; // dp[ターン数][どのマスにいるか]
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (int i = 1; i <= T; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < 6; k++) {
if (dp[i - 1][j] == 0)
continue;
int next = j + (k + 1);
if (next > N) {
next = next - 2 * (next - N);
}
switch (arr[next]) {
case 0:
dp[i][next] += dp[i - 1][j] / 6;
break;
case 1:
dp[i + 1][next] += dp[i - 1][j] / 6;
break;
case 2:
dp[i][0] += dp[i - 1][j] / 6;
break;
}
}
}
}
double res = 0;
for (int i = 1; i <= T; i++) {
res += dp[i][N];
}
return res;
}
int main() {
int in;
while (cin >> N >> T >> L >> B, N) {
memset(arr, 0, sizeof(arr));
for (int i = 0; i < L; i++) {
cin >> in;
arr[in] = 1;
}
for (int i = 0; i < B; i++) {
cin >> in;
arr[in] = 2;
}
printf("%.8f\n", solve());
}
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00858 | C++ | Runtime Error | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;
/* ??????????????¬ */
#include <complex>
typedef long double ld;
typedef complex<ld> Point;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const ld pi = acos(-1.0);
const ld dtop = pi / 180.;
const ld ptod = 1. / dtop;
namespace std {
bool operator<(const Point &lhs, const Point &rhs) {
if (lhs.real() < rhs.real() - eps)
return true;
if (lhs.real() > rhs.real() + eps)
return false;
return lhs.imag() < rhs.imag();
}
} // namespace std
// ????????\???
Point input_point() {
ld x, y;
cin >> x >> y;
return Point(x, y);
}
// ????????????????????????
bool eq(const ld a, const ld b) { return (abs(a - b) < eps); }
// ??????
ld dot(const Point &a, const Point &b) { return real(conj(a) * b); }
// ??????
ld cross(const Point &a, const Point &b) { return imag(conj(a) * b); }
// ??´????????????
class Line {
public:
Point a, b;
Line() : a(Point(0, 0)), b(Point(0, 0)) {}
Line(Point a, Point b) : a(a), b(b) {}
Point operator[](const int _num) const {
if (_num == 0)
return a;
else if (_num == 1)
return b;
else {
assert(false);
return Point();
}
}
};
// ????????????
class Circle {
public:
Point p;
ld r;
Circle() : p(Point(0, 0)), r(0) {}
Circle(Point p, ld r) : p(p), r(r) {}
};
// CCW
int ccw(const Point &a, const Point &b, const Point &c) {
const Point nb(b - a);
const Point nc(c - a);
if (cross(nb, nc) > eps)
return 1; // a,b,c??????????¨???¨?????????????????¶
if (cross(nb, nc) < -eps)
return -1; // a,b,c???????¨???¨?????????????????¶
if (dot(nb, nc) < 0)
return 2; // c,a,b???????????´???????????¶
if (norm(nb) < norm(nc))
return -2; // a,b,c???????????´???????????¶
return 0; // a,c,b???????????´???????????¶
}
/* ???????????? */
// ??´?????¨??´??????????????????
bool isis_ll(const Line &l, const Line &m) {
return !eq(cross(l.b - l.a, m.b - m.a), 0);
}
// ??´?????¨?????????????????????
bool isis_ls(const Line &l, const Line &s) {
return isis_ll(l, s) &&
(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);
}
// ????????¨?????????????????????
bool isis_ss(const Line &s, const Line &t) {
return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}
// ????????´????????????
bool isis_lp(const Line &l, const Point &p) {
return (abs(cross(l.b - p, l.a - p)) < eps);
}
// ?????????????????????
bool isis_sp(const Line &s, const Point &p) {
ld n = (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);
return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);
}
// ??????????¶?
Point proj(const Line &l, const Point &p) {
ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
return l.a + t * (l.a - l.b);
}
// ??´?????¨??´????????????
Point is_ll(const Line &s, const Line &t) {
Point sv = s.b - s.a, tv = t.b - t.a;
assert(cross(sv, tv) != 0);
return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);
}
// ??´?????¨??´????????????
vector<Point> is_ll2(const Line &s, const Line &t) {
Point sv = s.b - s.a, tv = t.b - t.a;
if (cross(sv, tv) != 0)
return vector<Point>(1, is_ll(s, t));
else {
vector<Point> ans;
for (int k = 0; k < 2; ++k) {
if (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end())
ans.push_back(t[k]);
if (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end())
ans.push_back(s[k]);
}
return ans;
}
}
// ????????¨???????????????
//???????????£????????¨???????????¨assert(false)
Point is_ss(const Line &s, const Line &t) {
if (isis_ss(s, t)) {
for (int k = 0; k < 2; ++k) {
for (int l = 0; l < 2; ++l) {
if (s[k] == t[l])
return s[k];
}
}
return is_ll(s, t);
} else {
//??????isis_ss?????????
assert(false);
return Point(0, 0);
}
}
// ????????¨???????????????
vector<Point> is_ss2(const Line &s, const Line &t) {
vector<Point> kouho(is_ll2(s, t));
vector<Point> ans;
for (auto p : kouho) {
if (isis_sp(s, p) && isis_sp(t, p))
ans.emplace_back(p);
}
return ans;
}
// ??´?????¨???????????¢
ld dist_lp(const Line &l, const Point &p) { return abs(p - proj(l, p)); }
// ??´?????¨??´???????????¢
ld dist_ll(const Line &l, const Line &m) {
return isis_ll(l, m) ? 0 : dist_lp(l, m.a);
}
// ??´?????¨??????????????¢
ld dist_ls(const Line &l, const Line &s) {
return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));
}
// ????????¨???????????¢
ld dist_sp(const Line &s, const Point &p) {
Point r = proj(s, p);
return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));
}
// ????????¨??????????????¢
ld dist_ss(const Line &s, const Line &t) {
if (isis_ss(s, t))
return 0;
return min(
{dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b)});
}
//??´?????¨??´?????????????????????????????????
Line bisection(const Line &s, const Line &t) {
const Point laglanju(is_ll(s, t));
const Point avec =
!(abs(laglanju - s[0]) < eps) ? s[0] - laglanju : s[1] - laglanju;
const Point bvec =
!(abs(laglanju - t[0]) < eps) ? t[0] - laglanju : t[1] - laglanju;
return Line(laglanju, laglanju + (abs(bvec) * avec + abs(avec) * bvec) /
(abs(avec) + abs(bvec)));
}
//???????????´?????????????????????
//???????????´??????????????§???????????¨????¢?????????¨?????????
Point inner_center(const vector<Line> &ls) {
vector<Point> vertics;
for (int i = 0; i < static_cast<int>(ls.size()); ++i) {
vertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));
}
if (vertics[0] == vertics[1] || vertics[1] == vertics[2] ||
vertics[2] == vertics[0])
return vertics[0];
Line bi1(
bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2])));
Line bi2(
bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0])));
if (bi1[0] == bi2[0])
return bi1[0];
else {
return is_ll(bi1, bi2);
}
}
//???????????´?????????????????????
//???????????´??????????????§???????????¨????¢?????????¨?????????
vector<Point> ex_center(const vector<Line> &ls) {
vector<Point> vertics;
for (int i = 0; i < static_cast<int>(ls.size()); ++i) {
vertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));
}
if (abs(vertics[0] - vertics[1]) < eps ||
abs(vertics[1] - vertics[2]) < eps ||
(abs(vertics[2] - vertics[0]) < eps))
return vector<Point>();
vector<Point> ecs;
for (int i = 0; i < 3; ++i) {
Line bi1(
bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]),
Line(vertics[i], vertics[(i + 1) % 3])));
Line bi2(bisection(Line(vertics[(i + 1) % 3],
vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]),
Line(vertics[(i + 1) % 3], vertics[i])));
ecs.push_back(is_ll(bi1, bi2));
}
return ecs;
}
// a,b:??????
// c:????????§??????
//???????????´?????????????????¢?????????????±??????????
vector<Point> same_dis(const vector<Line> &ls) {
vector<Point> vertics;
vertics.push_back(is_ll(ls[0], ls[2]));
vertics.push_back(is_ll(ls[1], ls[2]));
if (abs(vertics[0] - vertics[1]) < eps)
return vector<Point>{vertics[0]};
Line bis(bisection(ls[0], ls[1]));
vector<Point> ecs;
Line abi(bisection(Line(vertics[0], vertics[1]), ls[0]));
ecs.push_back(is_ll(bis, abi));
Line bbi(bisection(Line(vertics[0], 2.l * vertics[0] - vertics[1]), ls[0]));
ecs.push_back(is_ll(bis, bbi));
return ecs;
}
/* ??? */
// ?????¨????????????
vector<Point> is_cc(const Circle &c1, const Circle &c2) {
vector<Point> res;
ld d = abs(c1.p - c2.p);
ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);
ld dfr = c1.r * c1.r - rc * rc;
if (abs(dfr) < eps)
dfr = 0.0;
else if (dfr < 0.0)
return res; // no intersection
ld rs = sqrt(dfr);
Point diff = (c2.p - c1.p) / d;
res.push_back(c1.p + diff * Point(rc, rs));
if (dfr != 0.0)
res.push_back(c1.p + diff * Point(rc, -rs));
return res;
}
//???????????????????????????
// 0 => out
// 1 => on
// 2 => in
int is_in_circle(const Circle &cir, const Point &p) {
ld dis = abs(cir.p - p);
if (dis > cir.r + eps)
return 0;
else if (dis < cir.r - eps)
return 2;
else
return 1;
}
//???lc??????rc??????????????????
// 0 => out
// 1 => on
// 2 => in
int circle_in_circle(const Circle &lc, const Circle &rc) {
ld dis = abs(lc.p - rc.p);
if (dis < rc.r - lc.r - eps)
return 2;
else if (dis > rc.r - lc.r + eps)
return 0;
else
return 1;
}
// ?????¨??´????????????
vector<Point> is_lc(const Circle &c, const Line &l) {
vector<Point> res;
ld d = dist_lp(l, c.p);
if (d < c.r + eps) {
ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); // safety;
Point nor = (l.a - l.b) / abs(l.a - l.b);
res.push_back(proj(l, c.p) + len * nor);
res.push_back(proj(l, c.p) - len * nor);
}
return res;
}
// ?????¨??????????????¢
vector<Point> is_sc(const Circle &c, const Line &l) {
vector<Point> v = is_lc(c, l), res;
for (Point p : v)
if (isis_sp(l, p))
res.push_back(p);
return res;
}
// ?????¨????????\???
vector<Line> tangent_cp(const Circle &c, const Point &p) {
vector<Line> ret;
Point v = c.p - p;
ld d = abs(v);
ld l = sqrt(norm(v) - c.r * c.r);
if (isnan(l)) {
return ret;
}
Point v1 = v * Point(l / d, c.r / d);
Point v2 = v * Point(l / d, -c.r / d);
ret.push_back(Line(p, p + v1));
if (l < eps)
return ret;
ret.push_back(Line(p, p + v2));
return ret;
}
// ?????¨????????\???
vector<Line> tangent_cc(const Circle &c1, const Circle &c2) {
vector<Line> ret;
if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {
Point center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);
ret = tangent_cp(c1, center);
}
if (abs(c1.r - c2.r) > eps) {
Point out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);
vector<Line> nret = tangent_cp(c1, out);
ret.insert(ret.end(), ALL(nret));
} else {
Point v = c2.p - c1.p;
v /= abs(v);
Point q1 = c1.p + v * Point(0, 1) * c1.r;
Point q2 = c1.p + v * Point(0, -1) * c1.r;
ret.push_back(Line(q1, q1 + v));
ret.push_back(Line(q2, q2 + v));
}
return ret;
}
//??????????????????????????¢???
ld two_circle_area(const Circle &l, const Circle &r) {
ld dis = abs(l.p - r.p);
if (dis > l.r + r.r)
return 0;
else if (dis + r.r < l.r) {
return r.r * r.r * pi;
} else if (dis + l.r < r.r) {
return l.r * l.r * pi;
} else {
ld ans = (l.r) * (l.r) *
acos((dis * dis + l.r * l.r - r.r * r.r) / (2 * dis * l.r)) +
(r.r) * (r.r) *
acos((dis * dis + r.r * r.r - l.r * l.r) / (2 * dis * r.r)) -
sqrt(4 * dis * dis * l.r * l.r -
(dis * dis + l.r * l.r - r.r * r.r) *
(dis * dis + l.r * l.r - r.r * r.r)) /
2;
return ans;
}
}
/* ????§???¢ */
typedef vector<Point> Polygon;
// ??¢???
ld area(const Polygon &p) {
ld res = 0;
int n = p.size();
REP(j, n) res += cross(p[j], p[(j + 1) % n]);
return res / 2;
}
// ????§???¢????????¢??????
bool is_counter_clockwise(const Polygon &poly) {
ld angle = 0;
int n = poly.size();
REP(i, n) {
Point a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];
angle += arg((c - b) / (b - a));
}
return angle > eps;
}
// ??????????????????
// 0 => out
// 1 => on
// 2 => in
int is_in_polygon(const Polygon &poly, const Point &p) {
ld angle = 0;
int n = poly.size();
REP(i, n) {
Point a = poly[i], b = poly[(i + 1) % n];
if (isis_sp(Line(a, b), p))
return 1;
angle += arg((b - p) / (a - p));
}
return eq(angle, 0) ? 0 : 2;
}
//??????????????????2?????????
enum { OUT, ON, IN };
int convex_contains(const Polygon &P, const Point &p) {
const int n = P.size();
Point g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point
int a = 0, b = n;
while (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b]
int c = (a + b) / 2;
if (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg
if (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0)
b = c;
else
a = c;
} else {
if (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0)
a = c;
else
b = c;
}
}
b %= n;
if (cross(P[a] - p, P[b] - p) < 0)
return 0;
if (cross(P[a] - p, P[b] - p) > 0)
return 2;
return 1;
}
// ??????
// ???????????????????????¨????????????????????§??¨???
Polygon convex_hull(vector<Point> ps) {
int n = ps.size();
int k = 0;
sort(ps.begin(), ps.end());
Polygon ch(2 * n);
for (int i = 0; i < n; ch[k++] = ps[i++])
while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
--k;
for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])
while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
--k;
ch.resize(k - 1);
return ch;
}
// ????????????
vector<Polygon> convex_cut(const Polygon &ps, const Line &l) {
int n = ps.size();
Polygon Q;
Polygon R;
REP(i, n) {
Point A = ps[i], B = ps[(i + 1) % n];
Line m = Line(A, B);
if (ccw(l.a, l.b, A) != -1)
Q.push_back(A);
if (ccw(l.a, l.b, A) != 1)
R.push_back(A);
if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m)) {
Q.push_back(is_ll(l, m));
R.push_back(is_ll(l, m));
}
}
const vector<Polygon> polys{Q, R};
return polys;
}
/* ??¢??¬??????????????? */
void add_point(vector<Point> &ps, const Point p) {
for (Point q : ps)
if (abs(q - p) < eps)
return;
ps.push_back(p);
}
typedef ld Weight;
struct Edge {
int src, dst;
Weight weight;
Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
void add_edge(Graph &g, const int from, const int to, const Weight weight) {
g[from].push_back(Edge{from, to, weight});
}
pair<vector<Point>, Graph> segment_arrangement(const vector<Line> &s,
const vector<Point> &p) {
int n = p.size(), m = s.size();
Graph g(n);
REP(i, m) {
vector<pair<ld, int>> vec;
REP(j, n) if (isis_sp(s[i], p[j])) vec.emplace_back(abs(s[i].a - p[j]), j);
sort(ALL(vec));
REP(j, vec.size() - 1) {
int from = vec[j].second, to = vec[j + 1].second;
add_edge(g, from, to, static_cast<Weight>(abs(p[from] - p[to])));
add_edge(g, to, from, static_cast<Weight>(abs(p[from] - p[to])));
}
}
auto a(make_pair(p, g));
return a;
}
pair<vector<Point>, Graph> sennbunn_arrangement(const vector<Line> &s) {
vector<Point> crss;
for (int i = 0; i < static_cast<int>(s.size()); ++i) {
for (int j = i + 1; j < static_cast<int>(s.size()); ++j) {
if (isis_ss(s[i], s[j])) {
crss.push_back(is_ll(s[i], s[j]));
}
}
}
for (int i = 0; i < static_cast<int>(s.size()); ++i) {
crss.push_back(s[i][0]);
crss.push_back(s[i][1]);
}
sort(crss.begin(), crss.end());
crss.erase(unique(crss.begin(), crss.end()), crss.end());
return segment_arrangement(s, crss);
}
pair<bool, vector<Weight>> spfa(const int v_num, const vector<vector<Edge>> &es,
const int start) {
vector<Weight> diss(v_num, INT_MAX);
queue<int> que;
vector<bool> use(v_num);
vector<int> count(v_num);
que.emplace(start);
diss[start] = 0;
while (!que.empty()) {
int src(que.front());
que.pop();
use[src] = false;
for (auto e : es[src]) {
const int d = e.dst;
if (diss[src] + e.weight < diss[d]) {
diss[d] = diss[src] + e.weight;
if (!use[d]) {
use[d] = true;
count[d]++;
if (count[d] >= v_num)
return make_pair(false, vector<Weight>());
que.emplace(d);
}
}
}
}
return make_pair(true, diss);
}
struct aa {
int now;
ld time;
};
class Compare {
public:
// aa?????????????????¶
bool operator()(const aa &l, const aa &r) { return l.time > r.time; }
};
int main() {
while (1) {
int N;
cin >> N;
if (!N)
break;
int xs, ys, xg, yg;
cin >> xs >> ys >> xg >> yg;
vector<Line> ls;
Point start(xs, ys), goal(xg, yg);
for (int i = 0; i < N; ++i) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
ls.emplace_back(Point(x1, y1), Point(x2, y2));
}
vector<Line> roads;
vector<Line> signs;
for (int i = 0; i < N; ++i) {
int touchnum = 0;
for (int j = 0; j < 2; ++j) {
Point p(ls[i][j]);
bool touch = false;
for (int k = 0; k < N; ++k) {
if (i == k)
continue;
else {
if (isis_sp(ls[k], p))
touch = true;
}
}
if (touch)
touchnum++;
}
if (touchnum == 2)
roads.emplace_back(ls[i]);
else
signs.emplace_back(ls[i]);
}
auto pa(sennbunn_arrangement(roads));
Graph g(pa.second);
vector<Point> ps(pa.first);
int startid, goalid;
for (int i = 0; i < ps.size(); ++i) {
Point p(ps[i]);
if (p == start)
startid = i;
else if (p == goal)
goalid = i;
}
for (auto &es : g) {
for (auto &e : es) {
Line l(ps[e.src], ps[e.dst]);
for (int k = 0; k < signs.size(); ++k) {
Line si(signs[k]);
if (isis_ss(l, si)) {
if (isis_sp(l, si[1]))
swap(si.a, si.b);
ld theta = (dot(l[1] - l[0], si[1] - si[0]));
if (theta > -eps) {
e.weight = 1e18;
}
}
}
}
}
vector<ld> times(g.size(), 1e18);
vector<vector<int>> memos(g.size());
memos[startid].push_back(startid);
priority_queue<aa, vector<aa>, Compare> que;
times[startid] = 0;
que.push(aa{startid, 0});
while (!que.empty()) {
aa atop(que.top());
que.pop();
if (atop.now == goalid)
break;
for (auto e : g[atop.now]) {
if (times[e.dst] > atop.time + e.weight) {
times[e.dst] = atop.time + e.weight;
memos[e.dst] = memos[e.src];
memos[e.dst].emplace_back(e.dst);
que.push(aa{e.dst, atop.time + e.weight});
}
}
}
if (memos[goalid].empty()) {
cout << -1 << endl;
} else {
for (auto m : memos[goalid]) {
int x = int((ps[m].real() + eps));
int y = int(ps[m].imag() + eps);
cout << x << " " << y << endl;
}
cout << 0 << endl;
}
}
return 0;
} | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;
/* ??????????????¬ */
#include <complex>
typedef long double ld;
typedef complex<ld> Point;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const ld pi = acos(-1.0);
const ld dtop = pi / 180.;
const ld ptod = 1. / dtop;
namespace std {
bool operator<(const Point &lhs, const Point &rhs) {
if (lhs.real() < rhs.real() - eps)
return true;
if (lhs.real() > rhs.real() + eps)
return false;
return lhs.imag() < rhs.imag();
}
} // namespace std
// ????????\???
Point input_point() {
ld x, y;
cin >> x >> y;
return Point(x, y);
}
// ????????????????????????
bool eq(const ld a, const ld b) { return (abs(a - b) < eps); }
// ??????
ld dot(const Point &a, const Point &b) { return real(conj(a) * b); }
// ??????
ld cross(const Point &a, const Point &b) { return imag(conj(a) * b); }
// ??´????????????
class Line {
public:
Point a, b;
Line() : a(Point(0, 0)), b(Point(0, 0)) {}
Line(Point a, Point b) : a(a), b(b) {}
Point operator[](const int _num) const {
if (_num == 0)
return a;
else if (_num == 1)
return b;
else {
assert(false);
return Point();
}
}
};
// ????????????
class Circle {
public:
Point p;
ld r;
Circle() : p(Point(0, 0)), r(0) {}
Circle(Point p, ld r) : p(p), r(r) {}
};
// CCW
int ccw(const Point &a, const Point &b, const Point &c) {
const Point nb(b - a);
const Point nc(c - a);
if (cross(nb, nc) > eps)
return 1; // a,b,c??????????¨???¨?????????????????¶
if (cross(nb, nc) < -eps)
return -1; // a,b,c???????¨???¨?????????????????¶
if (dot(nb, nc) < 0)
return 2; // c,a,b???????????´???????????¶
if (norm(nb) < norm(nc))
return -2; // a,b,c???????????´???????????¶
return 0; // a,c,b???????????´???????????¶
}
/* ???????????? */
// ??´?????¨??´??????????????????
bool isis_ll(const Line &l, const Line &m) {
return !eq(cross(l.b - l.a, m.b - m.a), 0);
}
// ??´?????¨?????????????????????
bool isis_ls(const Line &l, const Line &s) {
return isis_ll(l, s) &&
(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);
}
// ????????¨?????????????????????
bool isis_ss(const Line &s, const Line &t) {
return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}
// ????????´????????????
bool isis_lp(const Line &l, const Point &p) {
return (abs(cross(l.b - p, l.a - p)) < eps);
}
// ?????????????????????
bool isis_sp(const Line &s, const Point &p) {
ld n = (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);
return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);
}
// ??????????¶?
Point proj(const Line &l, const Point &p) {
ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
return l.a + t * (l.a - l.b);
}
// ??´?????¨??´????????????
Point is_ll(const Line &s, const Line &t) {
Point sv = s.b - s.a, tv = t.b - t.a;
assert(cross(sv, tv) != 0);
return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);
}
// ??´?????¨??´????????????
vector<Point> is_ll2(const Line &s, const Line &t) {
Point sv = s.b - s.a, tv = t.b - t.a;
if (cross(sv, tv) != 0)
return vector<Point>(1, is_ll(s, t));
else {
vector<Point> ans;
for (int k = 0; k < 2; ++k) {
if (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end())
ans.push_back(t[k]);
if (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end())
ans.push_back(s[k]);
}
return ans;
}
}
// ????????¨???????????????
//???????????£????????¨???????????¨assert(false)
Point is_ss(const Line &s, const Line &t) {
if (isis_ss(s, t)) {
for (int k = 0; k < 2; ++k) {
for (int l = 0; l < 2; ++l) {
if (s[k] == t[l])
return s[k];
}
}
return is_ll(s, t);
} else {
//??????isis_ss?????????
assert(false);
return Point(0, 0);
}
}
// ????????¨???????????????
vector<Point> is_ss2(const Line &s, const Line &t) {
vector<Point> kouho(is_ll2(s, t));
vector<Point> ans;
for (auto p : kouho) {
if (isis_sp(s, p) && isis_sp(t, p))
ans.emplace_back(p);
}
return ans;
}
// ??´?????¨???????????¢
ld dist_lp(const Line &l, const Point &p) { return abs(p - proj(l, p)); }
// ??´?????¨??´???????????¢
ld dist_ll(const Line &l, const Line &m) {
return isis_ll(l, m) ? 0 : dist_lp(l, m.a);
}
// ??´?????¨??????????????¢
ld dist_ls(const Line &l, const Line &s) {
return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));
}
// ????????¨???????????¢
ld dist_sp(const Line &s, const Point &p) {
Point r = proj(s, p);
return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));
}
// ????????¨??????????????¢
ld dist_ss(const Line &s, const Line &t) {
if (isis_ss(s, t))
return 0;
return min(
{dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b)});
}
//??´?????¨??´?????????????????????????????????
Line bisection(const Line &s, const Line &t) {
const Point laglanju(is_ll(s, t));
const Point avec =
!(abs(laglanju - s[0]) < eps) ? s[0] - laglanju : s[1] - laglanju;
const Point bvec =
!(abs(laglanju - t[0]) < eps) ? t[0] - laglanju : t[1] - laglanju;
return Line(laglanju, laglanju + (abs(bvec) * avec + abs(avec) * bvec) /
(abs(avec) + abs(bvec)));
}
//???????????´?????????????????????
//???????????´??????????????§???????????¨????¢?????????¨?????????
Point inner_center(const vector<Line> &ls) {
vector<Point> vertics;
for (int i = 0; i < static_cast<int>(ls.size()); ++i) {
vertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));
}
if (vertics[0] == vertics[1] || vertics[1] == vertics[2] ||
vertics[2] == vertics[0])
return vertics[0];
Line bi1(
bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2])));
Line bi2(
bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0])));
if (bi1[0] == bi2[0])
return bi1[0];
else {
return is_ll(bi1, bi2);
}
}
//???????????´?????????????????????
//???????????´??????????????§???????????¨????¢?????????¨?????????
vector<Point> ex_center(const vector<Line> &ls) {
vector<Point> vertics;
for (int i = 0; i < static_cast<int>(ls.size()); ++i) {
vertics.push_back(is_ll(ls[i], ls[(i + 1) % 3]));
}
if (abs(vertics[0] - vertics[1]) < eps ||
abs(vertics[1] - vertics[2]) < eps ||
(abs(vertics[2] - vertics[0]) < eps))
return vector<Point>();
vector<Point> ecs;
for (int i = 0; i < 3; ++i) {
Line bi1(
bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]),
Line(vertics[i], vertics[(i + 1) % 3])));
Line bi2(bisection(Line(vertics[(i + 1) % 3],
vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]),
Line(vertics[(i + 1) % 3], vertics[i])));
ecs.push_back(is_ll(bi1, bi2));
}
return ecs;
}
// a,b:??????
// c:????????§??????
//???????????´?????????????????¢?????????????±??????????
vector<Point> same_dis(const vector<Line> &ls) {
vector<Point> vertics;
vertics.push_back(is_ll(ls[0], ls[2]));
vertics.push_back(is_ll(ls[1], ls[2]));
if (abs(vertics[0] - vertics[1]) < eps)
return vector<Point>{vertics[0]};
Line bis(bisection(ls[0], ls[1]));
vector<Point> ecs;
Line abi(bisection(Line(vertics[0], vertics[1]), ls[0]));
ecs.push_back(is_ll(bis, abi));
Line bbi(bisection(Line(vertics[0], 2.l * vertics[0] - vertics[1]), ls[0]));
ecs.push_back(is_ll(bis, bbi));
return ecs;
}
/* ??? */
// ?????¨????????????
vector<Point> is_cc(const Circle &c1, const Circle &c2) {
vector<Point> res;
ld d = abs(c1.p - c2.p);
ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);
ld dfr = c1.r * c1.r - rc * rc;
if (abs(dfr) < eps)
dfr = 0.0;
else if (dfr < 0.0)
return res; // no intersection
ld rs = sqrt(dfr);
Point diff = (c2.p - c1.p) / d;
res.push_back(c1.p + diff * Point(rc, rs));
if (dfr != 0.0)
res.push_back(c1.p + diff * Point(rc, -rs));
return res;
}
//???????????????????????????
// 0 => out
// 1 => on
// 2 => in
int is_in_circle(const Circle &cir, const Point &p) {
ld dis = abs(cir.p - p);
if (dis > cir.r + eps)
return 0;
else if (dis < cir.r - eps)
return 2;
else
return 1;
}
//???lc??????rc??????????????????
// 0 => out
// 1 => on
// 2 => in
int circle_in_circle(const Circle &lc, const Circle &rc) {
ld dis = abs(lc.p - rc.p);
if (dis < rc.r - lc.r - eps)
return 2;
else if (dis > rc.r - lc.r + eps)
return 0;
else
return 1;
}
// ?????¨??´????????????
vector<Point> is_lc(const Circle &c, const Line &l) {
vector<Point> res;
ld d = dist_lp(l, c.p);
if (d < c.r + eps) {
ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); // safety;
Point nor = (l.a - l.b) / abs(l.a - l.b);
res.push_back(proj(l, c.p) + len * nor);
res.push_back(proj(l, c.p) - len * nor);
}
return res;
}
// ?????¨??????????????¢
vector<Point> is_sc(const Circle &c, const Line &l) {
vector<Point> v = is_lc(c, l), res;
for (Point p : v)
if (isis_sp(l, p))
res.push_back(p);
return res;
}
// ?????¨????????\???
vector<Line> tangent_cp(const Circle &c, const Point &p) {
vector<Line> ret;
Point v = c.p - p;
ld d = abs(v);
ld l = sqrt(norm(v) - c.r * c.r);
if (isnan(l)) {
return ret;
}
Point v1 = v * Point(l / d, c.r / d);
Point v2 = v * Point(l / d, -c.r / d);
ret.push_back(Line(p, p + v1));
if (l < eps)
return ret;
ret.push_back(Line(p, p + v2));
return ret;
}
// ?????¨????????\???
vector<Line> tangent_cc(const Circle &c1, const Circle &c2) {
vector<Line> ret;
if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {
Point center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);
ret = tangent_cp(c1, center);
}
if (abs(c1.r - c2.r) > eps) {
Point out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);
vector<Line> nret = tangent_cp(c1, out);
ret.insert(ret.end(), ALL(nret));
} else {
Point v = c2.p - c1.p;
v /= abs(v);
Point q1 = c1.p + v * Point(0, 1) * c1.r;
Point q2 = c1.p + v * Point(0, -1) * c1.r;
ret.push_back(Line(q1, q1 + v));
ret.push_back(Line(q2, q2 + v));
}
return ret;
}
//??????????????????????????¢???
ld two_circle_area(const Circle &l, const Circle &r) {
ld dis = abs(l.p - r.p);
if (dis > l.r + r.r)
return 0;
else if (dis + r.r < l.r) {
return r.r * r.r * pi;
} else if (dis + l.r < r.r) {
return l.r * l.r * pi;
} else {
ld ans = (l.r) * (l.r) *
acos((dis * dis + l.r * l.r - r.r * r.r) / (2 * dis * l.r)) +
(r.r) * (r.r) *
acos((dis * dis + r.r * r.r - l.r * l.r) / (2 * dis * r.r)) -
sqrt(4 * dis * dis * l.r * l.r -
(dis * dis + l.r * l.r - r.r * r.r) *
(dis * dis + l.r * l.r - r.r * r.r)) /
2;
return ans;
}
}
/* ????§???¢ */
typedef vector<Point> Polygon;
// ??¢???
ld area(const Polygon &p) {
ld res = 0;
int n = p.size();
REP(j, n) res += cross(p[j], p[(j + 1) % n]);
return res / 2;
}
// ????§???¢????????¢??????
bool is_counter_clockwise(const Polygon &poly) {
ld angle = 0;
int n = poly.size();
REP(i, n) {
Point a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];
angle += arg((c - b) / (b - a));
}
return angle > eps;
}
// ??????????????????
// 0 => out
// 1 => on
// 2 => in
int is_in_polygon(const Polygon &poly, const Point &p) {
ld angle = 0;
int n = poly.size();
REP(i, n) {
Point a = poly[i], b = poly[(i + 1) % n];
if (isis_sp(Line(a, b), p))
return 1;
angle += arg((b - p) / (a - p));
}
return eq(angle, 0) ? 0 : 2;
}
//??????????????????2?????????
enum { OUT, ON, IN };
int convex_contains(const Polygon &P, const Point &p) {
const int n = P.size();
Point g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point
int a = 0, b = n;
while (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b]
int c = (a + b) / 2;
if (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg
if (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0)
b = c;
else
a = c;
} else {
if (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0)
a = c;
else
b = c;
}
}
b %= n;
if (cross(P[a] - p, P[b] - p) < 0)
return 0;
if (cross(P[a] - p, P[b] - p) > 0)
return 2;
return 1;
}
// ??????
// ???????????????????????¨????????????????????§??¨???
Polygon convex_hull(vector<Point> ps) {
int n = ps.size();
int k = 0;
sort(ps.begin(), ps.end());
Polygon ch(2 * n);
for (int i = 0; i < n; ch[k++] = ps[i++])
while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
--k;
for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])
while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
--k;
ch.resize(k - 1);
return ch;
}
// ????????????
vector<Polygon> convex_cut(const Polygon &ps, const Line &l) {
int n = ps.size();
Polygon Q;
Polygon R;
REP(i, n) {
Point A = ps[i], B = ps[(i + 1) % n];
Line m = Line(A, B);
if (ccw(l.a, l.b, A) != -1)
Q.push_back(A);
if (ccw(l.a, l.b, A) != 1)
R.push_back(A);
if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m)) {
Q.push_back(is_ll(l, m));
R.push_back(is_ll(l, m));
}
}
const vector<Polygon> polys{Q, R};
return polys;
}
/* ??¢??¬??????????????? */
void add_point(vector<Point> &ps, const Point p) {
for (Point q : ps)
if (abs(q - p) < eps)
return;
ps.push_back(p);
}
typedef ld Weight;
struct Edge {
int src, dst;
Weight weight;
Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
void add_edge(Graph &g, const int from, const int to, const Weight weight) {
g[from].push_back(Edge{from, to, weight});
}
pair<vector<Point>, Graph> segment_arrangement(const vector<Line> &s,
const vector<Point> &p) {
int n = p.size(), m = s.size();
Graph g(n);
REP(i, m) {
vector<pair<ld, int>> vec;
REP(j, n) if (isis_sp(s[i], p[j])) vec.emplace_back(abs(s[i].a - p[j]), j);
sort(ALL(vec));
REP(j, vec.size() - 1) {
int from = vec[j].second, to = vec[j + 1].second;
add_edge(g, from, to, static_cast<Weight>(abs(p[from] - p[to])));
add_edge(g, to, from, static_cast<Weight>(abs(p[from] - p[to])));
}
}
auto a(make_pair(p, g));
return a;
}
pair<vector<Point>, Graph> sennbunn_arrangement(const vector<Line> &s) {
vector<Point> crss;
for (int i = 0; i < static_cast<int>(s.size()); ++i) {
for (int j = i + 1; j < static_cast<int>(s.size()); ++j) {
if (isis_ss(s[i], s[j])) {
crss.push_back(is_ll2(s[i], s[j])[0]);
}
}
}
for (int i = 0; i < static_cast<int>(s.size()); ++i) {
crss.push_back(s[i][0]);
crss.push_back(s[i][1]);
}
sort(crss.begin(), crss.end());
crss.erase(unique(crss.begin(), crss.end()), crss.end());
return segment_arrangement(s, crss);
}
pair<bool, vector<Weight>> spfa(const int v_num, const vector<vector<Edge>> &es,
const int start) {
vector<Weight> diss(v_num, INT_MAX);
queue<int> que;
vector<bool> use(v_num);
vector<int> count(v_num);
que.emplace(start);
diss[start] = 0;
while (!que.empty()) {
int src(que.front());
que.pop();
use[src] = false;
for (auto e : es[src]) {
const int d = e.dst;
if (diss[src] + e.weight < diss[d]) {
diss[d] = diss[src] + e.weight;
if (!use[d]) {
use[d] = true;
count[d]++;
if (count[d] >= v_num)
return make_pair(false, vector<Weight>());
que.emplace(d);
}
}
}
}
return make_pair(true, diss);
}
struct aa {
int now;
ld time;
};
class Compare {
public:
// aa?????????????????¶
bool operator()(const aa &l, const aa &r) { return l.time > r.time; }
};
int main() {
while (1) {
int N;
cin >> N;
if (!N)
break;
int xs, ys, xg, yg;
cin >> xs >> ys >> xg >> yg;
vector<Line> ls;
Point start(xs, ys), goal(xg, yg);
for (int i = 0; i < N; ++i) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
ls.emplace_back(Point(x1, y1), Point(x2, y2));
}
vector<Line> roads;
vector<Line> signs;
for (int i = 0; i < N; ++i) {
int touchnum = 0;
for (int j = 0; j < 2; ++j) {
Point p(ls[i][j]);
bool touch = false;
for (int k = 0; k < N; ++k) {
if (i == k)
continue;
else {
if (isis_sp(ls[k], p))
touch = true;
}
}
if (touch)
touchnum++;
}
if (touchnum == 2)
roads.emplace_back(ls[i]);
else
signs.emplace_back(ls[i]);
}
auto pa(sennbunn_arrangement(roads));
Graph g(pa.second);
vector<Point> ps(pa.first);
int startid, goalid;
for (int i = 0; i < ps.size(); ++i) {
Point p(ps[i]);
if (p == start)
startid = i;
else if (p == goal)
goalid = i;
}
for (auto &es : g) {
for (auto &e : es) {
Line l(ps[e.src], ps[e.dst]);
for (int k = 0; k < signs.size(); ++k) {
Line si(signs[k]);
if (isis_ss(l, si)) {
if (isis_sp(l, si[1]))
swap(si.a, si.b);
ld theta = (dot(l[1] - l[0], si[1] - si[0]));
if (theta > -eps) {
e.weight = 1e18;
}
}
}
}
}
vector<ld> times(g.size(), 1e18);
vector<vector<int>> memos(g.size());
memos[startid].push_back(startid);
priority_queue<aa, vector<aa>, Compare> que;
times[startid] = 0;
que.push(aa{startid, 0});
while (!que.empty()) {
aa atop(que.top());
que.pop();
if (atop.now == goalid)
break;
for (auto e : g[atop.now]) {
if (times[e.dst] > atop.time + e.weight) {
times[e.dst] = atop.time + e.weight;
memos[e.dst] = memos[e.src];
memos[e.dst].emplace_back(e.dst);
que.push(aa{e.dst, atop.time + e.weight});
}
}
}
if (memos[goalid].empty()) {
cout << -1 << endl;
} else {
for (auto m : memos[goalid]) {
int x = int((ps[m].real() + eps));
int y = int(ps[m].imag() + eps);
cout << x << " " << y << endl;
}
cout << 0 << endl;
}
}
return 0;
} | replace | 557 | 558 | 557 | 558 | 0 | |
p00858 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef double ld;
typedef pair<int, int> P;
typedef pair<ll, P> P1;
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define mp1(a, b, c) P1(a, P(b, c))
const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1},
{-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
int gcd(int x, int y) {
if (y == 0)
return x;
return gcd(y, x % y);
}
vector<P> dir[1002][1002];
int f(int x, int y, int dx, int dy) {
int ret = 0;
rep(i, dir[x][y].size()) {
P p = dir[x][y][i];
int t = (p.fr - x) * dx + (p.sc - y) * dy;
if (t > 0)
ret |= 1;
else if (t == 0)
ret |= 3;
else
ret |= 2;
}
return ret;
}
ld cul_dist(ld x1, ld y1, ld x2, ld y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
static int n;
static int xs, ys, xg, yg;
static short cnt[1002][1002];
static vector<P> G[1002][1002];
static bool used[1002][1002];
static double dist[1002][1002];
static P pre[1002][1002];
priority_queue<pair<ld, P>, vector<pair<ld, P>>, greater<pair<ld, P>>> que;
int main() {
while (1) {
static int x1[202], y1[202], x2[202], y2[202];
scanf("%d", &n);
if (n == 0)
break;
scanf("%d%d%d%d", &xs, &ys, &xg, &yg);
rep(i, n) { scanf("%d%d%d%d", &x1[i], &y1[i], &x2[i], &y2[i]); }
rep(i, 1002) rep(j, 1002) cnt[i][j] = 0;
rep(i, n) {
if (x1[i] > x2[i] || (x1[i] == x2[i] && y1[i] > y2[i])) {
swap(x1[i], x2[i]);
swap(y1[i], y2[i]);
}
cnt[x1[i]][y1[i]]++;
cnt[x2[i]][y2[i]]++;
}
rep(i, n) {
int dx = x2[i] - x1[i];
int dy = y2[i] - y1[i];
int g = gcd(abs(dx), abs(dy));
dx /= g;
dy /= g;
int locx = x1[i] + dx, locy = y1[i] + dy;
while (!(locx == x2[i] && locy == y2[i])) {
cnt[locx][locy]++;
locx += dx;
locy += dy;
}
}
rep(i, 1002) rep(j, 1002) dir[i][j].clear();
rep(i, n) {
if (cnt[x1[i]][y1[i]] == 1) {
dir[x2[i]][y2[i]].pb(P(x1[i], y1[i]));
} else if (cnt[x2[i]][y2[i]] == 1) {
dir[x1[i]][y1[i]].pb(P(x2[i], y2[i]));
}
}
rep(i, 1002) rep(j, 1002) G[i][j].clear();
rep(i, n) {
if (cnt[x1[i]][y1[i]] == 1 || cnt[x2[i]][y2[i]] == 1)
continue;
int dx = x2[i] - x1[i];
int dy = y2[i] - y1[i];
int g = gcd(abs(dx), abs(dy));
dx /= g;
dy /= g;
int prex1 = -1, prey1 = -1;
int prex2 = -1, prey2 = -1;
int locx = x1[i], locy = y1[i];
while (locx <= x2[i] && !(dx == 0 && locy > y2[i])) {
if (cnt[locx][locy] >= 1) {
int t = f(locx, locy, dx, dy);
if ((t & 1) == 0) {
if (prex1 != -1)
G[prex1][prey1].pb(P(locx, locy));
prex1 = locx;
prey1 = locy;
} else {
prex1 = -1;
prey1 = -1;
}
if ((t & 2) == 0) {
if (prex2 != -1)
G[locx][locy].pb(P(prex2, prey2));
prex2 = locx;
prey2 = locy;
} else {
prex2 = -1;
prey2 = -1;
}
}
locx += dx;
locy += dy;
}
}
rep(i, 1002) rep(j, 1002) {
used[i][j] = false;
dist[i][j] = (ld)INF;
pre[i][j] = P(-1, -1);
}
dist[xs][ys] = 0.0;
que.push(pair<ld, P>(0.0, P(xs, ys)));
while (!que.empty()) {
pair<ld, P> p = que.top();
que.pop();
if (used[p.sc.fr][p.sc.sc])
continue;
used[p.sc.fr][p.sc.sc] = true;
rep(i, G[p.sc.fr][p.sc.sc].size()) {
P to = G[p.sc.fr][p.sc.sc][i];
if (dist[to.fr][to.sc] >
dist[p.sc.fr][p.sc.sc] + cul_dist(p.sc.fr, p.sc.sc, to.fr, to.sc)) {
dist[to.fr][to.sc] =
dist[p.sc.fr][p.sc.sc] + cul_dist(p.sc.fr, p.sc.sc, to.fr, to.sc);
pre[to.fr][to.sc] = p.sc;
que.push(pair<ld, P>(dist[to.fr][to.sc], to));
}
}
}
if (pre[xg][yg].fr == -1)
cout << -1 << endl;
else {
vector<P> ans;
P loc = P(xg, yg);
ans.pb(loc);
while (loc != P(xs, ys)) {
loc = pre[loc.fr][loc.sc];
ans.pb(loc);
}
rrep(i, ans.size()) {
if (!(cnt[ans[i].fr][ans[i].sc] - dir[ans[i].fr][ans[i].sc].size() ==
1))
printf("%d %d\n", ans[i].fr, ans[i].sc);
}
puts("0");
}
}
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef double ld;
typedef pair<short, short> P;
typedef pair<ll, P> P1;
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define mp1(a, b, c) P1(a, P(b, c))
const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1},
{-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
int gcd(int x, int y) {
if (y == 0)
return x;
return gcd(y, x % y);
}
vector<P> dir[1002][1002];
int f(int x, int y, int dx, int dy) {
int ret = 0;
rep(i, dir[x][y].size()) {
P p = dir[x][y][i];
int t = (p.fr - x) * dx + (p.sc - y) * dy;
if (t > 0)
ret |= 1;
else if (t == 0)
ret |= 3;
else
ret |= 2;
}
return ret;
}
ld cul_dist(ld x1, ld y1, ld x2, ld y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
static int n;
static int xs, ys, xg, yg;
static short cnt[1002][1002];
static vector<P> G[1002][1002];
static bool used[1002][1002];
static double dist[1002][1002];
static P pre[1002][1002];
priority_queue<pair<ld, P>, vector<pair<ld, P>>, greater<pair<ld, P>>> que;
int main() {
while (1) {
static int x1[202], y1[202], x2[202], y2[202];
scanf("%d", &n);
if (n == 0)
break;
scanf("%d%d%d%d", &xs, &ys, &xg, &yg);
rep(i, n) { scanf("%d%d%d%d", &x1[i], &y1[i], &x2[i], &y2[i]); }
rep(i, 1002) rep(j, 1002) cnt[i][j] = 0;
rep(i, n) {
if (x1[i] > x2[i] || (x1[i] == x2[i] && y1[i] > y2[i])) {
swap(x1[i], x2[i]);
swap(y1[i], y2[i]);
}
cnt[x1[i]][y1[i]]++;
cnt[x2[i]][y2[i]]++;
}
rep(i, n) {
int dx = x2[i] - x1[i];
int dy = y2[i] - y1[i];
int g = gcd(abs(dx), abs(dy));
dx /= g;
dy /= g;
int locx = x1[i] + dx, locy = y1[i] + dy;
while (!(locx == x2[i] && locy == y2[i])) {
cnt[locx][locy]++;
locx += dx;
locy += dy;
}
}
rep(i, 1002) rep(j, 1002) dir[i][j].clear();
rep(i, n) {
if (cnt[x1[i]][y1[i]] == 1) {
dir[x2[i]][y2[i]].pb(P(x1[i], y1[i]));
} else if (cnt[x2[i]][y2[i]] == 1) {
dir[x1[i]][y1[i]].pb(P(x2[i], y2[i]));
}
}
rep(i, 1002) rep(j, 1002) G[i][j].clear();
rep(i, n) {
if (cnt[x1[i]][y1[i]] == 1 || cnt[x2[i]][y2[i]] == 1)
continue;
int dx = x2[i] - x1[i];
int dy = y2[i] - y1[i];
int g = gcd(abs(dx), abs(dy));
dx /= g;
dy /= g;
int prex1 = -1, prey1 = -1;
int prex2 = -1, prey2 = -1;
int locx = x1[i], locy = y1[i];
while (locx <= x2[i] && !(dx == 0 && locy > y2[i])) {
if (cnt[locx][locy] >= 1) {
int t = f(locx, locy, dx, dy);
if ((t & 1) == 0) {
if (prex1 != -1)
G[prex1][prey1].pb(P(locx, locy));
prex1 = locx;
prey1 = locy;
} else {
prex1 = -1;
prey1 = -1;
}
if ((t & 2) == 0) {
if (prex2 != -1)
G[locx][locy].pb(P(prex2, prey2));
prex2 = locx;
prey2 = locy;
} else {
prex2 = -1;
prey2 = -1;
}
}
locx += dx;
locy += dy;
}
}
rep(i, 1002) rep(j, 1002) {
used[i][j] = false;
dist[i][j] = (ld)INF;
pre[i][j] = P(-1, -1);
}
dist[xs][ys] = 0.0;
que.push(pair<ld, P>(0.0, P(xs, ys)));
while (!que.empty()) {
pair<ld, P> p = que.top();
que.pop();
if (used[p.sc.fr][p.sc.sc])
continue;
used[p.sc.fr][p.sc.sc] = true;
rep(i, G[p.sc.fr][p.sc.sc].size()) {
P to = G[p.sc.fr][p.sc.sc][i];
if (dist[to.fr][to.sc] >
dist[p.sc.fr][p.sc.sc] + cul_dist(p.sc.fr, p.sc.sc, to.fr, to.sc)) {
dist[to.fr][to.sc] =
dist[p.sc.fr][p.sc.sc] + cul_dist(p.sc.fr, p.sc.sc, to.fr, to.sc);
pre[to.fr][to.sc] = p.sc;
que.push(pair<ld, P>(dist[to.fr][to.sc], to));
}
}
}
if (pre[xg][yg].fr == -1)
cout << -1 << endl;
else {
vector<P> ans;
P loc = P(xg, yg);
ans.pb(loc);
while (loc != P(xs, ys)) {
loc = pre[loc.fr][loc.sc];
ans.pb(loc);
}
rrep(i, ans.size()) {
if (!(cnt[ans[i].fr][ans[i].sc] - dir[ans[i].fr][ans[i].sc].size() ==
1))
printf("%d %d\n", ans[i].fr, ans[i].sc);
}
puts("0");
}
}
} | replace | 14 | 15 | 14 | 15 | MLE | |
p00859 | C++ | Runtime Error | #include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int n, m;
int a[110], b[110], w[110];
bool use[110];
vector<P> g[110];
int dfs(int x, int low, int up) {
if (use[x])
return 0;
use[x] = true;
int res = 0;
for (int i = 0; i < g[x].size(); i++) {
if (g[x][i].first >= low && g[x][i].first <= up)
res += dfs(g[x][i].second, low, up);
}
return res + 1;
}
int main() {
while (cin >> n >> m, n || m) {
for (int i = 0; i < n; i++)
g[i].clear();
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i] >> w[i];
a[i]--;
b[i]--;
g[a[i]].push_back(P(w[i], b[i]));
g[b[i]].push_back(P(w[i], a[i]));
}
int l = -1, r = 10001;
for (int i = 0; i < m; i++) {
while (r - l > 1) {
int mid = (l + r) / 2;
for (int j = 0; j < n; j++)
use[j] = false;
int tmp = dfs(a[i], w[i] - mid, w[i]);
if (tmp == n)
r = mid;
else
l = mid;
}
l = -1;
}
if (r == 10001)
cout << -1 << endl;
else
cout << r << endl;
}
}
| #include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int n, m;
int a[11000], b[11000], w[11000];
bool use[110];
vector<P> g[110];
int dfs(int x, int low, int up) {
if (use[x])
return 0;
use[x] = true;
int res = 0;
for (int i = 0; i < g[x].size(); i++) {
if (g[x][i].first >= low && g[x][i].first <= up)
res += dfs(g[x][i].second, low, up);
}
return res + 1;
}
int main() {
while (cin >> n >> m, n || m) {
for (int i = 0; i < n; i++)
g[i].clear();
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i] >> w[i];
a[i]--;
b[i]--;
g[a[i]].push_back(P(w[i], b[i]));
g[b[i]].push_back(P(w[i], a[i]));
}
int l = -1, r = 10001;
for (int i = 0; i < m; i++) {
while (r - l > 1) {
int mid = (l + r) / 2;
for (int j = 0; j < n; j++)
use[j] = false;
int tmp = dfs(a[i], w[i] - mid, w[i]);
if (tmp == n)
r = mid;
else
l = mid;
}
l = -1;
}
if (r == 10001)
cout << -1 << endl;
else
cout << r << endl;
}
}
| replace | 8 | 9 | 8 | 9 | 0 | |
p00859 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct UnionFind {
vector<int> data;
UnionFind(int n) : data(n, -1) {}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y);
data[x] += data[y]; //???????????´??°
data[y] = x; //????????´??°
}
return x != y;
}
bool same(int x, int y) { return find(x) == find(y); }
int find(int x) {
if (data[x] < 0)
return x;
return data[x] = find(data[x]);
}
};
struct Edge {
int cost, from, to;
Edge(int cost, int from, int to) : cost(cost), from(from), to(to) {}
};
bool operator<(const Edge &a, const Edge &b) {
return a.cost != b.cost ? a.cost < b.cost
: a.from != b.from ? a.from << b.from
: a.to < b.to;
}
int main() {
int n, m, INF = 1000000000;
while (cin >> n >> m, n) {
int a, b, w;
vector<Edge> E;
for (int i = 0; i < m; ++i) {
cin >> a >> b >> w;
--a;
--b;
// E.emplace_back(w,a,b);
E.push_back(Edge(w, a, b));
}
sort(E.begin(), E.end());
int ans = INF;
for (int i = 0; i < m; ++i) {
int e = n - 1;
UnionFind uf(n);
bool f = true;
for (int j = i; j < m; ++j) {
int cost = E[j].cost, from = E[j].from, to = E[j].to;
if (!uf.same(from, to)) {
--e;
uf.unite(from, to);
}
if (!e) {
ans = min(ans, cost - E[i].cost);
f = false;
break;
}
}
if (f)
break;
}
if (ans == INF)
ans = -1;
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct UnionFind {
vector<int> data;
UnionFind(int n) : data(n, -1) {}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y);
data[x] += data[y]; //???????????´??°
data[y] = x; //????????´??°
}
return x != y;
}
bool same(int x, int y) { return find(x) == find(y); }
int find(int x) {
if (data[x] < 0)
return x;
return data[x] = find(data[x]);
}
};
struct Edge {
int cost, from, to;
Edge(int cost, int from, int to) : cost(cost), from(from), to(to) {}
};
bool operator<(const Edge &a, const Edge &b) {
return a.cost != b.cost ? a.cost < b.cost
: a.from != b.from ? a.from < b.from
: a.to < b.to;
}
int main() {
int n, m, INF = 1000000000;
while (cin >> n >> m, n) {
int a, b, w;
vector<Edge> E;
for (int i = 0; i < m; ++i) {
cin >> a >> b >> w;
--a;
--b;
// E.emplace_back(w,a,b);
E.push_back(Edge(w, a, b));
}
sort(E.begin(), E.end());
int ans = INF;
for (int i = 0; i < m; ++i) {
int e = n - 1;
UnionFind uf(n);
bool f = true;
for (int j = i; j < m; ++j) {
int cost = E[j].cost, from = E[j].from, to = E[j].to;
if (!uf.same(from, to)) {
--e;
uf.unite(from, to);
}
if (!e) {
ans = min(ans, cost - E[i].cost);
f = false;
break;
}
}
if (f)
break;
}
if (ans == INF)
ans = -1;
cout << ans << endl;
}
return 0;
} | replace | 34 | 35 | 34 | 35 | 0 | |
p00860 | C++ | Time Limit Exceeded | #include <algorithm>
#include <array>
#include <cassert>
#include <cctype>
#include <iostream>
#include <map>
#include <queue>
#include <unordered_set>
#include <vector>
#define repeat(i, n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i, m, n) for (int i = (m); (i) < (n); ++(i))
#define repeat_reverse(i, n) for (int i = (n)-1; (i) >= 0; --(i))
#define repeat_from_reverse(i, m, n) for (int i = (n)-1; (i) >= (m); --(i))
#define dump(x) cerr << #x << " = " << (x) << endl
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl
typedef long long ll;
using namespace std;
struct point_t {
int y, x;
};
point_t operator+(point_t const &a, point_t const &b) {
return (point_t){a.y + b.y, a.x + b.x};
}
bool operator==(point_t const &a, point_t const &b) {
return make_pair(a.y, a.x) == make_pair(b.y, b.x);
}
bool operator<(point_t const &a, point_t const &b) {
return make_pair(a.y, a.x) < make_pair(b.y, b.x);
}
static point_t dp[5] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}, {0, 0}};
struct state_t {
array<point_t, 3> a;
int cost;
int dist;
};
bool operator<(state_t const &a, state_t const &b) {
return 2 * a.cost + a.dist > 2 * b.cost + b.dist;
}
uint32_t encode(array<point_t, 3> const &s) {
uint32_t q = 0;
for (point_t p : s) {
q = (q * 19) + p.y;
q = (q * 16) + p.x;
}
return q;
}
bool is_valid_move(int i, int j, array<point_t, 3> const &s,
array<point_t, 3> const &t) {
if (t[i] == t[j])
return false;
if (t[i] == s[j] and t[j] == s[i])
return false;
return true;
}
int main() {
while (true) {
int w, h, n;
cin >> w >> h >> n;
cin.ignore();
if (w == 0 and h == 0 and n == 0)
break;
vector<string> c(h);
repeat(i, h) getline(cin, c[i]);
assert(1 <= n and n <= 3);
array<point_t, 3> start, goal;
repeat(y, h) repeat(x, w) {
if (islower(c[y][x])) {
start[c[y][x] - 'a'] = {y, x};
} else if (isupper(c[y][x])) {
goal[c[y][x] - 'A'] = {y, x};
}
}
if (n == 1 or n == 2) {
assert(4 <= w);
string w = c.back();
c.push_back(w);
// c
c.back()[2] = '.';
start[2] = goal[2] = (point_t){h, 2};
if (n == 1) {
// b
c.back()[1] = '.';
start[1] = goal[1] = (point_t){h, 1};
}
c.push_back(w);
h += 2;
}
vector<vector<int>> dist[3]; // from goal
repeat(i, 3) {
dist[i].resize(h, vector<int>(w, 1000000007));
// bfs
vector<point_t> cur, nxt;
nxt.push_back(goal[i]);
dist[i][goal[i].y][goal[i].x] = 0;
int l = 1;
while (not nxt.empty()) {
cur.clear();
cur.swap(nxt);
for (auto p : cur) {
repeat(j, 4) {
auto q = p + dp[j];
if (c[q.y][q.x] == '#')
continue;
if (l < dist[i][q.y][q.x]) {
dist[i][q.y][q.x] = l;
nxt.push_back(q);
}
}
}
++l;
}
}
map<point_t, int> ix;
repeat(y, h) repeat(x, w) {
if (c[y][x] != '#') {
int i = ix.size();
ix[(point_t){y, x}] = i;
}
}
vector<vector<vector<bool>>> used(
ix.size(), vector<vector<bool>>(ix.size(), vector<bool>(ix.size())));
priority_queue<state_t> que;
{
state_t initial = {start, 0, 0};
repeat(i, 3) initial.dist += dist[i][start[i].y][start[i].x];
used[ix[start[0]]][ix[start[1]]][ix[start[2]]] = true;
que.push(initial);
}
while (not que.empty()) {
state_t st = que.top();
que.pop();
array<point_t, 3> &s = st.a;
if (s == goal) {
cout << st.cost << endl;
break;
}
state_t tt = st;
tt.cost += 1;
array<point_t, 3> &t = tt.a;
repeat(i, 5) { // a
t[0] = s[0] + dp[i];
if (c[t[0].y][t[0].x] == '#')
continue;
repeat(j, 5) { // b
t[1] = s[1] + dp[j];
if (c[t[1].y][t[1].x] == '#')
continue;
if (not is_valid_move(0, 1, s, t))
continue;
repeat(k, 5) { // c
t[2] = s[2] + dp[k];
if (c[t[2].y][t[2].x] == '#')
continue;
if (not is_valid_move(1, 2, s, t))
continue;
if (not is_valid_move(2, 0, s, t))
continue;
if (used[ix[t[0]]][ix[t[1]]][ix[t[2]]])
continue;
used[ix[t[0]]][ix[t[1]]][ix[t[2]]] = true;
tt.dist = 0;
repeat(i, 3) tt.dist = max(tt.dist, dist[i][t[i].y][t[i].x]);
que.push(tt);
}
}
}
}
}
return 0;
} | #include <algorithm>
#include <array>
#include <cassert>
#include <cctype>
#include <iostream>
#include <map>
#include <queue>
#include <unordered_set>
#include <vector>
#define repeat(i, n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i, m, n) for (int i = (m); (i) < (n); ++(i))
#define repeat_reverse(i, n) for (int i = (n)-1; (i) >= 0; --(i))
#define repeat_from_reverse(i, m, n) for (int i = (n)-1; (i) >= (m); --(i))
#define dump(x) cerr << #x << " = " << (x) << endl
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl
typedef long long ll;
using namespace std;
struct point_t {
int y, x;
};
point_t operator+(point_t const &a, point_t const &b) {
return (point_t){a.y + b.y, a.x + b.x};
}
bool operator==(point_t const &a, point_t const &b) {
return make_pair(a.y, a.x) == make_pair(b.y, b.x);
}
bool operator<(point_t const &a, point_t const &b) {
return make_pair(a.y, a.x) < make_pair(b.y, b.x);
}
static point_t dp[5] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}, {0, 0}};
struct state_t {
array<point_t, 3> a;
int cost;
int dist;
};
bool operator<(state_t const &a, state_t const &b) {
return 1.3 * a.cost + a.dist > 1.3 * b.cost + b.dist;
}
uint32_t encode(array<point_t, 3> const &s) {
uint32_t q = 0;
for (point_t p : s) {
q = (q * 19) + p.y;
q = (q * 16) + p.x;
}
return q;
}
bool is_valid_move(int i, int j, array<point_t, 3> const &s,
array<point_t, 3> const &t) {
if (t[i] == t[j])
return false;
if (t[i] == s[j] and t[j] == s[i])
return false;
return true;
}
int main() {
while (true) {
int w, h, n;
cin >> w >> h >> n;
cin.ignore();
if (w == 0 and h == 0 and n == 0)
break;
vector<string> c(h);
repeat(i, h) getline(cin, c[i]);
assert(1 <= n and n <= 3);
array<point_t, 3> start, goal;
repeat(y, h) repeat(x, w) {
if (islower(c[y][x])) {
start[c[y][x] - 'a'] = {y, x};
} else if (isupper(c[y][x])) {
goal[c[y][x] - 'A'] = {y, x};
}
}
if (n == 1 or n == 2) {
assert(4 <= w);
string w = c.back();
c.push_back(w);
// c
c.back()[2] = '.';
start[2] = goal[2] = (point_t){h, 2};
if (n == 1) {
// b
c.back()[1] = '.';
start[1] = goal[1] = (point_t){h, 1};
}
c.push_back(w);
h += 2;
}
vector<vector<int>> dist[3]; // from goal
repeat(i, 3) {
dist[i].resize(h, vector<int>(w, 1000000007));
// bfs
vector<point_t> cur, nxt;
nxt.push_back(goal[i]);
dist[i][goal[i].y][goal[i].x] = 0;
int l = 1;
while (not nxt.empty()) {
cur.clear();
cur.swap(nxt);
for (auto p : cur) {
repeat(j, 4) {
auto q = p + dp[j];
if (c[q.y][q.x] == '#')
continue;
if (l < dist[i][q.y][q.x]) {
dist[i][q.y][q.x] = l;
nxt.push_back(q);
}
}
}
++l;
}
}
map<point_t, int> ix;
repeat(y, h) repeat(x, w) {
if (c[y][x] != '#') {
int i = ix.size();
ix[(point_t){y, x}] = i;
}
}
vector<vector<vector<bool>>> used(
ix.size(), vector<vector<bool>>(ix.size(), vector<bool>(ix.size())));
priority_queue<state_t> que;
{
state_t initial = {start, 0, 0};
repeat(i, 3) initial.dist += dist[i][start[i].y][start[i].x];
used[ix[start[0]]][ix[start[1]]][ix[start[2]]] = true;
que.push(initial);
}
while (not que.empty()) {
state_t st = que.top();
que.pop();
array<point_t, 3> &s = st.a;
if (s == goal) {
cout << st.cost << endl;
break;
}
state_t tt = st;
tt.cost += 1;
array<point_t, 3> &t = tt.a;
repeat(i, 5) { // a
t[0] = s[0] + dp[i];
if (c[t[0].y][t[0].x] == '#')
continue;
repeat(j, 5) { // b
t[1] = s[1] + dp[j];
if (c[t[1].y][t[1].x] == '#')
continue;
if (not is_valid_move(0, 1, s, t))
continue;
repeat(k, 5) { // c
t[2] = s[2] + dp[k];
if (c[t[2].y][t[2].x] == '#')
continue;
if (not is_valid_move(1, 2, s, t))
continue;
if (not is_valid_move(2, 0, s, t))
continue;
if (used[ix[t[0]]][ix[t[1]]][ix[t[2]]])
continue;
used[ix[t[0]]][ix[t[1]]][ix[t[2]]] = true;
tt.dist = 0;
repeat(i, 3) tt.dist = max(tt.dist, dist[i][t[i].y][t[i].x]);
que.push(tt);
}
}
}
}
}
return 0;
} | replace | 40 | 41 | 40 | 41 | TLE | |
p00860 | C++ | Memory Limit Exceeded | #include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
char f[16][16];
int fx, fy, N;
int dx[] = {0, 0, -1, 1, 0};
int dy[] = {0, -1, 0, 0, 1};
class Ghost {
public:
int x[3], y[3], c;
bool r;
Ghost(int *i, int *j, int c, bool r) : r(r), c(c) {
for (int k = 0; k < 3; k++) {
x[k] = i[k];
y[k] = j[k];
}
}
};
bool move(int x, int y, int p) {
if (p >= N)
return true;
if (x < 0 || y < 0 || x >= fx || y >= fy)
return false;
if (f[x][y] == '#')
return false;
return true;
}
bool crash(int *x, int *y, int *gx, int *gy, int p) {
if (p < N)
return false;
for (int i = 0; i < p; i++)
for (int j = i + 1; j < p; j++) {
if (x[i] == x[j] && y[i] == y[j])
return true;
if (x[i] == gx[j] && y[i] == gy[j] && gx[i] == x[j] && gy[i] == y[j])
return true;
}
return false;
}
short h[256][256][256][2];
int main() {
while (scanf("%d%d%d", &fx, &fy, &N), (fx || fy || N)) {
int sx[3] = {0}, sy[3] = {0}, gx[3] = {0}, gy[3] = {0};
memset(h, 0, sizeof(h));
string s;
getchar();
for (int i = 0; i < fy; i++) {
getline(cin, s);
for (int j = 0; j < fx; j++) {
if (islower(s[j])) {
sx[s[j] - 'a'] = j;
sy[s[j] - 'a'] = i;
}
if (isupper(s[j])) {
gx[s[j] - 'A'] = j;
gy[s[j] - 'A'] = i;
}
f[j][i] = s[j];
}
}
queue<Ghost> q;
q.push(Ghost(sx, sy, 1, 0));
q.push(Ghost(gx, gy, 1, 1));
int th[3];
for (int l = 0; l < 3; l++) {
th[l] = (sx[l] << 4) + sy[l];
}
h[th[0]][th[1]][th[2]][0] = 1;
for (int l = 0; l < 3; l++) {
th[l] = (gx[l] << 4) + gy[l];
}
h[th[0]][th[1]][th[2]][1] = 1;
while (!q.empty()) {
Ghost g = q.front();
q.pop();
int th[3];
for (int l = 0; l < 3; l++) {
th[l] = (g.x[l] << 4) + g.y[l];
}
if (h[th[0]][th[1]][th[2]][!g.r]) {
printf("%d\n", g.c + h[th[0]][th[1]][th[2]][!g.r] - 1);
break;
}
int tx[3], ty[3];
for (int i = 0; i < 3; i++) {
tx[i] = g.x[i];
ty[i] = g.y[i];
}
for (int i = 0; i < 5; i++) {
tx[0] = g.x[0] + dx[i], ty[0] = g.y[0] + dy[i];
if (!move(tx[0], ty[0], 0)) {
tx[0] = g.x[0];
ty[0] = g.y[0];
continue;
}
for (int j = 0; j < (N > 1 ? 5 : 1); j++) {
tx[1] = g.x[1] + dx[j], ty[1] = g.y[1] + dy[j];
if (N >= 2) {
if (!move(tx[1], ty[1], 1) || (crash(tx, ty, g.x, g.y, 2))) {
tx[1] = g.x[1];
ty[1] = g.y[1];
continue;
}
}
for (int k = 0; k < (N > 2 ? 5 : 1); k++) {
tx[2] = g.x[2] + dx[k], ty[2] = g.y[2] + dy[k];
if (N >= 3) {
if (!move(tx[2], ty[2], 2) || (crash(tx, ty, g.x, g.y, 3))) {
tx[2] = g.x[2];
ty[2] = g.y[2];
continue;
}
}
for (int l = 0; l < 3; l++) {
th[l] = (tx[l] << 4) + ty[l];
}
if (h[th[0]][th[1]][th[2]][g.r]) {
tx[2] = g.x[2];
ty[2] = g.y[2];
continue;
}
h[th[0]][th[1]][th[2]][g.r] = g.c;
q.push(Ghost(tx, ty, g.c + 1, g.r));
}
}
}
}
}
} | #include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
char f[16][16];
int fx, fy, N;
int dx[] = {0, 0, -1, 1, 0};
int dy[] = {0, -1, 0, 0, 1};
class Ghost {
public:
int x[3], y[3], c;
bool r;
Ghost(int *i, int *j, int c, bool r) : r(r), c(c) {
for (int k = 0; k < 3; k++) {
x[k] = i[k];
y[k] = j[k];
}
}
};
bool move(int x, int y, int p) {
if (p >= N)
return true;
if (x < 0 || y < 0 || x >= fx || y >= fy)
return false;
if (f[x][y] == '#')
return false;
return true;
}
bool crash(int *x, int *y, int *gx, int *gy, int p) {
if (p < N)
return false;
for (int i = 0; i < p; i++)
for (int j = i + 1; j < p; j++) {
if (x[i] == x[j] && y[i] == y[j])
return true;
if (x[i] == gx[j] && y[i] == gy[j] && gx[i] == x[j] && gy[i] == y[j])
return true;
}
return false;
}
unsigned char h[256][256][256][2];
int main() {
while (scanf("%d%d%d", &fx, &fy, &N), (fx || fy || N)) {
int sx[3] = {0}, sy[3] = {0}, gx[3] = {0}, gy[3] = {0};
memset(h, 0, sizeof(h));
string s;
getchar();
for (int i = 0; i < fy; i++) {
getline(cin, s);
for (int j = 0; j < fx; j++) {
if (islower(s[j])) {
sx[s[j] - 'a'] = j;
sy[s[j] - 'a'] = i;
}
if (isupper(s[j])) {
gx[s[j] - 'A'] = j;
gy[s[j] - 'A'] = i;
}
f[j][i] = s[j];
}
}
queue<Ghost> q;
q.push(Ghost(sx, sy, 1, 0));
q.push(Ghost(gx, gy, 1, 1));
int th[3];
for (int l = 0; l < 3; l++) {
th[l] = (sx[l] << 4) + sy[l];
}
h[th[0]][th[1]][th[2]][0] = 1;
for (int l = 0; l < 3; l++) {
th[l] = (gx[l] << 4) + gy[l];
}
h[th[0]][th[1]][th[2]][1] = 1;
while (!q.empty()) {
Ghost g = q.front();
q.pop();
int th[3];
for (int l = 0; l < 3; l++) {
th[l] = (g.x[l] << 4) + g.y[l];
}
if (h[th[0]][th[1]][th[2]][!g.r]) {
printf("%d\n", g.c + h[th[0]][th[1]][th[2]][!g.r] - 1);
break;
}
int tx[3], ty[3];
for (int i = 0; i < 3; i++) {
tx[i] = g.x[i];
ty[i] = g.y[i];
}
for (int i = 0; i < 5; i++) {
tx[0] = g.x[0] + dx[i], ty[0] = g.y[0] + dy[i];
if (!move(tx[0], ty[0], 0)) {
tx[0] = g.x[0];
ty[0] = g.y[0];
continue;
}
for (int j = 0; j < (N > 1 ? 5 : 1); j++) {
tx[1] = g.x[1] + dx[j], ty[1] = g.y[1] + dy[j];
if (N >= 2) {
if (!move(tx[1], ty[1], 1) || (crash(tx, ty, g.x, g.y, 2))) {
tx[1] = g.x[1];
ty[1] = g.y[1];
continue;
}
}
for (int k = 0; k < (N > 2 ? 5 : 1); k++) {
tx[2] = g.x[2] + dx[k], ty[2] = g.y[2] + dy[k];
if (N >= 3) {
if (!move(tx[2], ty[2], 2) || (crash(tx, ty, g.x, g.y, 3))) {
tx[2] = g.x[2];
ty[2] = g.y[2];
continue;
}
}
for (int l = 0; l < 3; l++) {
th[l] = (tx[l] << 4) + ty[l];
}
if (h[th[0]][th[1]][th[2]][g.r]) {
tx[2] = g.x[2];
ty[2] = g.y[2];
continue;
}
h[th[0]][th[1]][th[2]][g.r] = g.c;
q.push(Ghost(tx, ty, g.c + 1, g.r));
}
}
}
}
}
} | replace | 50 | 51 | 50 | 51 | MLE | |
p00860 | C++ | Time Limit Exceeded |
#pragma GCC target "avx"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define all(v) v.begin(), v.end()
const int INF = 1 << 30;
const int dx[] = {0, 1, 0, -1, 0};
const int dy[] = {1, 0, -1, 0, 0};
struct State {
array<int, 3> xs, ys;
State() {
fill(all(xs), 0);
fill(all(ys), 0);
}
int hash() {
int a = 0;
rep(i, 3) a = a | (xs[i] << (4 * i));
rep(i, 3) a = a | (ys[i] << (4 * i + 4 * 3));
return a;
}
};
array<short, 1 << 24> dist;
bool solve() {
int W, H, N;
vector<string> G;
cin >> W >> H >> N;
if (W == 0)
return false;
cin.ignore();
rep(i, H) {
string s;
getline(cin, s);
G.push_back(s);
}
State init, last;
rep(y, H) rep(x, W) {
int i = G[y][x] - 'a';
int j = G[y][x] - 'A';
if (0 <= i and i < N) {
init.ys[i] = y;
init.xs[i] = x;
}
if (0 <= j and j < N) {
last.ys[j] = y;
last.xs[j] = x;
}
}
auto on_field = [&](int y, int x) {
return 0 <= y and y < H and 0 <= x and x < W and G[y][x] != '#';
};
auto move = [&](State &s, int idx, int dir) {
s.xs[idx] += dx[dir];
s.ys[idx] += dy[dir];
return on_field(s.ys[idx], s.xs[idx]);
};
auto same = [&](State &s, int i) {
rep(j, i) {
if (s.ys[i] == s.ys[j] and s.xs[i] == s.xs[j])
return true;
}
return false;
};
auto swapped = [&](State &n, State &p) {
rep(i, N) rep(j, N) if (i != j) {
if ((n.ys[j] == p.ys[i] and n.xs[j] == p.xs[i]) and
(n.ys[i] == p.ys[j] and n.xs[i] == p.xs[j]))
return true;
}
return false;
};
int last_hash = last.hash();
queue<State> que;
fill(all(dist), -1);
que.push(init);
dist[init.hash()] = 0;
while (que.size()) {
State now = que.front();
que.pop();
int nh = now.hash();
rep(i, 5) {
State nxt1 = now;
if (not move(nxt1, 0, i))
continue;
rep(j, 5) {
State nxt2 = nxt1;
if (N >= 2 and not move(nxt2, 1, j))
continue;
if (N >= 2 and same(nxt2, 1))
continue;
rep(k, 5) {
State nxt3 = nxt2;
if (N >= 3 and not move(nxt3, 2, k))
continue;
if (N >= 3 and same(nxt3, 2))
continue;
if (not swapped(nxt3, now)) {
int h = nxt3.hash();
if (last_hash == h) {
cout << dist[nh] + 1 << endl;
return 1;
}
if (dist[h] < 0) {
dist[h] = dist[nh] + 1;
que.push(nxt3);
}
}
}
}
}
}
assert(0);
return 1;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (solve())
;
return 0;
} | #pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define all(v) v.begin(), v.end()
const int INF = 1 << 30;
const int dx[] = {0, 1, 0, -1, 0};
const int dy[] = {1, 0, -1, 0, 0};
struct State {
array<int, 3> xs, ys;
State() {
fill(all(xs), 0);
fill(all(ys), 0);
}
int hash() {
int a = 0;
rep(i, 3) a = a | (xs[i] << (4 * i));
rep(i, 3) a = a | (ys[i] << (4 * i + 4 * 3));
return a;
}
};
array<short, 1 << 24> dist;
bool solve() {
int W, H, N;
vector<string> G;
cin >> W >> H >> N;
if (W == 0)
return false;
cin.ignore();
rep(i, H) {
string s;
getline(cin, s);
G.push_back(s);
}
State init, last;
rep(y, H) rep(x, W) {
int i = G[y][x] - 'a';
int j = G[y][x] - 'A';
if (0 <= i and i < N) {
init.ys[i] = y;
init.xs[i] = x;
}
if (0 <= j and j < N) {
last.ys[j] = y;
last.xs[j] = x;
}
}
auto on_field = [&](int y, int x) {
return 0 <= y and y < H and 0 <= x and x < W and G[y][x] != '#';
};
auto move = [&](State &s, int idx, int dir) {
s.xs[idx] += dx[dir];
s.ys[idx] += dy[dir];
return on_field(s.ys[idx], s.xs[idx]);
};
auto same = [&](State &s, int i) {
rep(j, i) {
if (s.ys[i] == s.ys[j] and s.xs[i] == s.xs[j])
return true;
}
return false;
};
auto swapped = [&](State &n, State &p) {
rep(i, N) rep(j, N) if (i != j) {
if ((n.ys[j] == p.ys[i] and n.xs[j] == p.xs[i]) and
(n.ys[i] == p.ys[j] and n.xs[i] == p.xs[j]))
return true;
}
return false;
};
int last_hash = last.hash();
queue<State> que;
fill(all(dist), -1);
que.push(init);
dist[init.hash()] = 0;
while (que.size()) {
State now = que.front();
que.pop();
int nh = now.hash();
rep(i, 5) {
State nxt1 = now;
if (not move(nxt1, 0, i))
continue;
rep(j, 5) {
State nxt2 = nxt1;
if (N >= 2 and not move(nxt2, 1, j))
continue;
if (N >= 2 and same(nxt2, 1))
continue;
rep(k, 5) {
State nxt3 = nxt2;
if (N >= 3 and not move(nxt3, 2, k))
continue;
if (N >= 3 and same(nxt3, 2))
continue;
if (not swapped(nxt3, now)) {
int h = nxt3.hash();
if (last_hash == h) {
cout << dist[nh] + 1 << endl;
return 1;
}
if (dist[h] < 0) {
dist[h] = dist[nh] + 1;
que.push(nxt3);
}
}
}
}
}
}
assert(0);
return 1;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (solve())
;
return 0;
} | replace | 0 | 2 | 0 | 2 | TLE | |
p00860 | C++ | Time Limit Exceeded |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define all(v) v.begin(), v.end()
const int INF = 1 << 30;
const int dx[] = {0, 1, 0, -1, 0};
const int dy[] = {1, 0, -1, 0, 0};
struct State {
array<int, 3> xs, ys;
State() {
fill(all(xs), 0);
fill(all(ys), 0);
}
int hash() {
int a = 0;
rep(i, 3) a = a | (xs[i] << (4 * i));
rep(i, 3) a = a | (ys[i] << (4 * i + 4 * 3));
return a;
}
};
array<short, 1 << 24> dist;
bool solve() {
int W, H, N;
vector<string> G;
cin >> W >> H >> N;
if (W == 0)
return false;
cin.ignore();
rep(i, H) {
string s;
getline(cin, s);
G.push_back(s);
}
State init, last;
rep(y, H) rep(x, W) {
int i = G[y][x] - 'a';
int j = G[y][x] - 'A';
if (0 <= i and i < N) {
init.ys[i] = y;
init.xs[i] = x;
}
if (0 <= j and j < N) {
last.ys[j] = y;
last.xs[j] = x;
}
}
auto on_field = [&](int y, int x) {
return 0 <= y and y < H and 0 <= x and x < W and G[y][x] != '#';
};
auto move = [&](State &s, int idx, int dir) {
s.xs[idx] += dx[dir];
s.ys[idx] += dy[dir];
return on_field(s.ys[idx], s.xs[idx]);
};
auto same = [&](State &s, int i) {
rep(j, i) {
if (s.ys[i] == s.ys[j] and s.xs[i] == s.xs[j])
return true;
}
return false;
};
auto swapped = [&](State &n, State &p) {
rep(i, N) rep(j, N) if (i != j) {
if ((n.ys[j] == p.ys[i] and n.xs[j] == p.xs[i]) and
(n.ys[i] == p.ys[j] and n.xs[i] == p.xs[j]))
return true;
}
return false;
};
int last_hash = last.hash();
queue<State> que;
fill(all(dist), -1);
que.push(init);
dist[init.hash()] = 0;
while (que.size()) {
State now = que.front();
que.pop();
int nh = now.hash();
rep(i, 5) {
State nxt1 = now;
if (not move(nxt1, 0, i))
continue;
rep(j, 5) {
State nxt2 = nxt1;
if (N >= 2 and not move(nxt2, 1, j))
continue;
if (N >= 2 and same(nxt2, 1))
continue;
rep(k, 5) {
State nxt3 = nxt2;
if (N >= 3 and not move(nxt3, 2, k))
continue;
if (N >= 3 and same(nxt3, 2))
continue;
if (not swapped(nxt3, now)) {
int h = nxt3.hash();
if (last_hash == h) {
cout << dist[nh] + 1 << endl;
return 1;
}
if (dist[h] < 0) {
dist[h] = dist[nh] + 1;
que.push(nxt3);
}
}
}
}
}
}
assert(0);
return 1;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (solve())
;
return 0;
} | #pragma GCC optimize "O3"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define all(v) v.begin(), v.end()
const int INF = 1 << 30;
const int dx[] = {0, 1, 0, -1, 0};
const int dy[] = {1, 0, -1, 0, 0};
struct State {
array<int, 3> xs, ys;
State() {
fill(all(xs), 0);
fill(all(ys), 0);
}
int hash() {
int a = 0;
rep(i, 3) a = a | (xs[i] << (4 * i));
rep(i, 3) a = a | (ys[i] << (4 * i + 4 * 3));
return a;
}
};
array<short, 1 << 24> dist;
bool solve() {
int W, H, N;
vector<string> G;
cin >> W >> H >> N;
if (W == 0)
return false;
cin.ignore();
rep(i, H) {
string s;
getline(cin, s);
G.push_back(s);
}
State init, last;
rep(y, H) rep(x, W) {
int i = G[y][x] - 'a';
int j = G[y][x] - 'A';
if (0 <= i and i < N) {
init.ys[i] = y;
init.xs[i] = x;
}
if (0 <= j and j < N) {
last.ys[j] = y;
last.xs[j] = x;
}
}
auto on_field = [&](int y, int x) {
return 0 <= y and y < H and 0 <= x and x < W and G[y][x] != '#';
};
auto move = [&](State &s, int idx, int dir) {
s.xs[idx] += dx[dir];
s.ys[idx] += dy[dir];
return on_field(s.ys[idx], s.xs[idx]);
};
auto same = [&](State &s, int i) {
rep(j, i) {
if (s.ys[i] == s.ys[j] and s.xs[i] == s.xs[j])
return true;
}
return false;
};
auto swapped = [&](State &n, State &p) {
rep(i, N) rep(j, N) if (i != j) {
if ((n.ys[j] == p.ys[i] and n.xs[j] == p.xs[i]) and
(n.ys[i] == p.ys[j] and n.xs[i] == p.xs[j]))
return true;
}
return false;
};
int last_hash = last.hash();
queue<State> que;
fill(all(dist), -1);
que.push(init);
dist[init.hash()] = 0;
while (que.size()) {
State now = que.front();
que.pop();
int nh = now.hash();
rep(i, 5) {
State nxt1 = now;
if (not move(nxt1, 0, i))
continue;
rep(j, 5) {
State nxt2 = nxt1;
if (N >= 2 and not move(nxt2, 1, j))
continue;
if (N >= 2 and same(nxt2, 1))
continue;
rep(k, 5) {
State nxt3 = nxt2;
if (N >= 3 and not move(nxt3, 2, k))
continue;
if (N >= 3 and same(nxt3, 2))
continue;
if (not swapped(nxt3, now)) {
int h = nxt3.hash();
if (last_hash == h) {
cout << dist[nh] + 1 << endl;
return 1;
}
if (dist[h] < 0) {
dist[h] = dist[nh] + 1;
que.push(nxt3);
}
}
}
}
}
}
assert(0);
return 1;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (solve())
;
return 0;
} | replace | 0 | 1 | 0 | 1 | TLE | |
p00860 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define all(v) v.begin(), v.end()
const int INF = 1 << 30;
const int dx[] = {0, 1, 0, -1, 0};
const int dy[] = {1, 0, -1, 0, 0};
struct State {
array<int, 3> xs, ys;
State() {
fill(all(xs), 0);
fill(all(ys), 0);
}
int hash() {
int a = 0;
rep(i, 3) a = a | (xs[i] << (4 * i));
rep(i, 3) a = a | (ys[i] << (4 * i + 4 * 3));
return a;
}
};
array<short, 1 << 24> dist;
bool solve() {
int W, H, N;
vector<string> G;
cin >> W >> H >> N;
if (W == 0)
return false;
cin.ignore();
rep(i, H) {
string s;
getline(cin, s);
G.push_back(s);
}
State init, last;
rep(y, H) rep(x, W) {
int i = G[y][x] - 'a';
int j = G[y][x] - 'A';
if (0 <= i and i < N) {
init.ys[i] = y;
init.xs[i] = x;
}
if (0 <= j and j < N) {
last.ys[j] = y;
last.xs[j] = x;
}
}
auto on_field = [&](int y, int x) {
return 0 <= y and y < H and 0 <= x and x < W and G[y][x] != '#';
};
auto move = [&](State &s, int idx, int dir) {
s.xs[idx] += dx[dir];
s.ys[idx] += dy[dir];
return on_field(s.ys[idx], s.xs[idx]);
};
auto same = [&](State &s, int i) {
rep(j, i) {
if (s.ys[i] == s.ys[j] and s.xs[i] == s.xs[j])
return true;
}
return false;
};
auto swapped = [&](State &n, State &p) {
rep(i, N) rep(j, N) if (i != j) {
if ((n.ys[j] == p.ys[i] and n.xs[j] == p.xs[i]) and
(n.ys[i] == p.ys[j] and n.xs[i] == p.xs[j]))
return true;
}
return false;
};
int last_hash = last.hash();
queue<State> que;
fill(all(dist), -1);
que.push(init);
dist[init.hash()] = 0;
while (que.size()) {
State now = que.front();
que.pop();
int nh = now.hash();
rep(i, 5) {
State nxt1 = now;
if (not move(nxt1, 0, i))
continue;
rep(j, 5) {
State nxt2 = nxt1;
if (N >= 2 and not move(nxt2, 1, j))
continue;
if (N >= 2 and same(nxt2, 1))
continue;
rep(k, 5) {
State nxt3 = nxt2;
if (N >= 3 and not move(nxt3, 2, k))
continue;
if (N >= 3 and same(nxt3, 2))
continue;
if (not swapped(nxt3, now)) {
int h = nxt3.hash();
if (last_hash == h) {
cout << dist[nh] + 1 << endl;
return 1;
}
if (dist[h] < 0) {
dist[h] = dist[nh] + 1;
que.push(nxt3);
}
}
}
}
}
}
assert(0);
return 1;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (solve())
;
return 0;
} | #pragma GCC optimize "O3"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define all(v) v.begin(), v.end()
const int INF = 1 << 30;
const int dx[] = {0, 1, 0, -1, 0};
const int dy[] = {1, 0, -1, 0, 0};
struct State {
array<int, 3> xs, ys;
State() {
fill(all(xs), 0);
fill(all(ys), 0);
}
int hash() {
int a = 0;
rep(i, 3) a = a | (xs[i] << (4 * i));
rep(i, 3) a = a | (ys[i] << (4 * i + 4 * 3));
return a;
}
};
array<short, 1 << 24> dist;
bool solve() {
int W, H, N;
vector<string> G;
cin >> W >> H >> N;
if (W == 0)
return false;
cin.ignore();
rep(i, H) {
string s;
getline(cin, s);
G.push_back(s);
}
State init, last;
rep(y, H) rep(x, W) {
int i = G[y][x] - 'a';
int j = G[y][x] - 'A';
if (0 <= i and i < N) {
init.ys[i] = y;
init.xs[i] = x;
}
if (0 <= j and j < N) {
last.ys[j] = y;
last.xs[j] = x;
}
}
auto on_field = [&](int y, int x) {
return 0 <= y and y < H and 0 <= x and x < W and G[y][x] != '#';
};
auto move = [&](State &s, int idx, int dir) {
s.xs[idx] += dx[dir];
s.ys[idx] += dy[dir];
return on_field(s.ys[idx], s.xs[idx]);
};
auto same = [&](State &s, int i) {
rep(j, i) {
if (s.ys[i] == s.ys[j] and s.xs[i] == s.xs[j])
return true;
}
return false;
};
auto swapped = [&](State &n, State &p) {
rep(i, N) rep(j, N) if (i != j) {
if ((n.ys[j] == p.ys[i] and n.xs[j] == p.xs[i]) and
(n.ys[i] == p.ys[j] and n.xs[i] == p.xs[j]))
return true;
}
return false;
};
int last_hash = last.hash();
queue<State> que;
fill(all(dist), -1);
que.push(init);
dist[init.hash()] = 0;
while (que.size()) {
State now = que.front();
que.pop();
int nh = now.hash();
rep(i, 5) {
State nxt1 = now;
if (not move(nxt1, 0, i))
continue;
rep(j, 5) {
State nxt2 = nxt1;
if (N >= 2 and not move(nxt2, 1, j))
continue;
if (N >= 2 and same(nxt2, 1))
continue;
rep(k, 5) {
State nxt3 = nxt2;
if (N >= 3 and not move(nxt3, 2, k))
continue;
if (N >= 3 and same(nxt3, 2))
continue;
if (not swapped(nxt3, now)) {
int h = nxt3.hash();
if (last_hash == h) {
cout << dist[nh] + 1 << endl;
return 1;
}
if (dist[h] < 0) {
dist[h] = dist[nh] + 1;
que.push(nxt3);
}
}
}
}
}
}
assert(0);
return 1;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (solve())
;
return 0;
} | insert | 0 | 0 | 0 | 1 | TLE | |
p00860 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define all(v) v.begin(), v.end()
const int INF = 1 << 30;
const int dx[] = {0, 1, 0, -1, 0};
const int dy[] = {1, 0, -1, 0, 0};
struct State {
array<int, 3> xs, ys;
State() {
fill(all(xs), 0);
fill(all(ys), 0);
}
int hash() {
int a = 0;
rep(i, 3) a = a | (xs[i] << (4 * i));
rep(i, 3) a = a | (ys[i] << (4 * i + 4 * 3));
return a;
}
};
array<short, 1 << 24> dist;
bool solve() {
int W, H, N;
vector<string> G;
cin >> W >> H >> N;
if (W == 0)
return false;
cin.ignore();
rep(i, H) {
string s;
getline(cin, s);
G.push_back(s);
}
State init, last;
rep(y, H) rep(x, W) {
int i = G[y][x] - 'a';
int j = G[y][x] - 'A';
if (0 <= i and i < N) {
init.ys[i] = y;
init.xs[i] = x;
}
if (0 <= j and j < N) {
last.ys[j] = y;
last.xs[j] = x;
}
}
auto on_field = [&](int y, int x) {
return 0 <= y and y < H and 0 <= x and x < W and G[y][x] != '#';
};
auto move = [&](State &s, int idx, int dir) {
s.xs[idx] += dx[dir];
s.ys[idx] += dy[dir];
return on_field(s.ys[idx], s.xs[idx]);
};
auto same = [&](State &s, int i) {
rep(j, i) {
if (s.ys[i] == s.ys[j] and s.xs[i] == s.xs[j])
return true;
}
return false;
};
auto swapped = [&](State &n, State &p) {
rep(i, N) rep(j, N) if (i != j) {
if ((n.ys[j] == p.ys[i] and n.xs[j] == p.xs[i]) and
(n.ys[i] == p.ys[j] and n.xs[i] == p.xs[j]))
return true;
}
return false;
};
int last_hash = last.hash();
queue<State> que;
fill(all(dist), -1);
que.push(init);
dist[init.hash()] = 0;
while (que.size()) {
State now = que.front();
que.pop();
int nh = now.hash();
rep(i, 5) {
State nxt1 = now;
if (not move(nxt1, 0, i))
continue;
rep(j, 5) {
State nxt2 = nxt1;
if (N >= 2 and not move(nxt2, 1, j))
continue;
if (N >= 2 and same(nxt2, 1))
continue;
rep(k, 5) {
State nxt3 = nxt2;
if (N >= 3 and not move(nxt3, 2, k))
continue;
if (N >= 3 and same(nxt3, 2))
continue;
if (not swapped(nxt3, now)) {
int h = nxt3.hash();
if (last_hash == h) {
cout << dist[nh] + 1 << endl;
return 1;
}
if (dist[h] < 0) {
dist[h] = dist[nh] + 1;
que.push(nxt3);
}
}
}
}
}
}
assert(0);
return 1;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (solve())
;
return 0;
} | #pragma GCC optimize "O3"
#pragma GCC target "tune=native"
#pragma GCC target "avx"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define all(v) v.begin(), v.end()
const int INF = 1 << 30;
const int dx[] = {0, 1, 0, -1, 0};
const int dy[] = {1, 0, -1, 0, 0};
struct State {
array<int, 3> xs, ys;
State() {
fill(all(xs), 0);
fill(all(ys), 0);
}
int hash() {
int a = 0;
rep(i, 3) a = a | (xs[i] << (4 * i));
rep(i, 3) a = a | (ys[i] << (4 * i + 4 * 3));
return a;
}
};
array<short, 1 << 24> dist;
bool solve() {
int W, H, N;
vector<string> G;
cin >> W >> H >> N;
if (W == 0)
return false;
cin.ignore();
rep(i, H) {
string s;
getline(cin, s);
G.push_back(s);
}
State init, last;
rep(y, H) rep(x, W) {
int i = G[y][x] - 'a';
int j = G[y][x] - 'A';
if (0 <= i and i < N) {
init.ys[i] = y;
init.xs[i] = x;
}
if (0 <= j and j < N) {
last.ys[j] = y;
last.xs[j] = x;
}
}
auto on_field = [&](int y, int x) {
return 0 <= y and y < H and 0 <= x and x < W and G[y][x] != '#';
};
auto move = [&](State &s, int idx, int dir) {
s.xs[idx] += dx[dir];
s.ys[idx] += dy[dir];
return on_field(s.ys[idx], s.xs[idx]);
};
auto same = [&](State &s, int i) {
rep(j, i) {
if (s.ys[i] == s.ys[j] and s.xs[i] == s.xs[j])
return true;
}
return false;
};
auto swapped = [&](State &n, State &p) {
rep(i, N) rep(j, N) if (i != j) {
if ((n.ys[j] == p.ys[i] and n.xs[j] == p.xs[i]) and
(n.ys[i] == p.ys[j] and n.xs[i] == p.xs[j]))
return true;
}
return false;
};
int last_hash = last.hash();
queue<State> que;
fill(all(dist), -1);
que.push(init);
dist[init.hash()] = 0;
while (que.size()) {
State now = que.front();
que.pop();
int nh = now.hash();
rep(i, 5) {
State nxt1 = now;
if (not move(nxt1, 0, i))
continue;
rep(j, 5) {
State nxt2 = nxt1;
if (N >= 2 and not move(nxt2, 1, j))
continue;
if (N >= 2 and same(nxt2, 1))
continue;
rep(k, 5) {
State nxt3 = nxt2;
if (N >= 3 and not move(nxt3, 2, k))
continue;
if (N >= 3 and same(nxt3, 2))
continue;
if (not swapped(nxt3, now)) {
int h = nxt3.hash();
if (last_hash == h) {
cout << dist[nh] + 1 << endl;
return 1;
}
if (dist[h] < 0) {
dist[h] = dist[nh] + 1;
que.push(nxt3);
}
}
}
}
}
}
assert(0);
return 1;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (solve())
;
return 0;
} | insert | 0 | 0 | 0 | 3 | TLE | |
p00860 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef vector<pair<int, int>> T;
const int vy[] = {0, 1, 0, -1, 0}, vx[] = {1, 0, -1, 0, 0};
int tail;
vector<pair<int, int>> vec[1000];
int W, H, N;
string S[16];
int v[16 * 16 * 16 * 16 * 16 * 16];
bool isover(int y, int x) {
return (y < 0 || y >= H || x < 0 || x >= W || S[y][x] == '#');
}
void CreateVector(int sz, vector<pair<int, int>> &vs) {
if (sz == 0) {
vec[tail++] = vs;
return;
}
for (int i = 0; i < 5; i++) {
int ny = vs[sz - 1].first + vy[i];
int nx = vs[sz - 1].second + vx[i];
if (isover(ny, nx))
continue;
vs[sz - 1].first += vy[i];
vs[sz - 1].second += vx[i];
CreateVector(sz - 1, vs);
vs[sz - 1].first -= vy[i];
vs[sz - 1].second -= vx[i];
}
}
int hashh(vector<pair<int, int>> &vs) {
int ret = 0;
for (int k = vs.size() - 1; k >= 0; k--) {
ret *= 16;
ret += vs[k].second;
ret *= 16;
ret += vs[k].first;
}
return (ret);
}
vector<pair<int, int>> hashd(int h) {
vector<pair<int, int>> vs(N);
int ret = 0;
for (auto &k : vs) {
vs[ret].first = h & 15;
h >>= 4;
vs[ret++].second = h & 15;
h >>= 4;
}
return (vs);
}
int main() {
while (cin >> W >> H >> N, W) {
vector<char> nums;
cin.ignore();
for (int i = 0; i < H; i++) {
getline(cin, S[i]);
for (int j = 0; j < W; j++) {
if (islower(S[i][j]))
nums.push_back(S[i][j]);
}
}
sort(begin(nums), end(nums));
vector<pair<int, int>> vs(N), ans(N);
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (islower(S[i][j])) {
auto q = lower_bound(begin(nums), end(nums), S[i][j]) - begin(nums);
vs[q] = {i, j};
} else if (isupper(S[i][j])) {
auto q = lower_bound(begin(nums), end(nums), tolower(S[i][j])) -
begin(nums);
ans[q] = {i, j};
}
}
}
queue<int> que;
que.emplace(hashh(vs));
memset(v, -1, sizeof(v));
v[hashh(vs)] = 0;
while (!que.empty()) {
auto ss = que.front();
int cost = v[ss];
que.pop();
auto p = hashd(ss);
if (p == ans) {
cout << cost << endl;
break;
}
tail = 0;
CreateVector(N, p);
for (int z = 0; z < tail; z++) {
auto &beet = vec[z];
bool isok = true;
for (int i = 0; i < p.size(); i++) {
for (int j = 0; j < i; j++) {
isok &= beet[i] != p[j] || beet[j] != p[i];
isok &= beet[i] != beet[j];
}
if (!isok)
break;
}
int hashed = hashh(beet);
if (isok && v[hashed] == -1) {
que.emplace(hashed);
v[hashed] = cost + 1;
}
}
}
}
} | #include <bits/stdc++.h>
using namespace std;
typedef vector<pair<int, int>> T;
const int vy[] = {0, 1, 0, -1, 0}, vx[] = {1, 0, -1, 0, 0};
int tail;
vector<pair<int, int>> vec[1000];
int W, H, N;
string S[16];
short v[16 * 16 * 16 * 16 * 16 * 16];
bool isover(int y, int x) {
return (y < 0 || y >= H || x < 0 || x >= W || S[y][x] == '#');
}
void CreateVector(int sz, vector<pair<int, int>> &vs) {
if (sz == 0) {
vec[tail++] = vs;
return;
}
for (int i = 0; i < 5; i++) {
int ny = vs[sz - 1].first + vy[i];
int nx = vs[sz - 1].second + vx[i];
if (isover(ny, nx))
continue;
vs[sz - 1].first += vy[i];
vs[sz - 1].second += vx[i];
CreateVector(sz - 1, vs);
vs[sz - 1].first -= vy[i];
vs[sz - 1].second -= vx[i];
}
}
int hashh(vector<pair<int, int>> &vs) {
int ret = 0;
for (int k = vs.size() - 1; k >= 0; k--) {
ret *= 16;
ret += vs[k].second;
ret *= 16;
ret += vs[k].first;
}
return (ret);
}
vector<pair<int, int>> hashd(int h) {
vector<pair<int, int>> vs(N);
int ret = 0;
for (auto &k : vs) {
vs[ret].first = h & 15;
h >>= 4;
vs[ret++].second = h & 15;
h >>= 4;
}
return (vs);
}
int main() {
while (cin >> W >> H >> N, W) {
vector<char> nums;
cin.ignore();
for (int i = 0; i < H; i++) {
getline(cin, S[i]);
for (int j = 0; j < W; j++) {
if (islower(S[i][j]))
nums.push_back(S[i][j]);
}
}
sort(begin(nums), end(nums));
vector<pair<int, int>> vs(N), ans(N);
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (islower(S[i][j])) {
auto q = lower_bound(begin(nums), end(nums), S[i][j]) - begin(nums);
vs[q] = {i, j};
} else if (isupper(S[i][j])) {
auto q = lower_bound(begin(nums), end(nums), tolower(S[i][j])) -
begin(nums);
ans[q] = {i, j};
}
}
}
queue<int> que;
que.emplace(hashh(vs));
memset(v, -1, sizeof(v));
v[hashh(vs)] = 0;
while (!que.empty()) {
auto ss = que.front();
int cost = v[ss];
que.pop();
auto p = hashd(ss);
if (p == ans) {
cout << cost << endl;
break;
}
tail = 0;
CreateVector(N, p);
for (int z = 0; z < tail; z++) {
auto &beet = vec[z];
bool isok = true;
for (int i = 0; i < p.size(); i++) {
for (int j = 0; j < i; j++) {
isok &= beet[i] != p[j] || beet[j] != p[i];
isok &= beet[i] != beet[j];
}
if (!isok)
break;
}
int hashed = hashh(beet);
if (isok && v[hashed] == -1) {
que.emplace(hashed);
v[hashed] = cost + 1;
}
}
}
}
} | replace | 13 | 14 | 13 | 14 | MLE | |
p00861 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;
map<string, ll> sz;
map<pair<string, ll>, ll> val;
string in[1000], s;
char buf[90];
int p;
ll expr() {
if (isdigit(s[p])) {
ll res = 0;
while (isdigit(s[p]))
res *= 10, res += s[p++] - '0';
return res;
}
string name;
while (isalpha(s[p]))
name += s[p++];
p++;
ll d = expr();
p++;
if (d < 0 || d >= sz[name] || !val.count(mp(name, d)))
throw "error";
return val[mp(name, d)];
}
int main() {
while (cin >> in[0], in[0] != ".") {
int n = 1;
while (cin >> in[n], in[n] != ".")
n++;
sz.clear();
val.clear();
rep(i, n) {
try {
int pos = in[i].find("=");
ll d, d2;
if (pos == in[i].npos) {
rep(j, in[i].size()) if (in[i][j] == '[' || in[i][j] == ']')
in[i][j] = ' ';
sscanf(in[i].c_str(), "%s%lld", buf, &d);
sz[buf] = d;
} else {
s = in[i].substr(pos + 1);
in[i] = in[i].substr(0, pos);
p = 0;
d = expr();
pos = in[i].find("[");
string name = in[i].substr(0, pos);
s = in[i].substr(pos + 1, in[i].rfind("]") - pos - 1);
p = 0;
d2 = expr();
if (d2 < 0 || d2 >= sz[name])
throw "error";
val[mp(name, d2)] = d;
}
} catch (const char *e) {
cout << i + 1 << endl;
goto END;
}
}
cout << 0 << endl;
END:;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;
map<string, ll> sz;
map<pair<string, ll>, ll> val;
string in[1001], s;
char buf[90];
int p;
ll expr() {
if (isdigit(s[p])) {
ll res = 0;
while (isdigit(s[p]))
res *= 10, res += s[p++] - '0';
return res;
}
string name;
while (isalpha(s[p]))
name += s[p++];
p++;
ll d = expr();
p++;
if (d < 0 || d >= sz[name] || !val.count(mp(name, d)))
throw "error";
return val[mp(name, d)];
}
int main() {
while (cin >> in[0], in[0] != ".") {
int n = 1;
while (cin >> in[n], in[n] != ".")
n++;
sz.clear();
val.clear();
rep(i, n) {
try {
int pos = in[i].find("=");
ll d, d2;
if (pos == in[i].npos) {
rep(j, in[i].size()) if (in[i][j] == '[' || in[i][j] == ']')
in[i][j] = ' ';
sscanf(in[i].c_str(), "%s%lld", buf, &d);
sz[buf] = d;
} else {
s = in[i].substr(pos + 1);
in[i] = in[i].substr(0, pos);
p = 0;
d = expr();
pos = in[i].find("[");
string name = in[i].substr(0, pos);
s = in[i].substr(pos + 1, in[i].rfind("]") - pos - 1);
p = 0;
d2 = expr();
if (d2 < 0 || d2 >= sz[name])
throw "error";
val[mp(name, d2)] = d;
}
} catch (const char *e) {
cout << i + 1 << endl;
goto END;
}
}
cout << 0 << endl;
END:;
}
return 0;
} | replace | 30 | 31 | 30 | 31 | 0 | |
p00861 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
map<char, int> declaration;
map<pair<char, int>, int> assignment;
int expression(string &s, int &idx, int now) {
char array_name = s[idx];
if (isalpha(array_name)) {
++idx;
++idx;
int get = expression(s, idx, now);
if (declaration[array_name] <= get)
throw(now);
if (!assignment.count({array_name, get}))
throw(now);
++idx;
return (assignment[{array_name, get}]);
} else {
int ret = 0;
while (isdigit(s[idx])) {
ret = ret * 10 - '0' + s[idx++];
}
return (ret);
}
}
void exec(string &s, int now) {
char array_name = s[0];
if (s.find('=') == string::npos) {
int number = 0, ptr = 2;
while (isdigit(s[ptr])) {
number = number * 10 - '0' + s[ptr++];
}
declaration.emplace(array_name, number);
} else {
int ptr = 2;
int number = expression(s, ptr, now);
if (declaration[array_name] <= number)
throw(now);
++ptr;
++ptr;
int get = expression(s, ptr, now);
assignment[{array_name, number}] = get;
}
}
int main() {
string S[1000];
while (cin >> S[0], S[0] != ".") {
for (int i = 1; S[i - 1] != "."; ++i) {
cin >> S[i];
}
try {
for (int i = 0; S[i] != "."; i++) {
exec(S[i], i + 1);
}
throw 0;
} catch (int x) {
cout << x << endl;
}
declaration.clear();
assignment.clear();
}
} | #include <bits/stdc++.h>
using namespace std;
map<char, int> declaration;
map<pair<char, int>, int> assignment;
int expression(string &s, int &idx, int now) {
char array_name = s[idx];
if (isalpha(array_name)) {
++idx;
++idx;
int get = expression(s, idx, now);
if (declaration[array_name] <= get)
throw(now);
if (!assignment.count({array_name, get}))
throw(now);
++idx;
return (assignment[{array_name, get}]);
} else {
int ret = 0;
while (isdigit(s[idx])) {
ret = ret * 10 - '0' + s[idx++];
}
return (ret);
}
}
void exec(string &s, int now) {
char array_name = s[0];
if (s.find('=') == string::npos) {
int number = 0, ptr = 2;
while (isdigit(s[ptr])) {
number = number * 10 - '0' + s[ptr++];
}
declaration.emplace(array_name, number);
} else {
int ptr = 2;
int number = expression(s, ptr, now);
if (declaration[array_name] <= number)
throw(now);
++ptr;
++ptr;
int get = expression(s, ptr, now);
assignment[{array_name, number}] = get;
}
}
int main() {
string S[1005];
while (cin >> S[0], S[0] != ".") {
for (int i = 1; S[i - 1] != "."; ++i) {
cin >> S[i];
}
try {
for (int i = 0; S[i] != "."; i++) {
exec(S[i], i + 1);
}
throw 0;
} catch (int x) {
cout << x << endl;
}
declaration.clear();
assignment.clear();
}
} | replace | 51 | 52 | 51 | 52 | 0 | |
p00862 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PI;
const double EPS = 1e-6;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define F first
#define S second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) a.rbegin(), a.rend()
#define FLL(a, b) memset((a), b, sizeof(a))
#define CLR(a) FLL(a, 0)
#define declare(a, it) __typeof(a) it = (a)
#define FOR(it, a) for (declare(a.begin(), it); it != a.end(); ++it)
#define FORR(it, a) for (declare(a.rbegin(), it); it != a.rend(); ++it)
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &v) {
return o << "(" << v.F << ", " << v.S << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
o << "{";
rep(i, SZ(v)) o << (i ? ", " : "") << v[i];
return o << "}";
}
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};
int s2i(string &a) {
stringstream ss(a);
int r;
ss >> r;
return r;
}
int geti() {
int n;
scanf("%d", &n);
return n;
}
typedef double FP;
typedef complex<FP> pt;
typedef pt P;
typedef pair<pt, pt> line;
namespace std {
bool operator<(const P &a, const P &b) {
if (abs(a.real() - b.real()) > EPS)
return a.real() < b.real();
return a.imag() < b.imag();
}
} // namespace std
FP dot(P a, P b) { return real(conj(a) * b); }
FP crs(P a, P b) { return imag(conj(a) * b); }
P ortho(P a) { return P(imag(a), -real(a)); }
P ortho(line a) { return ortho(a.second - a.first); }
P crspt(P a, P b, P c, P d) {
b -= a, d -= c;
return a + b * crs(d, c - a) / crs(d, b);
}
P crspt(line a, line b) { return crspt(a.first, a.second, b.first, b.second); }
bool onl(P a1, P a2, P b) {
return abs(b - a1) + abs(b - a2) < abs(a1 - a2) + EPS;
}
bool onl(line a, P b) { return onl(a.first, a.second, b); }
bool iscrs(line a, line b) {
P c = crspt(a, b);
return onl(a, c) && onl(b, c);
}
line mkl(P a, P v) { return line(a, a + v); }
FP lpdist(line a, P b) { return abs(b - crspt(a, mkl(b, ortho(a)))); }
void check(P a) {
// assert(!isnan(a.real()));
// assert(!isnan(a.imag()));
}
void check(line a) {
check(a.first);
check(a.second);
}
bool para(line a, line b) {
P av = a.first - a.second;
P bv = b.first - b.second;
return abs(dot(av / abs(av), bv / abs(bv))) > 1 - 1e-9;
}
double tri(P a, P b, P c) {
a -= c;
b -= c;
return abs(crs(a, b));
}
line midline(line a, line b) {
if (!para(a, b)) {
P av = a.second - a.first;
P bv = b.second - b.first;
av /= abs(av);
bv /= abs(bv);
auto ret = line(crspt(a, b), crspt(a, b) + av + bv);
check(ret);
return ret;
}
P st = (a.first + b.first) / 2.;
auto ret = line(st, st + a.second - a.first);
check(ret);
return ret;
}
int main(int argc, char *argv[]) {
int n;
while (cin >> n && n) {
int x[n], y[n];
for (int i = 0; i < n; ++i)
cin >> x[i] >> y[i];
double cx = accumulate(x, x + n, 0.) / n;
double cy = accumulate(y, y + n, 0.) / n;
double ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i == j)
continue;
double r = 10000000000;
for (int k = 0; k < n; ++k) {
if (i == k || j == k)
continue;
line il = line(P(x[i], y[i]), P(x[(i + 1) % n], y[(i + 1) % n]));
line jl = line(P(x[(j + 1) % n], y[(j + 1) % n]), P(x[j], y[j]));
line kl = line(P(x[(k + 1) % n], y[(k + 1) % n]), P(x[k], y[k]));
line ij = midline(il, jl);
line ik = midline(il, kl);
// if(!iscrs(ij, ik)){
// cout << ij << " " << ik << endl;
// continue;
// }
P ce = crspt(ij, ik);
if (!isnormal(ce.real()) || !isnormal(ce.imag())) {
// cout << ce << endl;
continue;
}
double me = 0;
double me2 = 0;
for (int ii = 0; ii < n; ++ii) {
me += tri(P(x[ii], y[ii]), P(x[(ii + 1) % n], y[(ii + 1) % n]), ce);
me2 += tri(P(x[ii], y[ii]), P(x[(ii + 1) % n], y[(ii + 1) % n]),
P(cx, cy));
}
if (abs(me - me2) / max(me, me2) > EPS) {
// cout << me << " " << me2 << endl;
continue;
}
double tr = 10000000000;
for (int ii = 0; ii < n; ++ii) {
line l(P(x[ii], y[ii]), P(x[(ii + 1) % n], y[(ii + 1) % n]));
tr = min(tr, lpdist(l, ce));
}
ans = max(tr, ans);
// cout << ce << endl;
double cr = lpdist(il, ce);
r = min(r, cr);
}
// ans = max(r, ans);
}
}
printf("%.8f\n", ans);
}
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PI;
const double EPS = 1e-6;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define F first
#define S second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) a.rbegin(), a.rend()
#define FLL(a, b) memset((a), b, sizeof(a))
#define CLR(a) FLL(a, 0)
#define declare(a, it) __typeof(a) it = (a)
#define FOR(it, a) for (declare(a.begin(), it); it != a.end(); ++it)
#define FORR(it, a) for (declare(a.rbegin(), it); it != a.rend(); ++it)
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &v) {
return o << "(" << v.F << ", " << v.S << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
o << "{";
rep(i, SZ(v)) o << (i ? ", " : "") << v[i];
return o << "}";
}
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};
int s2i(string &a) {
stringstream ss(a);
int r;
ss >> r;
return r;
}
int geti() {
int n;
scanf("%d", &n);
return n;
}
typedef double FP;
typedef complex<FP> pt;
typedef pt P;
typedef pair<pt, pt> line;
namespace std {
bool operator<(const P &a, const P &b) {
if (abs(a.real() - b.real()) > EPS)
return a.real() < b.real();
return a.imag() < b.imag();
}
} // namespace std
FP dot(P a, P b) { return real(conj(a) * b); }
FP crs(P a, P b) { return imag(conj(a) * b); }
P ortho(P a) { return P(imag(a), -real(a)); }
P ortho(line a) { return ortho(a.second - a.first); }
P crspt(P a, P b, P c, P d) {
b -= a, d -= c;
return a + b * crs(d, c - a) / crs(d, b);
}
P crspt(line a, line b) { return crspt(a.first, a.second, b.first, b.second); }
bool onl(P a1, P a2, P b) {
return abs(b - a1) + abs(b - a2) < abs(a1 - a2) + EPS;
}
bool onl(line a, P b) { return onl(a.first, a.second, b); }
bool iscrs(line a, line b) {
P c = crspt(a, b);
return onl(a, c) && onl(b, c);
}
line mkl(P a, P v) { return line(a, a + v); }
FP lpdist(line a, P b) { return abs(b - crspt(a, mkl(b, ortho(a)))); }
void check(P a) {
// assert(!isnan(a.real()));
// assert(!isnan(a.imag()));
}
void check(line a) {
check(a.first);
check(a.second);
}
bool para(line a, line b) {
P av = a.first - a.second;
P bv = b.first - b.second;
return abs(dot(av / abs(av), bv / abs(bv))) > 1 - 1e-9;
}
double tri(P a, P b, P c) {
a -= c;
b -= c;
return abs(crs(a, b));
}
line midline(line a, line b) {
if (!para(a, b)) {
P av = a.second - a.first;
P bv = b.second - b.first;
av /= abs(av);
bv /= abs(bv);
auto ret = line(crspt(a, b), crspt(a, b) + av + bv);
check(ret);
return ret;
}
P st = (a.first + b.first) / 2.;
auto ret = line(st, st + a.second - a.first);
check(ret);
return ret;
}
int main(int argc, char *argv[]) {
int n;
while (cin >> n && n) {
int x[n], y[n];
for (int i = 0; i < n; ++i)
cin >> x[i] >> y[i];
double cx = accumulate(x, x + n, 0.) / n;
double cy = accumulate(y, y + n, 0.) / n;
double ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (i == j)
continue;
double r = 10000000000;
for (int k = 0; k < n; ++k) {
if (i == k || j == k)
continue;
line il = line(P(x[i], y[i]), P(x[(i + 1) % n], y[(i + 1) % n]));
line jl = line(P(x[(j + 1) % n], y[(j + 1) % n]), P(x[j], y[j]));
line kl = line(P(x[(k + 1) % n], y[(k + 1) % n]), P(x[k], y[k]));
line ij = midline(il, jl);
line ik = midline(il, kl);
// if(!iscrs(ij, ik)){
// cout << ij << " " << ik << endl;
// continue;
// }
P ce = crspt(ij, ik);
if (!isnormal(ce.real()) || !isnormal(ce.imag())) {
// cout << ce << endl;
continue;
}
double me = 0;
double me2 = 0;
for (int ii = 0; ii < n; ++ii) {
me += tri(P(x[ii], y[ii]), P(x[(ii + 1) % n], y[(ii + 1) % n]), ce);
me2 += tri(P(x[ii], y[ii]), P(x[(ii + 1) % n], y[(ii + 1) % n]),
P(cx, cy));
}
if (abs(me - me2) / max(me, me2) > EPS) {
// cout << me << " " << me2 << endl;
continue;
}
double tr = 10000000000;
for (int ii = 0; ii < n; ++ii) {
line l(P(x[ii], y[ii]), P(x[(ii + 1) % n], y[(ii + 1) % n]));
tr = min(tr, lpdist(l, ce));
}
ans = max(tr, ans);
// cout << ce << endl;
double cr = lpdist(il, ce);
r = min(r, cr);
}
// ans = max(r, ans);
}
}
printf("%.8f\n", ans);
}
}
| replace | 125 | 126 | 125 | 126 | TLE | |
p00864 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int n, w;
int dat[100];
int maxi;
int l, r;
double calc() {
double ans = 0;
l = 0;
for (int i = l; i <= r; ++i) {
ans += ((double)(r - i) / (r - l)) * ((double)dat[i] / maxi);
}
return ans + 0.01;
}
int main() {
while ((cin >> n >> w) && (n || w)) {
fill(dat, dat + 101, 0);
for (int i = 0; i < n; ++i) {
int v;
cin >> v;
v /= w;
++dat[v];
l = i == 0 ? v : min(l, v);
r = i == 0 ? v : max(r, v);
maxi = i == 0 ? dat[v] : max(maxi, dat[v]);
}
cout << calc() << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int n, w;
int dat[101];
int maxi;
int l, r;
double calc() {
double ans = 0;
l = 0;
for (int i = l; i <= r; ++i) {
ans += ((double)(r - i) / (r - l)) * ((double)dat[i] / maxi);
}
return ans + 0.01;
}
int main() {
while ((cin >> n >> w) && (n || w)) {
fill(dat, dat + 101, 0);
for (int i = 0; i < n; ++i) {
int v;
cin >> v;
v /= w;
++dat[v];
l = i == 0 ? v : min(l, v);
r = i == 0 ? v : max(r, v);
maxi = i == 0 ? dat[v] : max(maxi, dat[v]);
}
cout << calc() << endl;
}
return 0;
} | replace | 6 | 7 | 6 | 7 | 0 | |
p00864 | C++ | Time Limit Exceeded | #include <stdio.h>
#include <string.h>
#define max(a, b) a > b ? a : b
int main(void) {
int n, w, v[100], i, j, count[11], num, max;
double sum;
while (1) {
scanf("%d%d", &n, &w);
max = 0;
for (i = 0; i < n; i++) {
scanf("%d", &v[i]);
max = max(max, v[i]);
}
num = max / w + 1;
memset(count, 0, sizeof(count));
max = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < num; j++) {
if (j * w <= v[i] && v[i] < (j + 1) * w)
count[j]++;
}
}
for (i = 0; i < num; i++)
max = max(max, count[i]);
sum = 0;
if (num != 1) {
for (i = 0; i < num; i++) {
sum += ((double)(num - 1 - i) / (num - 1)) * ((double)count[i] / max);
}
printf("%.16f\n", sum + 0.01);
} else
printf("1.01\n");
}
return 0;
} | #include <stdio.h>
#include <string.h>
#define max(a, b) a > b ? a : b
int main(void) {
int n, w, v[100], i, j, count[11], num, max;
double sum;
while (1) {
scanf("%d%d", &n, &w);
if (n == 0 && w == 0)
break;
max = 0;
for (i = 0; i < n; i++) {
scanf("%d", &v[i]);
max = max(max, v[i]);
}
num = max / w + 1;
memset(count, 0, sizeof(count));
max = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < num; j++) {
if (j * w <= v[i] && v[i] < (j + 1) * w)
count[j]++;
}
}
for (i = 0; i < num; i++)
max = max(max, count[i]);
sum = 0;
if (num != 1) {
for (i = 0; i < num; i++) {
sum += ((double)(num - 1 - i) / (num - 1)) * ((double)count[i] / max);
}
printf("%.16f\n", sum + 0.01);
} else
printf("1.01\n");
}
return 0;
} | insert | 9 | 9 | 9 | 11 | TLE | |
p00864 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
using namespace std;
#define LOG(x) // cerr << #x << " = " << x << endl
int main() {
while (true) {
int N, W;
cin >> N >> W;
if (N == 0 && W == 0)
break;
map<int, int> occ;
for (int i = 0; i < N; ++i) {
int v;
cin >> v;
occ[v / W]++;
}
int maxOcc = 0;
int numBars = 0;
for (auto &&iter : occ) {
cerr << "bar[" << iter.first << "] = " << iter.second << endl;
maxOcc = max(maxOcc, iter.second);
numBars = max(numBars, iter.first + 1);
}
LOG(maxOcc);
LOG(numBars);
double ans = 0.01;
for (auto &&iter : occ) {
ans += (1.0 - (1.0 * iter.first / (numBars - 1))) *
(1.0 * iter.second / maxOcc);
}
cout.setf(ios::fixed);
cout.precision(6);
cout << ans << endl;
}
} | #include <algorithm>
#include <iostream>
#include <map>
using namespace std;
#define LOG(x) // cerr << #x << " = " << x << endl
int main() {
while (true) {
int N, W;
cin >> N >> W;
if (N == 0 && W == 0)
break;
map<int, int> occ;
for (int i = 0; i < N; ++i) {
int v;
cin >> v;
occ[v / W]++;
}
int maxOcc = 0;
int numBars = 0;
for (auto &&iter : occ) {
maxOcc = max(maxOcc, iter.second);
numBars = max(numBars, iter.first + 1);
}
LOG(maxOcc);
LOG(numBars);
double ans = 0.01;
for (auto &&iter : occ) {
ans += (1.0 - (1.0 * iter.first / (numBars - 1))) *
(1.0 * iter.second / maxOcc);
}
cout.setf(ios::fixed);
cout.precision(6);
cout << ans << endl;
}
} | delete | 22 | 23 | 22 | 22 | 0 | bar[0] = 1
bar[2] = 2
bar[1] = 1
bar[2] = 2
bar[0] = 5
bar[1] = 3
bar[2] = 1
bar[3] = 1
|
p00865 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second
const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const int MAX_SIZE = 1000006;
int n, m, k;
double p[2][MAX_SIZE];
void solve() {
rep(i, MAX_SIZE) p[0][i] = p[1][i] = 0.0;
rep(i, m) p[0][i + 1] = 1.0 / m;
rep(i, n - 1) {
rep(j, MAX_SIZE) {
rep(k, m) {
if (k + j + 1 >= MAX_SIZE)
continue;
p[1][k + j + 1] += p[0][j] / m;
}
}
rep(j, MAX_SIZE) {
p[0][j] = p[1][j];
p[1][j] = 0.0;
}
}
double ans = 0.0;
rep(i, m * n) {
int b = max(1, i - k + 1);
// cout<<i+1<<" : "<<p[0][i+1]<<","<<b<<endl;
ans += p[0][i + 1] * b;
}
printf("%.8f\n", ans);
}
int main() {
while (cin >> n >> m >> k) {
if (n + m + k == 0)
break;
solve();
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second
const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const int MAX_SIZE = 100005;
int n, m, k;
double p[2][MAX_SIZE];
void solve() {
rep(i, MAX_SIZE) p[0][i] = p[1][i] = 0.0;
rep(i, m) p[0][i + 1] = 1.0 / m;
rep(i, n - 1) {
rep(j, MAX_SIZE) {
rep(k, m) {
if (k + j + 1 >= MAX_SIZE)
continue;
p[1][k + j + 1] += p[0][j] / m;
}
}
rep(j, MAX_SIZE) {
p[0][j] = p[1][j];
p[1][j] = 0.0;
}
}
double ans = 0.0;
rep(i, m * n) {
int b = max(1, i - k + 1);
// cout<<i+1<<" : "<<p[0][i+1]<<","<<b<<endl;
ans += p[0][i + 1] * b;
}
printf("%.8f\n", ans);
}
int main() {
while (cin >> n >> m >> k) {
if (n + m + k == 0)
break;
solve();
}
return 0;
} | replace | 33 | 34 | 33 | 34 | TLE | |
p00865 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000
#define MAXN 1000
#define MAXM 10000
typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
double dp[MAXN][MAXM]; // dp[n][m]はn回サイコロをふったとき合計値がmになる確率
int n, m, k;
int main(void) {
while (cin >> n >> m >> k) {
if (n == 0 && m == 0 && k == 0)
break;
for (int i = 0; i < MAXN; i++)
for (int j = 0; j < MAXM; j++)
dp[i][j] = 0;
dp[0][0] = 1;
int ma = n * m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= ma; j++) {
for (int k = 1; k <= m; k++) {
if (j - k >= 0) {
dp[i][j] += dp[i - 1][j - k] / m;
}
}
}
}
// for (int i = 0; i <= ma; i++) cout << dp[n][i] << endl;
double ans = 0;
for (int i = 1; i <= ma; i++) {
if (i <= k)
ans += 1.0 * dp[n][i];
else
ans += (i - k) * dp[n][i];
}
printf("%.10f\n", ans);
}
return 0;
} | #include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000
#define MAXN 100
#define MAXM 10000
typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
double dp[MAXN][MAXM]; // dp[n][m]はn回サイコロをふったとき合計値がmになる確率
int n, m, k;
int main(void) {
while (cin >> n >> m >> k) {
if (n == 0 && m == 0 && k == 0)
break;
for (int i = 0; i < MAXN; i++)
for (int j = 0; j < MAXM; j++)
dp[i][j] = 0;
dp[0][0] = 1;
int ma = n * m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= ma; j++) {
for (int k = 1; k <= m; k++) {
if (j - k >= 0) {
dp[i][j] += dp[i - 1][j - k] / m;
}
}
}
}
// for (int i = 0; i <= ma; i++) cout << dp[n][i] << endl;
double ans = 0;
for (int i = 1; i <= ma; i++) {
if (i <= k)
ans += 1.0 * dp[n][i];
else
ans += (i - k) * dp[n][i];
}
printf("%.10f\n", ans);
}
return 0;
} | replace | 17 | 18 | 17 | 18 | MLE | |
p00865 | C++ | Runtime Error | // Header {{{
// includes {{{
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/time.h>
#include <unistd.h>
#include <vector>
// }}}
using namespace std;
// consts {{{
static const int INF = 1e9;
static const double PI = acos(-1.0);
static const double EPS = 1e-10;
// }}}
// typedefs {{{
typedef long long int LL;
typedef unsigned long long int ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<ULL> VULL;
typedef vector<VULL> VVULL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<char> VC;
typedef vector<VC> VVC;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef pair<int, int> PII;
typedef complex<int> P;
// }}}
// macros & inline functions {{{
// syntax sugars {{{
#define FOR(i, b, e) for (typeof(e) i = (b); i < (e); ++i)
#define FORI(i, b, e) for (typeof(e) i = (b); i <= (e); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPI(i, n) FORI(i, 0, n)
#define OPOVER(_op, _type) inline bool operator _op(const _type &t) const
// }}}
// conversion {{{
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
// }}}
// array and STL {{{
#define ARRSIZE(a) (sizeof(a) / sizeof(a[0]))
#define ZERO(a, v) (assert(v == 0 || v == -1), memset(a, v, sizeof(a)))
#define F first
#define S second
#define MP(a, b) make_pair(a, b)
#define SIZE(a) ((LL)a.size())
#define PB(e) push_back(e)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(c, it) \
for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define REACH(c, it) \
for (typeof((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define EXIST(s, e) ((s).find(e) != (s).end())
// }}}
// bit manipulation {{{
// singed integers are not for bitwise operations, specifically arithmetic
// shifts ('>>', and maybe not good for '<<' too)
#define IS_UNSIGNED(n) (!numeric_limits<typeof(n)>::is_signed)
#define BIT(n) (assert(IS_UNSIGNED(n)), assert(n < 64), (1ULL << (n)))
#define BITOF(n, m) \
(assert(IS_UNSIGNED(n)), assert(m < 64), ((ULL)(n) >> (m)&1))
inline int BITS_COUNT(ULL b) {
int c = 0;
while (b != 0) {
c += (b & 1);
b >>= 1;
}
return c;
}
inline int MSB(ULL b) {
int c = 0;
while (b != 0) {
++c;
b >>= 1;
}
return c - 1;
}
inline int MAKE_MASK(ULL upper, ULL lower) {
assert(lower < 64 && upper < 64 && lower <= upper);
return (BIT(upper) - 1) ^ (BIT(lower) - 1);
}
// }}}
// for readable code {{{
#define EVEN(n) (n % 2 == 0)
#define ODD(n) (!EVEN(n))
// }}}
// debug {{{
#define arrsz(a) (sizeof(a) / sizeof(a[0]))
#define dprt(fmt, ...) \
if (opt_debug) { \
fprintf(stderr, fmt, ##__VA_ARGS__); \
}
#define darr(a) \
if (opt_debug) { \
copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " ")); \
cerr << endl; \
}
#define darr_range(a, f, t) \
if (opt_debug) { \
copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " ")); \
cerr << endl; \
}
#define dvec(v) \
if (opt_debug) { \
copy(ALL(v), ostream_iterator<int>(cerr, " ")); \
cerr << endl; \
}
#define darr2(a) \
if (opt_debug) { \
FOR(__i, 0, (arrsz(a))) { darr((a)[__i]); } \
}
#define WAIT() \
if (opt_debug) { \
string _wait_; \
cerr << "(hit return to continue)" << endl; \
getline(cin, _wait_); \
}
#define dump(x) \
if (opt_debug) { \
cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl; \
}
#define dumpl(x) \
if (opt_debug) { \
cerr << " [L" << __LINE__ << "] " << #x << endl << (x) << endl; \
}
#define dumpf() \
if (opt_debug) { \
cerr << __PRETTY_FUNCTION__ << endl; \
}
#define where() \
if (opt_debug) { \
cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__ \
<< "]" << endl; \
}
#define show_bits(b, s) \
if (opt_debug) { \
REP(i, s) { \
cerr << BITOF(b, s - 1 - i); \
if (i % 4 == 3) \
cerr << ' '; \
} \
cerr << endl; \
}
// ostreams {{{
// complex
template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) {
return s << "(" << d.real() << ", " << d.imag() << ")";
}
// pair
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
return s << "(" << d.first << ", " << d.second << ")";
}
// vector
template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) {
int len = d.size();
REP(i, len) {
s << d[i];
if (i < len - 1)
s << "\t";
}
return s;
}
// 2 dimentional vector
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &d) {
int len = d.size();
REP(i, len) { s << d[i] << endl; }
return s;
}
// map
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
s << "{" << endl;
for (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr) {
s << "\t" << (*itr).first << " : " << (*itr).second << endl;
}
s << "}" << endl;
return s;
}
// }}}
// }}}
// }}}
// time {{{
inline double now() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (static_cast<double>(tv.tv_sec) +
static_cast<double>(tv.tv_usec) * 1e-6);
}
// }}}
// string manipulation {{{
inline VS split(string s, char delimiter) {
VS v;
string t;
REP(i, s.length()) {
if (s[i] == delimiter)
v.PB(t), t = "";
else
t += s[i];
}
v.PB(t);
return v;
}
inline string join(VS s, string j) {
string t;
REP(i, s.size()) { t += s[i] + j; }
return t;
}
// }}}
// geometry {{{
#define Y real()
#define X imag()
// }}}
// 2 dimentional array {{{
P dydx4[4] = {P(-1, 0), P(0, 1), P(1, 0), P(0, -1)};
P dydx8[8] = {P(-1, 0), P(0, 1), P(1, 0), P(0, -1),
P(-1, 1), P(1, 1), P(1, -1), P(-1, -1)};
int g_height, g_width;
bool in_field(P p) {
return (0 <= p.Y && p.Y < g_height) && (0 <= p.X && p.X < g_width);
}
// }}}
// input and output {{{
inline void input(string filename) { freopen(filename.c_str(), "r", stdin); }
inline void output(string filename) { freopen(filename.c_str(), "w", stdout); }
// }}}
// }}}
bool opt_debug = false;
class Fraction {
public:
int numerator;
int denominator;
Fraction(int _numerator, int _denominator) {
numerator = _numerator;
denominator = _denominator;
};
Fraction operator*(const int rhs) {
return Fraction(this->numerator * rhs, this->denominator);
};
Fraction operator+(const Fraction &lhs) {
// TODO: Use lcm
assert(this->denominator == lhs.denominator);
return Fraction(this->numerator + lhs.numerator, this->denominator);
}
};
std::ostream &operator<<(std::ostream &s, const Fraction &a) {
if (a.denominator == 1) {
s << a.numerator;
} else {
s << a.numerator << "/" << a.denominator;
}
return s;
}
void solve(map<int, int> &count, int sum, int n, int m, int k) {
if (n == 0) {
int ans = max(1, sum - k);
if (!EXIST(count, ans)) {
count[ans] = 0;
}
count[ans]++;
return;
}
REP(i, m) { solve(count, sum + (i + 1), n - 1, m, k); }
}
int main(int argc, char **argv) {
std::ios_base::sync_with_stdio(false);
// set options {{{
int __c;
while ((__c = getopt(argc, argv, "d")) != -1) {
switch (__c) {
case 'd':
opt_debug = true;
break;
default:
abort();
}
}
// }}}
input("./inputs/0.txt");
// output("./outputs/0.txt");
int n, m, k;
while (cin >> n >> m >> k, n | m | k) {
map<int, int> count;
solve(count, 0, n, m, k);
Fraction f(0, pow(m, n));
EACH(count, itr) {
int ans = (*itr).first, cnt = (*itr).second;
f = f + Fraction(ans * cnt, pow(m, n));
}
printf("%.10lf\n", f.numerator / (double)f.denominator);
}
return 0;
}
// vim: foldmethod=marker | // Header {{{
// includes {{{
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/time.h>
#include <unistd.h>
#include <vector>
// }}}
using namespace std;
// consts {{{
static const int INF = 1e9;
static const double PI = acos(-1.0);
static const double EPS = 1e-10;
// }}}
// typedefs {{{
typedef long long int LL;
typedef unsigned long long int ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<ULL> VULL;
typedef vector<VULL> VVULL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<char> VC;
typedef vector<VC> VVC;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef pair<int, int> PII;
typedef complex<int> P;
// }}}
// macros & inline functions {{{
// syntax sugars {{{
#define FOR(i, b, e) for (typeof(e) i = (b); i < (e); ++i)
#define FORI(i, b, e) for (typeof(e) i = (b); i <= (e); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPI(i, n) FORI(i, 0, n)
#define OPOVER(_op, _type) inline bool operator _op(const _type &t) const
// }}}
// conversion {{{
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
// }}}
// array and STL {{{
#define ARRSIZE(a) (sizeof(a) / sizeof(a[0]))
#define ZERO(a, v) (assert(v == 0 || v == -1), memset(a, v, sizeof(a)))
#define F first
#define S second
#define MP(a, b) make_pair(a, b)
#define SIZE(a) ((LL)a.size())
#define PB(e) push_back(e)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(c, it) \
for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define REACH(c, it) \
for (typeof((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define EXIST(s, e) ((s).find(e) != (s).end())
// }}}
// bit manipulation {{{
// singed integers are not for bitwise operations, specifically arithmetic
// shifts ('>>', and maybe not good for '<<' too)
#define IS_UNSIGNED(n) (!numeric_limits<typeof(n)>::is_signed)
#define BIT(n) (assert(IS_UNSIGNED(n)), assert(n < 64), (1ULL << (n)))
#define BITOF(n, m) \
(assert(IS_UNSIGNED(n)), assert(m < 64), ((ULL)(n) >> (m)&1))
inline int BITS_COUNT(ULL b) {
int c = 0;
while (b != 0) {
c += (b & 1);
b >>= 1;
}
return c;
}
inline int MSB(ULL b) {
int c = 0;
while (b != 0) {
++c;
b >>= 1;
}
return c - 1;
}
inline int MAKE_MASK(ULL upper, ULL lower) {
assert(lower < 64 && upper < 64 && lower <= upper);
return (BIT(upper) - 1) ^ (BIT(lower) - 1);
}
// }}}
// for readable code {{{
#define EVEN(n) (n % 2 == 0)
#define ODD(n) (!EVEN(n))
// }}}
// debug {{{
#define arrsz(a) (sizeof(a) / sizeof(a[0]))
#define dprt(fmt, ...) \
if (opt_debug) { \
fprintf(stderr, fmt, ##__VA_ARGS__); \
}
#define darr(a) \
if (opt_debug) { \
copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " ")); \
cerr << endl; \
}
#define darr_range(a, f, t) \
if (opt_debug) { \
copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " ")); \
cerr << endl; \
}
#define dvec(v) \
if (opt_debug) { \
copy(ALL(v), ostream_iterator<int>(cerr, " ")); \
cerr << endl; \
}
#define darr2(a) \
if (opt_debug) { \
FOR(__i, 0, (arrsz(a))) { darr((a)[__i]); } \
}
#define WAIT() \
if (opt_debug) { \
string _wait_; \
cerr << "(hit return to continue)" << endl; \
getline(cin, _wait_); \
}
#define dump(x) \
if (opt_debug) { \
cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl; \
}
#define dumpl(x) \
if (opt_debug) { \
cerr << " [L" << __LINE__ << "] " << #x << endl << (x) << endl; \
}
#define dumpf() \
if (opt_debug) { \
cerr << __PRETTY_FUNCTION__ << endl; \
}
#define where() \
if (opt_debug) { \
cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__ \
<< "]" << endl; \
}
#define show_bits(b, s) \
if (opt_debug) { \
REP(i, s) { \
cerr << BITOF(b, s - 1 - i); \
if (i % 4 == 3) \
cerr << ' '; \
} \
cerr << endl; \
}
// ostreams {{{
// complex
template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) {
return s << "(" << d.real() << ", " << d.imag() << ")";
}
// pair
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
return s << "(" << d.first << ", " << d.second << ")";
}
// vector
template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) {
int len = d.size();
REP(i, len) {
s << d[i];
if (i < len - 1)
s << "\t";
}
return s;
}
// 2 dimentional vector
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &d) {
int len = d.size();
REP(i, len) { s << d[i] << endl; }
return s;
}
// map
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
s << "{" << endl;
for (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr) {
s << "\t" << (*itr).first << " : " << (*itr).second << endl;
}
s << "}" << endl;
return s;
}
// }}}
// }}}
// }}}
// time {{{
inline double now() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (static_cast<double>(tv.tv_sec) +
static_cast<double>(tv.tv_usec) * 1e-6);
}
// }}}
// string manipulation {{{
inline VS split(string s, char delimiter) {
VS v;
string t;
REP(i, s.length()) {
if (s[i] == delimiter)
v.PB(t), t = "";
else
t += s[i];
}
v.PB(t);
return v;
}
inline string join(VS s, string j) {
string t;
REP(i, s.size()) { t += s[i] + j; }
return t;
}
// }}}
// geometry {{{
#define Y real()
#define X imag()
// }}}
// 2 dimentional array {{{
P dydx4[4] = {P(-1, 0), P(0, 1), P(1, 0), P(0, -1)};
P dydx8[8] = {P(-1, 0), P(0, 1), P(1, 0), P(0, -1),
P(-1, 1), P(1, 1), P(1, -1), P(-1, -1)};
int g_height, g_width;
bool in_field(P p) {
return (0 <= p.Y && p.Y < g_height) && (0 <= p.X && p.X < g_width);
}
// }}}
// input and output {{{
inline void input(string filename) { freopen(filename.c_str(), "r", stdin); }
inline void output(string filename) { freopen(filename.c_str(), "w", stdout); }
// }}}
// }}}
bool opt_debug = false;
class Fraction {
public:
int numerator;
int denominator;
Fraction(int _numerator, int _denominator) {
numerator = _numerator;
denominator = _denominator;
};
Fraction operator*(const int rhs) {
return Fraction(this->numerator * rhs, this->denominator);
};
Fraction operator+(const Fraction &lhs) {
// TODO: Use lcm
assert(this->denominator == lhs.denominator);
return Fraction(this->numerator + lhs.numerator, this->denominator);
}
};
std::ostream &operator<<(std::ostream &s, const Fraction &a) {
if (a.denominator == 1) {
s << a.numerator;
} else {
s << a.numerator << "/" << a.denominator;
}
return s;
}
void solve(map<int, int> &count, int sum, int n, int m, int k) {
if (n == 0) {
int ans = max(1, sum - k);
if (!EXIST(count, ans)) {
count[ans] = 0;
}
count[ans]++;
return;
}
REP(i, m) { solve(count, sum + (i + 1), n - 1, m, k); }
}
int main(int argc, char **argv) {
std::ios_base::sync_with_stdio(false);
// set options {{{
int __c;
while ((__c = getopt(argc, argv, "d")) != -1) {
switch (__c) {
case 'd':
opt_debug = true;
break;
default:
abort();
}
}
// }}}
// input("./inputs/0.txt");
// output("./outputs/0.txt");
int n, m, k;
while (cin >> n >> m >> k, n | m | k) {
map<int, int> count;
solve(count, 0, n, m, k);
Fraction f(0, pow(m, n));
EACH(count, itr) {
int ans = (*itr).first, cnt = (*itr).second;
f = f + Fraction(ans * cnt, pow(m, n));
}
printf("%.10lf\n", f.numerator / (double)f.denominator);
}
return 0;
}
// vim: foldmethod=marker | replace | 329 | 330 | 329 | 330 | -11 | |
p00865 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c) \
for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int N = 5000000;
double dp[N], dp2[N];
int main() {
int n, m, k;
while (cin >> n >> m >> k, n | m | k) {
memset(dp, 0, sizeof(dp));
dp[0] = 1;
rep(i, n) {
memset(dp2, 0, sizeof(dp2));
for (int j = i; j <= m * i; ++j) {
for (int k = 1; k <= m; ++k) {
if (j + k >= N)
break;
dp2[j + k] += dp[j] / m;
}
}
rep(j, N) dp[j] = dp2[j];
}
double ans = 0;
rep(i, N) ans += max(1, i - k) * dp[i];
printf("%.10lf\n", ans);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c) \
for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int N = 1000000;
double dp[N], dp2[N];
int main() {
int n, m, k;
while (cin >> n >> m >> k, n | m | k) {
memset(dp, 0, sizeof(dp));
dp[0] = 1;
rep(i, n) {
memset(dp2, 0, sizeof(dp2));
for (int j = i; j <= m * i; ++j) {
for (int k = 1; k <= m; ++k) {
if (j + k >= N)
break;
dp2[j + k] += dp[j] / m;
}
}
rep(j, N) dp[j] = dp2[j];
}
double ans = 0;
rep(i, N) ans += max(1, i - k) * dp[i];
printf("%.10lf\n", ans);
}
return 0;
} | replace | 13 | 14 | 13 | 14 | MLE | |
p00865 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define first fi
#define second se
using namespace std;
typedef pair<int, int> P;
int d[30][300010];
signed main() {
int n, m, k;
while (1) {
cin >> n >> m >> k;
if (n + m + k == 0)
break;
rep(i, 0, 30) rep(j, 0, 300010) d[i][j] = 0;
d[0][0] = 1;
rep(i, 0, n) {
rep(j, i, 300000) {
if (d[i][j] == 0)
break;
rep(l, 1, m + 1) { d[i + 1][j + l] += d[i][j]; }
}
}
double sum = 0;
rep(i, 0, 300000) {
if (i <= k)
sum += d[n][i];
else
sum += d[n][i] * (i - k);
}
rep(i, 0, n) sum /= (double)m;
printf("%.14lf\n", sum);
}
} | #include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
// #define int long long
#define first fi
#define second se
using namespace std;
typedef pair<int, int> P;
int d[30][300010];
signed main() {
int n, m, k;
while (1) {
cin >> n >> m >> k;
if (n + m + k == 0)
break;
rep(i, 0, 30) rep(j, 0, 300010) d[i][j] = 0;
d[0][0] = 1;
rep(i, 0, n) {
rep(j, i, 300000) {
if (d[i][j] == 0)
break;
rep(l, 1, m + 1) { d[i + 1][j + l] += d[i][j]; }
}
}
double sum = 0;
rep(i, 0, 300000) {
if (i <= k)
sum += d[n][i];
else
sum += d[n][i] * (i - k);
}
rep(i, 0, n) sum /= (double)m;
printf("%.14lf\n", sum);
}
} | replace | 5 | 6 | 5 | 6 | MLE | |
p00865 | C++ | Time Limit Exceeded |
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
int main(void) {
cout.precision(15);
cout << fixed;
for (;;) {
int n, m, k;
cin >> n >> m >> k;
if (!n)
break;
// dp(i,j):=i??????m??¢???????????§j?????????????????°
// dp[i][j] = dp[i-1][j-k](k=1~m)
map<pair<int, int>, LL> dp;
dp[{0, 0}] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= i * m; ++j) {
for (int kk = 1; kk <= m; ++kk)
dp[{i, j}] += dp[{i - 1, j - kk}];
}
}
LL ans = 0;
LL div = 0;
for (int j = 0; j <= n * m; ++j) {
int score = max(1, j - k);
ans += dp[{n, j}] * score;
div += dp[{n, j}];
}
cout << (double)ans / div << endl;
}
return 0;
} |
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
int main(void) {
cout.precision(15);
cout << fixed;
for (;;) {
int n, m, k;
cin >> n >> m >> k;
if (!n)
break;
// dp(i,j):=i??????m??¢???????????§j?????????????????°
// dp[i][j] = dp[i-1][j-k](k=1~m)
map<pair<int, int>, LL> dp;
if (n == 1) {
for (int kk = 1; kk <= m; ++kk) {
dp[{1, kk}] = 1;
}
} else {
dp[{0, 0}] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= i * m; ++j) {
for (int kk = 1; kk <= m; ++kk)
dp[{i, j}] += dp[{i - 1, j - kk}];
}
}
}
LL ans = 0;
LL div = 0;
for (int j = 0; j <= n * m; ++j) {
int score = max(1, j - k);
ans += dp[{n, j}] * score;
div += dp[{n, j}];
}
cout << (double)ans / div << endl;
}
return 0;
} | replace | 37 | 42 | 37 | 48 | TLE | |
p00865 | C++ | Runtime Error | // include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
int hist[2][500000];
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int N, M, K;
while (cin >> N >> M >> K, N) {
fill((int *)hist, (int *)hist + 2 * 500000, 0);
int crt = 0, nxt = 1;
for (int i = 1; i <= M; ++i)
hist[crt][i] = 1;
REP(i, N - 1) {
fill(hist[nxt], hist[nxt] + 500000, 0);
REP(j, 500000) {
for (int k = 1; k <= M; ++k)
hist[nxt][j + k] += hist[crt][j];
}
swap(crt, nxt);
}
double ans = 0., tot = pow(M, N);
FOR(i, N, 500000) {
if (hist[crt][i] == 0)
break;
if (i <= K)
ans += hist[crt][i];
else
ans += hist[crt][i] * (i - K);
}
cout << fixed << setprecision(8) << ans / tot << endl;
}
return 0;
} | // include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
int hist[2][500000];
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int N, M, K;
while (cin >> N >> M >> K, N) {
fill((int *)hist, (int *)hist + 2 * 500000, 0);
int crt = 0, nxt = 1;
for (int i = 1; i <= M; ++i)
hist[crt][i] = 1;
REP(i, N - 1) {
fill(hist[nxt], hist[nxt] + 500000, 0);
REP(j, 500000) {
if (hist[crt][j] == 0)
continue;
for (int k = 1; k <= M; ++k)
hist[nxt][j + k] += hist[crt][j];
}
swap(crt, nxt);
}
double ans = 0., tot = pow(M, N);
FOR(i, N, 500000) {
if (hist[crt][i] == 0)
break;
if (i <= K)
ans += hist[crt][i];
else
ans += hist[crt][i] * (i - K);
}
cout << fixed << setprecision(8) << ans / tot << endl;
}
return 0;
} | insert | 74 | 74 | 74 | 76 | 0 | |
p00866 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define int long long
#define N 100010
using namespace std;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }
vector<int> getDegs(int h, int m, int s) {
vector<int> res({h, m, s});
res[1] = (res[1] - res[0] + 60) % 60;
res[2] = (res[2] - res[0] + 60) % 60;
res[0] = 0;
return res;
}
int toSec(int h, int m, int s) { return s + 60 * m + 60 * 60 * (h / 5); }
int toSec(vector<int> a) { return toSec(a[0], a[1], a[2]); }
bool can(int hh, int mm, int ss) { return hh % 5 == mm / 12; }
vector<vector<int>> calcSameTimes(vector<int> a) {
vector<vector<int>> res;
for (int h = 0; h < 60; h++)
for (int m = 0; m < 60; m++)
for (int s = 0; s < 60; s++) {
if (!can(h, m, s))
continue;
if (toSec(a) == toSec(getDegs(h, m, s)))
res.push_back(vector<int>({h, m, s}));
}
return res;
}
vector<vector<int>> calcSameTimes(int h, int m, int s) {
vector<int> a = {h, m, s};
sort(a.begin(), a.end());
vector<vector<int>> res;
do {
vector<vector<int>> tmp = calcSameTimes(getDegs(a[0], a[1], a[2]));
for (vector<int> t : tmp)
res.push_back(t);
} while (next_permutation(a.begin(), a.end()));
sort(res.begin(), res.end());
res.erase(unique(res.begin(), res.end()), res.end());
return res;
}
int n;
vector<vector<vector<int>>> times;
vector<int> calcTime(int h, int m, int s) {
vector<int> res;
int mx = -1;
for (int i = 0; i < n; i++) {
vector<int> tmp;
int mn = INF;
for (vector<int> t : times[i]) {
int a = toSec(h, m, s);
int b = toSec(t[0], t[1], t[2]);
if (b < a || mn <= b - a)
continue;
mn = b - a;
tmp = t;
break;
}
if (mn == INF)
return vector<int>();
if (mx < mn)
mx = mn, res = tmp;
}
return res;
}
signed main() {
while (1) {
cin >> n;
if (!n)
break;
times.clear();
times.resize(n);
for (int i = 0; i < n; i++) {
int h, m, s;
cin >> h >> m >> s;
times[i] = calcSameTimes(h, m, s);
}
/* for(int i=0;i<n;i++){
cout<<"begin"<<i<<" size="<<times[i].size()<<endl;
for(int j=0;j<times[i].size();j++)
cout<<times[i][j][0]<<" "<<times[i][j][1]<<" "<<times[i][j][2]<<endl;
}
*/
vector<int> from, to;
int mn = INF;
for (int h = 0; h < 60; h++)
for (int m = 0; m < 60; m++)
for (int s = 0; s < 60; s++) {
if (!can(h, m, s))
continue;
vector<int> a = {h, m, s};
vector<int> b = calcTime(h, m, s);
if (b.empty())
continue;
int as = toSec(h, m, s);
int bs = toSec(b[0], b[1], b[2]);
assert(bs >= as);
if (mn > bs - as)
mn = bs - as, from = a, to = b;
}
printf("%02lld:%02lld:%02lld ", from[0] / 5, from[1], from[2]);
printf("%02lld:%02lld:%02lld\n", to[0] / 5, to[1], to[2]);
}
return 0;
} | #include <bits/stdc++.h>
#define int long long
#define N 100010
using namespace std;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }
vector<int> getDegs(int h, int m, int s) {
vector<int> res({h, m, s});
res[1] = (res[1] - res[0] + 60) % 60;
res[2] = (res[2] - res[0] + 60) % 60;
res[0] = 0;
return res;
}
int toSec(int h, int m, int s) { return s + 60 * m + 60 * 60 * (h / 5); }
int toSec(vector<int> a) { return toSec(a[0], a[1], a[2]); }
bool can(int hh, int mm, int ss) { return hh % 5 == mm / 12; }
vector<vector<int>> calcSameTimes(vector<int> a) {
vector<vector<int>> res;
for (int h = 0; h < 60; h++)
for (int m = 0; m < 60; m++)
for (int s = 0; s < 60; s++) {
if (!can(h, m, s))
continue;
if (a == getDegs(h, m, s))
res.push_back(vector<int>({h, m, s}));
}
return res;
}
vector<vector<int>> calcSameTimes(int h, int m, int s) {
vector<int> a = {h, m, s};
sort(a.begin(), a.end());
vector<vector<int>> res;
do {
vector<vector<int>> tmp = calcSameTimes(getDegs(a[0], a[1], a[2]));
for (vector<int> t : tmp)
res.push_back(t);
} while (next_permutation(a.begin(), a.end()));
sort(res.begin(), res.end());
res.erase(unique(res.begin(), res.end()), res.end());
return res;
}
int n;
vector<vector<vector<int>>> times;
vector<int> calcTime(int h, int m, int s) {
vector<int> res;
int mx = -1;
for (int i = 0; i < n; i++) {
vector<int> tmp;
int mn = INF;
for (vector<int> t : times[i]) {
int a = toSec(h, m, s);
int b = toSec(t[0], t[1], t[2]);
if (b < a || mn <= b - a)
continue;
mn = b - a;
tmp = t;
break;
}
if (mn == INF)
return vector<int>();
if (mx < mn)
mx = mn, res = tmp;
}
return res;
}
signed main() {
while (1) {
cin >> n;
if (!n)
break;
times.clear();
times.resize(n);
for (int i = 0; i < n; i++) {
int h, m, s;
cin >> h >> m >> s;
times[i] = calcSameTimes(h, m, s);
}
/* for(int i=0;i<n;i++){
cout<<"begin"<<i<<" size="<<times[i].size()<<endl;
for(int j=0;j<times[i].size();j++)
cout<<times[i][j][0]<<" "<<times[i][j][1]<<" "<<times[i][j][2]<<endl;
}
*/
vector<int> from, to;
int mn = INF;
for (int h = 0; h < 60; h++)
for (int m = 0; m < 60; m++)
for (int s = 0; s < 60; s++) {
if (!can(h, m, s))
continue;
vector<int> a = {h, m, s};
vector<int> b = calcTime(h, m, s);
if (b.empty())
continue;
int as = toSec(h, m, s);
int bs = toSec(b[0], b[1], b[2]);
assert(bs >= as);
if (mn > bs - as)
mn = bs - as, from = a, to = b;
}
printf("%02lld:%02lld:%02lld ", from[0] / 5, from[1], from[2]);
printf("%02lld:%02lld:%02lld\n", to[0] / 5, to[1], to[2]);
}
return 0;
} | replace | 30 | 31 | 30 | 31 | TLE | |
p00867 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define MAX_N 1005
typedef complex<double> P;
double eps = 1e-6;
P calc(P a, P b, P c, P d) {
a -= d;
b -= d;
c -= d;
return d + a + (b - a) * imag(a / c) / imag(a / c - b / c);
}
bool isParallel(P a, P b, P c, P d) {
return (abs(imag((a - b) * conj(c - d))) < eps);
}
bool eq(P a, P b) { return (abs(a - b) < eps); }
int n;
P A[MAX_N], B[MAX_N];
int G[MAX_N][MAX_N];
int ans[10];
bool visited[MAX_N];
vector<int> vec;
int solve() {
if (vec.size() == 1)
return 1;
int cnt1 = 0, cnt2 = 0, size = vec.size();
for (int i = 0; i < size; i++) {
for (int j = 0; j < i; j++) {
if (G[vec[i]][vec[j]] == 1)
cnt1++;
if (G[vec[i]][vec[j]] == 2)
cnt2++;
}
}
// cout<<cnt1<<' '<<cnt2<<' '<<size<<endl;
P g = A[vec[0]];
for (int i = 0; i < size; i++) {
int id = vec[i];
// cout<<A[id]-g<<' '<<B[id]-g<<endl;
}
if (size == 3) {
if (cnt1 == 2 && cnt2 == 0)
return 7;
return 4;
// if(cnt1==1&&cnt2==1)return 4;
// assert(0);
}
if (size == 4) {
if (cnt1 == 4 && cnt2 == 0)
return 0;
if (cnt1 == 3 && cnt2 == 1)
return 9;
if (cnt1 == 2 && cnt2 == 1)
return 3;
return 3;
// assert(0);
}
if (size == 5) {
if (cnt1 == 4 && cnt2 == 1)
return 6;
if (cnt1 == 4 && cnt2 == 2)
return 8;
if (cnt1 == 4 && cnt2 == 0) {
for (int i = 0; i < size; i++) {
int C = 0, D;
for (int j = 0; j < size; j++) {
if (i != j && G[vec[i]][vec[j]]) {
C++;
D = j;
}
}
if (C == 1) {
P cp = calc(A[vec[i]], B[vec[i]], A[vec[D]], B[vec[D]]);
P from, to, prev;
if (eq(cp, A[vec[i]]))
prev = B[vec[i]], from = A[vec[i]];
else if (eq(cp, B[vec[i]]))
prev = A[vec[i]], from = B[vec[i]];
// else assert(0);
if (eq(cp, A[vec[D]]))
to = B[vec[D]];
else if (eq(cp, B[vec[D]]))
to = A[vec[D]];
// else assert(0);
P base = from - prev, target = to - prev;
if (imag(target * conj(base)) < -eps)
return 2;
else if (imag(target * conj(base)) > eps)
return 5;
// else assert(0);
}
}
}
return 2;
assert(0);
}
return -1;
}
void dfs(int pos) {
if (visited[pos])
return;
visited[pos] = true;
vec.push_back(pos);
for (int i = 0; i < n; i++)
if (G[pos][i] != 0)
dfs(i);
}
bool eq(double a, double b) { return (-eps < a - b && a - b < eps); }
bool onSegment(P a, P b, P c) {
return eq(abs(a - b), abs(a - c) + abs(b - c));
}
int check(int si, int ti) {
if (isParallel(A[si], B[si], A[ti], B[ti]))
return 0;
P k = calc(A[si], B[si], A[ti], B[ti]);
int count = 3;
if (eq(k, A[si]) || eq(k, B[si]))
count--;
if (eq(k, A[ti]) || eq(k, B[ti]))
count--;
if (onSegment(A[si], B[si], k) && onSegment(A[ti], B[ti], k))
return count;
return 0;
}
int main() {
while (1) {
cin >> n;
if (n == 0)
break;
memset(G, 0, sizeof(G));
for (int i = 0; i < n; i++) {
double ax, ay, bx, by;
cin >> ax >> ay >> bx >> by;
A[i] = P(ax, ay);
B[i] = P(bx, by);
for (int j = 0; j < i; j++) {
G[i][j] = G[j][i] = check(i, j);
}
}
memset(ans, 0, sizeof(ans));
memset(visited, false, sizeof(visited));
for (int i = 0; i < n; i++) {
if (visited[i])
continue;
vec.clear();
dfs(i);
ans[solve()]++;
}
for (int i = 0; i < 10; i++) {
if (i)
cout << ' ';
cout << ans[i];
}
cout << endl;
}
return true;
} | #include <bits/stdc++.h>
using namespace std;
#define MAX_N 1005
typedef complex<double> P;
double eps = 1e-6;
P calc(P a, P b, P c, P d) {
a -= d;
b -= d;
c -= d;
return d + a + (b - a) * imag(a / c) / imag(a / c - b / c);
}
bool isParallel(P a, P b, P c, P d) {
return (abs(imag((a - b) * conj(c - d))) < eps);
}
bool eq(P a, P b) { return (abs(a - b) < eps); }
int n;
P A[MAX_N], B[MAX_N];
int G[MAX_N][MAX_N];
int ans[10];
bool visited[MAX_N];
vector<int> vec;
int solve() {
if (vec.size() == 1)
return 1;
int cnt1 = 0, cnt2 = 0, size = vec.size();
for (int i = 0; i < size; i++) {
for (int j = 0; j < i; j++) {
if (G[vec[i]][vec[j]] == 1)
cnt1++;
if (G[vec[i]][vec[j]] == 2)
cnt2++;
}
}
// cout<<cnt1<<' '<<cnt2<<' '<<size<<endl;
P g = A[vec[0]];
for (int i = 0; i < size; i++) {
int id = vec[i];
// cout<<A[id]-g<<' '<<B[id]-g<<endl;
}
if (size == 3) {
if (cnt1 == 2 && cnt2 == 0)
return 7;
return 4;
// if(cnt1==1&&cnt2==1)return 4;
// assert(0);
}
if (size == 4) {
if (cnt1 == 4 && cnt2 == 0)
return 0;
if (cnt1 == 3 && cnt2 == 1)
return 9;
if (cnt1 == 2 && cnt2 == 1)
return 3;
return 3;
// assert(0);
}
if (size == 5) {
if (cnt1 == 4 && cnt2 == 1)
return 6;
if (cnt1 == 4 && cnt2 == 2)
return 8;
if (cnt1 == 4 && cnt2 == 0) {
for (int i = 0; i < size; i++) {
int C = 0, D;
for (int j = 0; j < size; j++) {
if (i != j && G[vec[i]][vec[j]]) {
C++;
D = j;
}
}
if (C == 1) {
P cp = calc(A[vec[i]], B[vec[i]], A[vec[D]], B[vec[D]]);
P from, to, prev;
if (eq(cp, A[vec[i]]))
prev = B[vec[i]], from = A[vec[i]];
else if (eq(cp, B[vec[i]]))
prev = A[vec[i]], from = B[vec[i]];
// else assert(0);
if (eq(cp, A[vec[D]]))
to = B[vec[D]];
else if (eq(cp, B[vec[D]]))
to = A[vec[D]];
// else assert(0);
P base = from - prev, target = to - prev;
if (imag(target * conj(base)) < -eps)
return 2;
else if (imag(target * conj(base)) > eps)
return 5;
// else assert(0);
}
}
}
return 2;
assert(0);
}
return -1;
}
void dfs(int pos) {
if (visited[pos])
return;
visited[pos] = true;
vec.push_back(pos);
for (int i = 0; i < n; i++)
if (G[pos][i] != 0)
dfs(i);
}
bool eq(double a, double b) { return (-eps < a - b && a - b < eps); }
bool onSegment(P a, P b, P c) {
return eq(abs(a - b), abs(a - c) + abs(b - c));
}
int check(int si, int ti) {
if (isParallel(A[si], B[si], A[ti], B[ti]))
return 0;
P k = calc(A[si], B[si], A[ti], B[ti]);
int count = 3;
if (eq(k, A[si]) || eq(k, B[si]))
count--;
if (eq(k, A[ti]) || eq(k, B[ti]))
count--;
if (onSegment(A[si], B[si], k) && onSegment(A[ti], B[ti], k))
return count;
return 0;
}
int main() {
while (1) {
cin >> n;
if (n == 0)
break;
memset(G, 0, sizeof(G));
for (int i = 0; i < n; i++) {
double ax, ay, bx, by;
cin >> ax >> ay >> bx >> by;
A[i] = P(ax, ay);
B[i] = P(bx, by);
for (int j = 0; j < i; j++) {
G[i][j] = G[j][i] = check(i, j);
}
}
memset(ans, 0, sizeof(ans));
memset(visited, false, sizeof(visited));
for (int i = 0; i < n; i++) {
if (visited[i])
continue;
vec.clear();
dfs(i);
ans[solve()]++;
}
for (int i = 0; i < 10; i++) {
if (i)
cout << ' ';
cout << ans[i];
}
cout << endl;
}
return 0;
} | replace | 169 | 170 | 169 | 170 | 1 | |
p00870 | C++ | Time Limit Exceeded | #define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
// #define int ll
// #define endl "\n"
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
tuple<T...> const &t) {
os << (n == 0 ? "" : " ") << get<n>(t);
_ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
_ot<0>(os, t);
return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
return os;
}
template <class T> inline bool chmax(T &x, T const &y) {
return x < y ? x = y, true : false;
}
template <class T> inline bool chmin(T &x, T const &y) {
return x > y ? x = y, true : false;
}
#ifdef DEBUG
#define dump(...) \
(cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \
<< endl)
#else
#define dump(...)
#endif
// ll const mod = 1000000007;
// ll const inf = 1LL<<60;
bool dp[6000][1 << 13];
ll const B = 1000000007;
int solve(vector<string> &ss, string &t) {
int n = ss.size();
int len = t.size();
vector<ull> shash(n);
rep(i, n) {
ull x = 0;
rep(j, ss[i].size()) { x = x * B + ss[i][j]; }
shash[i] = x;
}
vector<ull> pow(len + 1), thash(len + 1);
pow[0] = 1;
thash[0] = 0;
rep(i, len) {
pow[i + 1] = pow[i] * B;
thash[i + 1] = thash[i] * B + t[i];
}
rep(i, len + 1) rep(j, 1 << n) dp[i][j] = false;
rep(i, len + 1) dp[i][0] = true;
rep(pos, len) {
rep(mask, 1 << n) {
if (!dp[pos][mask])
continue;
rep(i, n) {
if (mask >> i & 1)
continue;
if (pos + ss[i].size() > t.size())
continue;
ull h1 = shash[i];
ull h2 = thash[pos + ss[i].size()] - thash[pos] * pow[ss[i].size()];
if (h1 != h2)
continue;
dp[pos + ss[i].size()][mask | 1 << i] =
t.substr(pos, ss[i].size()) == ss[i];
}
}
// dump(pos);
// rep(i,1<<n){
// dump(bitset<3>(i),dp[pos][i]);
// }
}
int ans = 0;
rep(i, len + 1) {
if (dp[i][(1 << n) - 1]) {
++ans;
}
}
return ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
while (cin >> n >> m && n) {
vector<string> s(n);
rep(i, n) cin >> s[i];
string t;
rep(i, m) {
string x;
cin >> x;
t += x;
}
cout << solve(s, t) << endl;
}
} | #define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
// #define int ll
// #define endl "\n"
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
tuple<T...> const &t) {
os << (n == 0 ? "" : " ") << get<n>(t);
_ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
_ot<0>(os, t);
return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
return os;
}
template <class T> inline bool chmax(T &x, T const &y) {
return x < y ? x = y, true : false;
}
template <class T> inline bool chmin(T &x, T const &y) {
return x > y ? x = y, true : false;
}
#ifdef DEBUG
#define dump(...) \
(cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \
<< endl)
#else
#define dump(...)
#endif
// ll const mod = 1000000007;
// ll const inf = 1LL<<60;
bool dp[6000][1 << 13];
ll const B = 1000000007;
int solve(vector<string> &ss, string &t) {
int n = ss.size();
int len = t.size();
vector<ull> shash(n);
rep(i, n) {
ull x = 0;
rep(j, ss[i].size()) { x = x * B + ss[i][j]; }
shash[i] = x;
}
vector<ull> pow(len + 1), thash(len + 1);
pow[0] = 1;
thash[0] = 0;
rep(i, len) {
pow[i + 1] = pow[i] * B;
thash[i + 1] = thash[i] * B + t[i];
}
rep(i, len + 1) rep(j, 1 << n) dp[i][j] = false;
rep(i, len + 1) dp[i][0] = true;
rep(pos, len) {
rep(mask, 1 << n) {
if (!dp[pos][mask])
continue;
rep(i, n) {
if (mask >> i & 1)
continue;
if (pos + ss[i].size() > t.size())
continue;
ull h1 = shash[i];
ull h2 = thash[pos + ss[i].size()] - thash[pos] * pow[ss[i].size()];
// if(h1 != h2) continue;
dp[pos + ss[i].size()][mask | 1 << i] |=
h1 == h2; // t.substr(pos,ss[i].size()) == ss[i];
}
}
// dump(pos);
// rep(i,1<<n){
// dump(bitset<3>(i),dp[pos][i]);
// }
}
int ans = 0;
rep(i, len + 1) {
if (dp[i][(1 << n) - 1]) {
++ans;
}
}
return ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
while (cin >> n >> m && n) {
vector<string> s(n);
rep(i, n) cin >> s[i];
string t;
rep(i, m) {
string x;
cin >> x;
t += x;
}
cout << solve(s, t) << endl;
}
} | replace | 91 | 95 | 91 | 94 | TLE | |
p00870 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
vector<int> edge[5001];
bool same(string a, string b, int len, int v) {
if (a.size() < len || b.size() < len + v)
return false;
a = a.substr(0, v);
b = b.substr(len, v);
return (a == b);
}
void KMP(string text, string pattern, int v) {
int textN = text.size();
int patternN = pattern.size();
vector<int> next(patternN + 1, 0);
next[0] = 1;
for (int i = 1; i <= patternN; i++) {
string s = pattern.substr(0, i);
int len = 1;
while (i - len > 0 && !same(s, pattern, len, i - len))
len++;
next[i] = len;
}
int cnt = 0;
int tp = 0, pp = 0;
while (tp < textN) {
if (text[tp] == pattern[pp]) {
tp++;
pp++;
} else {
pp -= next[pp];
if (pp < 0) {
tp++;
pp++;
}
}
if (pp == patternN) {
edge[tp - pattern.size()].push_back(v);
}
}
}
int n, m;
string e[13];
string text;
bool dp[5001][1 << 12];
int main(void) {
while (1) {
scanf("%d%d", &n, &m);
if (n == 0 && m == 0)
break;
for (int i = 0; i < n; i++) {
cin >> e[i];
}
text = "";
for (int i = 0; i < m; i++) {
string str;
cin >> str;
text += str;
}
int len = text.size();
for (int i = 0; i < len; i++) {
edge[i].clear();
}
for (int i = 0; i < n; i++) {
KMP(text, e[i], i);
}
memset(dp, false, sizeof(dp));
ll res = 0;
for (int i = 0; i < (1 << n); i++) {
for (int j = 0; j <= len; j++) {
if (i == 0)
dp[j][i] = true;
if (i == ((1 << n) - 1)) {
res += dp[j][i] ? 1 : 0;
}
for (int l = 0; l < edge[j].size(); l++) {
int k = edge[j][l];
if ((!((i >> k) & 1))) {
dp[j + e[k].size()][i | (1 << k)] |= dp[j][i];
}
}
}
}
printf("%lld\n", res);
}
return 0;
} | #include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
vector<int> edge[5001];
bool same(string a, string b, int len, int v) {
if (a.size() < len || b.size() < len + v)
return false;
a = a.substr(0, v);
b = b.substr(len, v);
return (a == b);
}
void KMP(string text, string pattern, int v) {
int textN = text.size();
int patternN = pattern.size();
vector<int> next(patternN + 1, 0);
next[0] = 1;
for (int i = 1; i <= patternN; i++) {
string s = pattern.substr(0, i);
int len = 1;
while (i - len > 0 && !same(s, pattern, len, i - len))
len++;
next[i] = len;
}
int cnt = 0;
int tp = 0, pp = 0;
while (tp < textN) {
if (text[tp] == pattern[pp]) {
tp++;
pp++;
} else {
pp -= next[pp];
if (pp < 0) {
tp++;
pp++;
}
}
if (pp == patternN) {
edge[tp - pattern.size()].push_back(v);
}
}
}
int n, m;
string e[13];
string text;
bool dp[5001][1 << 12];
int main(void) {
while (1) {
scanf("%d%d", &n, &m);
if (n == 0 && m == 0)
break;
for (int i = 0; i < n; i++) {
cin >> e[i];
}
text = "";
for (int i = 0; i < m; i++) {
string str;
cin >> str;
text += str;
}
int len = text.size();
for (int i = 0; i < len; i++) {
edge[i].clear();
}
for (int i = 0; i < n; i++) {
KMP(text, e[i], i);
}
memset(dp, false, sizeof(dp));
ll res = 0;
for (int i = 0; i < (1 << n); i++) {
for (int j = 0; j <= len; j++) {
if (i == 0)
dp[j][i] = true;
if (!dp[j][i])
continue;
if (i == ((1 << n) - 1)) {
res += dp[j][i] ? 1 : 0;
}
for (int l = 0; l < edge[j].size(); l++) {
int k = edge[j][l];
if ((!((i >> k) & 1))) {
dp[j + e[k].size()][i | (1 << k)] |= dp[j][i];
}
}
}
}
printf("%lld\n", res);
}
return 0;
} | insert | 80 | 80 | 80 | 82 | TLE | |
p00870 | C++ | Runtime Error | #include <bits/stdc++.h>
#define N 12
#define S 5005
#define M 1777771
using namespace std;
typedef unsigned long long ull;
bool dp[(1 << N)][S];
int n, m;
string s[N], t;
ull d1[N], d2[S], A[30];
int main() {
while (1) {
cin >> n >> m;
if (!n && !m)
break;
for (int i = 0; i < n; i++)
cin >> s[i];
t = "";
for (int i = 0; i < m; i++) {
string tmp;
cin >> tmp;
t += tmp;
}
for (int i = 0; i < n; i++) {
ull hash = s[i][0];
for (int j = 1; j < s[i].size(); j++)
hash = hash * M + s[i][j];
d1[i] = hash;
}
d2[0] = t[0];
for (int i = 1; i < t.size(); i++)
d2[i] = d2[i - 1] * M + t[i];
memset(dp, 0, sizeof(dp));
for (int i = 0; i < t.size(); i++)
dp[0][i] = true;
A[0] = 1;
for (int i = 1; i < 30; i++)
A[i] = A[i - 1] * M;
for (int i = 0; i < (1 << n); i++) {
for (int j = 0; j < t.size(); j++) {
if (!dp[i][j])
continue;
for (int k = 0; k < n; k++) {
if (i & (1 << k))
continue;
int slen = s[k].size();
if (t.size() < j + slen)
continue;
ull x = d2[j + A[slen] - 1];
if (j)
x -= d2[j - 1] * A[slen];
if (d1[k] != x)
continue;
dp[i | (1 << k)][j + slen] = true;
}
}
}
int ans = 0;
for (int i = 0; i <= t.size(); i++)
ans += dp[(1 << n) - 1][i];
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define N 12
#define S 5005
#define M 1777771
using namespace std;
typedef unsigned long long ull;
bool dp[(1 << N)][S];
int n, m;
string s[N], t;
ull d1[N], d2[S], A[30];
int main() {
while (1) {
cin >> n >> m;
if (!n && !m)
break;
for (int i = 0; i < n; i++)
cin >> s[i];
t = "";
for (int i = 0; i < m; i++) {
string tmp;
cin >> tmp;
t += tmp;
}
for (int i = 0; i < n; i++) {
ull hash = s[i][0];
for (int j = 1; j < s[i].size(); j++)
hash = hash * M + s[i][j];
d1[i] = hash;
}
d2[0] = t[0];
for (int i = 1; i < t.size(); i++)
d2[i] = d2[i - 1] * M + t[i];
memset(dp, 0, sizeof(dp));
for (int i = 0; i < t.size(); i++)
dp[0][i] = true;
A[0] = 1;
for (int i = 1; i < 30; i++)
A[i] = A[i - 1] * M;
for (int i = 0; i < (1 << n); i++) {
for (int j = 0; j < t.size(); j++) {
if (!dp[i][j])
continue;
for (int k = 0; k < n; k++) {
if (i & (1 << k))
continue;
int slen = s[k].size();
if (t.size() < j + slen)
continue;
ull x = d2[j + slen - 1];
if (j)
x -= d2[j - 1] * A[slen];
if (d1[k] != x)
continue;
dp[i | (1 << k)][j + slen] = true;
}
}
}
int ans = 0;
for (int i = 0; i <= t.size(); i++)
ans += dp[(1 << n) - 1][i];
cout << ans << endl;
}
return 0;
} | replace | 69 | 70 | 69 | 70 | -11 | |
p00870 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
#include <fstream>
#include <sys/timeb.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define replrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define reprev(i, n) replrev(i, 0, n)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define all(a) a.begin(), a.end()
#define mp make_pair
#define mt make_tuple
#define INF 2000000000
#define INFL 1000000000000000000LL
#define EPS (1e-10)
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<double>> vvd;
typedef vector<vector<P>> vvP;
typedef priority_queue<int, vector<int>, greater<int>> pqli;
typedef priority_queue<ll, vector<ll>, greater<ll>> pqlll;
typedef priority_queue<P, vector<P>, greater<P>> pqlP;
struct Edge {
int from, to, cost;
bool operator<(Edge e) { return cost < e.cost; }
};
int main() {
while (true) {
int N, M;
cin >> N >> M;
if (N == 0)
break;
vs pat(N);
string text = "";
rep(i, N) { cin >> pat[i]; }
rep(i, M) {
string str;
cin >> str;
text += str;
}
int L = text.size();
vvb dp(1 << N, vb(L, false));
rep(j, N) {
rep(k, L - pat[j].size() + 1) {
bool flag = true;
rep(l, pat[j].size()) {
if (text[k + l] != pat[j][l]) {
flag = false;
break;
}
}
if (flag) {
dp[1 << j][k + pat[j].size() - 1] = true;
}
}
}
repl(i, 1, 1 << N) {
rep(j, N) {
if ((i & (1 << j)) != 0) {
if ((i ^ (1 << j)) == 0)
continue;
repl(k, 1, L - pat[j].size() + 1) {
if (dp[(i ^ (1 << j))][k - 1]) {
bool flag = true;
rep(l, pat[j].size()) {
if (text[k + l] != pat[j][l]) {
flag = false;
break;
}
}
if (flag) {
dp[i][k + pat[j].size() - 1] = true;
}
}
}
}
}
}
int ans = 0;
rep(i, L) {
if (dp[(1 << N) - 1][i])
ans++;
}
/*
rep(i, 1 << N) {
rep(j, L) {
cout << "xo"[dp[i][j]];
}
cout << static_cast<std::bitset<3>>(i) << endl;
}
*/
cout << ans << endl;
}
return 0;
} | #include "bits/stdc++.h"
#include <fstream>
#include <sys/timeb.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define replrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define reprev(i, n) replrev(i, 0, n)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define all(a) a.begin(), a.end()
#define mp make_pair
#define mt make_tuple
#define INF 2000000000
#define INFL 1000000000000000000LL
#define EPS (1e-10)
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<double>> vvd;
typedef vector<vector<P>> vvP;
typedef priority_queue<int, vector<int>, greater<int>> pqli;
typedef priority_queue<ll, vector<ll>, greater<ll>> pqlll;
typedef priority_queue<P, vector<P>, greater<P>> pqlP;
struct Edge {
int from, to, cost;
bool operator<(Edge e) { return cost < e.cost; }
};
int main() {
while (true) {
int N, M;
cin >> N >> M;
if (N == 0)
break;
vs pat(N);
string text = "";
rep(i, N) { cin >> pat[i]; }
rep(i, M) {
string str;
cin >> str;
text += str;
}
int L = text.size();
vvb dp(1 << N, vb(L, false));
rep(j, N) {
rep(k, L - pat[j].size() + 1) {
bool flag = true;
rep(l, pat[j].size()) {
if (text[k + l] != pat[j][l]) {
flag = false;
break;
}
}
if (flag) {
dp[1 << j][k + pat[j].size() - 1] = true;
}
}
}
repl(i, 1, 1 << N) {
rep(j, N) {
if ((i & (1 << j)) != 0) {
if ((i ^ (1 << j)) == 0)
continue;
repl(k, 1, L - pat[j].size() + 1) {
if (dp[(i ^ (1 << j))][k - 1]) {
if (dp[1 << j][k + pat[j].size() - 1]) {
dp[i][k + pat[j].size() - 1] = true;
}
}
}
}
}
}
int ans = 0;
rep(i, L) {
if (dp[(1 << N) - 1][i])
ans++;
}
/*
rep(i, 1 << N) {
rep(j, L) {
cout << "xo"[dp[i][j]];
}
cout << static_cast<std::bitset<3>>(i) << endl;
}
*/
cout << ans << endl;
}
return 0;
} | replace | 85 | 93 | 85 | 86 | TLE | |
p00870 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
/*--------------------template--------------------*/
const ll mod[] = {999999937, 1000000007}, base = 10000103;
struct RollingHash {
int n;
vector<ll> hs[2], pw[2];
RollingHash(){};
RollingHash(const string &s) {
n = s.size();
REP(i, 2) {
hs[i].assign(n + 1, 0);
pw[i].assign(n + 1, 0);
hs[i][0] = 0;
pw[i][0] = 1;
REP(j, n) {
pw[i][j + 1] = pw[i][j] * base % mod[i];
hs[i][j + 1] = (hs[i][j] * base + s[j]) % mod[i];
}
}
}
ll hash(int l, int r, int i) {
return ((hs[i][r] - hs[i][l] * pw[i][r - l]) % mod[i] + mod[i]) % mod[i];
}
bool match(int l1, int r1, int l2, int r2) {
return (hash(l1, r1, 0) == hash(l2, r2, 0)) &&
(hash(l1, r1, 1) == hash(l2, r2, 1));
}
};
int n, m;
int dp[1 << 12][5555];
vs v;
// vector<set<int>> match;
vector<vi> match;
int solve(int bit, int p) {
if (bit == (1 << n) - 1)
return 1;
if (dp[bit][p] >= 0)
return dp[bit][p];
int res = 0;
REP(i, match[p].size()) {
if ((bit >> match[p][i]) & 1)
continue;
chmax(res, solve(bit | (1 << match[p][i]), p + v[match[p][i]].size()));
}
return dp[bit][p] = res;
}
int main() {
cin.sync_with_stdio(false);
cout << fixed << setprecision(10);
while (cin >> n >> m, n) {
MS(dp, -1);
v.clear(), match.clear();
v.resize(n);
string s;
REP(i, n) cin >> v[i];
REP(i, m) {
string t;
cin >> t;
s += t;
}
RollingHash hs(s);
match.resize(s.size());
REP(i, n) {
RollingHash pat(v[i]);
ll pat1 = pat.hash(0, v[i].size(), 0);
ll pat2 = pat.hash(0, v[i].size(), 1);
REP(j, (int)s.size() - (int)v[i].size() + 1) {
ll hs1 = hs.hash(j, j + v[i].size(), 0);
ll hs2 = hs.hash(j, j + v[i].size(), 1);
if (pat1 == hs1 && pat2 == hs2)
match[j].push_back(i);
}
}
int len = 0;
for (auto i : v)
len += i.size();
ll ans = 0;
REP(i, (int)s.size() - len + 1) { ans += solve(0, i); }
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
/*--------------------template--------------------*/
const ll mod[] = {999999937, 1000000007}, base = 10000103;
struct RollingHash {
int n;
vector<ll> hs[2], pw[2];
RollingHash(){};
RollingHash(const string &s) {
n = s.size();
REP(i, 2) {
hs[i].assign(n + 1, 0);
pw[i].assign(n + 1, 0);
hs[i][0] = 0;
pw[i][0] = 1;
REP(j, n) {
pw[i][j + 1] = pw[i][j] * base % mod[i];
hs[i][j + 1] = (hs[i][j] * base + s[j]) % mod[i];
}
}
}
ll hash(int l, int r, int i) {
return ((hs[i][r] - hs[i][l] * pw[i][r - l]) % mod[i] + mod[i]) % mod[i];
}
bool match(int l1, int r1, int l2, int r2) {
return (hash(l1, r1, 0) == hash(l2, r2, 0)) &&
(hash(l1, r1, 1) == hash(l2, r2, 1));
}
};
int n, m;
short dp[1 << 12][5555];
vs v;
// vector<set<int>> match;
vector<vi> match;
int solve(int bit, int p) {
if (bit == (1 << n) - 1)
return 1;
if (dp[bit][p] >= 0)
return dp[bit][p];
int res = 0;
REP(i, match[p].size()) {
if ((bit >> match[p][i]) & 1)
continue;
chmax(res, solve(bit | (1 << match[p][i]), p + v[match[p][i]].size()));
}
return dp[bit][p] = res;
}
int main() {
cin.sync_with_stdio(false);
cout << fixed << setprecision(10);
while (cin >> n >> m, n) {
MS(dp, -1);
v.clear(), match.clear();
v.resize(n);
string s;
REP(i, n) cin >> v[i];
REP(i, m) {
string t;
cin >> t;
s += t;
}
RollingHash hs(s);
match.resize(s.size());
REP(i, n) {
RollingHash pat(v[i]);
ll pat1 = pat.hash(0, v[i].size(), 0);
ll pat2 = pat.hash(0, v[i].size(), 1);
REP(j, (int)s.size() - (int)v[i].size() + 1) {
ll hs1 = hs.hash(j, j + v[i].size(), 0);
ll hs2 = hs.hash(j, j + v[i].size(), 1);
if (pat1 == hs1 && pat2 == hs2)
match[j].push_back(i);
}
}
int len = 0;
for (auto i : v)
len += i.size();
ll ans = 0;
REP(i, (int)s.size() - len + 1) { ans += solve(0, i); }
cout << ans << endl;
}
return 0;
} | replace | 49 | 50 | 49 | 50 | MLE | |
p00872 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)
#define SQ(x) ((x) * (x))
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
struct Poly {
vi coefs;
Poly() { coefs.resize(10, 0); }
Poly(const Poly &p) { coefs = p.coefs; }
Poly &operator+=(const Poly &q) {
REP(i, 10) { coefs[i] += q.coefs[i]; }
return *this;
}
Poly &operator-=(const Poly &q) {
REP(i, 10) { coefs[i] -= q.coefs[i]; }
return *this;
}
Poly &operator*=(const Poly &q) {
vi c(10);
REP(i, 10) REP(j, 10) { c[i + j] += coefs[i] * q.coefs[j]; }
coefs = c;
return *this;
}
Poly &operator*=(int n) {
REP(i, 10) { coefs[i] *= n; }
return *this;
}
Poly operator%(Poly q) {
Poly p(*this);
int x = p.highest_degree(), y = q.highest_degree();
int a = p.coefs[x], b = q.coefs[y];
if (x > y) {
Poly v;
v.coefs[y - x] = 1;
p *= v;
x = y;
}
if (a != b) {
p *= b;
q *= a;
}
RREP(i, x - y + 1) {
Poly r;
r.coefs[i] = p.coefs[i + y] / q.coefs[y];
r *= q;
p -= r;
}
return p;
}
bool is_zero() {
REP(i, 10) {
if (coefs[i] != 0)
return false;
}
return true;
}
int highest_degree() {
RREP(i, 10) {
if (coefs[i] != 0)
return i;
}
return 0;
}
void reduce() {
RFOR(i, 2, 100) {
bool divisiable = true;
REP(j, 10) {
if (coefs[j] != 0) {
if (coefs[j] % i != 0) {
divisiable = false;
break;
}
}
}
if (divisiable) {
REP(j, 10) { coefs[j] /= i; }
}
}
}
string dump() {
stringstream ss;
bool f = false;
int degree = highest_degree();
if (coefs[degree] < 0) {
REP(i, 10) { coefs[i] *= -1; }
}
RREP(i, 10) {
if (coefs[i] != 0) {
if (f && coefs[i] > 0)
ss << "+";
if (i > 0) {
if (coefs[i] == -1) {
ss << "-";
} else if (coefs[i] != 1) {
ss << coefs[i];
}
ss << "x";
if (i > 1)
ss << "^" << i;
} else {
ss << coefs[i];
}
f = true;
}
}
if (!f)
ss << "0";
return ss.str();
}
};
Poly gcd(Poly p, Poly q) {
if (q.is_zero())
return p;
return gcd(q, p % q);
}
Poly expr(string &s, int &idx);
Poly term(string &s, int &idx);
Poly factor(string &s, int &idx);
Poly primary(string &s, int &idx);
int number(string &s, int &idx);
int number(string &s, int &idx) {
int n = 0;
while (isdigit(s[idx])) {
n *= 10;
n += s[idx] - '0';
idx++;
}
return n;
}
Poly primary(string &s, int &idx) {
Poly p;
if (s[idx] == 'x') {
idx++;
p.coefs[1] = 1;
} else if (s[idx] == '(') {
idx++;
p = expr(s, idx);
idx++;
} else {
p.coefs[0] = number(s, idx);
}
return p;
}
Poly factor(string &s, int &idx) {
Poly p = primary(s, idx);
if (s[idx] == '^') {
idx++;
int n = number(s, idx);
Poly q = p;
REP(i, n - 1) { p *= q; }
}
return p;
}
Poly term(string &s, int &idx) {
Poly p = factor(s, idx);
for (; idx < (int)s.length();) {
if (s[idx] == ')' || s[idx] == '+' || s[idx] == '-') {
return p;
} else {
Poly q = factor(s, idx);
p *= q;
}
}
return p;
}
Poly expr(string &s, int &idx) {
Poly p;
for (; idx < (int)s.length();) {
if (s[idx] == ')') {
return p;
} else if (s[idx] == '+') {
idx++;
Poly q = term(s, idx);
p += q;
} else if (s[idx] == '-') {
idx++;
Poly q = term(s, idx);
p -= q;
} else {
Poly q = term(s, idx);
p += q;
}
}
return p;
}
int main() {
string s1;
while (cin >> s1, s1 != ".") {
string s2;
cin >> s2;
int idx = 0;
Poly p1 = expr(s1, idx);
idx = 0;
Poly p2 = expr(s2, idx);
Poly ans = gcd(p1, p2);
ans.reduce();
cout << ans.dump() << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)
#define SQ(x) ((x) * (x))
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
struct Poly {
vi coefs;
Poly() { coefs.resize(10, 0); }
Poly(const Poly &p) { coefs = p.coefs; }
Poly &operator+=(const Poly &q) {
REP(i, 10) { coefs[i] += q.coefs[i]; }
return *this;
}
Poly &operator-=(const Poly &q) {
REP(i, 10) { coefs[i] -= q.coefs[i]; }
return *this;
}
Poly &operator*=(const Poly &q) {
vi c(10);
REP(i, 10) REP(j, 10) { c[i + j] += coefs[i] * q.coefs[j]; }
coefs = c;
return *this;
}
Poly &operator*=(int n) {
REP(i, 10) { coefs[i] *= n; }
return *this;
}
Poly operator%(Poly q) {
Poly p(*this);
int x = p.highest_degree(), y = q.highest_degree();
int a = p.coefs[x], b = q.coefs[y];
if (x > y) {
// Poly v;
// v.coefs[x-y] = 1;
// q *= v;
// y = x;
} else if (x > y) {
Poly v;
v.coefs[y - x] = 1;
p *= v;
x = y;
}
if (a != b) {
p *= b;
q *= a;
}
RREP(i, x - y + 1) {
Poly r;
r.coefs[i] = p.coefs[i + y] / q.coefs[y];
r *= q;
p -= r;
}
return p;
}
bool is_zero() {
REP(i, 10) {
if (coefs[i] != 0)
return false;
}
return true;
}
int highest_degree() {
RREP(i, 10) {
if (coefs[i] != 0)
return i;
}
return 0;
}
void reduce() {
RFOR(i, 2, 100) {
bool divisiable = true;
REP(j, 10) {
if (coefs[j] != 0) {
if (coefs[j] % i != 0) {
divisiable = false;
break;
}
}
}
if (divisiable) {
REP(j, 10) { coefs[j] /= i; }
}
}
}
string dump() {
stringstream ss;
bool f = false;
int degree = highest_degree();
if (coefs[degree] < 0) {
REP(i, 10) { coefs[i] *= -1; }
}
RREP(i, 10) {
if (coefs[i] != 0) {
if (f && coefs[i] > 0)
ss << "+";
if (i > 0) {
if (coefs[i] == -1) {
ss << "-";
} else if (coefs[i] != 1) {
ss << coefs[i];
}
ss << "x";
if (i > 1)
ss << "^" << i;
} else {
ss << coefs[i];
}
f = true;
}
}
if (!f)
ss << "0";
return ss.str();
}
};
Poly gcd(Poly p, Poly q) {
if (q.is_zero())
return p;
return gcd(q, p % q);
}
Poly expr(string &s, int &idx);
Poly term(string &s, int &idx);
Poly factor(string &s, int &idx);
Poly primary(string &s, int &idx);
int number(string &s, int &idx);
int number(string &s, int &idx) {
int n = 0;
while (isdigit(s[idx])) {
n *= 10;
n += s[idx] - '0';
idx++;
}
return n;
}
Poly primary(string &s, int &idx) {
Poly p;
if (s[idx] == 'x') {
idx++;
p.coefs[1] = 1;
} else if (s[idx] == '(') {
idx++;
p = expr(s, idx);
idx++;
} else {
p.coefs[0] = number(s, idx);
}
return p;
}
Poly factor(string &s, int &idx) {
Poly p = primary(s, idx);
if (s[idx] == '^') {
idx++;
int n = number(s, idx);
Poly q = p;
REP(i, n - 1) { p *= q; }
}
return p;
}
Poly term(string &s, int &idx) {
Poly p = factor(s, idx);
for (; idx < (int)s.length();) {
if (s[idx] == ')' || s[idx] == '+' || s[idx] == '-') {
return p;
} else {
Poly q = factor(s, idx);
p *= q;
}
}
return p;
}
Poly expr(string &s, int &idx) {
Poly p;
for (; idx < (int)s.length();) {
if (s[idx] == ')') {
return p;
} else if (s[idx] == '+') {
idx++;
Poly q = term(s, idx);
p += q;
} else if (s[idx] == '-') {
idx++;
Poly q = term(s, idx);
p -= q;
} else {
Poly q = term(s, idx);
p += q;
}
}
return p;
}
int main() {
string s1;
while (cin >> s1, s1 != ".") {
string s2;
cin >> s2;
int idx = 0;
Poly p1 = expr(s1, idx);
idx = 0;
Poly p2 = expr(s2, idx);
Poly ans = gcd(p1, p2);
ans.reduce();
cout << ans.dump() << endl;
}
} | insert | 67 | 67 | 67 | 72 | -6 | double free or corruption (out)
|
p00874 | C++ | Time Limit Exceeded | #include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first1, BidirectionalIterator last1,
BidirectionalIterator first2,
BidirectionalIterator last2) {
if ((first1 == last1) || (first2 == last2)) {
return false;
}
BidirectionalIterator m1 = last1;
BidirectionalIterator m2 = last2;
--m2;
while (--m1 != first1 && !(*m1 < *m2)) {
}
bool result = (m1 == first1) && !(*first1 < *m2);
if (!result) {
// ?
while (first2 != m2 && !(*m1 < *first2)) {
++first2;
}
first1 = m1;
std::iter_swap(first1, first2);
++first1;
++first2;
}
if ((first1 != last1) && (first2 != last2)) {
// ?
m1 = last1;
m2 = first2;
while ((m1 != first1) && (m2 != last2)) {
std::iter_swap(--m1, m2);
++m2;
}
// ?
std::reverse(first1, m1);
std::reverse(first1, last1);
std::reverse(m2, last2);
std::reverse(first2, last2);
}
return !result;
}
template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first, BidirectionalIterator middle,
BidirectionalIterator last) {
return next_combination(first, middle, middle, last);
}
int main() {
while (1) {
int w, d;
cin >> w >> d;
vector<int> ws;
vector<int> ds;
if (w >= d) {
for (int i = 0; i < w; ++i) {
int a;
cin >> a;
ws.push_back(a);
}
for (int i = 0; i < d; ++i) {
int a;
cin >> a;
ds.push_back(a);
}
} else {
for (int i = 0; i < w; ++i) {
int a;
cin >> a;
ds.push_back(a);
}
for (int i = 0; i < d; ++i) {
int a;
cin >> a;
ws.push_back(a);
}
}
assert(ws.size() >= ds.size());
vector<int> perm;
for (int i = 0; i < ws.size(); ++i) {
perm.push_back(i);
}
int finans = 500;
do {
int nans = 0;
for (int i = 0; i < ds.size(); ++i) {
if (ds[i] == ws[perm[i]]) {
nans += ds[i];
} else {
nans += ds[i] + ws[perm[i]];
}
}
for (int i = ds.size(); i < ws.size(); ++i) {
nans += ws[perm[i]];
}
finans = min(finans, nans);
} while (next_permutation(perm.begin(), perm.end()));
cout << finans << endl;
}
return 0;
} | #include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first1, BidirectionalIterator last1,
BidirectionalIterator first2,
BidirectionalIterator last2) {
if ((first1 == last1) || (first2 == last2)) {
return false;
}
BidirectionalIterator m1 = last1;
BidirectionalIterator m2 = last2;
--m2;
while (--m1 != first1 && !(*m1 < *m2)) {
}
bool result = (m1 == first1) && !(*first1 < *m2);
if (!result) {
// ?
while (first2 != m2 && !(*m1 < *first2)) {
++first2;
}
first1 = m1;
std::iter_swap(first1, first2);
++first1;
++first2;
}
if ((first1 != last1) && (first2 != last2)) {
// ?
m1 = last1;
m2 = first2;
while ((m1 != first1) && (m2 != last2)) {
std::iter_swap(--m1, m2);
++m2;
}
// ?
std::reverse(first1, m1);
std::reverse(first1, last1);
std::reverse(m2, last2);
std::reverse(first2, last2);
}
return !result;
}
template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first, BidirectionalIterator middle,
BidirectionalIterator last) {
return next_combination(first, middle, middle, last);
}
int main() {
while (1) {
int w, d;
cin >> w >> d;
if (!w)
break;
vector<int> ws;
vector<int> ds;
if (w >= d) {
for (int i = 0; i < w; ++i) {
int a;
cin >> a;
ws.push_back(a);
}
for (int i = 0; i < d; ++i) {
int a;
cin >> a;
ds.push_back(a);
}
} else {
for (int i = 0; i < w; ++i) {
int a;
cin >> a;
ds.push_back(a);
}
for (int i = 0; i < d; ++i) {
int a;
cin >> a;
ws.push_back(a);
}
}
assert(ws.size() >= ds.size());
vector<int> perm;
for (int i = 0; i < ws.size(); ++i) {
perm.push_back(i);
}
int finans = 500;
do {
int nans = 0;
for (int i = 0; i < ds.size(); ++i) {
if (ds[i] == ws[perm[i]]) {
nans += ds[i];
} else {
nans += ds[i] + ws[perm[i]];
}
}
for (int i = ds.size(); i < ws.size(); ++i) {
nans += ws[perm[i]];
}
finans = min(finans, nans);
} while (next_permutation(perm.begin(), perm.end()));
cout << finans << endl;
}
return 0;
} | insert | 71 | 71 | 71 | 73 | TLE | |
p00875 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int n;
string a[21], b[21];
string f, t;
bool used[21];
int res;
void dfs(string v, int c) {
// cout << v << endl;
if (v == t) {
res = min(res, c);
return;
}
if (v.size() >= t.size() || c >= res)
return;
for (int i = 0; i < n; i++) {
memset(used, 0, sizeof(used));
bool flag = false;
for (int j = 0; j <= v.size() - a[i].size(); j++) {
if (v.substr(j, a[i].size()) == a[i]) {
used[j] = true;
j += a[i].size() - 1;
flag = true;
}
}
if (!flag)
continue;
string next = "";
for (int j = 0; j < v.size(); j++) {
if (!used[j])
next += v[j];
else {
next += b[i];
j += a[i].size() - 1;
}
}
dfs(next, c + 1);
}
}
int main(void) {
while (1) {
scanf("%d", &n);
if (n == 0)
break;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
}
cin >> f;
cin >> t;
res = t.size() - f.size() + 1;
dfs(f, 0);
printf("%d\n", res == t.size() - f.size() + 1 ? -1 : res);
}
return 0;
} | #include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int n;
string a[21], b[21];
string f, t;
bool used[21];
int res;
void dfs(string v, int c) {
// cout << v << endl;
if (v == t) {
res = min(res, c);
return;
}
if (v.size() >= t.size() || c >= res)
return;
for (int i = 0; i < n; i++) {
memset(used, 0, sizeof(used));
bool flag = false;
if (v.size() < a[i].size())
continue;
for (int j = 0; j <= v.size() - a[i].size(); j++) {
if (v.substr(j, a[i].size()) == a[i]) {
used[j] = true;
j += a[i].size() - 1;
flag = true;
}
}
if (!flag)
continue;
string next = "";
for (int j = 0; j < v.size(); j++) {
if (!used[j])
next += v[j];
else {
next += b[i];
j += a[i].size() - 1;
}
}
dfs(next, c + 1);
}
}
int main(void) {
while (1) {
scanf("%d", &n);
if (n == 0)
break;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
}
cin >> f;
cin >> t;
res = t.size() - f.size() + 1;
dfs(f, 0);
printf("%d\n", res == t.size() - f.size() + 1 ? -1 : res);
}
return 0;
} | insert | 21 | 21 | 21 | 23 | 0 | |
p00875 | C++ | Memory Limit Exceeded | #include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
struct po {
string a, b;
};
int n;
vector<po> str;
int ans;
string goal;
int cnt = 0;
set<string> memo;
void saiki(string y, int kai) {
cnt++;
// cout << y << endl;
if (y == goal && ans > kai)
ans = kai;
if (ans > kai && y.size() < 25)
for (int i = 0; i < n; i++) {
string ima = y;
for (int j = 0; j < ima.size(); j++) {
if (str[i].a.size() > ima.size() - j)
break;
int flg = 0;
for (int k = 0; k < str[i].a.size(); k++) {
if (str[i].a[k] != ima[j + k]) {
flg = 1;
break;
}
}
if (flg == 0) {
string nx;
int nj;
for (int k = 0; k < ima.size(); k++) {
if (k == j)
nx += str[i].b, k += str[i].a.size() - 1, nj = nx.size() - 1;
else
nx += ima[k];
}
j = nj;
ima = nx;
}
}
if (y != ima && memo.count(ima) == 0) {
memo.insert(ima);
saiki(ima, kai + 1);
}
}
}
int main() {
while (1) {
cin >> n;
if (n == 0)
break;
str.resize(n);
for (int i = 0; i < n; i++)
cin >> str[i].a >> str[i].b;
memo.clear();
string start;
cin >> start;
cin >> goal;
ans = 10000000;
saiki(start, 0);
if (ans == 10000000)
cout << -1 << endl;
else
cout << ans << endl;
// cout <<cnt <<endl;
}
return 0;
} | #include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
struct po {
string a, b;
};
int n;
vector<po> str;
int ans;
string goal;
int cnt = 0;
set<string> memo;
void saiki(string y, int kai) {
cnt++;
// cout << y << endl;
if (y == goal && ans > kai)
ans = kai;
if (ans > kai && y.size() < 11)
for (int i = 0; i < n; i++) {
string ima = y;
for (int j = 0; j < ima.size(); j++) {
if (str[i].a.size() > ima.size() - j)
break;
int flg = 0;
for (int k = 0; k < str[i].a.size(); k++) {
if (str[i].a[k] != ima[j + k]) {
flg = 1;
break;
}
}
if (flg == 0) {
string nx;
int nj;
for (int k = 0; k < ima.size(); k++) {
if (k == j)
nx += str[i].b, k += str[i].a.size() - 1, nj = nx.size() - 1;
else
nx += ima[k];
}
j = nj;
ima = nx;
}
}
if (y != ima && memo.count(ima) == 0) {
memo.insert(ima);
saiki(ima, kai + 1);
}
}
}
int main() {
while (1) {
cin >> n;
if (n == 0)
break;
str.resize(n);
for (int i = 0; i < n; i++)
cin >> str[i].a >> str[i].b;
memo.clear();
string start;
cin >> start;
cin >> goal;
ans = 10000000;
saiki(start, 0);
if (ans == 10000000)
cout << -1 << endl;
else
cout << ans << endl;
// cout <<cnt <<endl;
}
return 0;
} | replace | 21 | 22 | 21 | 22 | MLE | |
p00875 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
string sed(string s, string a, string b) {
int n = s.size();
int m = a.size();
string ret;
for (int i = 0; i < n; ++i) {
if (i + m <= n && s.substr(i, m) == a) {
ret += b;
i += m - 1;
} else {
ret += s[i];
}
}
return ret;
}
int main() {
for (;;) {
int n;
cin >> n;
if (n == 0)
return 0;
vector<string> a(n), b(n);
for (int i = 0; i < n; ++i)
cin >> a[i] >> b[i];
string s, t;
cin >> s >> t;
int ret = 0;
set<string> curr;
curr.insert(s);
bool ng = true;
while (!curr.empty()) {
set<string> next;
set<string>::iterator it;
for (it = curr.begin(); it != curr.end(); ++it) {
if (*it == t) {
next.clear();
ng = false;
cout << ret << endl;
break;
}
for (int i = 0; i < n; ++i) {
string x = sed(*it, a[i], b[i]);
if (x.size() <= t.size())
next.insert(x);
}
}
curr.swap(next);
++ret;
}
if (ng)
cout << -1 << endl;
}
} | #include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
string sed(string s, string a, string b) {
int n = s.size();
int m = a.size();
string ret;
for (int i = 0; i < n; ++i) {
if (i + m <= n && s.substr(i, m) == a) {
ret += b;
i += m - 1;
} else {
ret += s[i];
}
}
return ret;
}
int main() {
for (;;) {
int n;
cin >> n;
if (n == 0)
return 0;
vector<string> a(n), b(n);
for (int i = 0; i < n; ++i)
cin >> a[i] >> b[i];
string s, t;
cin >> s >> t;
int ret = 0;
set<string> curr;
curr.insert(s);
bool ng = true;
while (!curr.empty()) {
set<string> next;
set<string>::iterator it;
for (it = curr.begin(); it != curr.end(); ++it) {
if (*it == t) {
next.clear();
ng = false;
cout << ret << endl;
break;
}
for (int i = 0; i < n; ++i) {
string x = sed(*it, a[i], b[i]);
if (x != *it && x.size() <= t.size())
next.insert(x);
}
}
curr.swap(next);
++ret;
}
if (ng)
cout << -1 << endl;
}
} | replace | 65 | 66 | 65 | 66 | TLE | |
p00875 | C++ | Runtime Error | #include <iostream>
#include <queue>
#include <string>
using namespace std;
int n;
string a[100], b[100], S, T;
queue<pair<string, int>> Q;
int main() {
while (true) {
cin >> n;
if (n == 0)
break;
for (int i = 0; i < n; i++)
cin >> a[i] >> b[i];
cin >> S >> T;
while (!Q.empty())
Q.pop();
Q.push(make_pair(S, 0));
while (!Q.empty()) {
pair<string, int> U = Q.front();
Q.pop();
string W = U.first;
if (T == W) {
cout << U.second << endl;
goto E;
}
for (int i = 0; i < n; i++) {
string V = W;
for (int j = 0; j <= V.size() - a[i].size(); j++) {
if (V.substr(j, a[i].size()) == a[i]) {
V = V.substr(0, j) + b[i] +
V.substr(j + a[i].size(), V.size() - (j + a[i].size()));
j += b[i].size() - 1;
}
}
if (W != V && V.size() <= T.size()) {
Q.push(make_pair(V, U.second + 1));
}
}
}
cout << "-1" << endl;
E:;
}
return 0;
} | #include <iostream>
#include <queue>
#include <string>
using namespace std;
int n;
string a[100], b[100], S, T;
queue<pair<string, int>> Q;
int main() {
while (true) {
cin >> n;
if (n == 0)
break;
for (int i = 0; i < n; i++)
cin >> a[i] >> b[i];
cin >> S >> T;
while (!Q.empty())
Q.pop();
Q.push(make_pair(S, 0));
while (!Q.empty()) {
pair<string, int> U = Q.front();
Q.pop();
string W = U.first;
if (T == W) {
cout << U.second << endl;
goto E;
}
for (int i = 0; i < n; i++) {
string V = W;
for (int j = 0; j <= (int)V.size() - (int)a[i].size(); j++) {
if (V.substr(j, a[i].size()) == a[i]) {
V = V.substr(0, j) + b[i] +
V.substr(j + a[i].size(), V.size() - (j + a[i].size()));
j += b[i].size() - 1;
}
}
if (W != V && V.size() <= T.size()) {
Q.push(make_pair(V, U.second + 1));
}
}
}
cout << "-1" << endl;
E:;
}
return 0;
} | replace | 28 | 29 | 28 | 29 | 0 | |
p00875 | C++ | Time Limit Exceeded | ////////////////////////////////////////
/// tu3 pro-con template ///
////////////////////////////////////////
#include "bits/stdc++.h"
using namespace std;
// -- loop macros -- //
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (int)(s); i < (n); i++)
#define RFOR(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define FOREACH(i, container) for (auto &&i : container)
#define allof(c) c.begin(), c.end()
#define partof(c, i, n) c.begin() + (i), c.begin() + (i) + (n)
// -- functors -- //
#define PREDICATE(t, a, exp) [&](const t &a) -> bool { return exp; }
#define COMPARISON(t, a, b, exp) \
[&](const t &a, const t &b) -> bool { return exp; }
#define PRED(a, exp) [&](const auto &a) -> bool { return exp; }
#define COMP(a, b, exp) \
[&](const auto &a, const auto &b) -> bool { return exp; }
#define CONV1(a, exp) \
[&](const auto &a) -> auto{ return exp; }
#define CONV2(a, b, exp) \
[&](const auto &a, const auto &b) -> auto{ return exp; }
#define CONV3(a, b, c, exp) \
[&](const auto &a, const auto &b, const auto &c) -> auto{ return exp; }
// -- typedefs -- //
#define EPS 1e-9
typedef unsigned int uint;
typedef long long llong;
typedef unsigned long long ullong;
// -- I/O Helper -- //
struct _Reader {
_Reader(istream &cin) : cin(cin) {}
istream &cin;
template <class T> _Reader operator,(T &rhs) {
cin >> rhs;
return *this;
}
};
struct _Writer {
_Writer(ostream &cout) : cout(cout) {}
ostream &cout;
bool f{false};
template <class T> _Writer operator,(const T &rhs) {
cout << (f ? " " : "") << rhs;
f = true;
return *this;
}
};
#define READ(t, ...) \
t __VA_ARGS__; \
(_Reader{cin}), __VA_ARGS__
#define WRITE(...) \
(_Writer{cout}), __VA_ARGS__; \
cout << '\n'
#define DEBUG(...) \
(_Writer{cerr}), __VA_ARGS__; \
cerr << '\n'
// -- vevector -- //
template <class T> struct vevector : public vector<vector<T>> {
vevector(size_t n = 0, size_t m = 0, const T &initial = T())
: vector<vector<T>>(n, vector<T>(m, initial)) {}
};
template <class T> struct vevevector : public vector<vevector<T>> {
vevevector(size_t n = 0, size_t m = 0, size_t l = 0, const T &initial = T())
: vector<vevector<T>>(n, vevector<T>(m, l, initial)) {}
};
template <class T> struct vevevevector : public vector<vevevector<T>> {
vevevevector(size_t n = 0, size_t m = 0, size_t l = 0, size_t k = 0,
const T &initial = T())
: vector<vevevector<T>>(n, vevevector<T>(m, l, k, initial)) {}
};
namespace std {
template <class T1, class T2>
inline istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
}
template <class T1, class T2>
inline ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
out << p.first << " " << p.second;
return out;
}
} // namespace std
template <class T> T read() {
T t;
cin >> t;
return t;
}
template <class T> vector<T> read(int n) {
vector<T> v;
REP(i, n) { v.push_back(read<T>()); }
return v;
}
template <class T> vevector<T> read(int n, int m) {
vevector<T> v;
REP(i, n) v.push_back(read<T>(m));
return v;
}
template <class T> vector<T> readjag() { return read<T>(read<int>()); }
template <class T> vevector<T> readjag(int n) {
vevector<T> v;
REP(i, n) v.push_back(readjag<T>());
return v;
}
template <class T> struct iter_pair_t {
T beg, end;
};
template <class T> iter_pair_t<T> iter_pair(T beg, T end) {
return iter_pair_t<T>{beg, end};
}
template <class T> ostream &operator<<(ostream &out, iter_pair_t<T> v) {
if (v.beg != v.end) {
out << *v.beg++;
while (v.beg != v.end) {
out << " " << *v.beg++;
}
}
return out;
}
template <class T1> ostream &operator<<(ostream &out, const vector<T1> &v) {
return out << iter_pair(begin(v), end(v));
}
// -- etc -- //
template <class T> T infinity_value();
#define DEFINE_INFINITY_VALUE(T, val) \
template <> constexpr T infinity_value<T>() { return (val); }
DEFINE_INFINITY_VALUE(int, 1 << 28);
DEFINE_INFINITY_VALUE(uint, 1u << 28);
DEFINE_INFINITY_VALUE(llong, 1ll << 60);
DEFINE_INFINITY_VALUE(ullong, 1ull << 60);
DEFINE_INFINITY_VALUE(double, HUGE_VAL);
DEFINE_INFINITY_VALUE(float, HUGE_VAL);
#define INF(T) infinity_value<T>()
inline int sign_of(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }
template <class TInt> bool in_range(TInt val, TInt min, TInt max) {
return val >= min && val < max;
}
template <> bool in_range<double>(double val, double min, double max) {
return val - min > -EPS && val - max < EPS;
}
template <> bool in_range<float>(float val, float min, float max) {
return val - min > -EPS && val - max < EPS;
}
template <class TInt> bool in_range2d(TInt x, TInt y, TInt w, TInt h) {
return x >= 0 && x < w && y >= 0 && y < h;
}
vector<int> iotavn(int start, int count) {
vector<int> r(count);
iota(allof(r), start);
return r;
}
// 文字列の分割
vector<string> split(const string &str, const string &s,
bool removeEmpty = false) {
vector<string> ret;
size_t p = 0, sl = s.size();
while (true) {
size_t q = str.find(s, p);
if (q == str.npos)
break;
ret.push_back(str.substr(p, q - p));
p = q + sl;
}
ret.push_back(str.substr(p));
if (removeEmpty)
ret.erase(remove(allof(ret), string()), ret.end());
return ret;
}
//// start up ////
void solve();
int main() {
//// for local debugging
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// auto classic_table = ctype<char>::classic_table();
// vector<ctype<char>::mask> ctable(classic_table, classic_table +
// ctype<char>::table_size); ctable[':'] |= ctype_base::space; // as delimitor
// ctable[','] |= ctype_base::space; // as delimitor
// cin.imbue(locale(cin.getloc(), new ctype<char>(ctable.data())));
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed;
cout << setprecision(std::numeric_limits<double>::max_digits10);
solve();
return 0;
}
////////////////////
/// template end ///
////////////////////
// 文字列の置換
void inplaceReplace(string &target, const string &from, const string &to,
bool global = false) {
string::size_type p = 0;
do {
string::size_type pos = target.find(from, p);
if (pos == target.npos) {
break;
}
target.replace(pos, from.length(), to);
p = pos + to.length();
} while (global);
}
void solve() {
while (true) {
READ(int, N);
auto subst = read<pair<string, string>>(N);
READ(string, g, d);
map<string, int> a;
queue<pair<string, int>> q;
q.push({g, 0});
while (!q.empty()) {
auto p = q.front();
q.pop();
// DEBUG(p.first);
FOREACH(s, subst) {
auto n = p.first;
inplaceReplace(n, s.first, s.second, true);
if (n.size() <= d.size() && !a.count(n)) {
a[n] = p.second + 1;
q.push({n, p.second + 1});
}
if (n == d) {
break;
}
}
}
WRITE(a.count(d) ? a[d] : -1);
}
}
| ////////////////////////////////////////
/// tu3 pro-con template ///
////////////////////////////////////////
#include "bits/stdc++.h"
using namespace std;
// -- loop macros -- //
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (int)(s); i < (n); i++)
#define RFOR(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define FOREACH(i, container) for (auto &&i : container)
#define allof(c) c.begin(), c.end()
#define partof(c, i, n) c.begin() + (i), c.begin() + (i) + (n)
// -- functors -- //
#define PREDICATE(t, a, exp) [&](const t &a) -> bool { return exp; }
#define COMPARISON(t, a, b, exp) \
[&](const t &a, const t &b) -> bool { return exp; }
#define PRED(a, exp) [&](const auto &a) -> bool { return exp; }
#define COMP(a, b, exp) \
[&](const auto &a, const auto &b) -> bool { return exp; }
#define CONV1(a, exp) \
[&](const auto &a) -> auto{ return exp; }
#define CONV2(a, b, exp) \
[&](const auto &a, const auto &b) -> auto{ return exp; }
#define CONV3(a, b, c, exp) \
[&](const auto &a, const auto &b, const auto &c) -> auto{ return exp; }
// -- typedefs -- //
#define EPS 1e-9
typedef unsigned int uint;
typedef long long llong;
typedef unsigned long long ullong;
// -- I/O Helper -- //
struct _Reader {
_Reader(istream &cin) : cin(cin) {}
istream &cin;
template <class T> _Reader operator,(T &rhs) {
cin >> rhs;
return *this;
}
};
struct _Writer {
_Writer(ostream &cout) : cout(cout) {}
ostream &cout;
bool f{false};
template <class T> _Writer operator,(const T &rhs) {
cout << (f ? " " : "") << rhs;
f = true;
return *this;
}
};
#define READ(t, ...) \
t __VA_ARGS__; \
(_Reader{cin}), __VA_ARGS__
#define WRITE(...) \
(_Writer{cout}), __VA_ARGS__; \
cout << '\n'
#define DEBUG(...) \
(_Writer{cerr}), __VA_ARGS__; \
cerr << '\n'
// -- vevector -- //
template <class T> struct vevector : public vector<vector<T>> {
vevector(size_t n = 0, size_t m = 0, const T &initial = T())
: vector<vector<T>>(n, vector<T>(m, initial)) {}
};
template <class T> struct vevevector : public vector<vevector<T>> {
vevevector(size_t n = 0, size_t m = 0, size_t l = 0, const T &initial = T())
: vector<vevector<T>>(n, vevector<T>(m, l, initial)) {}
};
template <class T> struct vevevevector : public vector<vevevector<T>> {
vevevevector(size_t n = 0, size_t m = 0, size_t l = 0, size_t k = 0,
const T &initial = T())
: vector<vevevector<T>>(n, vevevector<T>(m, l, k, initial)) {}
};
namespace std {
template <class T1, class T2>
inline istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
}
template <class T1, class T2>
inline ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
out << p.first << " " << p.second;
return out;
}
} // namespace std
template <class T> T read() {
T t;
cin >> t;
return t;
}
template <class T> vector<T> read(int n) {
vector<T> v;
REP(i, n) { v.push_back(read<T>()); }
return v;
}
template <class T> vevector<T> read(int n, int m) {
vevector<T> v;
REP(i, n) v.push_back(read<T>(m));
return v;
}
template <class T> vector<T> readjag() { return read<T>(read<int>()); }
template <class T> vevector<T> readjag(int n) {
vevector<T> v;
REP(i, n) v.push_back(readjag<T>());
return v;
}
template <class T> struct iter_pair_t {
T beg, end;
};
template <class T> iter_pair_t<T> iter_pair(T beg, T end) {
return iter_pair_t<T>{beg, end};
}
template <class T> ostream &operator<<(ostream &out, iter_pair_t<T> v) {
if (v.beg != v.end) {
out << *v.beg++;
while (v.beg != v.end) {
out << " " << *v.beg++;
}
}
return out;
}
template <class T1> ostream &operator<<(ostream &out, const vector<T1> &v) {
return out << iter_pair(begin(v), end(v));
}
// -- etc -- //
template <class T> T infinity_value();
#define DEFINE_INFINITY_VALUE(T, val) \
template <> constexpr T infinity_value<T>() { return (val); }
DEFINE_INFINITY_VALUE(int, 1 << 28);
DEFINE_INFINITY_VALUE(uint, 1u << 28);
DEFINE_INFINITY_VALUE(llong, 1ll << 60);
DEFINE_INFINITY_VALUE(ullong, 1ull << 60);
DEFINE_INFINITY_VALUE(double, HUGE_VAL);
DEFINE_INFINITY_VALUE(float, HUGE_VAL);
#define INF(T) infinity_value<T>()
inline int sign_of(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }
template <class TInt> bool in_range(TInt val, TInt min, TInt max) {
return val >= min && val < max;
}
template <> bool in_range<double>(double val, double min, double max) {
return val - min > -EPS && val - max < EPS;
}
template <> bool in_range<float>(float val, float min, float max) {
return val - min > -EPS && val - max < EPS;
}
template <class TInt> bool in_range2d(TInt x, TInt y, TInt w, TInt h) {
return x >= 0 && x < w && y >= 0 && y < h;
}
vector<int> iotavn(int start, int count) {
vector<int> r(count);
iota(allof(r), start);
return r;
}
// 文字列の分割
vector<string> split(const string &str, const string &s,
bool removeEmpty = false) {
vector<string> ret;
size_t p = 0, sl = s.size();
while (true) {
size_t q = str.find(s, p);
if (q == str.npos)
break;
ret.push_back(str.substr(p, q - p));
p = q + sl;
}
ret.push_back(str.substr(p));
if (removeEmpty)
ret.erase(remove(allof(ret), string()), ret.end());
return ret;
}
//// start up ////
void solve();
int main() {
//// for local debugging
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// auto classic_table = ctype<char>::classic_table();
// vector<ctype<char>::mask> ctable(classic_table, classic_table +
// ctype<char>::table_size); ctable[':'] |= ctype_base::space; // as delimitor
// ctable[','] |= ctype_base::space; // as delimitor
// cin.imbue(locale(cin.getloc(), new ctype<char>(ctable.data())));
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed;
cout << setprecision(std::numeric_limits<double>::max_digits10);
solve();
return 0;
}
////////////////////
/// template end ///
////////////////////
// 文字列の置換
void inplaceReplace(string &target, const string &from, const string &to,
bool global = false) {
string::size_type p = 0;
do {
string::size_type pos = target.find(from, p);
if (pos == target.npos) {
break;
}
target.replace(pos, from.length(), to);
p = pos + to.length();
} while (global);
}
void solve() {
while (true) {
READ(int, N);
if (!N) {
break;
}
auto subst = read<pair<string, string>>(N);
READ(string, g, d);
map<string, int> a;
queue<pair<string, int>> q;
q.push({g, 0});
while (!q.empty()) {
auto p = q.front();
q.pop();
// DEBUG(p.first);
FOREACH(s, subst) {
auto n = p.first;
inplaceReplace(n, s.first, s.second, true);
if (n.size() <= d.size() && !a.count(n)) {
a[n] = p.second + 1;
q.push({n, p.second + 1});
}
if (n == d) {
break;
}
}
}
WRITE(a.count(d) ? a[d] : -1);
}
}
| insert | 227 | 227 | 227 | 230 | TLE | |
p00881 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
/*--------------------template--------------------*/
vi v;
int m, n;
int dp[1 << 12][1 << 12];
int solve(int asked, int resp) {
if (dp[asked][resp] >= 0)
return dp[asked][resp];
int cnt = 0;
REP(i, n) {
if ((v[i] & asked) == resp)
cnt++;
}
int res = m;
if (cnt <= 1) {
res = 0;
} else {
REP(i, m) {
int nx = asked | (1 << i);
if (nx == asked)
continue;
chmin(res, max(solve(nx, resp | (1 << i)), solve(nx, resp)) + 1);
}
}
return dp[asked][resp] = res;
}
int tonum(string s) {
int res = 0;
REP(i, s.size()) {
res *= 2;
res += s[i] - '0';
}
return res;
}
int main() {
while (cin >> m >> n, m) {
MS(dp, -1);
v.resize(n);
REP(i, n) {
string s;
cin >> s;
v[i] = tonum(s);
}
cout << solve(0, 0) << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
/*--------------------template--------------------*/
vi v;
int m, n;
short dp[1 << 12][1 << 12];
int solve(int asked, int resp) {
if (dp[asked][resp] >= 0)
return dp[asked][resp];
int cnt = 0;
REP(i, n) {
if ((v[i] & asked) == resp)
cnt++;
}
int res = m;
if (cnt <= 1) {
res = 0;
} else {
REP(i, m) {
int nx = asked | (1 << i);
if (nx == asked)
continue;
chmin(res, max(solve(nx, resp | (1 << i)), solve(nx, resp)) + 1);
}
}
return dp[asked][resp] = res;
}
int tonum(string s) {
int res = 0;
REP(i, s.size()) {
res *= 2;
res += s[i] - '0';
}
return res;
}
int main() {
while (cin >> m >> n, m) {
MS(dp, -1);
v.resize(n);
REP(i, n) {
string s;
cin >> s;
v[i] = tonum(s);
}
cout << solve(0, 0) << endl;
}
return 0;
} | replace | 21 | 22 | 21 | 22 | MLE | |
p00881 | C++ | Memory Limit Exceeded | #include <climits>
#include <cstring>
#include <iostream>
using namespace std;
int w, h, dp[3000][3000];
int bit[200];
bool t[200][200];
int solve(int q, int a) {
if (dp[q][a] != -1)
return dp[q][a];
int cnt = 0;
for (int i = 0; i < h; i++) {
if ((bit[i] & q) == a) {
cnt++;
if (cnt > 1)
break;
}
}
if (cnt == 1) {
return 0;
}
int res = INT_MAX;
for (int i = 0; i < w; i++) {
if (!(q & (1 << i))) {
int tmp = max(solve(q | (1 << i), a), solve(q | (1 << i), a | (1 << i)));
res = min(res, tmp);
}
}
return dp[q][a] = res + 1;
}
int main() {
while (cin >> w >> h, w || h) {
for (int i = 0; i < h; i++) {
bit[i] = 0;
for (int j = 0; j < w; j++) {
char ch;
cin >> ch;
t[i][j] = ch - '0';
bit[i] = (bit[i] << 1) + t[i][j];
}
}
memset(dp, -1, sizeof(dp));
cout << solve(0, 0) << endl;
}
} | #include <climits>
#include <cstring>
#include <iostream>
using namespace std;
int w, h;
short dp[1 << 11][1 << 11];
int bit[200];
bool t[200][200];
int solve(int q, int a) {
if (dp[q][a] != -1)
return dp[q][a];
int cnt = 0;
for (int i = 0; i < h; i++) {
if ((bit[i] & q) == a) {
cnt++;
if (cnt > 1)
break;
}
}
if (cnt == 1) {
return 0;
}
int res = INT_MAX;
for (int i = 0; i < w; i++) {
if (!(q & (1 << i))) {
int tmp = max(solve(q | (1 << i), a), solve(q | (1 << i), a | (1 << i)));
res = min(res, tmp);
}
}
return dp[q][a] = res + 1;
}
int main() {
while (cin >> w >> h, w || h) {
for (int i = 0; i < h; i++) {
bit[i] = 0;
for (int j = 0; j < w; j++) {
char ch;
cin >> ch;
t[i][j] = ch - '0';
bit[i] = (bit[i] << 1) + t[i][j];
}
}
memset(dp, -1, sizeof(dp));
cout << solve(0, 0) << endl;
}
} | replace | 5 | 6 | 5 | 7 | MLE | |
p00881 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int m, n;
int ans[15];
int dp[2050][2050]; // S?????????????????£??????
using Set = bitset<12>;
int rec(int asked,
int answer) { // ??????????????????aked???answer??¨??????????????????
int &res = dp[asked][answer];
if (res != -1)
return res;
res = m;
// vector<int> a;
{
int cnt = 0;
for (int i = 0; i < n; ++i) {
if ((ans[i] & asked) == answer) {
++cnt;
}
}
if (cnt <= 1)
return res = 0;
}
// cout << "A " << Set(asked) << ' ' << Set(answer) << endl;
// cout << "B ";
// for(int &x : a) cout << Set(ans[x]) << ' ';
// cout << endl;
for (int i = 0; i < m; ++i) {
if (asked >> i & 1)
continue;
int cnt[2] = {};
for (int j = 0; j < n; ++j) {
if ((ans[j] & asked) == answer) {
++cnt[ans[i] >> j & 1];
}
}
// if(cnt[0] == 0 || cnt[1] == 0) continue;
int tmp = 0;
for (int j = 0; j < 2; ++j) {
tmp = max(tmp, 1 + rec(asked | 1 << i, answer | (j << i)));
}
res = min(res, tmp);
}
// assert(res != 1e9);
return res;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
while (cin >> m >> n && m) {
for (int i = 0; i < n; ++i) {
char buf[100];
cin >> buf;
ans[i] = 0;
for (int j = 0; j < m; ++j) {
if (buf[j] - '0')
ans[i] |= 1 << j;
}
}
memset(dp, -1, sizeof(dp));
cout << rec(0, 0) << endl;
// cout << "=================" << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int m, n;
int ans[150];
int dp[2050][2050]; // S?????????????????£??????
using Set = bitset<12>;
int rec(int asked,
int answer) { // ??????????????????aked???answer??¨??????????????????
int &res = dp[asked][answer];
if (res != -1)
return res;
res = m;
// vector<int> a;
{
int cnt = 0;
for (int i = 0; i < n; ++i) {
if ((ans[i] & asked) == answer) {
++cnt;
}
}
if (cnt <= 1)
return res = 0;
}
// cout << "A " << Set(asked) << ' ' << Set(answer) << endl;
// cout << "B ";
// for(int &x : a) cout << Set(ans[x]) << ' ';
// cout << endl;
for (int i = 0; i < m; ++i) {
if (asked >> i & 1)
continue;
int cnt[2] = {};
for (int j = 0; j < n; ++j) {
if ((ans[j] & asked) == answer) {
++cnt[ans[i] >> j & 1];
}
}
// if(cnt[0] == 0 || cnt[1] == 0) continue;
int tmp = 0;
for (int j = 0; j < 2; ++j) {
tmp = max(tmp, 1 + rec(asked | 1 << i, answer | (j << i)));
}
res = min(res, tmp);
}
// assert(res != 1e9);
return res;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
while (cin >> m >> n && m) {
for (int i = 0; i < n; ++i) {
char buf[100];
cin >> buf;
ans[i] = 0;
for (int j = 0; j < m; ++j) {
if (buf[j] - '0')
ans[i] |= 1 << j;
}
}
memset(dp, -1, sizeof(dp));
cout << rec(0, 0) << endl;
// cout << "=================" << endl;
}
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00881 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int inf = 1e9;
bool issame(string a, string b) {
for (int i = 0; i < (int)a.length(); i++) {
if (a[i] != 'x' && b[i] != 'x' && a[i] != b[i]) {
return false;
}
}
return true;
}
int solve(string &str, vector<string> &s, map<string, int> &mincost) {
if (mincost.count(str) != 0) {
return mincost[str];
}
int n = s.size();
int count = 0;
for (int i = 0; i < n; i++) {
if (issame(s[i], str))
count++;
}
if (count <= 1) {
mincost[str] = 0;
return 0;
}
int m = str.length();
int res = inf;
for (int i = 0; i < m; i++) {
if (str[i] == 'x') {
string tmpstr[2] = {str, str};
tmpstr[0][i] = '0';
tmpstr[1][i] = '1';
res = min(
res,
max(solve(tmpstr[0], s, mincost), solve(tmpstr[1], s, mincost)) + 1);
}
}
mincost[str] = res;
return res;
}
int main() {
while (1) {
int m, n;
cin >> m >> n;
if (m == 0)
break;
vector<string> s(n);
for (int i = 0; i < n; i++)
cin >> s[i];
map<string, int> mincost;
string ini = string(m, 'x');
cout << solve(ini, s, mincost) << endl;
}
return 0;
}
| #include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int inf = 1e9;
bool issame(string &a, string &b) {
for (int i = 0; i < (int)a.length(); i++) {
if (a[i] != 'x' && b[i] != 'x' && a[i] != b[i]) {
return false;
}
}
return true;
}
int solve(string &str, vector<string> &s, map<string, int> &mincost) {
if (mincost.count(str) != 0) {
return mincost[str];
}
int n = s.size();
int count = 0;
for (int i = 0; i < n; i++) {
if (issame(s[i], str))
count++;
}
if (count <= 1) {
mincost[str] = 0;
return 0;
}
int m = str.length();
int res = inf;
for (int i = 0; i < m; i++) {
if (str[i] == 'x') {
string tmpstr[2] = {str, str};
tmpstr[0][i] = '0';
tmpstr[1][i] = '1';
res = min(
res,
max(solve(tmpstr[0], s, mincost), solve(tmpstr[1], s, mincost)) + 1);
}
}
mincost[str] = res;
return res;
}
int main() {
while (1) {
int m, n;
cin >> m >> n;
if (m == 0)
break;
vector<string> s(n);
for (int i = 0; i < n; i++)
cin >> s[i];
map<string, int> mincost;
string ini = string(m, 'x');
cout << solve(ini, s, mincost) << endl;
}
return 0;
}
| replace | 7 | 8 | 7 | 8 | TLE | |
p00881 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;
int m, n, in[100];
int dp[1600000], pw[13];
int rec(int bit) {
int &res = dp[bit];
if (res >= 0)
return res;
int t[13] = {}, cnt = 0;
rep(j, m) t[j] = bit / pw[j] % 3;
rep(i, n) {
bool ok = 1;
rep(j, m) if (t[j] < 2 && (in[i] >> j & 1) != t[j]) {
ok = 0;
break;
}
if (ok)
cnt++;
}
if (cnt <= 1)
return res = 0;
res = inf;
rep(i, m) if (t[i] == 2) {
res = min(res, 1 + max(rec(bit - 2 * pw[i]), rec(bit - pw[i])));
}
return res;
}
int main() {
pw[0] = 1;
rep(i, 12) pw[i + 1] = pw[i] * 3;
while (cin >> m >> n, m) {
rep(i, n) {
char buf[20];
cin >> buf;
in[i] = 0;
rep(j, m) if (buf[j] == '1') in[i] |= 1 << j;
}
memset(dp, -1, sizeof(dp));
cout << rec(pw[m] - 1) << endl;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;
int m, n, in[128];
int dp[1600000], pw[13];
int rec(int bit) {
int &res = dp[bit];
if (res >= 0)
return res;
int t[13] = {}, cnt = 0;
rep(j, m) t[j] = bit / pw[j] % 3;
rep(i, n) {
bool ok = 1;
rep(j, m) if (t[j] < 2 && (in[i] >> j & 1) != t[j]) {
ok = 0;
break;
}
if (ok)
cnt++;
}
if (cnt <= 1)
return res = 0;
res = inf;
rep(i, m) if (t[i] == 2) {
res = min(res, 1 + max(rec(bit - 2 * pw[i]), rec(bit - pw[i])));
}
return res;
}
int main() {
pw[0] = 1;
rep(i, 12) pw[i + 1] = pw[i] * 3;
while (cin >> m >> n, m) {
rep(i, n) {
char buf[20];
cin >> buf;
in[i] = 0;
rep(j, m) if (buf[j] == '1') in[i] |= 1 << j;
}
memset(dp, -1, sizeof(dp));
cout << rec(pw[m] - 1) << endl;
}
return 0;
} | replace | 28 | 29 | 28 | 29 | 0 | |
p00883 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int n;
bool onmp(int x, int y) { return x >= 0 && y >= 0 && x < n && y < n; }
int count(int x, int y, const vector<string> &A) {
int res = 0;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++) {
int nx = x + i, ny = y + j;
if ((!i && !j) || !onmp(nx, ny))
continue;
res += A[ny][nx] == '#' || A[ny][nx] == '@';
}
return res;
}
bool check(const vector<string> &A) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (A[i][j] == '#')
return 0;
return 1;
}
vector<string> mkmp(const vector<string> &A) {
vector<string> res(n, string(n, '.'));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
int cnt = count(j, i, A);
if (A[i][j] == '#')
res[i][j] = (cnt == 2 || cnt == 3) ? '#' : '.';
if (A[i][j] == '.')
res[i][j] = (cnt == 3) ? '#' : '.';
if (A[i][j] == '@')
res[i][j] = '@';
}
return res;
}
map<vector<string>, int> mem;
int ans;
void dfs(int x, int y, int cnt, vector<string> A) {
if (check(A))
ans = min(cnt, ans);
if (cnt >= ans)
return;
if (mem.count(A) && mem[A] <= cnt)
return;
mem[A] = cnt;
A[y][x] = '.';
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
int nx = x + dx, ny = y + dy;
if ((!dx && !dy) || !onmp(nx, ny) || A[ny][nx] == '#')
continue;
A[ny][nx] = '@';
dfs(nx, ny, cnt + 1, mkmp(A));
A[ny][nx] = '.';
}
}
}
int main() {
while (cin >> n, n) {
vector<string> mp(n);
for (int i = 0; i < n; i++)
cin >> mp[i];
int sx, sy;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (mp[i][j] == '@')
sy = i, sx = j;
ans = 11;
dfs(sx, sy, 0, mp);
cout << (ans < 11 ? ans : -1) << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int n;
bool onmp(int x, int y) { return x >= 0 && y >= 0 && x < n && y < n; }
int count(int x, int y, const vector<string> &A) {
int res = 0;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++) {
int nx = x + i, ny = y + j;
if ((!i && !j) || !onmp(nx, ny))
continue;
res += A[ny][nx] == '#' || A[ny][nx] == '@';
}
return res;
}
bool check(const vector<string> &A) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (A[i][j] == '#')
return 0;
return 1;
}
vector<string> mkmp(const vector<string> &A) {
vector<string> res(n, string(n, '.'));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
int cnt = count(j, i, A);
if (A[i][j] == '#')
res[i][j] = (cnt == 2 || cnt == 3) ? '#' : '.';
if (A[i][j] == '.')
res[i][j] = (cnt == 3) ? '#' : '.';
if (A[i][j] == '@')
res[i][j] = '@';
}
return res;
}
map<vector<string>, int> mem;
int ans;
void dfs(int x, int y, int cnt, vector<string> A) {
if (check(A))
ans = min(cnt, ans);
if (cnt >= ans)
return;
if (mem.count(A) && mem[A] <= cnt)
return;
mem[A] = cnt;
A[y][x] = '.';
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
int nx = x + dx, ny = y + dy;
if ((!dx && !dy) || !onmp(nx, ny) || A[ny][nx] == '#')
continue;
A[ny][nx] = '@';
dfs(nx, ny, cnt + 1, mkmp(A));
A[ny][nx] = '.';
}
}
}
int main() {
while (cin >> n, n) {
vector<string> mp(n);
for (int i = 0; i < n; i++)
cin >> mp[i];
int sx, sy;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (mp[i][j] == '@')
sy = i, sx = j;
ans = 11;
mem.clear();
dfs(sx, sy, 0, mp);
cout << (ans < 11 ? ans : -1) << endl;
}
return 0;
} | insert | 75 | 75 | 75 | 76 | MLE | |
p00884 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int n;
vector<string> group;
vector<vector<string>> member;
set<string> ret;
void solve(string s) {
int k = find(group.begin(), group.end(), s) - group.begin();
if (k == n) {
ret.insert(s);
return;
}
for (unsigned i = 0; i < member[k].size(); ++i)
solve(member[k][i]);
}
int main() {
for (;;) {
cin >> n;
if (n == 0)
return 0;
group.assign(n, "");
member.assign(n, vector<string>(0));
for (int i = 0; i < n; ++i) {
char c;
for (;;) {
cin >> c;
if (c == ':')
break;
group[i] += c;
}
string s;
for (;;) {
cin >> c;
if (c == '.') {
member[i].push_back(s);
break;
}
if (c == ',') {
member[i].push_back(s);
s.clear();
} else {
s += c;
}
}
}
ret.clear();
solve(group[0]);
cout << ret.size() << endl;
}
} | #include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int n;
vector<string> group;
vector<vector<string>> member;
set<string> ret;
void solve(string s) {
int k = find(group.begin(), group.end(), s) - group.begin();
if (k == n) {
ret.insert(s);
return;
}
for (unsigned i = 0; i < member[k].size(); ++i)
solve(member[k][i]);
member[k].clear();
}
int main() {
for (;;) {
cin >> n;
if (n == 0)
return 0;
group.assign(n, "");
member.assign(n, vector<string>(0));
for (int i = 0; i < n; ++i) {
char c;
for (;;) {
cin >> c;
if (c == ':')
break;
group[i] += c;
}
string s;
for (;;) {
cin >> c;
if (c == '.') {
member[i].push_back(s);
break;
}
if (c == ',') {
member[i].push_back(s);
s.clear();
} else {
s += c;
}
}
}
ret.clear();
solve(group[0]);
cout << ret.size() << endl;
}
} | insert | 32 | 32 | 32 | 33 | TLE | |
p00884 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) max((a), -(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000
typedef long long LL;
typedef long double LD;
map<string, set<string>> mp;
vector<string> tt;
int solve(string str) {
// cout << str << endl;
auto itr = mp.find(str);
list<set<string>::iterator> ers;
list<string> psh;
for (auto s = itr->second.begin(); s != itr->second.end(); s++) {
auto itr2 = mp.find(*s);
if (itr2 != mp.end()) {
solve(itr2->first);
for (auto t = itr2->second.begin(); t != itr2->second.end(); t++) {
psh.push_back(*t);
}
ers.push_back(s);
}
}
for (auto s : ers) {
itr->second.erase(s);
}
for (auto s : psh) {
itr->second.insert(s);
}
return itr->second.size();
}
int main() {
while (1) {
int n;
cin >> n;
tt = vector<string>(n);
mp = map<string, set<string>>();
string s, t;
int j, pre;
for (int i = 0; i < n; i++) {
cin >> s;
j = 0;
for (; j < s.length(); j++) {
if (s[j] == ':') {
t = s.substr(0, j);
j++;
pre = j;
break;
}
}
// cout << t << endl;
mp[t] = set<string>();
tt[i] = t;
for (; j < s.length(); j++) {
if (s[j] == ',' || s[j] == '.') {
mp[t].insert(s.substr(pre, j - pre));
// cout << s.substr(pre,j-pre) << endl;
j++;
pre = j;
}
}
}
cout << solve(tt[0]) << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) max((a), -(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000
typedef long long LL;
typedef long double LD;
map<string, set<string>> mp;
vector<string> tt;
int solve(string str) {
// cout << str << endl;
auto itr = mp.find(str);
list<set<string>::iterator> ers;
list<string> psh;
for (auto s = itr->second.begin(); s != itr->second.end(); s++) {
auto itr2 = mp.find(*s);
if (itr2 != mp.end()) {
solve(itr2->first);
for (auto t = itr2->second.begin(); t != itr2->second.end(); t++) {
psh.push_back(*t);
}
ers.push_back(s);
}
}
for (auto s : ers) {
itr->second.erase(s);
}
for (auto s : psh) {
itr->second.insert(s);
}
return itr->second.size();
}
int main() {
while (1) {
int n;
cin >> n;
if (n == 0)
return 0;
tt = vector<string>(n);
mp = map<string, set<string>>();
string s, t;
int j, pre;
for (int i = 0; i < n; i++) {
cin >> s;
j = 0;
for (; j < s.length(); j++) {
if (s[j] == ':') {
t = s.substr(0, j);
j++;
pre = j;
break;
}
}
// cout << t << endl;
mp[t] = set<string>();
tt[i] = t;
for (; j < s.length(); j++) {
if (s[j] == ',' || s[j] == '.') {
mp[t].insert(s.substr(pre, j - pre));
// cout << s.substr(pre,j-pre) << endl;
j++;
pre = j;
}
}
}
cout << solve(tt[0]) << endl;
}
return 0;
}
| insert | 50 | 50 | 50 | 52 | -11 | |
p00884 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
vi dp, visit;
vvi tG;
void dfs(int a) {
if (visit[a])
return;
if (tG[a].size() == 0) {
dp[a] = true;
return;
}
rep(i, tG[a].size()) dfs(tG[a][i]);
return;
}
int main() {
int n;
while (cin >> n, n) {
vector<vector<string>> in(n);
rep(i, n) {
string s;
cin >> s;
string t = "";
rep(j, s.size()) {
if (!isalpha(s[j])) {
in[i].pb(t);
j++;
t = "";
}
t += s[j];
}
}
vvi G(n);
map<string, int> m;
int co = 1;
rep(i, n) rep(j, in[i].size()) {
if (m[in[i][j]]) {
G[i].pb(m[in[i][j]]);
} else {
G[i].pb(co);
m[in[i][j]] = co;
co++;
}
}
tG = vvi(co);
rep(i, n) { rep(j, G[i].size() - 1) tG[G[i][0]].pb(G[i][j + 1]); }
dp = vi(co);
visit = vi(co);
dfs(1);
int out = 0;
rep(i, co) if (dp[i]) out++;
cout << out << endl;
// cout<<dfs(1)<<endl;
}
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
vi dp, visit;
vvi tG;
void dfs(int a) {
if (visit[a])
return;
if (tG[a].size() == 0) {
dp[a] = true;
return;
}
visit[a] = true;
rep(i, tG[a].size()) dfs(tG[a][i]);
return;
}
int main() {
int n;
while (cin >> n, n) {
vector<vector<string>> in(n);
rep(i, n) {
string s;
cin >> s;
string t = "";
rep(j, s.size()) {
if (!isalpha(s[j])) {
in[i].pb(t);
j++;
t = "";
}
t += s[j];
}
}
vvi G(n);
map<string, int> m;
int co = 1;
rep(i, n) rep(j, in[i].size()) {
if (m[in[i][j]]) {
G[i].pb(m[in[i][j]]);
} else {
G[i].pb(co);
m[in[i][j]] = co;
co++;
}
}
tG = vvi(co);
rep(i, n) { rep(j, G[i].size() - 1) tG[G[i][0]].pb(G[i][j + 1]); }
dp = vi(co);
visit = vi(co);
dfs(1);
int out = 0;
rep(i, co) if (dp[i]) out++;
cout << out << endl;
// cout<<dfs(1)<<endl;
}
} | insert | 41 | 41 | 41 | 42 | TLE | |
p00884 | C++ | Time Limit Exceeded | #include <cctype>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
map<string, vector<string>> grp;
map<string, bool> prn;
vector<string> split(string str) {
vector<string> vec;
string s;
for (int i = 0; i < str.size(); i++) {
s = "";
while (i < str.size() && isalpha(str[i]))
s += str[i++];
if (s.size())
vec.push_back(s);
}
return vec;
}
int search(string tg) {
int ln = 0;
vector<string> vec = grp[tg];
prn[tg] = false;
for (int i = 0; i < vec.size(); i++) {
if (!prn[vec[i]] && grp.find(vec[i]) != grp.end())
ln += search(vec[i]);
else if (!prn[vec[i]]) {
ln++;
prn[vec[i]] = true;
}
}
return ln;
}
int main() {
int n;
string str, tg;
while (cin >> n && n) {
bool f = true;
while (n--) {
cin >> str;
vector<string> vec = split(str);
str = vec[0];
vec.erase(vec.begin());
grp[str] = vec;
if (f) {
tg = str;
f = false;
}
}
cout << search(tg) << endl;
grp.clear();
prn.clear();
}
return 0;
} | #include <cctype>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
map<string, vector<string>> grp;
map<string, bool> prn;
vector<string> split(string str) {
vector<string> vec;
string s;
for (int i = 0; i < str.size(); i++) {
s = "";
while (i < str.size() && isalpha(str[i]))
s += str[i++];
if (s.size())
vec.push_back(s);
}
return vec;
}
int search(string tg) {
int ln = 0;
vector<string> vec = grp[tg];
prn[tg] = true;
for (int i = 0; i < vec.size(); i++) {
if (!prn[vec[i]] && grp.find(vec[i]) != grp.end())
ln += search(vec[i]);
else if (!prn[vec[i]]) {
ln++;
prn[vec[i]] = true;
}
}
return ln;
}
int main() {
int n;
string str, tg;
while (cin >> n && n) {
bool f = true;
while (n--) {
cin >> str;
vector<string> vec = split(str);
str = vec[0];
vec.erase(vec.begin());
grp[str] = vec;
if (f) {
tg = str;
f = false;
}
}
cout << search(tg) << endl;
grp.clear();
prn.clear();
}
return 0;
} | replace | 29 | 30 | 29 | 30 | TLE | |
p00884 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define RREP(i, n) for (ll i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < n; ++i)
#define RFOR(i, m, n) for (ll i = n - 1; i >= m; --i)
#define ALL(v) (v).begin(), (v).end()
#define PB(a) push_back(a)
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define DUMP(v) \
REP(aa, (v).size()) { \
cout << v[a]; \
if (a != v.size() - 1) \
cout << " "; \
else \
cout << endl; \
}
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9
const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)///
map<string, int> mp;
set<string> cnt;
void add(int i, vi &p, vector<vector<string>> &v, vector<bool> &b) {
if (b[i])
return;
set<string> tmp;
REP(j, v[i].size()) {
if (mp[v[i][j]] != 0) {
int nx = mp[v[i][j]] - 1;
add(nx, p, v, b);
} else
cnt.insert(v[i][j]);
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
while (cin >> n, n) {
mp.clear();
cnt.clear();
vector<vector<string>> v(n);
vi p(n);
REP(i, n) {
string s;
cin >> s;
s.back() = ',';
REP(j, s.size()) {
if (s[j] == ':') {
p[i] = j;
break;
}
}
mp[s.substr(0, p[i])] = i + 1;
string tmp;
FOR(j, p[i] + 1, s.size()) {
if (s[j] == ',') {
v[i].push_back(tmp);
tmp = "";
} else
tmp += s[j];
}
}
vector<bool> b(n, false);
add(0, p, v, b);
cout << cnt.size() << endl;
}
} | #include "bits/stdc++.h"
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define RREP(i, n) for (ll i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < n; ++i)
#define RFOR(i, m, n) for (ll i = n - 1; i >= m; --i)
#define ALL(v) (v).begin(), (v).end()
#define PB(a) push_back(a)
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define DUMP(v) \
REP(aa, (v).size()) { \
cout << v[a]; \
if (a != v.size() - 1) \
cout << " "; \
else \
cout << endl; \
}
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9
const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)///
map<string, int> mp;
set<string> cnt;
void add(int i, vi &p, vector<vector<string>> &v, vector<bool> &b) {
if (b[i])
return;
set<string> tmp;
b[i] = true;
REP(j, v[i].size()) {
if (mp[v[i][j]] != 0) {
int nx = mp[v[i][j]] - 1;
add(nx, p, v, b);
} else
cnt.insert(v[i][j]);
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
while (cin >> n, n) {
mp.clear();
cnt.clear();
vector<vector<string>> v(n);
vi p(n);
REP(i, n) {
string s;
cin >> s;
s.back() = ',';
REP(j, s.size()) {
if (s[j] == ':') {
p[i] = j;
break;
}
}
mp[s.substr(0, p[i])] = i + 1;
string tmp;
FOR(j, p[i] + 1, s.size()) {
if (s[j] == ',') {
v[i].push_back(tmp);
tmp = "";
} else
tmp += s[j];
}
}
vector<bool> b(n, false);
add(0, p, v, b);
cout << cnt.size() << endl;
}
} | insert | 43 | 43 | 43 | 44 | TLE | |
p00884 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-7;
const double PI = acos(-1.0);
set<string> dfs(map<string, vs> &m, string &member,
map<string, set<string>> &dp) {
if (EXIST(dp, member)) {
return dp[member];
}
if (!EXIST(m, member)) {
set<string> ret;
ret.insert(member);
return ret;
}
set<string> ret;
REP(i, m[member].size()) {
set<string> s = dfs(m, m[member][i], dp);
ret.insert(ALL(s));
}
return ret;
}
int main() {
int n;
while (cin >> n, n) {
map<string, vs> m;
string first_group;
REP(i, n) {
string s;
cin >> s;
string group;
int pos = 0;
while (s[pos] != ':') {
group += s[pos++];
}
if (i == 0) {
first_group = group;
}
pos++;
m[group] = vs();
do {
string member;
while (s[pos] != ',' && s[pos] != '.') {
member += s[pos++];
}
m[group].push_back(member);
} while (s[pos++] != '.');
}
map<string, set<string>> dp;
set<string> ans;
REP(i, m[first_group].size()) {
set<string> r = dfs(m, m[first_group][i], dp);
ans.insert(ALL(r));
}
cout << ans.size() << endl;
}
} | #include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-7;
const double PI = acos(-1.0);
set<string> dfs(map<string, vs> &m, string &member,
map<string, set<string>> &dp) {
if (EXIST(dp, member)) {
return dp[member];
}
if (!EXIST(m, member)) {
set<string> ret;
ret.insert(member);
return ret;
}
set<string> ret;
REP(i, m[member].size()) {
set<string> s = dfs(m, m[member][i], dp);
ret.insert(ALL(s));
}
return dp[member] = ret;
}
int main() {
int n;
while (cin >> n, n) {
map<string, vs> m;
string first_group;
REP(i, n) {
string s;
cin >> s;
string group;
int pos = 0;
while (s[pos] != ':') {
group += s[pos++];
}
if (i == 0) {
first_group = group;
}
pos++;
m[group] = vs();
do {
string member;
while (s[pos] != ',' && s[pos] != '.') {
member += s[pos++];
}
m[group].push_back(member);
} while (s[pos++] != '.');
}
map<string, set<string>> dp;
set<string> ans;
REP(i, m[first_group].size()) {
set<string> r = dfs(m, m[first_group][i], dp);
ans.insert(ALL(r));
}
cout << ans.size() << endl;
}
} | replace | 61 | 62 | 61 | 62 | TLE | |
p00884 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second
const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
int n;
string parse(string s, vector<string> &vec) {
int idx = 0;
string res = "";
string tmp = "";
while (s[idx] != ':') {
tmp += s[idx];
idx++;
}
res = tmp;
tmp = "";
idx++;
while (idx < s.size()) {
if (s[idx] == ',') {
vec.pb(tmp);
tmp = "";
idx++;
}
if (s[idx] == '.')
break;
tmp += s[idx];
idx++;
}
vec.pb(tmp);
return res;
}
map<string, int> ids;
int to_id(string &s) {
if (!ids.count(s))
ids[s] = ids.size();
return ids[s];
}
vector<int> G[1003];
bool flag[1003];
int dfs(int v, int u) {
if (G[v].size() == 0 && !flag[v]) {
flag[v] = true;
return 1;
}
int ret = 0;
rep(i, G[v].size()) {
if (G[v][i] == u)
continue;
ret += dfs(G[v][i], v);
}
return ret;
}
void solve() {
string first_root = "";
ids.clear();
rep(i, 1003) G[i].clear();
rep(i, 1003) flag[i] = false;
rep(i, n) {
vector<string> vec;
string s;
cin >> s;
string root = parse(s, vec), t;
if (first_root == "")
first_root = root;
rep(j, vec.size()) {
t = vec[j];
G[to_id(root)].pb(to_id(t));
}
}
cout << dfs(to_id(first_root), -1) << endl;
}
int main() {
while (cin >> n) {
if (n == 0)
break;
solve();
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second
const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
int n;
string parse(string s, vector<string> &vec) {
int idx = 0;
string res = "";
string tmp = "";
while (s[idx] != ':') {
tmp += s[idx];
idx++;
}
res = tmp;
tmp = "";
idx++;
while (idx < s.size()) {
if (s[idx] == ',') {
vec.pb(tmp);
tmp = "";
idx++;
}
if (s[idx] == '.')
break;
tmp += s[idx];
idx++;
}
vec.pb(tmp);
return res;
}
map<string, int> ids;
int to_id(string &s) {
if (!ids.count(s))
ids[s] = ids.size();
return ids[s];
}
vector<int> G[1003];
bool flag[1003];
int dfs(int v, int u) {
if (flag[v])
return 0;
flag[v] = true;
if (G[v].size() == 0) {
return 1;
}
int ret = 0;
rep(i, G[v].size()) {
if (G[v][i] == u)
continue;
ret += dfs(G[v][i], v);
}
return ret;
}
void solve() {
string first_root = "";
ids.clear();
rep(i, 1003) G[i].clear();
rep(i, 1003) flag[i] = false;
rep(i, n) {
vector<string> vec;
string s;
cin >> s;
string root = parse(s, vec), t;
if (first_root == "")
first_root = root;
rep(j, vec.size()) {
t = vec[j];
G[to_id(root)].pb(to_id(t));
}
}
cout << dfs(to_id(first_root), -1) << endl;
}
int main() {
while (cin >> n) {
if (n == 0)
break;
solve();
}
return 0;
} | replace | 70 | 72 | 70 | 74 | TLE | |
p00884 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int maxn = 110;
int n, cnt, ans;
bool vis[maxn];
map<string, int> mp;
vector<int> G[maxn];
void init() {
for (int i = 0; i < maxn; i++)
G[i].clear();
mp.clear();
memset(vis, 0, sizeof(vis));
cnt = ans = 0;
}
int get_ID(string s) {
if (!mp.count(s))
return mp[s] = ++cnt;
else
return mp[s];
}
void dfs(int u) {
vis[u] = 1;
if (G[u].size() == 0)
ans++;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (!vis[v]) {
dfs(v);
}
}
}
int main() {
string s;
while (~scanf("%d", &n) && n) {
init();
for (int i = 0; i < n; i++) {
cin >> s;
int last = 0, now = 0;
while (now < s.size()) {
int t, v;
if (s[now] == ':') {
v = get_ID(s.substr(last, now - last));
last = ++now;
}
if (s[now] == ',' || s[now] == '.') {
t = get_ID(s.substr(last, now - last));
G[v].push_back(t);
last = ++now;
}
now++;
}
}
dfs(1);
printf("%d\n", ans);
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int maxn = 112345;
int n, cnt, ans;
bool vis[maxn];
map<string, int> mp;
vector<int> G[maxn];
void init() {
for (int i = 0; i < maxn; i++)
G[i].clear();
mp.clear();
memset(vis, 0, sizeof(vis));
cnt = ans = 0;
}
int get_ID(string s) {
if (!mp.count(s))
return mp[s] = ++cnt;
else
return mp[s];
}
void dfs(int u) {
vis[u] = 1;
if (G[u].size() == 0)
ans++;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (!vis[v]) {
dfs(v);
}
}
}
int main() {
string s;
while (~scanf("%d", &n) && n) {
init();
for (int i = 0; i < n; i++) {
cin >> s;
int last = 0, now = 0;
while (now < s.size()) {
int t, v;
if (s[now] == ':') {
v = get_ID(s.substr(last, now - last));
last = ++now;
}
if (s[now] == ',' || s[now] == '.') {
t = get_ID(s.substr(last, now - last));
G[v].push_back(t);
last = ++now;
}
now++;
}
}
dfs(1);
printf("%d\n", ans);
}
return 0;
} | replace | 8 | 9 | 8 | 9 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.