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
p00202
C++
Runtime Error
#include <iostream> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) using namespace std; bool prime[1000001]; int main() { int n, x; rep(i, 1000002) prime[i] = true; prime[0] = prime[1] = false; loop(i, 2, 1010) for (int j = i * 2; j < 1000001; j += i) prime[j] = false; while (cin >> n >> x, n || x) { int dish[n]; rep(i, n) cin >> dish[i]; bool cost[x + 10]; rep(i, x + 10) cost[i] = false; rep(i, n) cost[dish[i]] = true; cost[0] = true; rep(i, x + 1) { if (!cost[i]) continue; rep(j, n) { if (i + dish[j] > x) continue; cost[i + dish[j]] = true; } } bool check = true; for (int i = x; i >= 0; i--) { if (prime[i] && cost[i]) { check = false; cout << i << endl; break; } } if (check) cout << "NA" << endl; } return 0; }
#include <iostream> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) using namespace std; bool prime[1000001]; int main() { int n, x; rep(i, 1000002) prime[i] = true; prime[0] = prime[1] = false; loop(i, 2, 1010) for (int j = i * 2; j < 1000001; j += i) prime[j] = false; while (cin >> n >> x, n || x) { int dish[n]; rep(i, n) cin >> dish[i]; bool cost[x + 10]; rep(i, x + 10) cost[i] = false; rep(i, n) if (dish[i] < x + 1) cost[dish[i]] = true; cost[0] = true; rep(i, x + 1) { if (!cost[i]) continue; rep(j, n) { if (i + dish[j] > x) continue; cost[i + dish[j]] = true; } } bool check = true; for (int i = x; i >= 0; i--) { if (prime[i] && cost[i]) { check = false; cout << i << endl; break; } } if (check) cout << "NA" << endl; } return 0; }
replace
17
18
17
18
0
p00202
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; const int N = 1000001; int main(void) { vector<bool> isP(N); fill(ALL(isP), true); rep(i, 2) isP[i] = false; for (int i = 2; i * i < N; i++) if (isP[i]) for (int j = i * i; j < N; j += i) isP[j] = false; for (int n, x; cin >> n >> x, n;) { vector<bool> dp(x + 1); fill(ALL(dp), false); dp[0] = true; while (n--) { int m; cin >> m; rep(i, x) if (dp[i]) dp[i + m] = true; } while (~x && !(dp[x] && isP[x])) x--; if (~x) cout << x << endl; else cout << "NA" << endl; } 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; const int N = 1000001; int main(void) { vector<bool> isP(N); fill(ALL(isP), true); rep(i, 2) isP[i] = false; for (int i = 2; i * i < N; i++) if (isP[i]) for (int j = i * i; j < N; j += i) isP[j] = false; for (int n, x; cin >> n >> x, n;) { vector<bool> dp(x + 1); fill(ALL(dp), false); dp[0] = true; while (n--) { int m; cin >> m; rep(i, x - m + 1) if (dp[i]) dp[i + m] = true; } while (~x && !(dp[x] && isP[x])) x--; if (~x) cout << x << endl; else cout << "NA" << endl; } return 0; }
replace
51
52
51
52
-6
malloc(): corrupted top size
p00202
C++
Memory Limit Exceeded
#include <cmath> #include <iostream> #define rep(i, n) for (int i = 0; i < n; i++) bool is_prime(int n) { if (n % 2 == 0) return false; for (int i = 3; i <= std::sqrt(n); i += 2) if (n % i == 0) return false; return true; } int main() { int n, x; while (std::cin >> n >> x && n > 0 && x > 0) { auto course = new bool[x + 1](); rep(i, x + 1) course[i] = false; int prices[n]; rep(i, n) { std::cin >> prices[i]; } course[0] = true; int max = -1; rep(i, x + 1) rep(j, n) { if (i - prices[j] >= 0 && course[i - prices[j]]) { if (is_prime(i)) max = i; course[i] = true; break; } } if (max < 0) std::cout << "NA" << std::endl; else std::cout << max << std::endl; } }
#include <cmath> #include <iostream> #define rep(i, n) for (int i = 0; i < n; i++) bool is_prime(int n) { if (n % 2 == 0) return false; for (int i = 3; i <= std::sqrt(n); i += 2) if (n % i == 0) return false; return true; } int main() { int n, x; while (std::cin >> n >> x && n > 0 && x > 0) { auto course = new bool[x + 1](); rep(i, x + 1) course[i] = false; int prices[n]; rep(i, n) { std::cin >> prices[i]; } course[0] = true; int max = -1; rep(i, x + 1) rep(j, n) { if (i - prices[j] >= 0 && course[i - prices[j]]) { if (is_prime(i)) max = i; course[i] = true; break; } } if (max < 0) std::cout << "NA" << std::endl; else std::cout << max << std::endl; delete[] course; } }
insert
37
37
37
38
MLE
p00202
C++
Runtime Error
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; bool isPrime[1000001]; bool dp[1000001]; int main() { fill(isPrime, isPrime + 1000001, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i < 1000001; ++i) { if (isPrime[i]) { for (int j = 2 * i; j < 1000001; j += i) isPrime[j] = false; } } int n, x; while (scanf("%d %d", &n, &x), n | x) { int v[30]; for (int i = 0; i < n; ++i) { int t; scanf("%d", &t); v[i] = t; } fill(dp, dp + x + 1, false); dp[0] = true; for (int i = 0; i <= x; ++i) if (dp[i]) for (int j = 0; j < n; ++j) dp[i + v[j]] = true; int ans; for (ans = x; ans > 0; --ans) if (dp[ans] && isPrime[ans]) break; if (ans) printf("%d\n", ans); else puts("NA"); } return 0; }
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; bool isPrime[1000001]; bool dp[1000001]; int main() { fill(isPrime, isPrime + 1000001, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i < 1000001; ++i) { if (isPrime[i]) { for (int j = 2 * i; j < 1000001; j += i) isPrime[j] = false; } } int n, x; while (scanf("%d %d", &n, &x), n | x) { int v[30]; for (int i = 0; i < n; ++i) { int t; scanf("%d", &t); v[i] = t; } fill(dp, dp + x + 1, false); dp[0] = true; for (int i = 0; i <= x; ++i) if (dp[i]) for (int j = 0; j < n; ++j) if (i + v[j] <= x) dp[i + v[j]] = true; int ans; for (ans = x; ans > 0; --ans) if (dp[ans] && isPrime[ans]) break; if (ans) printf("%d\n", ans); else puts("NA"); } return 0; }
replace
45
46
45
47
0
p00202
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; typedef long long ll; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(a) (a).begin(), (a).end() #define pb push_back bool isp(int n) { if (n == 2) return true; if (n == 1) return false; if (n == 0) return false; for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) return false; } return true; } int main() { int n, x; while (cin >> n >> x) { if (n == 0 && x == 0) break; int data[40]; rep(i, n) { cin >> data[i]; } bool dp[1000001] = {}; rep(i, 1000001) dp[i] = false; dp[0] = true; for (int i = 1; i <= n; i++) { for (int j = 0; j <= x; j++) { // dp[i][j] = dp[i-1][j]; dp[j] |= dp[j - data[i - 1]]; } } int ans = -1; for (int i = x; i >= 0; i--) { if (dp[i] == true) { if (isp(i)) { ans = i; break; } } } if (ans == -1) cout << "NA" << endl; else cout << ans << endl; } }
#include "bits/stdc++.h" using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; typedef long long ll; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(a) (a).begin(), (a).end() #define pb push_back bool isp(int n) { if (n == 2) return true; if (n == 1) return false; if (n == 0) return false; for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) return false; } return true; } int main() { int n, x; while (cin >> n >> x) { if (n == 0 && x == 0) break; int data[40]; rep(i, n) { cin >> data[i]; } bool dp[1000001] = {}; rep(i, 1000001) dp[i] = false; dp[0] = true; for (int i = 1; i <= n; i++) { for (int j = 0; j <= x; j++) { // dp[i][j] = dp[i-1][j]; if (j - data[i - 1] >= 0) dp[j] |= dp[j - data[i - 1]]; } } int ans = -1; for (int i = x; i >= 0; i--) { if (dp[i] == true) { if (isp(i)) { ans = i; break; } } } if (ans == -1) cout << "NA" << endl; else cout << ans << endl; } }
replace
44
46
44
46
0
p00202
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define shosu(n) setprecision(n) #define INF 999999999 using namespace std; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<double> vd; typedef vector<char> vc; typedef vector<bool> vb; typedef vector<string> vs; typedef vector<pii> vpii; typedef vector<vi> vvi; typedef vector<vb> vvb; typedef vector<vpii> vvpii; bool sosu(int n) { if (n <= 1) return false; for (int i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } int main() { int n, x; while (cin >> n >> x, n) { bool dp[1000001] = {}; dp[0] = 1; int a[50]; rep(i, n) { cin >> a[i]; } rep(i, n) rep(j, x) { if (dp[j]) dp[j + a[i]] = 1; } for (int i = x; i > 1; i--) { if (dp[i]) if (sosu(i)) { cout << i << endl; goto end; } } cout << "NA" << endl; end:; } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define shosu(n) setprecision(n) #define INF 999999999 using namespace std; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<double> vd; typedef vector<char> vc; typedef vector<bool> vb; typedef vector<string> vs; typedef vector<pii> vpii; typedef vector<vi> vvi; typedef vector<vb> vvb; typedef vector<vpii> vvpii; bool sosu(int n) { if (n <= 1) return false; for (int i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } int main() { int n, x; while (cin >> n >> x, n) { bool dp[1000001] = {}; dp[0] = 1; int a[50]; rep(i, n) { cin >> a[i]; } rep(i, n) rep(j, x) { if (j + a[i] < 1000001) if (dp[j]) dp[j + a[i]] = 1; } for (int i = x; i > 1; i--) { if (dp[i]) if (sosu(i)) { cout << i << endl; goto end; } } cout << "NA" << endl; end:; } return 0; }
replace
31
33
31
34
0
p00202
C++
Runtime Error
#include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <string> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define reps(i, n) for (int i = 1; i <= n; i++) int sosu[1111111]; int dp[1211111]; int main() { sosu[0] = 1; sosu[1] = 1; for (int i = 2; i < 1111111; i++) { if (sosu[i] == 1) continue; for (int j = i + i; j < 1111111; j += i) { sosu[j] = 1; } } while (1) { int n, m; cin >> n >> m; if (n == 0) break; rep(i, 1111111) dp[i] = 0; dp[0] = 1; int item[33]; rep(i, n) { cin >> item[i]; } int ans = -1; rep(i, m + 1) { if (dp[i] == 0) continue; if (sosu[i] == 0) ans = i; rep(j, n) { dp[i + item[j]] = 1; } } if (ans == -1) { puts("NA"); } else { printf("%d\n", ans); } } }
#include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <string> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define reps(i, n) for (int i = 1; i <= n; i++) int sosu[1111111]; int dp[1211111]; int main() { sosu[0] = 1; sosu[1] = 1; for (int i = 2; i < 1111111; i++) { if (sosu[i] == 1) continue; for (int j = i + i; j < 1111111; j += i) { sosu[j] = 1; } } while (1) { int n, m; cin >> n >> m; if (n == 0) break; rep(i, 1111111) dp[i] = 0; dp[0] = 1; int item[33]; rep(i, n) { cin >> item[i]; } int ans = -1; rep(i, m + 1) { if (dp[i] == 0) continue; if (sosu[i] == 0) ans = i; rep(j, n) { if (i + item[j] > m) continue; dp[i + item[j]] = 1; } } if (ans == -1) { puts("NA"); } else { printf("%d\n", ans); } } }
replace
45
46
45
50
0
p00202
C++
Runtime Error
#include <bits/stdc++.h> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) range(i, 0, b) #define all(a) (a).begin(), (a).end() #define debug(x) cout << "debug " << x << endl; #define INF (1 << 30) using namespace std; const int kN = 1000005; void primeNumber(bool prime[kN]) { rep(i, kN) prime[i] = 1; prime[0] = prime[1] = 0; rep(i, kN) { if (prime[i]) { for (int j = i + i; j < kN; j += i) { prime[j] = 0; } } } } void primeOfCuisine(int n, int a[40], bool prime[kN]) { prime[0] = 1; rep(i, n) { rep(j, kN) { if (prime[j] == 1) { prime[j + a[i]] = 1; } } } } void outputAns(bool a[kN], bool b[kN], int x) { for (int i = x; i >= 0; i--) { if (a[i] == 1 && b[i] == 1) { cout << i << endl; return; } } cout << "NA" << endl; return; } int main() { int n, x, a[40]; while (cin >> n >> x, n || x) { bool is_prime[kN] = {0}; bool is_prime_of_cuisine[kN] = {0}; rep(i, n) cin >> a[i]; primeNumber(is_prime); primeOfCuisine(n, a, is_prime_of_cuisine); outputAns(is_prime, is_prime_of_cuisine, x); } }
#include <bits/stdc++.h> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) range(i, 0, b) #define all(a) (a).begin(), (a).end() #define debug(x) cout << "debug " << x << endl; #define INF (1 << 30) using namespace std; const int kN = 1000005; void primeNumber(bool prime[kN]) { rep(i, kN) prime[i] = 1; prime[0] = prime[1] = 0; rep(i, kN) { if (prime[i]) { for (int j = i + i; j < kN; j += i) { prime[j] = 0; } } } } void primeOfCuisine(int n, int a[40], bool prime[kN]) { prime[0] = 1; rep(i, n) { rep(j, kN) { if (prime[j] == 1) { if (j + a[i] >= kN) continue; prime[j + a[i]] = 1; } } } } void outputAns(bool a[kN], bool b[kN], int x) { for (int i = x; i >= 0; i--) { if (a[i] == 1 && b[i] == 1) { cout << i << endl; return; } } cout << "NA" << endl; return; } int main() { int n, x, a[40]; while (cin >> n >> x, n || x) { bool is_prime[kN] = {0}; bool is_prime_of_cuisine[kN] = {0}; rep(i, n) cin >> a[i]; primeNumber(is_prime); primeOfCuisine(n, a, is_prime_of_cuisine); outputAns(is_prime, is_prime_of_cuisine, x); } }
insert
27
27
27
29
-6
*** stack smashing detected ***: terminated
p00202
C++
Runtime Error
#include <algorithm> #include <iomanip> #include <iostream> #include <list> #include <map> #include <math.h> #include <queue> #include <stack> #include <stdio.h> #include <string.h> #include <vector> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) range(i, 0, b) #define debug(x) cout << "debug " << x << endl; using namespace std; int main() { int n, x, ord[35]; bool prime[1000000] = {1, 1, 0}; rep(i, 1000000) { if (!prime[i]) { for (int j = i + i; j < 1000000; j += i) { prime[j] = 1; } } } while (cin >> n >> x, n || x) { bool total[1000000] = {1}; rep(i, n) { cin >> ord[i]; } rep(i, x) { if (total[i]) { rep(j, n) { total[i + ord[j]] = 1; } } } bool c = true; for (int i = x; i >= 0; i--) { if (prime[i] == 0 && total[i] == 1) { cout << i << endl; c = false; break; } } if (c) cout << "NA" << endl; } }
#include <algorithm> #include <iomanip> #include <iostream> #include <list> #include <map> #include <math.h> #include <queue> #include <stack> #include <stdio.h> #include <string.h> #include <vector> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) range(i, 0, b) #define debug(x) cout << "debug " << x << endl; using namespace std; int main() { int n, x, ord[35]; bool prime[1000000] = {1, 1, 0}; rep(i, 1000000) { if (!prime[i]) { for (int j = i + i; j < 1000000; j += i) { prime[j] = 1; } } } while (cin >> n >> x, n || x) { bool total[1000000] = {1}; rep(i, n) { cin >> ord[i]; } rep(i, x) { if (total[i]) { rep(j, n) { if (i + ord[j] > x) continue; total[i + ord[j]] = 1; } } } bool c = true; for (int i = x; i >= 0; i--) { if (prime[i] == 0 && total[i] == 1) { cout << i << endl; c = false; break; } } if (c) cout << "NA" << endl; } }
replace
33
34
33
38
0
p00202
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> const int Max = 1000001; int dishes[31]; bool dish[Max]; bool isPrime[Max]; int main() { for (int i = 2; i < Max; i++) isPrime[i] = true; for (int i = 2; i < std::sqrt(Max); i++) { if (isPrime[i]) { for (int j = i * 2; j < Max; j += i) isPrime[j] = false; } } int n, x; while (std::cin >> n, n) { std::cin >> x; memset(dish, 0, sizeof(dish)); for (int i = 0; i < n; i++) { std::cin >> dishes[i]; } dish[0] = true; for (int i = 0; i < x; i++) { if (dish[i]) { for (int j = 0; j < n; j++) { dish[i + dishes[j]] = true; } } } for (int i = x; i >= 0; i--) { if (dish[i] && isPrime[i]) { std::cout << i << std::endl; break; } if (i == 0) std::cout << "NA" << std::endl; } } return 0; }
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> const int Max = 1000001; int dishes[31]; bool dish[Max]; bool isPrime[Max]; int main() { for (int i = 2; i < Max; i++) isPrime[i] = true; for (int i = 2; i < std::sqrt(Max); i++) { if (isPrime[i]) { for (int j = i * 2; j < Max; j += i) isPrime[j] = false; } } int n, x; while (std::cin >> n, n) { std::cin >> x; memset(dish, 0, sizeof(dish)); for (int i = 0; i < n; i++) { std::cin >> dishes[i]; } dish[0] = true; for (int i = 0; i < x; i++) { if (dish[i]) { for (int j = 0; j < n; j++) { if (i + dishes[j] <= Max) dish[i + dishes[j]] = true; } } } for (int i = x; i >= 0; i--) { if (dish[i] && isPrime[i]) { std::cout << i << std::endl; break; } if (i == 0) std::cout << "NA" << std::endl; } } return 0; }
replace
36
37
36
38
0
p00202
C++
Memory Limit Exceeded
#include <iostream> #include <math.h> #define VMAX 1000000 #define KMAX 30 using namespace std; int K[KMAX]; int T[KMAX][VMAX]; void eratos(int n, bool prime[]) { for (int i = 0; i <= n; i++) prime[i] = false; for (int i = 3; i <= n; i += 2) prime[i] = true; prime[2] = true; int lim = (int)sqrt((double)n) + 1; for (int i = 3; i <= lim; i += 2) { if (!prime[i]) continue; for (int j = i + i; j <= n; j += i) prime[j] = false; } } int main() { bool prime[VMAX + 1]; eratos(VMAX, prime); int k, v; while (cin >> k >> v && !(k == 0 && v == 0)) { for (int i = 0; i < k; i++) cin >> K[i]; for (int j = 0; j <= v; j++) T[0][j] = 0; for (int j = K[0]; j <= v; j += K[0]) T[0][j] = 1; for (int i = 0; i < k; i++) T[i][0] = 1; for (int i = 1; i < k; i++) { for (int j = 1; j < K[i]; j++) T[i][j] = T[i - 1][j]; for (int j = K[i]; j <= v; j++) { T[i][j] = (T[i - 1][j] | T[i][j - K[i]]); } } int ans = -1; for (int i = v; i >= 2; i--) { if (prime[i] && T[k - 1][i]) { ans = i; break; } } if (ans == -1) cout << "NA" << endl; else cout << ans << endl; } }
#include <iostream> #include <math.h> #define VMAX 1000000 #define KMAX 30 using namespace std; int K[KMAX]; char T[KMAX][VMAX]; void eratos(int n, bool prime[]) { for (int i = 0; i <= n; i++) prime[i] = false; for (int i = 3; i <= n; i += 2) prime[i] = true; prime[2] = true; int lim = (int)sqrt((double)n) + 1; for (int i = 3; i <= lim; i += 2) { if (!prime[i]) continue; for (int j = i + i; j <= n; j += i) prime[j] = false; } } int main() { bool prime[VMAX + 1]; eratos(VMAX, prime); int k, v; while (cin >> k >> v && !(k == 0 && v == 0)) { for (int i = 0; i < k; i++) cin >> K[i]; for (int j = 0; j <= v; j++) T[0][j] = 0; for (int j = K[0]; j <= v; j += K[0]) T[0][j] = 1; for (int i = 0; i < k; i++) T[i][0] = 1; for (int i = 1; i < k; i++) { for (int j = 1; j < K[i]; j++) T[i][j] = T[i - 1][j]; for (int j = K[i]; j <= v; j++) { T[i][j] = (T[i - 1][j] | T[i][j - K[i]]); } } int ans = -1; for (int i = v; i >= 2; i--) { if (prime[i] && T[k - 1][i]) { ans = i; break; } } if (ans == -1) cout << "NA" << endl; else cout << ans << endl; } }
replace
9
10
9
10
MLE
p00202
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int n, x; int v[30], dp[1000001]; bool sosu[1000001]; int main() { for (int i = 2; i < 1000000; i++) { if (!sosu[i]) { for (int j = 2; j * i <= 1000000; j++) { sosu[i * j] = 1; } } } while (1) { cin >> n >> x; if (n == 0 && x == 0) break; for (int i = 0; i < n; i++) { cin >> v[i]; } memset(dp, 0, sizeof(dp)); dp[0] = 1; for (int i = 0; i < n; i++) for (int j = 0; j <= x; j++) if (dp[j]) dp[j + v[i]] = dp[j]; for (int i = x; i >= 0; i--) { if (!sosu[i] && dp[i]) { if (i) cout << i << endl; else cout << "NA" << endl; break; } } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, x; int v[30], dp[1000001]; bool sosu[1000001]; int main() { for (int i = 2; i < 1000000; i++) { if (!sosu[i]) { for (int j = 2; j * i <= 1000000; j++) { sosu[i * j] = 1; } } } while (1) { cin >> n >> x; if (n == 0 && x == 0) break; for (int i = 0; i < n; i++) { cin >> v[i]; } memset(dp, 0, sizeof(dp)); dp[0] = 1; for (int i = 0; i < n; i++) for (int j = 0; j <= x - v[i]; j++) if (dp[j]) dp[j + v[i]] = dp[j]; for (int i = x; i >= 0; i--) { if (!sosu[i] && dp[i]) { if (i) cout << i << endl; else cout << "NA" << endl; break; } } } return 0; }
replace
24
25
24
25
0
p00202
C++
Runtime Error
#include <cstring> #include <iostream> #define MAX 1000000 using namespace std; int n, x, d[31]; bool p[MAX + 1], dp[MAX + 1]; void isPrime() { memset(p, 1, sizeof(p)); p[0] = p[1] = false; for (int i = 2; i * i <= MAX; i++) { for (int j = i + i; j <= MAX; j += i) p[j] = false; } } int main() { isPrime(); while (cin >> n >> x, n || x) { memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) cin >> d[i]; dp[0] = true; for (int i = 0; i <= x; i++) { if (dp[i]) { for (int j = 0; j < n; j++) { if (i + d[i] >= x) continue; dp[i + d[j]] = true; } } } bool ju = true; for (int i = x; i > 0; i--) { if (p[i] && dp[i]) { cout << i << endl; ju = false; break; } } if (ju) cout << "NA" << endl; } }
#include <cstring> #include <iostream> #define MAX 1000000 using namespace std; int n, x, d[31]; bool p[MAX + 1], dp[MAX + 1]; void isPrime() { memset(p, 1, sizeof(p)); p[0] = p[1] = false; for (int i = 2; i * i <= MAX; i++) { for (int j = i + i; j <= MAX; j += i) p[j] = false; } } int main() { isPrime(); while (cin >> n >> x, n || x) { memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) cin >> d[i]; dp[0] = true; for (int i = 0; i <= x; i++) { if (dp[i]) { for (int j = 0; j < n; j++) { if (i + d[j] > x) continue; dp[i + d[j]] = true; } } } bool ju = true; for (int i = x; i > 0; i--) { if (p[i] && dp[i]) { cout << i << endl; ju = false; break; } } if (ju) cout << "NA" << endl; } }
replace
28
29
28
29
0
p00202
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <stack> #include <string.h> #include <utility> #include <vector> #define ll long long int #define ld long double #define INF 1000000000 #define EPS 0.0000000001 #define rep(i, n) for (i = 0; i < n; i++) using namespace std; typedef pair<int, int> P; string str; int main() { bool prime[1000005] = {}; // true int n, x, i, j, k; rep(i, 1000005) prime[i] = true; for (i = 2; i < 1000005; i++) { if (prime[i]) for (j = 2; i * j < 1000005; j++) prime[i * j] = false; } while (1) { int price[30] = {}; int cost[1000005] = {}; cin >> n >> x; if (n == 0 && x == 0) break; rep(i, n) { cin >> price[i]; cost[price[i]]++; } rep(i, x) if (cost[i]) rep(j, n) cost[i + price[j]]++; for (i = x; i > 0; i--) if (cost[i] > 0 && prime[i]) break; if (i) cout << i << endl; else cout << "NA" << endl; } }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <stack> #include <string.h> #include <utility> #include <vector> #define ll long long int #define ld long double #define INF 1000000000 #define EPS 0.0000000001 #define rep(i, n) for (i = 0; i < n; i++) using namespace std; typedef pair<int, int> P; string str; int main() { bool prime[1000005] = {}; // true int n, x, i, j, k; rep(i, 1000005) prime[i] = true; for (i = 2; i < 1000005; i++) { if (prime[i]) for (j = 2; i * j < 1000005; j++) prime[i * j] = false; } while (1) { int price[30] = {}; int cost[2000005] = {}; cin >> n >> x; if (n == 0 && x == 0) break; rep(i, n) { cin >> price[i]; cost[price[i]]++; } rep(i, x) if (cost[i]) rep(j, n) cost[i + price[j]]++; for (i = x; i > 0; i--) if (cost[i] > 0 && prime[i]) break; if (i) cout << i << endl; else cout << "NA" << endl; } }
replace
32
33
32
33
0
p00202
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string.h> #include <vector> #define pb push_back #define MAX 1000000 int dp[MAX]; using namespace std; int main(void) { int n, x; int i1, i2; while (1) { cin >> n >> x; if (n + x == 0) break; vector<int> v(n); memset(dp, 0, sizeof(dp)); for (i1 = 0; i1 < n; i1++) { cin >> v[i1]; dp[v[i1]] = 1; } for (i1 = 0; i1 <= x; i1++) { if (dp[i1] == 1) { for (i2 = 0; i2 < n; i2++) { dp[i1 + v[i2]] = 1; } } } for (i1 = x; i1 > 0; i1--) { if (dp[i1] == 1) { for (i2 = 2; i2 * i2 <= i1; i2++) { if (i1 % i2 == 0) break; } if (i2 * i2 > i1) break; } } if (i1 == 0) cout << "NA" << endl; else cout << i1 << endl; } return 0; }
#include <algorithm> #include <iostream> #include <string.h> #include <vector> #define pb push_back #define MAX 1000000 int dp[MAX]; using namespace std; int main(void) { int n, x; int i1, i2; while (1) { cin >> n >> x; if (n + x == 0) break; vector<int> v(n); memset(dp, 0, sizeof(dp)); for (i1 = 0; i1 < n; i1++) { cin >> v[i1]; dp[v[i1]] = 1; } for (i1 = 0; i1 <= x; i1++) { if (dp[i1] == 1) { for (i2 = 0; i2 < n; i2++) { if ((i1 + v[i2]) < MAX) dp[i1 + v[i2]] = 1; } } } for (i1 = x; i1 > 0; i1--) { if (dp[i1] == 1) { for (i2 = 2; i2 * i2 <= i1; i2++) { if (i1 % i2 == 0) break; } if (i2 * i2 > i1) break; } } if (i1 == 0) cout << "NA" << endl; else cout << i1 << endl; } return 0; }
replace
32
33
32
34
0
p00202
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <ctype.h> #include <fstream> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define REP(i, x, n) for (int i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) #define lengthof(x) (sizeof(x) / sizeof(*(x))) #define FILL(ptr, value) FILL_((ptr), sizeof(ptr) / sizeof(value), (value)) template <typename T> void FILL_(void *ptr, size_t size, T value) { std::fill((T *)ptr, (T *)ptr + size, value); } // 4方向ベクトル→↑←↓ int dx[] = {1, 0, -1, 0}; int dy[] = {0, -1, 0, 1}; int n; // 料理の種類 int x; int sum; bool prime[1000001]; int main() { for (int i = 0; i < 1000001; i++) { prime[i] = true; } prime[0] = false; prime[1] = false; for (int i = 2; i < 1000001; i++) { if (prime[i]) { for (int j = 2 * i; j < 1000001; j += i) { prime[j] = false; } } } while (cin >> n >> x, n || x) { bool flag1 = false; int dish[40]; bool check[1000001]; for (int i = 0; i < 1000001; i++) { check[i] = false; } for (int i = 0; i < n; i++) { cin >> dish[i]; } check[0] = true; // 0はどの料理からでも作れる for (int i = 0; i <= x; i++) { if (check[i]) { for (int j = 0; j < n; j++) { if (i + dish[j] <= x) check[i + dish[j]] = true; } } } int ans = 0; for (int i = x; i > 0; i--) { // 上から調べる if (check[i] && prime[i]) { flag1 = true; ans = i; break; } } for (int i = 0; i < 10000000000; i++) { x += 1; x -= 1; } if (flag1) cout << ans << endl; else cout << "NA" << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <ctype.h> #include <fstream> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define REP(i, x, n) for (int i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) #define lengthof(x) (sizeof(x) / sizeof(*(x))) #define FILL(ptr, value) FILL_((ptr), sizeof(ptr) / sizeof(value), (value)) template <typename T> void FILL_(void *ptr, size_t size, T value) { std::fill((T *)ptr, (T *)ptr + size, value); } // 4方向ベクトル→↑←↓ int dx[] = {1, 0, -1, 0}; int dy[] = {0, -1, 0, 1}; int n; // 料理の種類 int x; int sum; bool prime[1000001]; int main() { for (int i = 0; i < 1000001; i++) { prime[i] = true; } prime[0] = false; prime[1] = false; for (int i = 2; i < 1000001; i++) { if (prime[i]) { for (int j = 2 * i; j < 1000001; j += i) { prime[j] = false; } } } while (cin >> n >> x, n || x) { bool flag1 = false; int dish[40]; bool check[1000001]; for (int i = 0; i < 1000001; i++) { check[i] = false; } for (int i = 0; i < n; i++) { cin >> dish[i]; } check[0] = true; // 0はどの料理からでも作れる for (int i = 0; i <= x; i++) { if (check[i]) { for (int j = 0; j < n; j++) { if (i + dish[j] <= x) check[i + dish[j]] = true; } } } int ans = 0; for (int i = x; i > 0; i--) { // 上から調べる if (check[i] && prime[i]) { flag1 = true; ans = i; break; } } for (int i = 0; i < 100000000; i++) { x += 1; x -= 1; } if (flag1) cout << ans << endl; else cout << "NA" << endl; } return 0; }
replace
84
85
84
85
TLE
p00202
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; bool dp[1000120]; bool notPrime[1000120]; int main() { notPrime[0] = notPrime[1] = true; for (int i = 2; i * i < 1000010; i++) { for (int j = 2 * i; j < 1000010; j += i) { notPrime[j] = true; } } int n, x; while (cin >> n >> x, n) { dp[0] = true; while (n--) { int a; cin >> a; for (int i = 0; i <= x; i++) { if (dp[i]) dp[i + a] = true; } } bool flg = false; for (int i = x; i >= 2; i--) { if (!notPrime[i] & dp[i]) { cout << i << endl; flg = true; break; } } if (!flg) cout << "NA" << endl; for (int i = 0; i < 1000020; i++) dp[i] = false; } }
#include <algorithm> #include <iostream> using namespace std; bool dp[1000120]; bool notPrime[1000120]; int main() { notPrime[0] = notPrime[1] = true; for (int i = 2; i * i < 1000010; i++) { for (int j = 2 * i; j < 1000010; j += i) { notPrime[j] = true; } } int n, x; while (cin >> n >> x, n) { dp[0] = true; while (n--) { int a; cin >> a; for (int i = 0; i <= x; i++) { if (dp[i] && i + a <= 1000010) dp[i + a] = true; } } bool flg = false; for (int i = x; i >= 2; i--) { if (!notPrime[i] & dp[i]) { cout << i << endl; flg = true; break; } } if (!flg) cout << "NA" << endl; for (int i = 0; i < 1000020; i++) dp[i] = false; } }
replace
19
20
19
20
0
p00202
C++
Runtime Error
#include <algorithm> #include <cstring> #include <iostream> constexpr int MAX_N = 31, MAX_C = 1000001; int n, max; int cost[MAX_N]; bool dp[MAX_C]; bool prime_num[MAX_C]; int main() { while (1) { std::cin >> n >> max; if (n + max == 0) break; std::memset(prime_num, false, sizeof(prime_num)); std::memset(dp, false, sizeof(dp)); for (int i = 0; i < n; ++i) std::cin >> cost[i]; for (int i = 2; i * i <= max; ++i) { if (!prime_num[i]) { for (int j = 2; i * j <= max; ++j) prime_num[i * j] = true; } } int ans = 0; for (int i = 0; i < n; ++i) dp[cost[i]] = true; for (int i = 1; i <= MAX_C; ++i) { for (int j = 0; j < n; ++j) { if (dp[i]) dp[i + cost[j]] = true; } } for (int i = max; i >= 0; --i) { if (dp[i] && !prime_num[i]) { ans = i; break; } } if (!ans) std::cout << "NA" << std::endl; else std::cout << ans << std::endl; } return 0; }
#include <algorithm> #include <cstring> #include <iostream> constexpr int MAX_N = 31, MAX_C = 1000001; int n, max; int cost[MAX_N]; bool dp[MAX_C]; bool prime_num[MAX_C]; int main() { while (1) { std::cin >> n >> max; if (n + max == 0) break; std::memset(prime_num, false, sizeof(prime_num)); std::memset(dp, false, sizeof(dp)); for (int i = 0; i < n; ++i) std::cin >> cost[i]; for (int i = 2; i * i <= max; ++i) { if (!prime_num[i]) { for (int j = 2; i * j <= max; ++j) prime_num[i * j] = true; } } int ans = 0; for (int i = 0; i < n; ++i) dp[cost[i]] = true; for (int i = 1; i <= MAX_C; ++i) { for (int j = 0; j < n; ++j) { if (i + cost[j] <= max && dp[i]) dp[i + cost[j]] = true; } } for (int i = max; i >= 0; --i) { if (dp[i] && !prime_num[i]) { ans = i; break; } } if (!ans) std::cout << "NA" << std::endl; else std::cout << ans << std::endl; } return 0; }
replace
31
32
31
32
0
p00203
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int W, H, mas[16][16]; while (cin >> W >> H, W) { for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> mas[i][j]; } } vector<vector<int>> dp(H + 1, vector<int>(W, 0)); for (int i = 0; i < W; i++) { if (mas[0][i] != 1) dp[0][i] = 1; } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (dp[i][j] == 0) continue; if (mas[i][j] == 0) { for (int k = -1; k <= 1; k++) { if (j + k < 0 || j + k >= W) continue; if ((i == H - 1 && k == 0) || (i != H - 1 && k == 0 && mas[i + 1][j + k] == 2) || (i != H - 1 && mas[i + 1][j + k] == 0)) { dp[i + 1][j + k] += dp[i][j]; } } } else if (mas[i][j] == 2) { if (i >= H - 2 || mas[i + 2][j] != 1) { dp[max(H, i + 2)][j] += dp[i][j]; } } } } int ret = 0; for (int i = 0; i < W; i++) { ret += dp[H][i]; } cout << ret << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int W, H, mas[16][16]; while (cin >> W >> H, W) { for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> mas[i][j]; } } vector<vector<int>> dp(H + 1, vector<int>(W, 0)); for (int i = 0; i < W; i++) { if (mas[0][i] != 1) dp[0][i] = 1; } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (dp[i][j] == 0) continue; if (mas[i][j] == 0) { for (int k = -1; k <= 1; k++) { if (j + k < 0 || j + k >= W) continue; if ((i == H - 1 && k == 0) || (i != H - 1 && k == 0 && mas[i + 1][j + k] == 2) || (i != H - 1 && mas[i + 1][j + k] == 0)) { dp[i + 1][j + k] += dp[i][j]; } } } else if (mas[i][j] == 2) { if (i >= H - 2 || mas[i + 2][j] != 1) { dp[min(H, i + 2)][j] += dp[i][j]; } } } } int ret = 0; for (int i = 0; i < W; i++) { ret += dp[H][i]; } cout << ret << endl; } }
replace
32
33
32
33
0
p00203
C++
Time Limit Exceeded
#include <cstdio> int main() { while (true) { int X, Y; scanf("%d%d", &X, &Y); int field[16][17] = {{0}}; for (int y = 1; y <= Y; y++) { for (int x = 1; x <= X; x++) { scanf("%d", &(field[y][x])); } } int dp[16][17] = {{0}}; for (int x = 1; x <= X; x++) { if (field[1][x] != 1) { dp[1][x] = 1; } } for (int y = 2; y <= Y; y++) { for (int x = 1; x <= X; x++) { if (field[y][x] == 1) { continue; } int sum = 0; if (field[y][x] == 0) { if (field[y - 1][x - 1] == 0) { sum += dp[y - 1][x - 1]; } if (field[y - 1][x + 1] == 0) { sum += dp[y - 1][x + 1]; } } if (field[y - 1][x] == 0) { sum += dp[y - 1][x]; } if (field[y - 2][x] == 2) { sum += dp[y - 2][x]; } dp[y][x] = sum; } } int answer = 0; for (int x = 1; x <= X; x++) { if (field[Y - 1][x] == 2) { answer += dp[Y - 1][x]; } answer += dp[Y][x]; } printf("%d\n", answer); } return 0; }
#include <cstdio> int main() { while (true) { int X, Y; scanf("%d%d", &X, &Y); if (X == 0 || Y == 0) { break; } int field[16][17] = {{0}}; for (int y = 1; y <= Y; y++) { for (int x = 1; x <= X; x++) { scanf("%d", &(field[y][x])); } } int dp[16][17] = {{0}}; for (int x = 1; x <= X; x++) { if (field[1][x] != 1) { dp[1][x] = 1; } } for (int y = 2; y <= Y; y++) { for (int x = 1; x <= X; x++) { if (field[y][x] == 1) { continue; } int sum = 0; if (field[y][x] == 0) { if (field[y - 1][x - 1] == 0) { sum += dp[y - 1][x - 1]; } if (field[y - 1][x + 1] == 0) { sum += dp[y - 1][x + 1]; } } if (field[y - 1][x] == 0) { sum += dp[y - 1][x]; } if (field[y - 2][x] == 2) { sum += dp[y - 2][x]; } dp[y][x] = sum; } } int answer = 0; for (int x = 1; x <= X; x++) { if (field[Y - 1][x] == 2) { answer += dp[Y - 1][x]; } answer += dp[Y][x]; } printf("%d\n", answer); } return 0; }
insert
6
6
6
9
TLE
p00204
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <complex> #include <cstdlib> #include <iomanip> #include <iostream> #include <vector> #define REP(i, s, n) for (int i = s; i < n; i++) #define rep(i, n) REP(i, 0, n) #define inf (1 << 29) #define EPS (1e-8) #define COUNTER_CLOCKWISE 1 #define CLOCKWISE -1 #define ONLINE_BACK 2 #define ONLINE_FRONT -2 #define ON_SEGMENT 0 #define equals(a, b) (fabs((a) - (b)) < EPS) #define MAX 110 using namespace std; // template class Point { public: long double x, y; Point(long double x = -inf, long double y = -inf) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(long double a) { return Point(a * x, a * y); } Point operator/(long double a) { return Point(x / a, y / a); } bool operator<(const Point &p) const { return !equals(x, p.x) ? x < p.x : y < p.y; } }; struct Segment { Point p1, p2; Segment(Point p1 = Point(-1, -1), Point p2 = Point(-1, -1)) : p1(p1), p2(p2) {} }; typedef Point Vector; typedef Segment Line; typedef vector<Point> Polygon; double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } double norm(Point a) { return a.x * a.x + a.y * a.y; } bool point_equals(Point a, Point b) { return equals(a.x, b.x) && equals(a.y, b.y); } Point rotate(Point a, double rad) { return Point(cos(rad) * a.x - sin(rad) * a.y, sin(rad) * a.x + cos(rad) * a.y); } double toRad(double agl) { return agl * M_PI / 180.0; } int ccw(Point p0, Point p1, Point p2) { Point a = p1 - p0; Point b = p2 - p0; if (cross(a, b) > EPS) return COUNTER_CLOCKWISE; if (cross(a, b) < -EPS) return CLOCKWISE; if (dot(a, b) < -EPS) return ONLINE_BACK; if (norm(a) < norm(b)) return ONLINE_FRONT; return ON_SEGMENT; } double abs(Point a) { return sqrt(norm(a)); } double getDistanceLP(Line s, Point p) { return abs(cross(s.p2 - s.p1, p - s.p1)) / abs(s.p2 - s.p1); } double getDistanceSP(Segment s, Point p) { if (dot(s.p2 - s.p1, p - s.p1) < 0.0) return abs(p - s.p1); if (dot(s.p1 - s.p2, p - s.p2) < 0.0) return abs(p - s.p2); return getDistanceLP(s, p); } // cross product of pq and pr double cross3p(Point p, Point q, Point r) { return (r.x - q.x) * (p.y - q.y) - (r.y - q.y) * (p.x - q.x); } // returns true if point r is on the same line as the line pq bool collinear(Point p, Point q, Point r) { return fabs(cross3p(p, q, r)) < EPS; } // returns true if point t is on the left side of line pq bool ccwtest(Point p, Point q, Point r) { return cross3p(p, q, r) > 0; // can be modified to accept collinear points } bool onSegment(Point p, Point q, Point r) { return collinear(p, q, r) && equals(sqrt(pow(p.x - r.x, 2) + pow(p.y - r.y, 2)) + sqrt(pow(r.x - q.x, 2) + pow(r.y - q.y, 2)), sqrt(pow(p.x - q.x, 2) + pow(p.y - q.y, 2))); } //-------------------------------------------------------------------------- struct UFO { Point p; long double r, v; UFO(Point p = Point(), long double r = -inf, long double v = -inf) : p(p), r(r), v(v) {} }; long double R; int N; UFO ufo[MAX]; bool alive[MAX]; int main() { int rx; while (cin >> rx >> N, rx | N) { R = (long double)rx; rep(i, N) { cin >> ufo[i].p.x >> ufo[i].p.y >> ufo[i].r >> ufo[i].v; alive[i] = true; } int pass = 0; while (true) { int nearest = -inf; rep(i, N) { if (!alive[i]) continue; Point e = ufo[i].p / abs(ufo[i].p); Point next = ufo[i].p - e * ufo[i].v; long double dist_next = abs(next); if (onSegment(ufo[i].p, next, Point(0, 0)) || equals(dist_next, R) || dist_next < R || equals(abs(ufo[i].p), R) || abs(ufo[i].p) < R) { alive[i] = false; pass++; continue; } ufo[i].p = next; if (nearest == -inf) nearest = i; else { long double d1 = abs(ufo[nearest].p); long double d2 = abs(ufo[i].p); if (!equals(d1, d2) && d1 > d2) nearest = i; } } if (nearest == -inf) break; Vector e = ufo[nearest].p / abs(ufo[nearest].p); Segment seg = Segment(e * R, e * 1000000.0); rep(i, N) { if (!alive[i]) continue; assert(abs(e) > abs(ufo[i].p)); long double dist = getDistanceSP(seg, ufo[i].p); if (equals(dist, ufo[i].r) || dist < ufo[i].r) { alive[i] = false; continue; } } } cout << pass << endl; } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <complex> #include <cstdlib> #include <iomanip> #include <iostream> #include <vector> #define REP(i, s, n) for (int i = s; i < n; i++) #define rep(i, n) REP(i, 0, n) #define inf (1 << 29) #define EPS (1e-8) #define COUNTER_CLOCKWISE 1 #define CLOCKWISE -1 #define ONLINE_BACK 2 #define ONLINE_FRONT -2 #define ON_SEGMENT 0 #define equals(a, b) (fabs((a) - (b)) < EPS) #define MAX 110 using namespace std; // template class Point { public: long double x, y; Point(long double x = -inf, long double y = -inf) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(long double a) { return Point(a * x, a * y); } Point operator/(long double a) { return Point(x / a, y / a); } bool operator<(const Point &p) const { return !equals(x, p.x) ? x < p.x : y < p.y; } }; struct Segment { Point p1, p2; Segment(Point p1 = Point(-1, -1), Point p2 = Point(-1, -1)) : p1(p1), p2(p2) {} }; typedef Point Vector; typedef Segment Line; typedef vector<Point> Polygon; double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } double norm(Point a) { return a.x * a.x + a.y * a.y; } bool point_equals(Point a, Point b) { return equals(a.x, b.x) && equals(a.y, b.y); } Point rotate(Point a, double rad) { return Point(cos(rad) * a.x - sin(rad) * a.y, sin(rad) * a.x + cos(rad) * a.y); } double toRad(double agl) { return agl * M_PI / 180.0; } int ccw(Point p0, Point p1, Point p2) { Point a = p1 - p0; Point b = p2 - p0; if (cross(a, b) > EPS) return COUNTER_CLOCKWISE; if (cross(a, b) < -EPS) return CLOCKWISE; if (dot(a, b) < -EPS) return ONLINE_BACK; if (norm(a) < norm(b)) return ONLINE_FRONT; return ON_SEGMENT; } double abs(Point a) { return sqrt(norm(a)); } double getDistanceLP(Line s, Point p) { return abs(cross(s.p2 - s.p1, p - s.p1)) / abs(s.p2 - s.p1); } double getDistanceSP(Segment s, Point p) { if (dot(s.p2 - s.p1, p - s.p1) < 0.0) return abs(p - s.p1); if (dot(s.p1 - s.p2, p - s.p2) < 0.0) return abs(p - s.p2); return getDistanceLP(s, p); } // cross product of pq and pr double cross3p(Point p, Point q, Point r) { return (r.x - q.x) * (p.y - q.y) - (r.y - q.y) * (p.x - q.x); } // returns true if point r is on the same line as the line pq bool collinear(Point p, Point q, Point r) { return fabs(cross3p(p, q, r)) < EPS; } // returns true if point t is on the left side of line pq bool ccwtest(Point p, Point q, Point r) { return cross3p(p, q, r) > 0; // can be modified to accept collinear points } bool onSegment(Point p, Point q, Point r) { return collinear(p, q, r) && equals(sqrt(pow(p.x - r.x, 2) + pow(p.y - r.y, 2)) + sqrt(pow(r.x - q.x, 2) + pow(r.y - q.y, 2)), sqrt(pow(p.x - q.x, 2) + pow(p.y - q.y, 2))); } //-------------------------------------------------------------------------- struct UFO { Point p; long double r, v; UFO(Point p = Point(), long double r = -inf, long double v = -inf) : p(p), r(r), v(v) {} }; long double R; int N; UFO ufo[MAX]; bool alive[MAX]; int main() { int rx; while (cin >> rx >> N, rx | N) { R = (long double)rx; rep(i, N) { cin >> ufo[i].p.x >> ufo[i].p.y >> ufo[i].r >> ufo[i].v; alive[i] = true; } int pass = 0; while (true) { int nearest = -inf; rep(i, N) { if (!alive[i]) continue; Point e = ufo[i].p / abs(ufo[i].p); Point next = ufo[i].p - e * ufo[i].v; long double dist_next = abs(next); if (onSegment(ufo[i].p, next, Point(0, 0)) || equals(dist_next, R) || dist_next < R || equals(abs(ufo[i].p), R) || abs(ufo[i].p) < R) { alive[i] = false; pass++; continue; } ufo[i].p = next; if (nearest == -inf) nearest = i; else { long double d1 = abs(ufo[nearest].p); long double d2 = abs(ufo[i].p); if (!equals(d1, d2) && d1 > d2) nearest = i; } } if (nearest == -inf) break; Vector e = ufo[nearest].p / abs(ufo[nearest].p); Segment seg = Segment(e * R, e * 1000000.0); rep(i, N) { if (!alive[i]) continue; // assert(abs(e) > abs(ufo[i].p)); long double dist = getDistanceSP(seg, ufo[i].p); if (equals(dist, ufo[i].r) || dist < ufo[i].r) { alive[i] = false; continue; } } } cout << pass << endl; } return 0; }
replace
173
174
173
174
-6
107cc6a8-a1a2-4d92-9dcc-32961919b3e1.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00204/C++/s585032916.cpp:167: int main(): Assertion `abs(e) > abs(ufo[i].p)' failed.
p00205
C++
Time Limit Exceeded
#include <stdio.h> int main(void) { int a[5] = {0}; int guu = 0, tyoki = 0, paa = 0; int n = 1; int i = 0; while (n != 0) { for (i = 0; i < 5; i++) { scanf("%d", &a[i]); if (a[i] == 0) { break; } else if (a[i] == 1) { guu = 1; } else if (a[i] == 2) { tyoki = 1; } else if (a[i] == 3) { paa = 1; } } for (i = 0; i < 5; i++) { if (guu == 1 && tyoki == 1 && paa == 1) { printf("3\n"); } else if (guu == 1 && tyoki == 1 && paa == 0) { if (a[i] == 1) { printf("1\n"); } else { printf("2\n"); } } else if (guu == 1 && tyoki == 0 && paa == 0) { printf("3\n"); } else if (guu == 1 && tyoki == 0 && paa == 1) { if (a[i] == 1) { printf("2\n"); } else { printf("1\n"); } } else if (tyoki == 1 && paa == 1) { if (a[i] == 2) { printf("1\n"); } else { printf("2\n"); } } else if (tyoki == 1 && paa == 0) { printf("3\n"); } else if (paa == 1) { printf("3\n"); } a[i] = 0; } guu = 0, tyoki = 0, paa = 0; } }
#include <stdio.h> int main(void) { int a[5] = {0}; int guu = 0, tyoki = 0, paa = 0; int n = 1; int i = 0; while (n != 0) { for (i = 0; i < 5; i++) { scanf("%d", &a[i]); if (a[i] == 0) { break; } else if (a[i] == 1) { guu = 1; } else if (a[i] == 2) { tyoki = 1; } else if (a[i] == 3) { paa = 1; } } if (a[i] == 0) { break; } for (i = 0; i < 5; i++) { if (guu == 1 && tyoki == 1 && paa == 1) { printf("3\n"); } else if (guu == 1 && tyoki == 1 && paa == 0) { if (a[i] == 1) { printf("1\n"); } else { printf("2\n"); } } else if (guu == 1 && tyoki == 0 && paa == 0) { printf("3\n"); } else if (guu == 1 && tyoki == 0 && paa == 1) { if (a[i] == 1) { printf("2\n"); } else { printf("1\n"); } } else if (tyoki == 1 && paa == 1) { if (a[i] == 2) { printf("1\n"); } else { printf("2\n"); } } else if (tyoki == 1 && paa == 0) { printf("3\n"); } else if (paa == 1) { printf("3\n"); } a[i] = 0; } guu = 0, tyoki = 0, paa = 0; } }
insert
19
19
19
22
TLE
p00205
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) int main() { while (true) { int cur = 0, d[5]; rep(i, 5) cin >> d[i], d[i]--, cur |= (1 << d[i]); if (__builtin_popcount(cur) == 2) { rep(i, 5) cout << 2 - !!count(d, d + 5, (d[i] + 1) % 3) << endl; } else { rep(i, 5) cout << 3 << endl; } } }
#include <algorithm> #include <iostream> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) int main() { while (true) { int cur = 0, d[5]; rep(i, 5) { if (cin >> d[i]) d[i]--, cur |= (1 << d[i]); else return 0; } if (__builtin_popcount(cur) == 2) { rep(i, 5) cout << 2 - !!count(d, d + 5, (d[i] + 1) % 3) << endl; } else { rep(i, 5) cout << 3 << endl; } } }
replace
7
8
7
13
TLE
p00205
C++
Time Limit Exceeded
#include <iostream> int main() { while (1) { int s[6], g[4] = {0}, n = -2, v = 0, i; for (i = 5; i; i--) { std::cin >> s[i]; if (!*s) goto l; if (!g[s[i]]) { n++; g[s[i]] = 1; if (v - 1 == s[i] % 3 || !v) v = s[i]; } } for (i = 5; i; i--) std::cout << (!n ? (v == s[i] ? 1 : 2) : 3) << "\n"; } l:; return 0; }
#include <iostream> int main() { while (1) { int s[6], g[4] = {0}, n = -2, v = 0, i; for (i = 5; i; i--) { std::cin >> s[i]; if (!s[5]) goto l; if (!g[s[i]]) { n++; g[s[i]] = 1; if (v - 1 == s[i] % 3 || !v) v = s[i]; } } for (i = 5; i; i--) std::cout << (!n ? (v == s[i] ? 1 : 2) : 3) << "\n"; } l:; return 0; }
replace
6
7
6
7
TLE
p00205
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <string> using namespace std; int main(void) { for (;;) { int a[5]; int b[5]; int r = 0, p = 0, s = 0; for (int i = 0; i < 5; i++) { cin >> a[i]; if (a[i] == 0) break; else if (a[i] == 1) r++; else if (a[i] == 2) s++; else if (a[i] == 3) p++; } // for(int i=0;i<5;i++)cout<<a[i]<<"+"; if (r > 0 && p > 0 && s > 0) { for (int i = 0; i < 5; i++) { b[i] = 3; // cout<<a[i]<<"-"; } } else if (r > 0 && p == 0 && s == 0) { for (int i = 0; i < 5; i++) { b[i] = 3; // cout<<a[i]<<"--"; } } else if (r == 0 && p > 0 && s == 0) { for (int i = 0; i < 5; i++) { b[i] = 3; // cout<<a[i]<<"---"; } } else if (r == 0 && p == 0 && s > 0) { for (int i = 0; i < 5; i++) { b[i] = 3; // cout<<a[i]<<"."; } } else if (r == 0 && s > 0 && p > 0) { for (int i = 0; i < 5; i++) { if (a[i] == 2) b[i] = 1; else if (a[i] == 3) b[i] = 2; // cout<<a[i]<<".."; } } else if (r > 0 && s == 0 && p > 0) { for (int i = 0; i < 5; i++) { if (a[i] == 3) b[i] = 1; else if (a[i] == 1) b[i] = 2; // cout<<a[i]<<"..."; } } else if (r > 0 && s > 0 && p == 0) { for (int i = 0; i < 5; i++) { if (a[i] == 1) b[i] = 1; else if (a[i] == 2) b[i] = 2; // cout<<a[i]<<"...."; } } // cout<<endl; for (int i = 0; i < 5; i++) { cout << b[i] << endl; } } return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <string> using namespace std; int main(void) { for (;;) { int a[5]; int b[5]; int r = 0, p = 0, s = 0; for (int i = 0; i < 5; i++) { cin >> a[i]; if (a[i] == 0) break; else if (a[i] == 1) r++; else if (a[i] == 2) s++; else if (a[i] == 3) p++; } if (a[0] == 0) break; // for(int i=0;i<5;i++)cout<<a[i]<<"+"; if (r > 0 && p > 0 && s > 0) { for (int i = 0; i < 5; i++) { b[i] = 3; // cout<<a[i]<<"-"; } } else if (r > 0 && p == 0 && s == 0) { for (int i = 0; i < 5; i++) { b[i] = 3; // cout<<a[i]<<"--"; } } else if (r == 0 && p > 0 && s == 0) { for (int i = 0; i < 5; i++) { b[i] = 3; // cout<<a[i]<<"---"; } } else if (r == 0 && p == 0 && s > 0) { for (int i = 0; i < 5; i++) { b[i] = 3; // cout<<a[i]<<"."; } } else if (r == 0 && s > 0 && p > 0) { for (int i = 0; i < 5; i++) { if (a[i] == 2) b[i] = 1; else if (a[i] == 3) b[i] = 2; // cout<<a[i]<<".."; } } else if (r > 0 && s == 0 && p > 0) { for (int i = 0; i < 5; i++) { if (a[i] == 3) b[i] = 1; else if (a[i] == 1) b[i] = 2; // cout<<a[i]<<"..."; } } else if (r > 0 && s > 0 && p == 0) { for (int i = 0; i < 5; i++) { if (a[i] == 1) b[i] = 1; else if (a[i] == 2) b[i] = 2; // cout<<a[i]<<"...."; } } // cout<<endl; for (int i = 0; i < 5; i++) { cout << b[i] << endl; } } return 0; }
insert
24
24
24
26
TLE
p00207
C++
Runtime Error
#include <iostream> using namespace std; int Board[102][102]; // Board[j, x, w][i, y, h] bool isOK; // 迷路がOKかNGか int xs, ys, xg, yg; // StartとGoalの座標 void DFS(int c, int x, int y) { if (Board[x][y] != c) { return; } else if (x == xg && y == yg) { isOK = true; // Goalなら迷路はOK return; } Board[x][y] = 0; DFS(c, x + 1, y); DFS(c, x, y + 1); DFS(c, x - 1, y); DFS(c, x, y - 1); } int main() { int w, h; while (true) { cin >> w >> h; if (w == 0 && h == 0) break; isOK = false; // 初期値をNGに for (int i = 0; i < 102; i++) { // Boardの初期化 for (int j = 0; j < 102; j++) Board[j][i] = 0; } cin >> xs >> ys >> xg >> yg; int n, c, d, x, y; cin >> n; for (int t = 0; t < n; t++) { cin >> c >> d >> x >> y; int wb = 2 + (!d * 2); // d=0なら横長 int hb = 2 + (d * 2); // d=1なら縦長 for (int i = 0; i < hb; i++) { for (int j = 0; j < wb; j++) Board[x + j][y + i] = c; } } DFS(Board[xs][ys], xs, ys); if (isOK) cout << "OK" << endl; else cout << "NG" << endl; } return 0; }
#include <iostream> using namespace std; int Board[102][102]; // Board[j, x, w][i, y, h] bool isOK; // 迷路がOKかNGか int xs, ys, xg, yg; // StartとGoalの座標 void DFS(int c, int x, int y) { if (Board[x][y] != c) { return; } else if (x == xg && y == yg) { isOK = true; // Goalなら迷路はOK return; } Board[x][y] = 0; DFS(c, x + 1, y); DFS(c, x, y + 1); DFS(c, x - 1, y); DFS(c, x, y - 1); } int main() { int w, h; while (true) { cin >> w >> h; if (w == 0 && h == 0) break; isOK = false; // 初期値をNGに for (int i = 0; i < 102; i++) { // Boardの初期化 for (int j = 0; j < 102; j++) Board[j][i] = -1; } cin >> xs >> ys >> xg >> yg; int n, c, d, x, y; cin >> n; for (int t = 0; t < n; t++) { cin >> c >> d >> x >> y; int wb = 2 + (!d * 2); // d=0なら横長 int hb = 2 + (d * 2); // d=1なら縦長 for (int i = 0; i < hb; i++) { for (int j = 0; j < wb; j++) Board[x + j][y + i] = c; } } DFS(Board[xs][ys], xs, ys); if (isOK) cout << "OK" << endl; else cout << "NG" << endl; } return 0; }
replace
33
34
33
34
-11
p00207
C++
Runtime Error
#include <iostream> #include <queue> #include <vector> using namespace std; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; class cordinate { public: int x; int y; }; int main() { while (1) { int w; int h; cin >> w >> h; if (w == 0 && h == 0) { break; } int xs; int ys; cin >> xs >> ys; int xg; int yg; cin >> xg >> yg; int n; cin >> n; vector<vector<int>> vobj; for (int i = 0; i < h; i++) { vector<int> lineobj; for (int j = 0; j < w; j++) { lineobj.push_back(0); } vobj.push_back(lineobj); } for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; x--; y--; if (d == 0) { vobj[x][y] = c; vobj[x + 1][y] = c; vobj[x + 2][y] = c; vobj[x + 3][y] = c; vobj[x][y + 1] = c; vobj[x + 1][y + 1] = c; vobj[x + 2][y + 1] = c; vobj[x + 3][y + 1] = c; } else if (d == 1) { vobj[x][y] = c; vobj[x][y + 1] = c; vobj[x][y + 2] = c; vobj[x][y + 3] = c; vobj[x + 1][y] = c; vobj[x + 1][y + 1] = c; vobj[x + 1][y + 2] = c; vobj[x + 1][y + 3] = c; } } int startColor; startColor = vobj[xs - 1][ys - 1]; queue<cordinate> que; cordinate obj; obj.x = xs - 1; obj.y = ys - 1; que.push(obj); bool flag = false; while (1) { if (startColor == 0) { break; } cordinate buf; buf = que.front(); que.pop(); int bx; int by; bx = buf.x; by = buf.y; if (bx == xg - 1 && by == yg - 1) { flag = true; break; } for (int i = 0; i < 4; i++) { if (bx + dx[i] >= 0 && bx + dx[i] <= h - 1) { if (by + dy[i] >= 0 && by + dy[i] <= w - 1) { if (vobj[bx + dx[i]][by + dy[i]] == startColor) { cordinate newObject; newObject.x = bx + dx[i]; newObject.y = by + dy[i]; que.push(newObject); } } } } vobj[bx][by] = -1; if (que.size() == 0) { break; } } if (flag == true) { cout << "OK" << endl; } else { cout << "NG" << endl; } /*for(int i = 0; i < w - 1; i++) { for(int j = 0; j < h - 1; j++) { cout << vobj[i][j] << " "; } cout << endl; } */ } }
#include <iostream> #include <queue> #include <vector> using namespace std; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; class cordinate { public: int x; int y; }; int main() { while (1) { int w; int h; cin >> w >> h; if (w == 0 && h == 0) { break; } w = 110; h = 110; int xs; int ys; cin >> xs >> ys; int xg; int yg; cin >> xg >> yg; int n; cin >> n; vector<vector<int>> vobj; for (int i = 0; i < h; i++) { vector<int> lineobj; for (int j = 0; j < w; j++) { lineobj.push_back(0); } vobj.push_back(lineobj); } for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; x--; y--; if (d == 0) { vobj[x][y] = c; vobj[x + 1][y] = c; vobj[x + 2][y] = c; vobj[x + 3][y] = c; vobj[x][y + 1] = c; vobj[x + 1][y + 1] = c; vobj[x + 2][y + 1] = c; vobj[x + 3][y + 1] = c; } else if (d == 1) { vobj[x][y] = c; vobj[x][y + 1] = c; vobj[x][y + 2] = c; vobj[x][y + 3] = c; vobj[x + 1][y] = c; vobj[x + 1][y + 1] = c; vobj[x + 1][y + 2] = c; vobj[x + 1][y + 3] = c; } } int startColor; startColor = vobj[xs - 1][ys - 1]; queue<cordinate> que; cordinate obj; obj.x = xs - 1; obj.y = ys - 1; que.push(obj); bool flag = false; while (1) { if (startColor == 0) { break; } cordinate buf; buf = que.front(); que.pop(); int bx; int by; bx = buf.x; by = buf.y; if (bx == xg - 1 && by == yg - 1) { flag = true; break; } for (int i = 0; i < 4; i++) { if (bx + dx[i] >= 0 && bx + dx[i] <= h - 1) { if (by + dy[i] >= 0 && by + dy[i] <= w - 1) { if (vobj[bx + dx[i]][by + dy[i]] == startColor) { cordinate newObject; newObject.x = bx + dx[i]; newObject.y = by + dy[i]; que.push(newObject); } } } } vobj[bx][by] = -1; if (que.size() == 0) { break; } } if (flag == true) { cout << "OK" << endl; } else { cout << "NG" << endl; } /*for(int i = 0; i < w - 1; i++) { for(int j = 0; j < h - 1; j++) { cout << vobj[i][j] << " "; } cout << endl; } */ } }
insert
24
24
24
27
0
p00207
C++
Runtime Error
#include <iostream> using namespace std; char F[102][102]; bool MazeJudge; int xg, yg; char ItoC(int i) { if (i == 1) { return '1'; } else if (i == 2) { return '2'; } else if (i == 3) { return '3'; } else if (i == 4) { return '4'; } else if (i == 5) { return '5'; } } void DFS(int X, int Y, char C) { if (F[X][Y] != C) return; F[X][Y] = '.'; if (X == xg && Y == yg) { MazeJudge = true; return; } DFS(X - 1, Y, C); DFS(X, Y + 1, C); DFS(X + 1, Y, C); DFS(X, Y - 1, C); } int main() { while (true) { // デバッグ用迷路表示 /*for(int j=0;j<102;j++){ for(int i=0;i<102;i++){ F[i][j] = '0'; } }*/ MazeJudge = false; int w, h; cin >> w >> h; if (w == 0 && h == 0) break; int xs, ys; cin >> xs >> ys >> xg >> yg; int n; cin >> n; for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; char color = ItoC(c); if (d == 0) { F[x][y] = color; F[x + 1][y] = color; F[x + 2][y] = color; F[x + 3][y] = color; F[x][y + 1] = color; F[x + 1][y + 1] = color; F[x + 2][y + 1] = color; F[x + 3][y + 1] = color; } else if (d == 1) { F[x][y] = color; F[x + 1][y] = color; F[x][y + 1] = color; F[x + 1][y + 1] = color; F[x][y + 2] = color; F[x + 1][y + 2] = color; F[x][y + 3] = color; F[x + 1][y + 3] = color; } } // デバッグ用迷路表示 /*for(int j=0;j<102;j++){ for(int i=0;i<102;i++){ cout << F[i][j]; } cout << endl; }*/ char rc = F[xs][ys]; DFS(xs, ys, rc); if (MazeJudge == true) { cout << "OK" << endl; } else if (MazeJudge == false) { cout << "NG" << endl; } } return 0; }
#include <iostream> using namespace std; char F[102][102]; bool MazeJudge; int xg, yg; char ItoC(int i) { if (i == 1) { return '1'; } else if (i == 2) { return '2'; } else if (i == 3) { return '3'; } else if (i == 4) { return '4'; } else if (i == 5) { return '5'; } } void DFS(int X, int Y, char C) { if (F[X][Y] != C) return; F[X][Y] = '.'; if (X == xg && Y == yg) { MazeJudge = true; return; } DFS(X - 1, Y, C); DFS(X, Y + 1, C); DFS(X + 1, Y, C); DFS(X, Y - 1, C); } int main() { while (true) { for (int j = 0; j < 102; j++) { for (int i = 0; i < 102; i++) { F[i][j] = '0'; } } MazeJudge = false; int w, h; cin >> w >> h; if (w == 0 && h == 0) break; int xs, ys; cin >> xs >> ys >> xg >> yg; int n; cin >> n; for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; char color = ItoC(c); if (d == 0) { F[x][y] = color; F[x + 1][y] = color; F[x + 2][y] = color; F[x + 3][y] = color; F[x][y + 1] = color; F[x + 1][y + 1] = color; F[x + 2][y + 1] = color; F[x + 3][y + 1] = color; } else if (d == 1) { F[x][y] = color; F[x + 1][y] = color; F[x][y + 1] = color; F[x + 1][y + 1] = color; F[x][y + 2] = color; F[x + 1][y + 2] = color; F[x][y + 3] = color; F[x + 1][y + 3] = color; } } // デバッグ用迷路表示 /*for(int j=0;j<102;j++){ for(int i=0;i<102;i++){ cout << F[i][j]; } cout << endl; }*/ char rc = F[xs][ys]; DFS(xs, ys, rc); if (MazeJudge == true) { cout << "OK" << endl; } else if (MazeJudge == false) { cout << "NG" << endl; } } return 0; }
replace
40
47
40
45
-11
p00207
C++
Runtime Error
#include <iostream> using namespace std; int F[102][102]; void DFS(int Y, int X, int A) { if (F[Y][X] != A) return; F[Y][X] = 7; DFS(Y - 1, X, A); DFS(Y, X + 1, A); DFS(Y + 1, X, A); DFS(Y, X - 1, A); } int main() { int w, h; int xs, ys; int xg, yg; int n; int c, d, x, y; while (true) { cin >> w >> h; if (w == 0 && h == 0) break; cin >> xs >> ys; cin >> xg >> yg; cin >> n; for (int i = 0; i < n; i++) { cin >> c >> d >> x >> y; if (d == 0) { for (int a = 0; a < 4; a++) { for (int b = 0; b < 2; b++) { F[y + b][x + a] = c; } } } else { for (int a = 0; a < 2; a++) { for (int b = 0; b < 4; b++) { F[y + b][x + a] = c; } } } } if (F[ys][xs] == 0 || F[ys][xs] == 1 || F[ys][xs] == 2 || F[ys][xs] == 3 || F[ys][xs] == 4 || F[ys][xs] == 5) { DFS(ys, xs, F[ys][xs]); } if (F[yg][xg] == F[ys][xs]) { cout << "OK" << endl; } else { cout << "NG" << endl; } for (int y = 1; y <= h; y++) { for (int x = 1; x <= w; x++) { F[y][x] = 0; } } } return 0; }
#include <iostream> using namespace std; int F[102][102]; void DFS(int Y, int X, int A) { if (F[Y][X] != A) return; F[Y][X] = 7; DFS(Y - 1, X, A); DFS(Y, X + 1, A); DFS(Y + 1, X, A); DFS(Y, X - 1, A); } int main() { int w, h; int xs, ys; int xg, yg; int n; int c, d, x, y; while (true) { cin >> w >> h; if (w == 0 && h == 0) break; cin >> xs >> ys; cin >> xg >> yg; cin >> n; for (int i = 0; i < n; i++) { cin >> c >> d >> x >> y; if (d == 0) { for (int a = 0; a < 4; a++) { for (int b = 0; b < 2; b++) { F[y + b][x + a] = c; } } } else { for (int a = 0; a < 2; a++) { for (int b = 0; b < 4; b++) { F[y + b][x + a] = c; } } } } if (F[ys][xs] == 0 || F[ys][xs] == 1 || F[ys][xs] == 2 || F[ys][xs] == 3 || F[ys][xs] == 4 || F[ys][xs] == 5) { DFS(ys, xs, F[ys][xs]); } if (F[yg][xg] == F[ys][xs]) { cout << "OK" << endl; } else { cout << "NG" << endl; } /*for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { cout << F[y][x]; } cout << endl; }*/ /*for (int y = 1; y <= h; y++) { for (int x = 1; x <= w; x++) { F[y][x] = 0; } }*/ } return 0; }
replace
61
66
61
77
-11
p00207
C++
Time Limit Exceeded
#include <iostream> using namespace std; int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; int w, h, xs, ys, xg, yg, n, cc; int field[100][100]; bool flag; void dfs(int x, int y, int c) { if (flag) return; if (x == xg && y == yg) { flag = true; return; } for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if (nx >= 0 && nx < w && ny >= 0 && ny < h && field[nx][ny] == c) { field[nx][ny] = 0; dfs(nx, ny, c); field[nx][ny] = c; } } } int main() { while (1) { flag = false; cin >> w >> h; if (!w) break; for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) field[i][j] = 0; } cin >> xs >> ys >> xg >> yg >> n; xs--; ys--; xg--; yg--; for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; if (d == 1) { for (int k = 0; k < 2; k++) { for (int j = 0; j < 4; j++) field[x + k - 1][y + j - 1] = c; } } else { for (int k = 0; k < 4; k++) { for (int j = 0; j < 2; j++) field[x + k - 1][y + j - 1] = c; } } } if (field[xs][ys] == 0) { cout << "NG" << endl; continue; } cc = field[xs][ys]; field[xs][ys] = 0; dfs(xs, ys, cc); if (flag) cout << "OK" << endl; else cout << "NG" << endl; } return 0; }
#include <iostream> using namespace std; int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; int w, h, xs, ys, xg, yg, n, cc; int field[100][100]; bool flag; void dfs(int x, int y, int c) { if (flag) return; if (x == xg && y == yg) { flag = true; return; } for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if (nx >= 0 && nx < w && ny >= 0 && ny < h && field[nx][ny] == c) { field[nx][ny] = 0; dfs(nx, ny, c); } } } int main() { while (1) { flag = false; cin >> w >> h; if (!w) break; for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) field[i][j] = 0; } cin >> xs >> ys >> xg >> yg >> n; xs--; ys--; xg--; yg--; for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; if (d == 1) { for (int k = 0; k < 2; k++) { for (int j = 0; j < 4; j++) field[x + k - 1][y + j - 1] = c; } } else { for (int k = 0; k < 4; k++) { for (int j = 0; j < 2; j++) field[x + k - 1][y + j - 1] = c; } } } if (field[xs][ys] == 0) { cout << "NG" << endl; continue; } cc = field[xs][ys]; field[xs][ys] = 0; dfs(xs, ys, cc); if (flag) cout << "OK" << endl; else cout << "NG" << endl; } return 0; }
delete
21
22
21
21
TLE
p00207
C++
Runtime Error
#include <iostream> using namespace std; int F[102][102]; void DFS(int Y, int X, int c) { if (F[Y][X] != c) { return; } F[Y][X] = -1; DFS(Y - 1, X, c); DFS(Y, X + 1, c); DFS(Y + 1, X, c); DFS(Y, X - 1, c); } int main() { int w, h, xs, ys, xg, yg, n; while (1) { cin >> w >> h; if (w == 0 && h == 0) break; for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { F[i][j] = -1; } } cin >> xs >> ys >> xg >> yg >> n; for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; if (d == 0) { for (int j = 0; j < 4; j++) { F[x + j][y] = c; F[x + j][y + 1] = c; } } else { for (int j = 0; j < 4; j++) { F[x][y + j] = c; F[x + 1][y + j] = c; } } } if (F[xs][ys] == 0) { cout << "NG" << endl; } else { DFS(xs, ys, F[xs][ys]); if (F[xg][yg] == -1) { cout << "OK" << endl; } else { cout << "NG" << endl; } } } }
#include <iostream> using namespace std; int F[102][102]; void DFS(int Y, int X, int c) { if (F[Y][X] != c) { return; } F[Y][X] = -1; DFS(Y - 1, X, c); DFS(Y, X + 1, c); DFS(Y + 1, X, c); DFS(Y, X - 1, c); } int main() { int w, h, xs, ys, xg, yg, n; while (1) { cin >> w >> h; if (w == 0 && h == 0) break; for (int i = 0; i < 102; i++) { for (int j = 0; j < 102; j++) { F[i][j] = 0; } } cin >> xs >> ys >> xg >> yg >> n; for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; if (d == 0) { for (int j = 0; j < 4; j++) { F[x + j][y] = c; F[x + j][y + 1] = c; } } else { for (int j = 0; j < 4; j++) { F[x][y + j] = c; F[x + 1][y + j] = c; } } } if (F[xs][ys] == 0) { cout << "NG" << endl; } else { DFS(xs, ys, F[xs][ys]); if (F[xg][yg] == -1) { cout << "OK" << endl; } else { cout << "NG" << endl; } } } }
replace
24
27
24
27
-11
p00207
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, j, k) for (int i = j; i < k; i++) int w, h, xs, ys, xg, yg, n; int m[102][102]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool DFS(int x, int y, int c) { if (m[x][y] != c) return false; if (x == xg && y == yg) return true; m[x][y] = -1; bool ans = false; rep(i, 0, 4) { ans = max(ans, DFS(x + dx[i], y + dy[i], c)); } return ans; } int main() { cin >> w >> h; while (w != 0) { cin >> xs >> ys >> xg >> yg >> n; xs++; ys++; xg++; yg++; rep(i, 0, 102) rep(j, 0, 102) m[i][j] = -1; rep(i, 0, n) { int c, d, x, y; cin >> c >> d >> x >> y; x++; y++; if (d == 0) { rep(j, x, x + 4) rep(k, y, y + 2) m[j][k] = c; } else { rep(j, x, x + 2) rep(k, y, y + 4) m[j][k] = c; } } string ans[2] = {"NG", "OK"}; cout << ans[DFS(xs, ys, m[xs][ys])] << endl; cin >> w >> h; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, j, k) for (int i = j; i < k; i++) int w, h, xs, ys, xg, yg, n; int m[102][102]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool DFS(int x, int y, int c) { if (m[x][y] != c) return false; if (x == xg && y == yg) return true; m[x][y] = -1; bool ans = false; rep(i, 0, 4) { ans = max(ans, DFS(x + dx[i], y + dy[i], c)); } return ans; } int main() { cin >> w >> h; while (w != 0) { cin >> xs >> ys >> xg >> yg >> n; xs++; ys++; xg++; yg++; rep(i, 0, 102) rep(j, 0, 102) m[i][j] = -1; rep(i, 0, n) { int c, d, x, y; cin >> c >> d >> x >> y; x++; y++; if (d == 0) { rep(j, x, x + 4) rep(k, y, y + 2) m[j][k] = c; } else { rep(j, x, x + 2) rep(k, y, y + 4) m[j][k] = c; } } string ans[2] = {"NG", "OK"}; if (m[xs][ys] == -1) cout << "NG" << endl; else cout << ans[DFS(xs, ys, m[xs][ys])] << endl; cin >> w >> h; } return 0; }
replace
38
39
38
42
-11
p00207
C++
Runtime Error
#include <iostream> using namespace std; int F[102][102]; int xg, yg, w, h; void DFS(int Y, int X, int c) { // if(Y < 1 || X < 1) // return; if (F[Y][X] != c) { return; } F[Y][X] = -1; DFS(Y + 1, X, c); DFS(Y - 1, X, c); DFS(Y, X + 1, c); DFS(Y, X - 1, c); } int main() { int xs, ys, n; while (1) { cin >> w >> h; if (w == 0 && h == 0) break; for (int i = 0; i < 102; i++) { for (int j = 0; j < 102; j++) { F[i][j] = 0; } } cin >> xs >> ys >> xg >> yg >> n; for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; if (c == 0) c = 1919810; if (d == 0) { for (int j = 0; j < 4; j++) { F[x + j][y] = c; F[x + j][y + 1] = c; } } else { for (int j = 0; j < 4; j++) { F[x][y + j] = c; F[x + 1][y + j] = c; } } } DFS(xs, ys, F[xs][ys]); if (F[xg][yg] == -1) { cout << "OK" << endl; } else { cout << "NG" << endl; } } }
#include <iostream> using namespace std; int F[102][102]; int xg, yg, w, h; void DFS(int Y, int X, int c) { // if(Y < 1 || X < 1) // return; if (F[Y][X] != c) { return; } F[Y][X] = -1; DFS(Y + 1, X, c); DFS(Y - 1, X, c); DFS(Y, X + 1, c); DFS(Y, X - 1, c); } int main() { int xs, ys, n; while (1) { cin >> w >> h; if (w == 0 && h == 0) break; for (int i = 0; i < 102; i++) { for (int j = 0; j < 102; j++) { F[i][j] = 10; } } cin >> xs >> ys >> xg >> yg >> n; for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; if (c == 0) c = 1919810; if (d == 0) { for (int j = 0; j < 4; j++) { F[x + j][y] = c; F[x + j][y + 1] = c; } } else { for (int j = 0; j < 4; j++) { F[x][y + j] = c; F[x + 1][y + j] = c; } } } DFS(xs, ys, F[xs][ys]); if (F[xg][yg] == -1) { cout << "OK" << endl; } else { cout << "NG" << endl; } } }
replace
28
29
28
29
-11
p00207
C++
Runtime Error
#include <iostream> using namespace std; int F[105][105]; int xg, yg, w, h; void DFS(int Y, int X, int c) { if (F[Y][X] != c || Y > w || X > h) { return; } F[Y][X] = -1; DFS(Y + 1, X, c); DFS(Y - 1, X, c); DFS(Y, X + 1, c); DFS(Y, X - 1, c); } int main() { int xs, ys, n; while (1) { cin >> w >> h; if (w == 0 && h == 0) break; for (int i = 0; i < 105; i++) { for (int j = 0; j < 105; j++) { F[i][j] = 0; } } cin >> xs >> ys >> xg >> yg >> n; for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; if (d == 0) { for (int j = 0; j < 4; j++) { F[x + j][y] = c; F[x + j][y + 1] = c; } } else { for (int j = 0; j < 4; j++) { F[x][y + j] = c; F[x + 1][y + j] = c; } } } DFS(xs, ys, F[xs][ys]); if (F[xg][yg] == -1) { cout << "OK" << endl; } else { cout << "NG" << endl; } } }
#include <iostream> using namespace std; int F[105][105]; int xg, yg, w, h; void DFS(int Y, int X, int c) { if (Y < 1 || X < 1) return; if (F[Y][X] != c || Y > w || X > h) { return; } F[Y][X] = -1; DFS(Y + 1, X, c); DFS(Y - 1, X, c); DFS(Y, X + 1, c); DFS(Y, X - 1, c); } int main() { int xs, ys, n; while (1) { cin >> w >> h; if (w == 0 && h == 0) break; for (int i = 0; i < 105; i++) { for (int j = 0; j < 105; j++) { F[i][j] = 0; } } cin >> xs >> ys >> xg >> yg >> n; for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; if (d == 0) { for (int j = 0; j < 4; j++) { F[x + j][y] = c; F[x + j][y + 1] = c; } } else { for (int j = 0; j < 4; j++) { F[x][y + j] = c; F[x + 1][y + j] = c; } } } DFS(xs, ys, F[xs][ys]); if (F[xg][yg] == -1) { cout << "OK" << endl; } else { cout << "NG" << endl; } } }
insert
7
7
7
9
-11
p00207
C++
Runtime Error
#include <iostream> using namespace std; int F[105][105]; int xg, yg, w, h; void DFS(int Y, int X, int c) { if (F[Y][X] != c) { return; } F[Y][X] = -1; DFS(Y + 1, X, c); DFS(Y - 1, X, c); DFS(Y, X + 1, c); DFS(Y, X - 1, c); } int main() { int xs, ys, n; while (1) { cin >> w >> h; if (w == 0 && h == 0) break; for (int i = 0; i < 105; i++) { for (int j = 0; j < 105; j++) { F[i][j] = 10; } } cin >> xs >> ys >> xg >> yg >> n; for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; if (d == 0) { for (int j = 0; j < 4; j++) { F[x + j][y] = c; F[x + j][y + 1] = c; } } else { for (int j = 0; j < 4; j++) { F[x][y + j] = c; F[x + 1][y + j] = c; } } } DFS(xs, ys, F[xs][ys]); if (F[xg][yg] == -1) { cout << "OK" << endl; } else { cout << "NG" << endl; } } }
#include <iostream> using namespace std; int F[105][105]; int xg, yg, w, h; void DFS(int Y, int X, int c) { if (F[Y][X] != c) { return; } F[Y][X] = -1; DFS(Y + 1, X, c); DFS(Y - 1, X, c); DFS(Y, X + 1, c); DFS(Y, X - 1, c); } int main() { int xs, ys, n; while (1) { cin >> w >> h; if (w == 0 && h == 0) break; for (int i = 0; i < 102; i++) { for (int j = 0; j < 102; j++) { F[i][j] = 10; } } cin >> xs >> ys >> xg >> yg >> n; for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; if (d == 0) { for (int j = 0; j < 4; j++) { F[x + j][y] = c; F[x + j][y + 1] = c; } } else { for (int j = 0; j < 4; j++) { F[x][y + j] = c; F[x + 1][y + j] = c; } } } DFS(xs, ys, F[xs][ys]); if (F[xg][yg] == -1) { cout << "OK" << endl; } else { cout << "NG" << endl; } } }
replace
24
26
24
26
-11
p00207
C++
Runtime Error
#include <iostream> using namespace std; int F[105][105]; int xg, yg, w, h; void DFS(int Y, int X, int c) { if (F[Y][X] != c) { return; } F[Y][X] = -1; DFS(Y + 1, X, c); DFS(Y - 1, X, c); DFS(Y, X + 1, c); DFS(Y, X - 1, c); } int main() { int xs, ys, n; while (1) { cin >> w >> h; if (w == 0 && h == 0) break; for (int i = 0; i < 105; i++) { for (int j = 0; j < 105; j++) { F[i][j] = 10; } } cin >> xs >> ys >> xg >> yg >> n; for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; if (d == 0) { for (int j = 0; j < 4; j++) { F[x + j][y] = c; F[x + j][y + 1] = c; } } else { for (int j = 0; j < 4; j++) { F[x][y + j] = c; F[x + 1][y + j] = c; } } } DFS(xs, ys, F[xs][ys]); if (F[xg][yg] == -1) { cout << "OK" << endl; } else { cout << "NG" << endl; } } }
#include <iostream> using namespace std; int F[105][105]; int xg, yg, w, h; void DFS(int Y, int X, int c) { if (F[Y][X] != c) { return; } F[Y][X] = -1; DFS(Y + 1, X, c); DFS(Y - 1, X, c); DFS(Y, X + 1, c); DFS(Y, X - 1, c); } int main() { int xs, ys, n; while (1) { cin >> w >> h; if (w == 0 && h == 0) break; for (int i = 0; i < 105; i++) { for (int j = 0; j < 105; j++) { F[i][j] = 10; } } cin >> xs >> ys >> xg >> yg >> n; for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; if (d == 0) { for (int j = 0; j < 4; j++) { F[x + j][y] = c; F[x + j][y + 1] = c; } } else { for (int j = 0; j < 4; j++) { F[x][y + j] = c; F[x + 1][y + j] = c; } } } if (F[xs][ys] == 10) { cout << "NG" << endl; continue; } DFS(xs, ys, F[xs][ys]); if (F[xg][yg] == -1) { cout << "OK" << endl; } else { cout << "NG" << endl; } } }
replace
48
49
48
52
-11
p00207
C++
Runtime Error
#include <iostream> using namespace std; int F[105][105]; int xg, yg; void DFS(int Y, int X, int c) { if (F[Y][X] != c) { return; } F[Y][X] = -2000; DFS(Y - 1, X, c); DFS(Y, X + 1, c); DFS(Y + 1, X, c); DFS(Y, X - 1, c); } int main() { int w, h, xs, ys, n; while (1) { cin >> w >> h; if (w == 0 && h == 0) break; cin >> xs >> ys >> xg >> yg >> n; int c[n], d[n], x[n], y[n], ca; for (int i = 0; i < n; i++) { cin >> c[i] >> d[i] >> x[i] >> y[i]; } for (int i = 0; i < n; i++) { if (c[i] == 0) { ca = 1000; break; } else if (i == n - 1) { ca = 0; } } for (int i = 0; i < 102; i++) { for (int j = 0; j < 102; j++) { F[i][j] = ca; } } for (int i = 0; i < n; i++) { if (d[i] == 0) { for (int j = 0; j < 4; j++) { F[x[i] + j][y[i]] = c[i]; F[x[i] + j][y[i] + 1] = c[i]; } } else { for (int j = 0; j < 4; j++) { F[x[i]][y[i] + j] = c[i]; F[x[i] + 1][y[i] + j] = c[i]; } } } DFS(xs, ys, F[xs][ys]); if (F[xg][yg] == -2000) { cout << "OK" << endl; } else { cout << "NG" << endl; } } }
#include <iostream> using namespace std; int F[105][105]; int xg, yg; void DFS(int Y, int X, int c) { if (F[Y][X] != c) { return; } F[Y][X] = -2000; DFS(Y - 1, X, c); DFS(Y, X + 1, c); DFS(Y + 1, X, c); DFS(Y, X - 1, c); } int main() { int w, h, xs, ys, n; while (1) { cin >> w >> h; if (w == 0 && h == 0) break; cin >> xs >> ys >> xg >> yg >> n; int c[n], d[n], x[n], y[n], ca; for (int i = 0; i < n; i++) { cin >> c[i] >> d[i] >> x[i] >> y[i]; } for (int i = 0; i < n; i++) { if (c[i] == 0) { ca = 1000; break; } else if (i == n - 1) { ca = 1000; } } for (int i = 0; i < 102; i++) { for (int j = 0; j < 102; j++) { F[i][j] = ca; } } for (int i = 0; i < n; i++) { if (d[i] == 0) { for (int j = 0; j < 4; j++) { F[x[i] + j][y[i]] = c[i]; F[x[i] + j][y[i] + 1] = c[i]; } } else { for (int j = 0; j < 4; j++) { F[x[i]][y[i] + j] = c[i]; F[x[i] + 1][y[i] + j] = c[i]; } } } DFS(xs, ys, F[xs][ys]); if (F[xg][yg] == -2000) { cout << "OK" << endl; } else { cout << "NG" << endl; } } }
replace
35
36
35
36
-11
p00207
C++
Time Limit Exceeded
#include <iostream> using namespace std; int block[100][100]; int xs, ys, xg, yg, n, w, h; bool dfs(int x, int y, int c) { if (block[x][y] != c || block[x][y] == 0) return false; if (x == xg && y == yg) return true; bool b = false; block[x][y] = 0; if (x < w - 1) b |= dfs(x + 1, y, c); if (x > 0 && !b) b |= dfs(x - 1, y, c); if (y < h - 1 && !b) b |= dfs(x, y + 1, c); if (y > 0 && !b) b |= dfs(x, y - 1, c); block[x][y] = c; return b; } int main() { while (true) { cin >> w >> h; if (!w && !h) break; cin >> xs >> ys >> xg >> yg >> n; xs--; ys--; xg--; yg--; for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; x--; y--; block[x][y] = c; block[x + 1][y] = c; block[x][y + 1] = c; block[x + 1][y + 1] = c; if (d == 0) x += 2; else y += 2; block[x][y] = c; block[x + 1][y] = c; block[x][y + 1] = c; block[x + 1][y + 1] = c; } if (dfs(xs, ys, block[xs][ys])) cout << "OK\n"; else cout << "NG\n"; } }
#include <iostream> using namespace std; int block[100][100]; int xs, ys, xg, yg, n, w, h; bool dfs(int x, int y, int c) { if (block[x][y] != c || block[x][y] == 0) return false; if (x == xg && y == yg) return true; bool b = false; block[x][y] = 0; if (x < w - 1) b |= dfs(x + 1, y, c); if (x > 0 && !b) b |= dfs(x - 1, y, c); if (y < h - 1 && !b) b |= dfs(x, y + 1, c); if (y > 0 && !b) b |= dfs(x, y - 1, c); return b; } int main() { while (true) { cin >> w >> h; if (!w && !h) break; cin >> xs >> ys >> xg >> yg >> n; xs--; ys--; xg--; yg--; for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; x--; y--; block[x][y] = c; block[x + 1][y] = c; block[x][y + 1] = c; block[x + 1][y + 1] = c; if (d == 0) x += 2; else y += 2; block[x][y] = c; block[x + 1][y] = c; block[x][y + 1] = c; block[x + 1][y + 1] = c; } if (dfs(xs, ys, block[xs][ys])) cout << "OK\n"; else cout << "NG\n"; } }
delete
21
22
21
21
TLE
p00207
C++
Runtime Error
#include <iostream> using namespace std; int F[105][105]; int xg, yg, w, h; void DFS(int Y, int X, int c) { if (F[Y][X] != c || Y > w || X > h) { return; } F[Y][X] = -1; DFS(Y + 1, X, c); DFS(Y - 1, X, c); DFS(Y, X + 1, c); DFS(Y, X - 1, c); } int main() { int xs, ys, n; while (1) { cin >> w >> h; if (w == 0 && h == 0) break; for (int i = 0; i < 105; i++) { for (int j = 0; j < 105; j++) { F[i][j] = 0; } } cin >> xs >> ys >> xg >> yg >> n; for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; if (d == 0) { for (int j = 0; j < 4; j++) { F[x + j][y] = c; F[x + j][y + 1] = c; } } else { for (int j = 0; j < 4; j++) { F[x][y + j] = c; F[x + 1][y + j] = c; } } } DFS(xs, ys, F[xs][ys]); if (F[xg][yg] == -1) { cout << "OK" << endl; } else { cout << "NG" << endl; } } }
#include <iostream> using namespace std; int F[105][105]; int xg, yg, w, h; void DFS(int Y, int X, int c) { if (F[Y][X] != c || Y > w || X > h) { return; } F[Y][X] = -1; DFS(Y + 1, X, c); DFS(Y - 1, X, c); DFS(Y, X + 1, c); DFS(Y, X - 1, c); } int main() { int xs, ys, n; while (1) { cin >> w >> h; if (w == 0 && h == 0) break; for (int i = 0; i < 105; i++) { for (int j = 0; j < 105; j++) { F[i][j] = 10; } } cin >> xs >> ys >> xg >> yg >> n; for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; if (d == 0) { for (int j = 0; j < 4; j++) { F[x + j][y] = c; F[x + j][y + 1] = c; } } else { for (int j = 0; j < 4; j++) { F[x][y + j] = c; F[x + 1][y + j] = c; } } } DFS(xs, ys, F[xs][ys]); if (F[xg][yg] == -1) { cout << "OK" << endl; } else { cout << "NG" << endl; } } }
replace
26
27
26
27
-11
p00207
C++
Runtime Error
#include <algorithm> // require sort next_permutation count etc. #include <cctype> // require tolower, toupper #include <cfloat> #include <climits> #include <cmath> #include <cstdio> // require printf #include <cstdlib> // require abs #include <cstring> // require memset #include <deque> #include <fstream> // require freopen #include <functional> #include <iomanip> // require setw #include <iostream> #include <limits> #include <map> #include <numeric> // require accumulate #include <queue> #include <set> #include <sstream> // require stringstream #include <stack> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; int maze[20][20]; bool used[20][20]; int main() { const int dir[][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // cut here before submit // freopen ("testcase.block", "r", stdin ); int w, h; while (cin >> w >> h && w && h) { memset(maze, 0, sizeof(maze)); memset(used, false, sizeof(used)); P start, goal; cin >> start.first >> start.second; start.first--; start.second--; cin >> goal.first >> goal.second; goal.first--; goal.second--; int n; cin >> n; for (int i = 0; i < n; ++i) { int color, dir; P block; cin >> color >> dir >> block.first >> block.second; block.first--; block.second--; if (dir == 0) { for (int col = 0; col < 4; ++col) { maze[block.second][block.first + col] = color; maze[block.second + 1][block.first + col] = color; } // end for } else { for (int row = 0; row < 4; ++row) { maze[block.second + row][block.first] = color; maze[block.second + row][block.first + 1] = color; } // end for } // end if } // end for queue<P> que; que.push(P(start.first, start.second)); int search_color = maze[start.second][start.first]; bool found = false; while (!que.empty()) { P curr = que.front(); que.pop(); int x = curr.first; int y = curr.second; if (x == goal.first && y == goal.second) { found = true; break; } // end if used[y][x] = true; for (int k = 0; k < 4; ++k) { int nx = x + dir[k][0]; int ny = y + dir[k][1]; if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue; if ((maze[ny][nx] == search_color) && !used[ny][nx]) { P next; next.first = nx; next.second = ny; que.push(next); } // end if } // end for } // end while if (found) cout << "OK" << endl; else cout << "NG" << endl; } // end loop return 0; }
#include <algorithm> // require sort next_permutation count etc. #include <cctype> // require tolower, toupper #include <cfloat> #include <climits> #include <cmath> #include <cstdio> // require printf #include <cstdlib> // require abs #include <cstring> // require memset #include <deque> #include <fstream> // require freopen #include <functional> #include <iomanip> // require setw #include <iostream> #include <limits> #include <map> #include <numeric> // require accumulate #include <queue> #include <set> #include <sstream> // require stringstream #include <stack> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; int maze[100][100]; bool used[100][100]; int main() { const int dir[][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // cut here before submit // freopen ("testcase.block", "r", stdin ); int w, h; while (cin >> w >> h && w && h) { memset(maze, 0, sizeof(maze)); memset(used, false, sizeof(used)); P start, goal; cin >> start.first >> start.second; start.first--; start.second--; cin >> goal.first >> goal.second; goal.first--; goal.second--; int n; cin >> n; for (int i = 0; i < n; ++i) { int color, dir; P block; cin >> color >> dir >> block.first >> block.second; block.first--; block.second--; if (dir == 0) { for (int col = 0; col < 4; ++col) { maze[block.second][block.first + col] = color; maze[block.second + 1][block.first + col] = color; } // end for } else { for (int row = 0; row < 4; ++row) { maze[block.second + row][block.first] = color; maze[block.second + row][block.first + 1] = color; } // end for } // end if } // end for queue<P> que; que.push(P(start.first, start.second)); int search_color = maze[start.second][start.first]; bool found = false; while (!que.empty()) { P curr = que.front(); que.pop(); int x = curr.first; int y = curr.second; if (x == goal.first && y == goal.second) { found = true; break; } // end if used[y][x] = true; for (int k = 0; k < 4; ++k) { int nx = x + dir[k][0]; int ny = y + dir[k][1]; if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue; if ((maze[ny][nx] == search_color) && !used[ny][nx]) { P next; next.first = nx; next.second = ny; que.push(next); } // end if } // end for } // end while if (found) cout << "OK" << endl; else cout << "NG" << endl; } // end loop return 0; }
replace
25
27
25
27
0
p00207
C++
Runtime Error
#include <iostream> using namespace std; int F[105][105]; int xg, yg; void DFS(int Y, int X, int c) { if (F[Y][X] != c) { return; } F[Y][X] = -2000; DFS(Y - 1, X, c); DFS(Y, X + 1, c); DFS(Y + 1, X, c); DFS(Y, X - 1, c); } int main() { int w, h, xs, ys, n; while (1) { cin >> w >> h; if (w == 0 && h == 0) break; cin >> xs >> ys >> xg >> yg >> n; int c[n], d[n], x[n], y[n], ca; for (int i = 0; i < n; i++) { cin >> c[i] >> d[i] >> x[i] >> y[i]; } for (int i = 0; i < n; i++) { if (c[i] == 0) ca = 10; else if (i == n - 1) ca = 0; } for (int i = 0; i < 102; i++) { for (int j = 0; j < 102; j++) { F[i][j] = ca; } } for (int i = 0; i < n; i++) { if (d[i] == 0) { for (int j = 0; j < 4; j++) { F[x[i] + j][y[i]] = c[i]; F[x[i] + j][y[i] + 1] = c[i]; } } else { for (int j = 0; j < 4; j++) { F[x[i]][y[i] + j] = c[i]; F[x[i] + 1][y[i] + j] = c[i]; } } } DFS(xs, ys, F[xs][ys]); if (F[xg][yg] == -2000) { cout << "OK" << endl; } else { cout << "NG" << endl; } } }
#include <iostream> using namespace std; int F[105][105]; int xg, yg; void DFS(int Y, int X, int c) { if (F[Y][X] != c) { return; } F[Y][X] = -2000; DFS(Y - 1, X, c); DFS(Y, X + 1, c); DFS(Y + 1, X, c); DFS(Y, X - 1, c); } int main() { int w, h, xs, ys, n; while (1) { cin >> w >> h; if (w == 0 && h == 0) break; cin >> xs >> ys >> xg >> yg >> n; int c[n], d[n], x[n], y[n], ca; for (int i = 0; i < n; i++) { cin >> c[i] >> d[i] >> x[i] >> y[i]; } for (int i = 0; i < n; i++) { if (c[i] == 0) ca = 10; else if (i == n - 1) ca = 0; } for (int i = 0; i < 102; i++) { for (int j = 0; j < 102; j++) { F[i][j] = 10; } } for (int i = 0; i < n; i++) { if (d[i] == 0) { for (int j = 0; j < 4; j++) { F[x[i] + j][y[i]] = c[i]; F[x[i] + j][y[i] + 1] = c[i]; } } else { for (int j = 0; j < 4; j++) { F[x[i]][y[i] + j] = c[i]; F[x[i] + 1][y[i] + j] = c[i]; } } } DFS(xs, ys, F[xs][ys]); if (F[xg][yg] == -2000) { cout << "OK" << endl; } else { cout << "NG" << endl; } } }
replace
38
39
38
39
-11
p00207
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int w, h; int gx, gy; int vx[] = {0, 1, 0, -1}; int vy[] = {1, 0, -1, 0}; int block[100][100]; bool check[100][100]; bool flag; void dfs(int y, int x) { /* for(int i = 0; i < h; ++i) { for(int j = 0; j < w; ++j) { if(i == y && j == x) cout << 'P'; else if(i == gy && j == gx) cout << 'G'; else cout << block[i][j]; } cout << endl; } cout << endl; */ if (y == gy && x == gx) { flag = true; return; } else { for (int i = 0; i < 4; ++i) { int ny = y + vy[i]; int nx = x + vx[i]; if (ny >= 0 && ny < h && nx >= 0 && nx < w && !check[ny][nx] && block[ny][nx] && block[ny][nx] == block[y][x]) { check[ny][nx] = true; dfs(ny, nx); check[ny][nx] = false; } } } } int main() { int sx, sy; int n; int c, d, x, y; while (cin >> w >> h) { if (w == 0 && h == 0) break; cin >> sx >> sy; cin >> gx >> gy; cin >> n; --sx; --sy; --gx; --gy; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { block[i][j] = 0; check[i][j] = false; } } for (int i = 0; i < n; ++i) { cin >> c >> d >> x >> y; --x; --y; int bw, bh; if (d) { bw = 2; bh = 4; } else { bw = 4; bh = 2; } for (int j = 0; j < bh; ++j) { for (int k = 0; k < bw; ++k) { block[y + j][x + k] = c; } } } flag = false; check[sy][sx] = true; dfs(sy, sx); cout << (flag ? "OK" : "NG") << endl; } }
#include <bits/stdc++.h> using namespace std; int w, h; int gx, gy; int vx[] = {0, 1, 0, -1}; int vy[] = {1, 0, -1, 0}; int block[100][100]; bool check[100][100]; bool flag; void dfs(int y, int x) { /* for(int i = 0; i < h; ++i) { for(int j = 0; j < w; ++j) { if(i == y && j == x) cout << 'P'; else if(i == gy && j == gx) cout << 'G'; else cout << block[i][j]; } cout << endl; } cout << endl; */ if (y == gy && x == gx) { flag = true; return; } else { for (int i = 0; i < 4; ++i) { int ny = y + vy[i]; int nx = x + vx[i]; if (ny >= 0 && ny < h && nx >= 0 && nx < w && !check[ny][nx] && block[ny][nx] && block[ny][nx] == block[y][x]) { check[ny][nx] = true; dfs(ny, nx); // check[ny][nx] = false; } } } } int main() { int sx, sy; int n; int c, d, x, y; while (cin >> w >> h) { if (w == 0 && h == 0) break; cin >> sx >> sy; cin >> gx >> gy; cin >> n; --sx; --sy; --gx; --gy; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { block[i][j] = 0; check[i][j] = false; } } for (int i = 0; i < n; ++i) { cin >> c >> d >> x >> y; --x; --y; int bw, bh; if (d) { bw = 2; bh = 4; } else { bw = 4; bh = 2; } for (int j = 0; j < bh; ++j) { for (int k = 0; k < bw; ++k) { block[y + j][x + k] = c; } } } flag = false; check[sy][sx] = true; dfs(sy, sx); cout << (flag ? "OK" : "NG") << endl; } }
replace
43
44
43
44
TLE
p00207
C++
Runtime Error
#include <iostream> using namespace std; int F[102][102]; bool DFS(int X, int Y, int Xg, int Yg, int c) { if (F[Y][X] != c) { return false; } else if (F[Y][X] == c && X == Xg && Y == Yg) { return true; } F[Y][X] = -1; if (DFS(X, Y - 1, Xg, Yg, c)) { return true; } else if (DFS(X + 1, Y, Xg, Yg, c)) { return true; } else if (DFS(X, Y + 1, Xg, Yg, c)) { return true; } else if (DFS(X - 1, Y, Xg, Yg, c)) { return true; } return false; } int main() { while (true) { int w, h; cin >> w >> h; if (w == 0 && h == 0) { break; } int xs, ys, xg, yg; cin >> xs >> ys >> xg >> yg; int n; cin >> n; for (int y = 0; y < 102; y++) { for (int x = 0; x < 102; x++) { F[y][x] = -1; } } for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; if (d == 0) { for (int X = x; X < x + 4; X++) { for (int Y = y; Y < y + 2; Y++) { F[Y][X] = c; } } } else if (d == 1) { for (int X = x; X < x + 2; X++) { for (int Y = y; Y < y + 4; Y++) { F[Y][X] = c; } } } } if (DFS(xs, ys, xg, yg, F[xs][ys])) { cout << "OK" << endl; } else { cout << "NG" << endl; } } return 0; }
#include <iostream> using namespace std; int F[102][102]; bool DFS(int X, int Y, int Xg, int Yg, int c) { if (F[Y][X] != c) { return false; } else if (F[Y][X] == c && X == Xg && Y == Yg) { return true; } F[Y][X] = -1; if (DFS(X, Y - 1, Xg, Yg, c)) { return true; } else if (DFS(X + 1, Y, Xg, Yg, c)) { return true; } else if (DFS(X, Y + 1, Xg, Yg, c)) { return true; } else if (DFS(X - 1, Y, Xg, Yg, c)) { return true; } return false; } int main() { while (true) { int w, h; cin >> w >> h; if (w == 0 && h == 0) { break; } int xs, ys, xg, yg; cin >> xs >> ys >> xg >> yg; int n; cin >> n; for (int y = 0; y < 102; y++) { for (int x = 0; x < 102; x++) { F[y][x] = -1; } } for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; if (d == 0) { for (int X = x; X < x + 4; X++) { for (int Y = y; Y < y + 2; Y++) { F[Y][X] = c; } } } else if (d == 1) { for (int X = x; X < x + 2; X++) { for (int Y = y; Y < y + 4; Y++) { F[Y][X] = c; } } } } if (F[ys][xs] != -1) { if (DFS(xs, ys, xg, yg, F[ys][xs])) { cout << "OK" << endl; } else { cout << "NG" << endl; } } else { cout << "NG" << endl; } } return 0; }
replace
58
60
58
64
-11
p00207
C++
Runtime Error
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <queue> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define reg(i, a, b) for (int i = (a); i <= (b); i++) #define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--) #define ireg(i, a, b) for (int i = (b); i >= (a); i--) typedef long long int lli; typedef pair<int, int> mp; #define fir first #define sec second #define IINF INT_MAX #define LINF LLONG_MAX int map[150][150] = {}; int gone[150][150] = {}; int gx, gy; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, -1, 0, 1}; bool dfs(int nx, int ny) { if (gone[nx][ny]) return false; gone[nx][ny] = 1; if (nx == gx && ny == gy) return true; bool res = false; rep(i, 4) { int tx = nx + dx[i], ty = ny + dy[i]; if (map[tx][ty] != map[nx][ny]) continue; res |= dfs(tx, ty); } return res; } int main(void) { while (1) { int w, h; scanf("%d%d", &w, &h); if (w == 0 && h == 0) break; int sx, sy, n; scanf("%d%d%d%d%d", &sx, &sy, &gx, &gy, &n); memset(map, 0, sizeof(map)); memset(gone, 0, sizeof(gone)); rep(i, n) { int c, d, nx, ny; scanf("%d%d%d%d", &c, &d, &nx, &ny); int fx = 2, fy = 4; if (d == 0) swap(fx, fy); rep(x, fx) rep(y, fy) { map[x + nx][y + ny] = c; } } /* rep(y,20)rep(x,20){ printf("%d%c",map[x][y],x==19?'\n':' '); }*/ if (dfs(sx, sy)) printf("OK\n"); else printf("NG\n"); } return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <queue> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define reg(i, a, b) for (int i = (a); i <= (b); i++) #define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--) #define ireg(i, a, b) for (int i = (b); i >= (a); i--) typedef long long int lli; typedef pair<int, int> mp; #define fir first #define sec second #define IINF INT_MAX #define LINF LLONG_MAX int map[150][150] = {}; int gone[150][150] = {}; int gx, gy; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, -1, 0, 1}; bool dfs(int nx, int ny) { if (gone[nx][ny]) return false; gone[nx][ny] = 1; if (nx == gx && ny == gy) return true; bool res = false; rep(i, 4) { int tx = nx + dx[i], ty = ny + dy[i]; if (map[tx][ty] != map[nx][ny]) continue; res |= dfs(tx, ty); } return res; } int main(void) { while (1) { int w, h; scanf("%d%d", &w, &h); if (w == 0 && h == 0) break; int sx, sy, n; scanf("%d%d%d%d%d", &sx, &sy, &gx, &gy, &n); memset(map, 0, sizeof(map)); memset(gone, 0, sizeof(gone)); rep(i, n) { int c, d, nx, ny; scanf("%d%d%d%d", &c, &d, &nx, &ny); int fx = 2, fy = 4; if (d == 0) swap(fx, fy); rep(x, fx) rep(y, fy) { map[x + nx][y + ny] = c; } } /* rep(y,20)rep(x,20){ printf("%d%c",map[x][y],x==19?'\n':' '); }*/ if (map[sx][sy] != 0 && dfs(sx, sy)) printf("OK\n"); else printf("NG\n"); } return 0; }
replace
64
65
64
65
-11
p00207
C++
Runtime Error
#include <iostream> using namespace std; #define MAX_H 100 #define MAX_W 100 #define INF 1000; int h, w; int field[MAX_H][MAX_W]; int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0}; void dfs(int y, int x, const int sc) { if (y <= 0 || x <= 0 || y > h || x > w) return; if (field[y][x] == sc) { field[y][x] = 0; for (int i = 0; i < 4; i++) dfs(y + dy[i], x + dx[i], sc); } return; } int main() { int sx, sy, gx, gy, n; while (cin >> w >> h) { if (!w && !h) break; cin >> sx >> sy >> gx >> gy >> n; for (int i = 0; i < MAX_H; i++) { for (int j = 0; j < MAX_W; j++) { field[i][j] = INF; } } while (n--) { int c, d, x, y; cin >> c >> d >> x >> y; for (int i = 0; i < 2; i++) { for (int j = 0; j < 4; j++) { if (d) field[y + j][x + i] = c; else field[y + i][x + j] = c; } } } dfs(sy, sx, field[sy][sx]); if (field[gy][gx]) cout << "NG" << endl; else cout << "OK" << endl; } return 0; }
#include <iostream> using namespace std; #define MAX_H 100 #define MAX_W 100 #define INF 1000; int h, w; int field[MAX_H + 1][MAX_W + 1]; int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0}; void dfs(int y, int x, const int sc) { if (y <= 0 || x <= 0 || y > h || x > w) return; if (field[y][x] == sc) { field[y][x] = 0; for (int i = 0; i < 4; i++) dfs(y + dy[i], x + dx[i], sc); } return; } int main() { int sx, sy, gx, gy, n; while (cin >> w >> h) { if (!w && !h) break; cin >> sx >> sy >> gx >> gy >> n; for (int i = 0; i < MAX_H; i++) { for (int j = 0; j < MAX_W; j++) { field[i][j] = INF; } } while (n--) { int c, d, x, y; cin >> c >> d >> x >> y; for (int i = 0; i < 2; i++) { for (int j = 0; j < 4; j++) { if (d) field[y + j][x + i] = c; else field[y + i][x + j] = c; } } } dfs(sy, sx, field[sy][sx]); if (field[gy][gx]) cout << "NG" << endl; else cout << "OK" << endl; } return 0; }
replace
6
7
6
7
0
p00207
C++
Runtime Error
#include <algorithm> #include <array> #include <cmath> #include <complex> #include <iostream> #include <map> #include <set> #include <string> #include <utility> #include <vector> int needleX[4] = {-1, 0, 1, 0}; int needleY[4] = {0, 1, 0, -1}; bool dfs(int start, int goal, int x, int y, int goalx, int goaly, std::vector<std::vector<bool>> &canGo) { if (x == goalx && y == goaly) { return true; } if (start == -1 || goal == -1) { return false; } if (start != goal) { return false; } canGo[y][x] = false; for (int i = 0; i < 4; i++) { int next_x = x + needleX[i]; int next_y = y + needleY[i]; if (canGo[next_y][next_x]) { bool flag = dfs(start, goal, next_x, next_y, goalx, goaly, canGo); if (flag) return true; } } return false; } int main(void) { int h, w, n; std::cin >> w >> h; std::vector<std::string> ans; while (h != 0 || w != 0) { int color = -1, orient, leftX, leftY; int sx, sy, gx, gy; std::cin >> sx >> sy >> gx >> gy; std::vector<std::vector<int>> board(h + 2, std::vector<int>(w + 2, 0)); std::vector<std::vector<bool>> canGo(h + 2, std::vector<bool>(w + 2, false)); std::cin >> n; int start = -1, goal = -1; for (int i = 0; i < n; i++) { std::cin >> color >> orient >> leftX >> leftY; if (orient == 0) { for (int j = 0; j <= 3; j++) { for (int k = 0; k <= 1; k++) { board[leftY + k][leftX + j] = color; if (leftX + j == sx && leftY + k == sy) { start = color; } else if (leftX + j == gx && leftY + k == gy) { goal = color; } } } } else { for (int j = 0; j <= 1; j++) { for (int k = 0; k <= 3; k++) { board[leftY + k][leftX + j] = color; if (leftX + j == sx && leftY + k == sy) { start = color; } else if (leftX + j == gx && leftY + k == gy) { goal = color; } } } } } /*for (int i = 0; i <= w; i++) { for (int j = 0; j <= h; j++) { std::cout << board[i][j] << " "; } std::cout << std::endl; }*/ if (start != -1) { for (int i = 0; i <= h + 1; i++) { for (int j = 0; j <= w + 1; j++) { if (board[i][j] == start) { canGo[i][j] = true; } } } } bool result = dfs(start, goal, sx, sy, gx, gy, canGo); if (result) { ans.push_back("OK"); } else { ans.push_back("NG"); } std::cin >> h >> w; } for (std::string i : ans) { std::cout << i << std::endl; } return 0; }
#include <algorithm> #include <array> #include <cmath> #include <complex> #include <iostream> #include <map> #include <set> #include <string> #include <utility> #include <vector> int needleX[4] = {-1, 0, 1, 0}; int needleY[4] = {0, 1, 0, -1}; bool dfs(int start, int goal, int x, int y, int goalx, int goaly, std::vector<std::vector<bool>> &canGo) { if (x == goalx && y == goaly) { return true; } if (start == -1 || goal == -1) { return false; } if (start != goal) { return false; } canGo[y][x] = false; for (int i = 0; i < 4; i++) { int next_x = x + needleX[i]; int next_y = y + needleY[i]; if (canGo[next_y][next_x]) { bool flag = dfs(start, goal, next_x, next_y, goalx, goaly, canGo); if (flag) return true; } } return false; } int main(void) { int h, w, n; std::cin >> w >> h; std::vector<std::string> ans; while (h != 0 || w != 0) { int color = -1, orient, leftX, leftY; int sx, sy, gx, gy; std::cin >> sx >> sy >> gx >> gy; std::vector<std::vector<int>> board(h + 2, std::vector<int>(w + 2, 0)); std::vector<std::vector<bool>> canGo(h + 2, std::vector<bool>(w + 2, false)); std::cin >> n; int start = -1, goal = -1; for (int i = 0; i < n; i++) { std::cin >> color >> orient >> leftX >> leftY; if (orient == 0) { for (int j = 0; j <= 3; j++) { for (int k = 0; k <= 1; k++) { board[leftY + k][leftX + j] = color; if (leftX + j == sx && leftY + k == sy) { start = color; } else if (leftX + j == gx && leftY + k == gy) { goal = color; } } } } else { for (int j = 0; j <= 1; j++) { for (int k = 0; k <= 3; k++) { board[leftY + k][leftX + j] = color; if (leftX + j == sx && leftY + k == sy) { start = color; } else if (leftX + j == gx && leftY + k == gy) { goal = color; } } } } } /*for (int i = 0; i <= w; i++) { for (int j = 0; j <= h; j++) { std::cout << board[i][j] << " "; } std::cout << std::endl; }*/ if (start != -1) { for (int i = 0; i <= h + 1; i++) { for (int j = 0; j <= w + 1; j++) { if (board[i][j] == start) { canGo[i][j] = true; } } } } bool result = dfs(start, goal, sx, sy, gx, gy, canGo); if (result) { ans.push_back("OK"); } else { ans.push_back("NG"); } std::cin >> w >> h; } for (std::string i : ans) { std::cout << i << std::endl; } return 0; }
replace
98
99
98
99
0
p00207
C++
Time Limit Exceeded
#include <cstring> #include <iostream> using namespace std; int map[110][110]; int com[110][110]; int lx, ly, sx, sy, gx, gy; int py[4] = {1, 0, -1, 0}; int px[4] = {0, 1, 0, -1}; int fl; int mei(int zx, int zy, int cei[][110]) { int i; if (zx == gx && zy == gy) { fl = 1; return 0; } cei[zy][zx] = 1; for (i = 0; i < 4; i++) { if (cei[zy + py[i]][zx + px[i]] == 0 && map[sy][sx] == map[zy + py[i]][zx + px[i]]) { mei(zx + px[i], zy + py[i], cei); if (fl) return 0; } } cei[zy][zx] = 0; return 0; } int main() { int n, col, muk, vx, vy; int i, j, t; while (cin >> lx >> ly, lx != 0 && ly != 0) { fl = 0; cin >> sx >> sy >> gx >> gy >> n; memset(map, 0, sizeof(map)); memset(com, 0, sizeof(com)); for (i = 0; i < n; i++) { cin >> col >> muk >> vx >> vy; if (muk == 0) { for (j = vy; j < 2 + vy; j++) { for (t = vx; t < 4 + vx; t++) { map[j][t] = col; } } } else { for (j = vy; j < 4 + vy; j++) { for (t = vx; t < 2 + vx; t++) { map[j][t] = col; } } } } if (map[sy][sx] != map[gy][gx]) { cout << "NG" << endl; continue; } mei(sx, sy, com); if (fl) cout << "OK" << endl; else cout << "NG" << endl; } return 0; }
#include <cstring> #include <iostream> using namespace std; int map[110][110]; int com[110][110]; int lx, ly, sx, sy, gx, gy; int py[4] = {1, 0, -1, 0}; int px[4] = {0, 1, 0, -1}; int fl; int mei(int zx, int zy, int cei[][110]) { int i; if (zx == gx && zy == gy) { fl = 1; return 0; } cei[zy][zx] = 1; for (i = 0; i < 4; i++) { if (cei[zy + py[i]][zx + px[i]] == 0 && map[sy][sx] == map[zy + py[i]][zx + px[i]]) { mei(zx + px[i], zy + py[i], cei); if (fl) return 0; } } return 0; } int main() { int n, col, muk, vx, vy; int i, j, t; while (cin >> lx >> ly, lx != 0 && ly != 0) { fl = 0; cin >> sx >> sy >> gx >> gy >> n; memset(map, 0, sizeof(map)); memset(com, 0, sizeof(com)); for (i = 0; i < n; i++) { cin >> col >> muk >> vx >> vy; if (muk == 0) { for (j = vy; j < 2 + vy; j++) { for (t = vx; t < 4 + vx; t++) { map[j][t] = col; } } } else { for (j = vy; j < 4 + vy; j++) { for (t = vx; t < 2 + vx; t++) { map[j][t] = col; } } } } if (map[sy][sx] != map[gy][gx]) { cout << "NG" << endl; continue; } mei(sx, sy, com); if (fl) cout << "OK" << endl; else cout << "NG" << endl; } return 0; }
delete
24
25
24
24
TLE
p00208
C++
Time Limit Exceeded
#include <iostream> using namespace std; int main() { while (1) { int a, j = 0; cin >> a; j = a; string s = ""; int b = 0; for (b = 1; b * 8 <= a; b *= 8) { } for (int c = b; c != 1; c /= 8) { int o = a / c; switch (a / c) { case 0: case 1: case 2: case 3: cout << a / c; break; case 4: cout << 5; break; default: cout << a / c + 2; break; } a -= c * o; } switch (j % 8) { case 0: case 1: case 2: case 3: cout << j % 8; break; case 4: cout << 5; break; default: cout << j % 8 + 2; } cout << endl; } }
#include <iostream> using namespace std; int main() { while (1) { int a, j = 0; cin >> a; if (a == 0) break; j = a; string s = ""; int b = 0; for (b = 1; b * 8 <= a; b *= 8) { } for (int c = b; c != 1; c /= 8) { int o = a / c; switch (a / c) { case 0: case 1: case 2: case 3: cout << a / c; break; case 4: cout << 5; break; default: cout << a / c + 2; break; } a -= c * o; } switch (j % 8) { case 0: case 1: case 2: case 3: cout << j % 8; break; case 4: cout << 5; break; default: cout << j % 8 + 2; } cout << endl; } }
insert
7
7
7
9
TLE
p00208
C++
Time Limit Exceeded
#include <cmath> #include <iostream> using namespace std; int x[8] = {0, 1, 2, 3, 5, 7, 8, 9}; int n, ok, k; int main() { while (true) { ok = 0; cin >> n; if (!n) { break; } for (int i = 10; i >= 0; i++) { k = (n / (int)pow(8, i)) % 8; if (k > 0) { ok = 1; } if (ok == 1) { cout << x[k]; } } cout << endl; } }
#include <cmath> #include <iostream> using namespace std; int x[8] = {0, 1, 2, 3, 5, 7, 8, 9}; int n, ok, k; int main() { while (true) { ok = 0; cin >> n; if (!n) { break; } for (int i = 10; i >= 0; i--) { k = (n / (int)pow(8, i)) % 8; if (k > 0) { ok = 1; } if (ok == 1) { cout << x[k]; } } cout << endl; } }
replace
12
13
12
13
TLE
p00208
C++
Runtime Error
#include <iostream> using namespace std; int main() { int n, num[100]; while (cin >> n, n) { int k = -1; do { num[++k] = n % 8; if (num[k] >= 5) num[k] += 2; else if (num[k] >= 4) ++num[k]; } while (n /= 8); for (int i = k; i >= 0; --i) cout << num[i]; cout << endl; } return 1; }
#include <iostream> using namespace std; int main() { int n, num[100]; while (cin >> n, n) { int k = -1; do { num[++k] = n % 8; if (num[k] >= 5) num[k] += 2; else if (num[k] >= 4) ++num[k]; } while (n /= 8); for (int i = k; i >= 0; --i) cout << num[i]; cout << endl; } return 0; }
replace
19
20
19
20
1
p00208
C++
Time Limit Exceeded
#include <stdio.h> int main() { int n, i, c; char s[16]; while (scanf("%d", &n)) { sprintf(s, "%o", n); for (i = 0; c = s[i]; i++) { c -= 48; printf("%d", c + c / 4 + c / 5); } printf("\n"); } }
#include <stdio.h> int main() { int n, i, c; char s[16]; while (scanf("%d", &n), n) { sprintf(s, "%o", n); for (i = 0; c = s[i]; i++) { c -= 48; printf("%d", c + c / 4 + c / 5); } printf("\n"); } }
replace
5
6
5
6
TLE
p00209
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #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() const double PI = acos(-1); const double EPS = 1e-10; const int inf = 1e9; using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; int main() { int n, m; while (scanf("%d %d", n, m), n) { vvi in(n, vi(n)); vvi pic(m, vi(m)); rep(i, n) rep(j, n) scanf("%d", &in[i][j]); rep(i, m) rep(j, m) scanf("%d", &pic[i][j]); pii out(inf, inf); rep(e, 4) { rep(i, n - m + 1) rep(j, n - m + 1) { pii t(inf, inf); rep(k, m) rep(l, m) if (pic[k][l] != -1 && in[i + k][j + l] != pic[k][l]) goto en; else if (pic[k][l] != -1) t = min(t, pii(i + k, j + l)); out = min(out, t); goto end; en:; } end:; vvi tmp = pic; rep(i, m) rep(j, m) pic[i][j] = tmp[j][m - i - 1]; } if (out.first == inf) cout << "NA" << endl; else cout << out.second + 1 << " " << out.first + 1 << endl; } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #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() const double PI = acos(-1); const double EPS = 1e-10; const int inf = 1e9; using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; int main() { int n, m; while (scanf("%d %d", &n, &m), n) { vvi in(n, vi(n)); vvi pic(m, vi(m)); rep(i, n) rep(j, n) scanf("%d", &in[i][j]); rep(i, m) rep(j, m) scanf("%d", &pic[i][j]); pii out(inf, inf); rep(e, 4) { rep(i, n - m + 1) rep(j, n - m + 1) { pii t(inf, inf); rep(k, m) rep(l, m) if (pic[k][l] != -1 && in[i + k][j + l] != pic[k][l]) goto en; else if (pic[k][l] != -1) t = min(t, pii(i + k, j + l)); out = min(out, t); goto end; en:; } end:; vvi tmp = pic; rep(i, m) rep(j, m) pic[i][j] = tmp[j][m - i - 1]; } if (out.first == inf) cout << "NA" << endl; else cout << out.second + 1 << " " << out.first + 1 << endl; } }
replace
26
27
26
27
-11
p00209
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <iostream> #include <list> #include <set> #include <string> #include <unordered_map> #include <vector> #define P pair<int, int> using namespace std; int a, b; vector<P> V; unordered_map<int, unordered_map<int, int>> c; unordered_map<int, unordered_map<int, int>> f; unordered_map<int, unordered_map<int, int>> g; void kaiten() { int sum = 0; for (int i = 0; i < b; i++) { for (int j = b - 1; j >= 0; j--, sum++) { g[sum / b][sum % b] = f[j][i]; } } for (int i = 0; i < b; i++) { for (int j = 0; j < b; j++) { f[i][j] = g[i][j]; } } } signed main() { while (cin >> a >> b, a | b) { V.clear(); for (int d = 0; d < a; d++) { for (int e = 0; e < a; e++) { scanf("%d", &c[d][e]); } } for (int d = 0; d < b; d++) { for (int e = 0; e < b; e++) { scanf("%d", &f[d][e]); } } for (int i = 0; i < 4; i++) { kaiten(); for (int x = 0; x < a - b + 1; x++) { for (int y = 0; y < a - b + 1; y++) { bool S = true; for (int n = 0; n < b; n++) { for (int m = 0; m < b; m++) { if (f[n][m] != -1 && c[x + n][y + m] != f[n][m]) S = false; } } if (S) { for (int n = 0; n < b; n++) { for (int m = 0; m < b; m++) { if (f[n][m] != -1) { V.push_back(P(x + n, y + m)); break; } } } } } } } if (V.size() >= 1) { P Q = P(1 << 29, 1 << 29); for (P i : V) { Q = min(Q, i); } cout << Q.second + 1 << " " << Q.first + 1 << endl; } else { puts("NA"); } } }
#include <algorithm> #include <cstdio> #include <iostream> #include <list> #include <set> #include <string> #include <unordered_map> #include <vector> #define P pair<int, int> using namespace std; int a, b; vector<P> V; int c[100][100]; int f[50][50]; int g[50][50]; void kaiten() { int sum = 0; for (int i = 0; i < b; i++) { for (int j = b - 1; j >= 0; j--, sum++) { g[sum / b][sum % b] = f[j][i]; } } for (int i = 0; i < b; i++) { for (int j = 0; j < b; j++) { f[i][j] = g[i][j]; } } } signed main() { while (cin >> a >> b, a | b) { V.clear(); for (int d = 0; d < a; d++) { for (int e = 0; e < a; e++) { scanf("%d", &c[d][e]); } } for (int d = 0; d < b; d++) { for (int e = 0; e < b; e++) { scanf("%d", &f[d][e]); } } for (int i = 0; i < 4; i++) { kaiten(); for (int x = 0; x < a - b + 1; x++) { for (int y = 0; y < a - b + 1; y++) { bool S = true; for (int n = 0; n < b; n++) { for (int m = 0; m < b; m++) { if (f[n][m] != -1 && c[x + n][y + m] != f[n][m]) S = false; } } if (S) { for (int n = 0; n < b; n++) { for (int m = 0; m < b; m++) { if (f[n][m] != -1) { V.push_back(P(x + n, y + m)); break; } } } } } } } if (V.size() >= 1) { P Q = P(1 << 29, 1 << 29); for (P i : V) { Q = min(Q, i); } cout << Q.second + 1 << " " << Q.first + 1 << endl; } else { puts("NA"); } } }
replace
13
16
13
16
TLE
p00209
C++
Runtime Error
#include <iostream> using namespace std; int n, m, a[111][111], b[55][55]; void rotate() { int z[55][55]; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { z[i][j] = b[m - j - 1][i]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { b[i][j] = z[i][j]; } } } int main() { while (cin >> n >> m, n | m) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { cin >> b[i][j]; } } int rx = n, ry = n; for (int i = 0; i < 4; i++) { int sx = m; for (int j = m - 1; j >= 0; j--) { if (b[0][j] != -1) sx = j; } for (int j = 0; j <= n - m; j++) { for (int k = 0; k <= n - m; k++) { bool flag = true; for (int ey = j; ey < j + m; ey++) { for (int ex = k; ex < k + m; ex++) { int v = b[ey - j][ex - k]; if (v != -1 && a[ey][ex] != v) flag = false; } } if (flag && (ry > j || (ry == j && rx > k + sx))) { rx = k + sx, ry = j; } } } rotate(); } if (rx == n && ry == n) cout << "NA" << endl; else cout << rx + 1 << ' ' << ry + 1 << endl; } return 0; }
#include <iostream> using namespace std; int n, m, a[111][111], b[55][55]; void rotate() { int z[55][55]; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { z[i][j] = b[m - j - 1][i]; } } for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { b[i][j] = z[i][j]; } } } int main() { while (cin >> n >> m, n | m) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { cin >> b[i][j]; } } int rx = n, ry = n; for (int i = 0; i < 4; i++) { int sx = m; for (int j = m - 1; j >= 0; j--) { if (b[0][j] != -1) sx = j; } for (int j = 0; j <= n - m; j++) { for (int k = 0; k <= n - m; k++) { bool flag = true; for (int ey = j; ey < j + m; ey++) { for (int ex = k; ex < k + m; ex++) { int v = b[ey - j][ex - k]; if (v != -1 && a[ey][ex] != v) flag = false; } } if (flag && (ry > j || (ry == j && rx > k + sx))) { rx = k + sx, ry = j; } } } rotate(); } if (rx == n && ry == n) cout << "NA" << endl; else cout << rx + 1 << ' ' << ry + 1 << endl; } return 0; }
replace
10
12
10
12
0
p00211
C++
Time Limit Exceeded
// 26 #include <iostream> using namespace std; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } int main() { for (int n; cin >> n, n;) { int d[10], v[10]; for (int i = 0; i < n; i++) { cin >> d[i] >> v[i]; } int m[9]; for (int i = 0; i < n - 1; i++) { for (long long j = 1;; j++) { if (j * d[0] * v[i + 1] % (v[0] * d[i + 1]) == 0) { m[i] = j; break; } } } long long f = 1; for (int i = 0; i < n - 1; i++) { f = f * m[i] / gcd(f, m[i]); } cout << f << endl; for (int i = 1; i < n; i++) { cout << f * d[0] * v[i] / v[0] / d[i] << endl; } } return 0; }
// 26 #include <iostream> using namespace std; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } int main() { for (int n; cin >> n, n;) { int d[10], v[10]; for (int i = 0; i < n; i++) { cin >> d[i] >> v[i]; } int m[9]; for (int i = 0; i < n - 1; i++) { m[i] = d[i + 1] * v[0] / gcd(d[0] * v[i + 1], d[i + 1] * v[0]); } long long f = 1; for (int i = 0; i < n - 1; i++) { f = f * m[i] / gcd(f, m[i]); } cout << f << endl; for (int i = 1; i < n; i++) { cout << f * d[0] * v[i] / v[0] / d[i] << endl; } } return 0; }
replace
15
21
15
16
TLE
p00211
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; ll gcd(ll a, ll b) { if (a == 0 || b == 0) return 0; while (a != b) { if (a > b) a -= b; else b -= a; } return a; } ll lcm(ll a, ll b) { if (a == 0 || b == 0) return 0; return ((a / gcd(a, b) * b)); } int main() { int n; while (cin >> n && n) { vector<pair<ll, ll>> v; rep(i, n) { ll a, b; cin >> a >> b; v.push_back(make_pair(a, b)); } rep(i, n) { ll temp = gcd(v[i].first, v[i].second); v[i].first /= temp; v[i].second /= temp; } ll t, t2; rep(i, n) { if (i == 0) { t = v[i].first; t2 = v[i].second; } else { t = lcm(t, v[i].first); t2 = gcd(t2, v[i].second); } } rep(i, n) { ll ans = t * v[i].second; ans /= t2; ans /= v[i].first; cout << ans << endl; } } return 0; }
#include <algorithm> #include <iostream> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; ll gcd(ll a, ll b) { if (b == 0) return a; else return gcd(b, a % b); } ll lcm(ll a, ll b) { if (a == 0 || b == 0) return 0; return ((a / gcd(a, b) * b)); } int main() { int n; while (cin >> n && n) { vector<pair<ll, ll>> v; rep(i, n) { ll a, b; cin >> a >> b; v.push_back(make_pair(a, b)); } rep(i, n) { ll temp = gcd(v[i].first, v[i].second); v[i].first /= temp; v[i].second /= temp; } ll t, t2; rep(i, n) { if (i == 0) { t = v[i].first; t2 = v[i].second; } else { t = lcm(t, v[i].first); t2 = gcd(t2, v[i].second); } } rep(i, n) { ll ans = t * v[i].second; ans /= t2; ans /= v[i].first; cout << ans << endl; } } return 0; }
replace
12
22
12
16
TLE
p00212
C++
Memory Limit Exceeded
#include <cstring> #include <iostream> #include <queue> #include <vector> using namespace std; struct node { int cost; int c; int now; node(int C, int cc, int n) : cost(C), c(cc), now(n) {} bool operator>(const node &e) const { return cost > e.cost; } }; int main() { int c, n, m, s, d; for (; cin >> c >> n >> m >> s >> d, c;) { int edge[n][n]; bool close[c + 1][n]; memset(edge, -1, sizeof(edge)); memset(close, false, sizeof(close)); for (int i = 0; i < m; i++) { int a, b, f; cin >> a >> b >> f; a--; b--; edge[a][b] = f; edge[b][a] = f; } s--; d--; priority_queue<node, vector<node>, greater<node>> qu; qu.push(node(0, c, s)); while (!qu.empty()) { node tmp = qu.top(); // cout<<tmp.now<<" "<<tmp.cost<<" "<<tmp.c<<endl; qu.pop(); close[tmp.c][tmp.now] = true; if (tmp.now == d) { cout << tmp.cost << endl; break; } for (int i = 0; i < n; i++) { if (edge[tmp.now][i] != -1) { if (!close[tmp.c][i]) qu.push(node(edge[tmp.now][i] + tmp.cost, tmp.c, i)); if (tmp.c > 0 && !close[tmp.c - 1][i]) qu.push(node(edge[tmp.now][i] / 2 + tmp.cost, tmp.c - 1, i)); } } } } }
#include <cstring> #include <iostream> #include <queue> #include <vector> using namespace std; struct node { int cost; int c; int now; node(int C, int cc, int n) : cost(C), c(cc), now(n) {} bool operator>(const node &e) const { return cost > e.cost; } }; int main() { int c, n, m, s, d; for (; cin >> c >> n >> m >> s >> d, c;) { int edge[n][n]; bool close[c + 1][n]; memset(edge, -1, sizeof(edge)); memset(close, false, sizeof(close)); for (int i = 0; i < m; i++) { int a, b, f; cin >> a >> b >> f; a--; b--; edge[a][b] = f; edge[b][a] = f; } s--; d--; priority_queue<node, vector<node>, greater<node>> qu; qu.push(node(0, c, s)); while (!qu.empty()) { node tmp = qu.top(); // cout<<tmp.now<<" "<<tmp.cost<<" "<<tmp.c<<endl; qu.pop(); if (close[tmp.c][tmp.now]) continue; close[tmp.c][tmp.now] = true; if (tmp.now == d) { cout << tmp.cost << endl; break; } for (int i = 0; i < n; i++) { if (edge[tmp.now][i] != -1) { if (!close[tmp.c][i]) qu.push(node(edge[tmp.now][i] + tmp.cost, tmp.c, i)); if (tmp.c > 0 && !close[tmp.c - 1][i]) qu.push(node(edge[tmp.now][i] / 2 + tmp.cost, tmp.c - 1, i)); } } } } }
insert
38
38
38
40
MLE
p00212
C++
Runtime Error
#include <algorithm> #include <iostream> #include <queue> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define INF 1 << 28 using namespace std; typedef pair<int, int> P; struct edge { int to; int cost; edge(int to, int cost) : to(to), cost(cost) {} }; int d[101][11]; vector<edge> G[101]; void dijkstra(int s, int n, int ticket) { priority_queue<P, vector<P>, greater<P>> que; rep(i, n) d[i][ticket] = INF; d[s][ticket] = 0; que.push(P(0, s)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (d[v][ticket] < p.first) continue; rep(i, G[v].size()) { edge e = G[v][i]; int temp; if (ticket == 0) { temp = d[v][ticket] + e.cost; } else { temp = min(d[v][ticket] + e.cost, d[v][ticket - 1] + e.cost / 2); } if (d[e.to][ticket] > temp) { d[e.to][ticket] = temp; que.push(P(d[e.to][ticket], e.to)); } } } } int main() { int c, n, m, s, t; while (cin >> c >> n >> m >> s >> t) { if (c == 0 && n == 0 && m == 0 && s == 0 && t == 0) break; s--; t--; rep(i, 100) G[i].clear(); rep(i, m) { int a, b, cost; cin >> a >> b >> cost; a--; b--; edge e(b, cost); G[a].push_back(e); edge e2(a, cost); G[b].push_back(e2); } rep(i, n) dijkstra(s, n, i); cout << d[t][c] << endl; } return 0; }
#include <algorithm> #include <iostream> #include <queue> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define INF 1 << 28 using namespace std; typedef pair<int, int> P; struct edge { int to; int cost; edge(int to, int cost) : to(to), cost(cost) {} }; int d[101][11]; vector<edge> G[101]; void dijkstra(int s, int n, int ticket) { priority_queue<P, vector<P>, greater<P>> que; rep(i, n) d[i][ticket] = INF; d[s][ticket] = 0; que.push(P(0, s)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (d[v][ticket] < p.first) continue; rep(i, G[v].size()) { edge e = G[v][i]; int temp; if (ticket == 0) { temp = d[v][ticket] + e.cost; } else { temp = min(d[v][ticket] + e.cost, d[v][ticket - 1] + e.cost / 2); } if (d[e.to][ticket] > temp) { d[e.to][ticket] = temp; que.push(P(d[e.to][ticket], e.to)); } } } } int main() { int c, n, m, s, t; while (cin >> c >> n >> m >> s >> t) { if (c == 0 && n == 0 && m == 0 && s == 0 && t == 0) break; s--; t--; rep(i, 100) G[i].clear(); rep(i, m) { int a, b, cost; cin >> a >> b >> cost; a--; b--; edge e(b, cost); G[a].push_back(e); edge e2(a, cost); G[b].push_back(e2); } rep(i, c + 1) dijkstra(s, n, i); cout << d[t][c] << endl; } return 0; }
replace
78
79
78
79
0
p00212
C++
Runtime Error
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) #define foreach(c, i) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) struct edge { int to, cost; }; struct state { int cur, card, cost; bool operator<(const state &lhs) const { return cost > lhs.cost; } }; typedef pair<int, int> P; const int kInf = 1 << 28; int C, N, M, S, D; vector<edge> es[102]; int d[102][12]; int solve() { // init for (int i = 0; i < 102; ++i) for (int j = 0; j < 12; ++j) d[i][j] = kInf; // dijkstra priority_queue<state> Q; d[S][C] = 0; Q.push((state){S, C, 0}); while (!Q.empty()) { state s = Q.top(); Q.pop(); if (d[s.cur][s.card] < s.cost) continue; rep(i, es[s.cur].size()) { const edge &e = es[s.cur][i]; // without ticket if (d[e.to][s.card] > s.cost + e.cost) { d[e.to][s.card] = s.cost + e.cost; Q.push((state){e.to, s.card, s.cost + e.cost}); } // use ticket if (s.card > 0 && d[e.to][s.card - 1] > s.cost + (e.cost / 2)) { d[e.to][s.card - 1] = s.cost + (e.cost / 2); Q.push((state){e.to, s.card - 1, s.cost + (e.cost / 2)}); } } } return *min_element(d[D], d[D] + 12); } int main() { while (scanf("%d%d%d%d%d", &C, &N, &M, &S, &D), C) { --S; --D; rep(i, N) { es[i].clear(); } rep(i, N) { int a, b, f; scanf("%d%d%d", &a, &b, &f); --a; --b; es[a].push_back((edge){b, f}); es[b].push_back((edge){a, f}); } printf("%d\n", solve()); } return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) #define foreach(c, i) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) struct edge { int to, cost; }; struct state { int cur, card, cost; bool operator<(const state &lhs) const { return cost > lhs.cost; } }; typedef pair<int, int> P; const int kInf = 1 << 28; int C, N, M, S, D; vector<edge> es[102]; int d[102][12]; int solve() { // init for (int i = 0; i < 102; ++i) for (int j = 0; j < 12; ++j) d[i][j] = kInf; // dijkstra priority_queue<state> Q; d[S][C] = 0; Q.push((state){S, C, 0}); while (!Q.empty()) { state s = Q.top(); Q.pop(); if (d[s.cur][s.card] < s.cost) continue; rep(i, es[s.cur].size()) { const edge &e = es[s.cur][i]; // without ticket if (d[e.to][s.card] > s.cost + e.cost) { d[e.to][s.card] = s.cost + e.cost; Q.push((state){e.to, s.card, s.cost + e.cost}); } // use ticket if (s.card > 0 && d[e.to][s.card - 1] > s.cost + (e.cost / 2)) { d[e.to][s.card - 1] = s.cost + (e.cost / 2); Q.push((state){e.to, s.card - 1, s.cost + (e.cost / 2)}); } } } return *min_element(d[D], d[D] + 12); } int main() { while (scanf("%d%d%d%d%d", &C, &N, &M, &S, &D), C) { --S; --D; rep(i, N) { es[i].clear(); } rep(i, M) { int a, b, f; scanf("%d%d%d", &a, &b, &f); --a; --b; es[a].push_back((edge){b, f}); es[b].push_back((edge){a, f}); } printf("%d\n", solve()); } return 0; }
replace
74
75
74
75
0
p00212
C++
Time Limit Exceeded
#include <functional> #include <iostream> #include <queue> #include <vector> using namespace std; const int INF = 10000000; int city[101][101]; int solve(int s, int d, int c, int n) { int dist[11][101]; int history[101]; for (int i = 0; i < 11; i++) { for (int j = 1; j < 101; j++) { dist[i][j] = INF; } } for (int i = 1; i < 101; i++) { history[i] = 0; } for (int i = 0; i < 11; i++) dist[i][s] = 0; typedef pair<int, int> data; typedef pair<int, data> graph; priority_queue<graph, vector<graph>, greater<graph>> q; q.push(graph(0, data(s, c))); while (!q.empty()) { graph a = q.top(); int u = a.second.first; int uc = a.second.second; q.pop(); history[u] = 1; for (int i = 1; i <= n; i++) { if (dist[uc][i] > dist[uc][u] + city[u][i]) { dist[uc][i] = dist[uc][u] + city[u][i]; q.push(graph(dist[uc][i], data(i, uc))); } if (uc && dist[uc - 1][i] > dist[uc][i] + city[u][i] / 2) { dist[uc - 1][i] = dist[uc][u] + city[u][i] / 2; q.push(graph(dist[uc - 1][i], data(i, uc - 1))); } } } int answer = INF; for (int i = 0; i < 11; i++) answer = min(dist[i][d], answer); return answer; } int main(int argc, char const *argv[]) { int c, n, m, s, d; while (cin >> c >> n >> m >> s >> d && c != 0) { int a, b, f; for (int i = 1; i < 101; i++) { for (int j = 1; j < 101; j++) { city[i][j] = INF * 2; } } for (int i = 0; i < m; i++) { cin >> a >> b >> f; city[a][b] = f; city[b][a] = f; } cout << solve(s, d, c, n) << endl; } return 0; }
#include <functional> #include <iostream> #include <queue> #include <vector> using namespace std; const int INF = 10000000; int city[101][101]; int solve(int s, int d, int c, int n) { int dist[11][101]; int history[101]; for (int i = 0; i < 11; i++) { for (int j = 1; j < 101; j++) { dist[i][j] = INF; } } for (int i = 1; i < 101; i++) { history[i] = 0; } for (int i = 0; i < 11; i++) dist[i][s] = 0; typedef pair<int, int> data; typedef pair<int, data> graph; priority_queue<graph, vector<graph>, greater<graph>> q; q.push(graph(0, data(s, c))); while (!q.empty()) { graph a = q.top(); int u = a.second.first; int uc = a.second.second; q.pop(); history[u] = 1; for (int i = 1; i <= n; i++) { if (dist[uc][i] > dist[uc][u] + city[u][i]) { dist[uc][i] = dist[uc][u] + city[u][i]; q.push(graph(dist[uc][i], data(i, uc))); } if (uc && dist[uc - 1][i] > dist[uc][u] + city[u][i] / 2) { dist[uc - 1][i] = dist[uc][u] + city[u][i] / 2; q.push(graph(dist[uc - 1][i], data(i, uc - 1))); } } } int answer = INF; for (int i = 0; i < 11; i++) answer = min(dist[i][d], answer); return answer; } int main(int argc, char const *argv[]) { int c, n, m, s, d; while (cin >> c >> n >> m >> s >> d && c != 0) { int a, b, f; for (int i = 1; i < 101; i++) { for (int j = 1; j < 101; j++) { city[i][j] = INF * 2; } } for (int i = 0; i < m; i++) { cin >> a >> b >> f; city[a][b] = f; city[b][a] = f; } cout << solve(s, d, c, n) << endl; } return 0; }
replace
48
49
48
49
TLE
p00212
C++
Runtime Error
#include <iostream> #include <queue> #include <vector> using namespace std; struct state { int node; int ticket; int cost; state(int n, int t, int c) : node(n), ticket(t), cost(c) {} bool operator<(const state &s) const { return cost > s.cost; } }; int main() { int c, n, m, s, d; while (cin >> c >> n >> m >> s >> d && c != 0) { --s; --d; vector<vector<pair<int, int>>> g(n); for (int i = 0; i < m; i++) { int a, b, f; cin >> a >> b >> f; --a; --b; g[a].push_back(make_pair(b, f)); g[b].push_back(make_pair(a, f)); } priority_queue<state> q; q.push(state(s, c, 0)); vector<vector<int>> city(n, vector<int>(c, 1000000)); city[s][c] = 0; while (!q.empty()) { const state s = q.top(); q.pop(); if (s.node == d) { cout << s.cost << endl; break; } city[s.node][s.ticket] = s.cost; for (vector<pair<int, int>>::const_iterator it(g[s.node].begin()); it != g[s.node].end(); ++it) { const int next = it->first; const int cost = s.cost + it->second; if (cost < city[next][s.ticket]) { city[next][s.ticket] = cost; q.push(state(next, s.ticket, cost)); } if (s.ticket > 0) { const int cost2 = s.cost + it->second / 2; if (cost2 < city[next][s.ticket - 1]) { city[next][s.ticket - 1] = cost2; q.push(state(next, s.ticket - 1, cost2)); } } } } } return 0; }
#include <iostream> #include <queue> #include <vector> using namespace std; struct state { int node; int ticket; int cost; state(int n, int t, int c) : node(n), ticket(t), cost(c) {} bool operator<(const state &s) const { return cost > s.cost; } }; int main() { int c, n, m, s, d; while (cin >> c >> n >> m >> s >> d && c != 0) { --s; --d; vector<vector<pair<int, int>>> g(n); for (int i = 0; i < m; i++) { int a, b, f; cin >> a >> b >> f; --a; --b; g[a].push_back(make_pair(b, f)); g[b].push_back(make_pair(a, f)); } priority_queue<state> q; q.push(state(s, c, 0)); vector<vector<int>> city(n, vector<int>(c + 1, 1000000)); city[s][c] = 0; while (!q.empty()) { const state s = q.top(); q.pop(); if (s.node == d) { cout << s.cost << endl; break; } city[s.node][s.ticket] = s.cost; for (vector<pair<int, int>>::const_iterator it(g[s.node].begin()); it != g[s.node].end(); ++it) { const int next = it->first; const int cost = s.cost + it->second; if (cost < city[next][s.ticket]) { city[next][s.ticket] = cost; q.push(state(next, s.ticket, cost)); } if (s.ticket > 0) { const int cost2 = s.cost + it->second / 2; if (cost2 < city[next][s.ticket - 1]) { city[next][s.ticket - 1] = cost2; q.push(state(next, s.ticket - 1, cost2)); } } } } } return 0; }
replace
30
31
30
31
0
p00212
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <queue> #define mp make_pair using namespace std; typedef pair<int, int> pii; const int inf = 1 << 29; int adj[100][100], memo[100][11]; bool visited[100][11]; int main() { for (int c, m, n, src, dst; scanf("%d%d%d%d%d", &c, &m, &n, &src, &dst), c;) { src--, dst--; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) adj[i][j] = (i == j ? 0 : inf); for (int i = 0; i < n; i++) for (int j = 0; j <= c; j++) memo[i][j] = inf, visited[i][j] = false; for (int i = 0; i < m; i++) { int a, b, fee; scanf("%d%d%d", &a, &b, &fee); a--, b--; adj[a][b] = adj[b][a] = fee; } memo[src][0] = 0; priority_queue<pair<int, pii>> pq; pq.push(mp(0, mp(src, 0))); while (!pq.empty()) { pair<int, pii> a = pq.top(); pq.pop(); int fee = -a.first; int u = a.second.first; int tic = a.second.second; if (u == dst) break; if (visited[u][tic]) continue; visited[u][tic] = true; for (int v = 0; v < n; v++) { if (!visited[v][tic] && fee + adj[u][v] < memo[v][tic]) { memo[v][tic] = fee + adj[u][v]; pq.push(mp(-memo[v][tic], mp(v, tic))); } if (tic < c && !visited[v][tic + 1] && fee + adj[u][v] / 2 < memo[v][tic + 1]) { memo[v][tic + 1] = fee + adj[u][v] / 2; pq.push(mp(-memo[v][tic + 1], mp(v, tic + 1))); } } } int feemin = inf; for (int i = 0; i <= c; i++) feemin = min(feemin, memo[dst][i]); printf("%d\n", feemin); } return 0; }
#include <algorithm> #include <cstdio> #include <queue> #define mp make_pair using namespace std; typedef pair<int, int> pii; const int inf = 1 << 29; int adj[100][100], memo[100][11]; bool visited[100][11]; int main() { for (int c, n, m, src, dst; scanf("%d%d%d%d%d", &c, &n, &m, &src, &dst), c;) { src--, dst--; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) adj[i][j] = (i == j ? 0 : inf); for (int i = 0; i < n; i++) for (int j = 0; j <= c; j++) memo[i][j] = inf, visited[i][j] = false; for (int i = 0; i < m; i++) { int a, b, fee; scanf("%d%d%d", &a, &b, &fee); a--, b--; adj[a][b] = adj[b][a] = fee; } memo[src][0] = 0; priority_queue<pair<int, pii>> pq; pq.push(mp(0, mp(src, 0))); while (!pq.empty()) { pair<int, pii> a = pq.top(); pq.pop(); int fee = -a.first; int u = a.second.first; int tic = a.second.second; if (u == dst) break; if (visited[u][tic]) continue; visited[u][tic] = true; for (int v = 0; v < n; v++) { if (!visited[v][tic] && fee + adj[u][v] < memo[v][tic]) { memo[v][tic] = fee + adj[u][v]; pq.push(mp(-memo[v][tic], mp(v, tic))); } if (tic < c && !visited[v][tic + 1] && fee + adj[u][v] / 2 < memo[v][tic + 1]) { memo[v][tic + 1] = fee + adj[u][v] / 2; pq.push(mp(-memo[v][tic + 1], mp(v, tic + 1))); } } } int feemin = inf; for (int i = 0; i <= c; i++) feemin = min(feemin, memo[dst][i]); printf("%d\n", feemin); } return 0; }
replace
15
16
15
16
0
p00212
C++
Time Limit Exceeded
#include <iostream> #include <vector> using namespace std; #define INF 100000000 struct edge { int to, cost; }; int main() { int c, n, m, s, d; while (cin >> c >> n >> m >> s >> d) { if (c == 0 && n == 0 && m == 0 && s == 0 && d == 0) { break; } vector<edge> ff[101]; vector<vector<int>> ddd(n + 1, vector<int>(n + 1, INF)); int a, b, f; for (int i = 0; i < m; i++) { cin >> a >> b >> f; edge e; e.to = b; e.cost = f; ff[a].push_back(e); e.to = a; ff[b].push_back(e); ddd[a][b] = ddd[b][a] = f; } for (int i = 1; i <= n; i++) { ddd[i][i] = 0; } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { ddd[i][j] = min(ddd[i][j], ddd[i][k] + ddd[k][j]); } } } vector<int> isd[101]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (ddd[i][j] != INF) { isd[i].push_back(j); } } } vector<vector<vector<int>>> dp( n + 1, vector<vector<int>>(n + 1, vector<int>(c + 1, INF))); for (int i = 1; i <= n; i++) { dp[s][i][0] = ddd[s][i]; } for (int k = 1; k <= c; k++) { for (int p = 0; p < isd[s].size(); p++) { int pp = isd[s][p]; dp[s][pp][k] = dp[s][pp][k - 1]; for (int i = 0; i < isd[s].size(); i++) { int ii = isd[s][i]; for (int j = 0; j < isd[ii].size(); j++) { int jj = isd[ii][j]; for (int q = 0; q < ff[jj].size(); q++) { int qq = ff[jj][q].to; if (ddd[qq][pp] == INF) continue; if (dp[s][pp][k] > dp[s][ii][k - 1] + ddd[ii][jj] + (ff[jj][q].cost / 2) + ddd[qq][pp]) { dp[s][pp][k] = dp[s][ii][k - 1] + ddd[ii][jj] + (ff[jj][q].cost / 2) + ddd[qq][pp]; } } } } } } cout << dp[s][d][c] << endl; } return 0; }
#include <iostream> #include <vector> using namespace std; #define INF 100000000 struct edge { int to, cost; }; int main() { int c, n, m, s, d; while (cin >> c >> n >> m >> s >> d) { if (c == 0 && n == 0 && m == 0 && s == 0 && d == 0) { break; } vector<edge> ff[101]; vector<vector<int>> ddd(n + 1, vector<int>(n + 1, INF)); int a, b, f; for (int i = 0; i < m; i++) { cin >> a >> b >> f; edge e; e.to = b; e.cost = f; ff[a].push_back(e); e.to = a; ff[b].push_back(e); ddd[a][b] = ddd[b][a] = f; } for (int i = 1; i <= n; i++) { ddd[i][i] = 0; } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { ddd[i][j] = min(ddd[i][j], ddd[i][k] + ddd[k][j]); } } } vector<int> isd[101]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (ddd[i][j] != INF) { isd[i].push_back(j); } } } vector<vector<vector<int>>> dp( n + 1, vector<vector<int>>(n + 1, vector<int>(c + 1, INF))); for (int i = 1; i <= n; i++) { dp[s][i][0] = ddd[s][i]; } for (int k = 1; k <= c; k++) { for (int p = 0; p < isd[s].size(); p++) { int pp = isd[s][p]; dp[s][pp][k] = dp[s][pp][k - 1]; for (int i = 0; i < isd[s].size(); i++) { int ii = isd[s][i]; for (int q = 0; q < ff[ii].size(); q++) { int qq = ff[ii][q].to; if (ddd[qq][pp] == INF) continue; if (dp[s][pp][k] > dp[s][ii][k - 1] + (ff[ii][q].cost / 2) + ddd[qq][pp]) { dp[s][pp][k] = dp[s][ii][k - 1] + (ff[ii][q].cost / 2) + ddd[qq][pp]; } } } } } cout << dp[s][d][c] << endl; } return 0; }
replace
59
70
59
68
TLE
p00212
C++
Runtime Error
#include <algorithm> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <vector> const int MAX_V = 101; const int INF = 0x3f3f3f3f; //??? T???cap??????rev????????°???????????§?????£??????????????? struct Edge { int to; int cost; Edge(int to_, int cost_) : to(to_), cost(cost_) {} }; std::vector<Edge> G[MAX_V]; //??£??\????????? int d[11][MAX_V]; //[?????¨???????????±??????????????°][?????????] int pre[MAX_V]; void AddEdge(int from, int to, int cost) { G[from].push_back(Edge(to, cost)); } void ClearEdge() { for (int i = 0; i < MAX_V; ++i) G[i].clear(); memset(d, 0, sizeof(d)); memset(pre, -1, sizeof(pre)); } void Dijkstra(int c, int s) { typedef std::pair<int, int> P; // first:???????????¢, second:???????????? std::priority_queue<P, std::vector<P>, std::greater<P>> q; std::fill(d[c], d[c] + MAX_V, INF); memset(pre, -1, sizeof(pre)); d[c][s] = 0; q.push(P(0, s)); while (!q.empty()) { P p = q.top(); q.pop(); int from = p.second; if (d[c][from] < p.first) continue; for (Edge e : G[from]) { int dmin; if (c == 0) dmin = d[c][from] + e.cost; else dmin = std::min(d[c][from] + e.cost, d[c - 1][from] + e.cost / 2); if (d[c][e.to] > dmin) { d[c][e.to] = dmin; q.push(P(d[c][e.to], e.to)); pre[e.to] = from; } } } } std::vector<int> GetPath(int t) { std::vector<int> path; for (; t != -1; t = pre[t]) path.push_back(t); std::reverse(path.begin(), path.end()); return path; } using namespace std; int main(void) { int c, n, m, s, g; while (1) { cin >> c >> n >> m >> s >> g; if (c == n && n == m && m == s && s == g && g == 0) return 0; int a, b, f; for (int i = 0; i < n; ++i) { cin >> a >> b >> f; AddEdge(a, b, f); AddEdge(b, a, f); } for (int i = 0; i <= c; ++i) { Dijkstra(i, s); } cout << d[c][g] << endl; ClearEdge(); } }
#include <algorithm> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <vector> const int MAX_V = 101; const int INF = 0x3f3f3f3f; //??? T???cap??????rev????????°???????????§?????£??????????????? struct Edge { int to; int cost; Edge(int to_, int cost_) : to(to_), cost(cost_) {} }; std::vector<Edge> G[MAX_V]; //??£??\????????? int d[11][MAX_V]; //[?????¨???????????±??????????????°][?????????] int pre[MAX_V]; void AddEdge(int from, int to, int cost) { G[from].push_back(Edge(to, cost)); } void ClearEdge() { for (int i = 0; i < MAX_V; ++i) G[i].clear(); memset(d, 0, sizeof(d)); memset(pre, -1, sizeof(pre)); } void Dijkstra(int c, int s) { typedef std::pair<int, int> P; // first:???????????¢, second:???????????? std::priority_queue<P, std::vector<P>, std::greater<P>> q; std::fill(d[c], d[c] + MAX_V, INF); memset(pre, -1, sizeof(pre)); d[c][s] = 0; q.push(P(0, s)); while (!q.empty()) { P p = q.top(); q.pop(); int from = p.second; if (d[c][from] < p.first) continue; for (Edge e : G[from]) { int dmin; if (c == 0) dmin = d[c][from] + e.cost; else dmin = std::min(d[c][from] + e.cost, d[c - 1][from] + e.cost / 2); if (d[c][e.to] > dmin) { d[c][e.to] = dmin; q.push(P(d[c][e.to], e.to)); pre[e.to] = from; } } } } std::vector<int> GetPath(int t) { std::vector<int> path; for (; t != -1; t = pre[t]) path.push_back(t); std::reverse(path.begin(), path.end()); return path; } using namespace std; int main(void) { int c, n, m, s, g; while (1) { cin >> c >> n >> m >> s >> g; if (c == n && n == m && m == s && s == g && g == 0) return 0; int a, b, f; for (int i = 0; i < m; ++i) { cin >> a >> b >> f; AddEdge(a, b, f); AddEdge(b, a, f); } for (int i = 0; i <= c; ++i) { Dijkstra(i, s); } cout << d[c][g] << endl; ClearEdge(); } }
replace
81
82
81
82
0
p00212
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #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> #define ALL(A) (A).begin(), (A).end() #define DUMP(A) cout << #A << "=" << (A) << endl #define SIZE(A) (int)((A).size()) using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; int main() { int c, n, m, s, d; for (;;) { scanf("%d%d%d%d%d", &c, &n, &m, &s, &d); if (c == 0 && n == 0 && m == 0 && s == 0 && d == 0) break; s--; d--; int path[100][100]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { path[i][j] = -1; } } int fr, to, co; for (int i = 0; i < m; i++) { scanf("%d%d%d", &fr, &to, &co); fr--; to--; path[fr][to] = path[to][fr] = co; } int dist[100][11]; for (int i = 0; i < n; i++) { for (int j = 0; j <= c; j++) { dist[i][j] = -1; } } priority_queue<PP> pque; pque.push(PP(0, P(0, s))); dist[s][0] = 0; while (!pque.empty()) { PP now = pque.top(); pque.pop(); int ndi = now.first, ncar = now.second.first, nat = now.second.second; if (dist[nat][ncar] != -1 && dist[nat][ncar] < ndi) continue; for (int i = 0; i < n; i++) { if (path[nat][i] != -1 && (dist[i][ncar] == -1 || ndi + path[nat][i] < dist[i][ncar])) { pque.push(PP(ndi + path[nat][i], P(ncar, i))); dist[i][ncar] = ndi + path[nat][i]; } if (path[nat][i] != -1 && ncar + 1 <= c && (dist[i][ncar + 1] == -1 || ndi + path[nat][i] / 2 < dist[i][ncar + 1])) { pque.push(PP(ndi + path[nat][i] / 2, P(ncar + 1, i))); dist[i][ncar + 1] = ndi + path[nat][i] / 2; } } } int mi = INT_MAX; for (int i = 0; i <= c; i++) { if (dist[d][i] != -1 && dist[d][i] < mi) mi = dist[d][i]; } printf("%d\n", mi); } }
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #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> #define ALL(A) (A).begin(), (A).end() #define DUMP(A) cout << #A << "=" << (A) << endl #define SIZE(A) (int)((A).size()) using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; int main() { int c, n, m, s, d; for (;;) { scanf("%d%d%d%d%d", &c, &n, &m, &s, &d); if (c == 0 && n == 0 && m == 0 && s == 0 && d == 0) break; s--; d--; int path[100][100]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { path[i][j] = -1; } } int fr, to, co; for (int i = 0; i < m; i++) { scanf("%d%d%d", &fr, &to, &co); fr--; to--; path[fr][to] = path[to][fr] = co; } int dist[100][11]; for (int i = 0; i < n; i++) { for (int j = 0; j <= c; j++) { dist[i][j] = -1; } } priority_queue<PP, vector<PP>, greater<PP>> pque; pque.push(PP(0, P(0, s))); dist[s][0] = 0; while (!pque.empty()) { PP now = pque.top(); pque.pop(); int ndi = now.first, ncar = now.second.first, nat = now.second.second; if (dist[nat][ncar] != -1 && dist[nat][ncar] < ndi) continue; for (int i = 0; i < n; i++) { if (path[nat][i] != -1 && (dist[i][ncar] == -1 || ndi + path[nat][i] < dist[i][ncar])) { pque.push(PP(ndi + path[nat][i], P(ncar, i))); dist[i][ncar] = ndi + path[nat][i]; } if (path[nat][i] != -1 && ncar + 1 <= c && (dist[i][ncar + 1] == -1 || ndi + path[nat][i] / 2 < dist[i][ncar + 1])) { pque.push(PP(ndi + path[nat][i] / 2, P(ncar + 1, i))); dist[i][ncar + 1] = ndi + path[nat][i] / 2; } } } int mi = INT_MAX; for (int i = 0; i <= c; i++) { if (dist[d][i] != -1 && dist[d][i] < mi) mi = dist[d][i]; } printf("%d\n", mi); } }
replace
56
57
56
57
TLE
p00212
C++
Runtime Error
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #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; #define INF 1e8 struct edge { int to, cost; }; struct P { int from, cost, ticket; bool operator>(const P &p) const { return cost > p.cost; } }; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; int ticket_n, node_n, edge_n, start, goal; int main() { while (cin >> ticket_n >> node_n >> edge_n >> start >> goal, ticket_n | node_n | edge_n | start | goal) { vector<vector<edge>> E(node_n + 1); REP(i, edge_n) { int from, to, cost; cin >> from >> to >> cost; E[from].push_back({to, cost}); E[to].push_back({from, cost}); } vvi G(edge_n + 1, vi(ticket_n + 1, INF)); priority_queue<P, vector<P>, greater<P>> q; q.push({start, 0, ticket_n}); while (!q.empty()) { P p = q.top(); q.pop(); if (p.from == goal) { cout << p.cost << endl; break; } int from = p.from, cost = p.cost, ticket = p.ticket; for (edge e : E[from]) { // use ticket if (G[e.to][ticket] > cost + e.cost) { G[e.to][ticket] = cost + e.cost; q.push({e.to, cost + e.cost, ticket}); } // don't use ticket if (ticket - 1 >= 0) { if (G[e.to][ticket - 1] > cost + e.cost / 2) { G[e.to][ticket - 1] = cost + e.cost / 2; q.push({e.to, cost + e.cost / 2, ticket - 1}); } } } } } }
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #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; #define INF 1e8 struct edge { int to, cost; }; struct P { int from, cost, ticket; bool operator>(const P &p) const { return cost > p.cost; } }; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; int ticket_n, node_n, edge_n, start, goal; int main() { while (cin >> ticket_n >> node_n >> edge_n >> start >> goal, ticket_n | node_n | edge_n | start | goal) { vector<vector<edge>> E(node_n + 1); REP(i, edge_n) { int from, to, cost; cin >> from >> to >> cost; E[from].push_back({to, cost}); E[to].push_back({from, cost}); } vvi G(node_n + 1, vi(ticket_n + 1, INF)); priority_queue<P, vector<P>, greater<P>> q; q.push({start, 0, ticket_n}); while (!q.empty()) { P p = q.top(); q.pop(); if (p.from == goal) { cout << p.cost << endl; break; } int from = p.from, cost = p.cost, ticket = p.ticket; for (edge e : E[from]) { // use ticket if (G[e.to][ticket] > cost + e.cost) { G[e.to][ticket] = cost + e.cost; q.push({e.to, cost + e.cost, ticket}); } // don't use ticket if (ticket - 1 >= 0) { if (G[e.to][ticket - 1] > cost + e.cost / 2) { G[e.to][ticket - 1] = cost + e.cost / 2; q.push({e.to, cost + e.cost / 2, ticket - 1}); } } } } } }
replace
69
70
69
70
0
p00212
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(v) (v).begin(), (v).end() #define fi first #define se second template <typename A, typename B> inline bool chmax(A &a, B b) { if (a < b) { a = b; return 1; } return 0; } template <typename A, typename B> inline bool chmin(A &a, B b) { if (a > b) { a = b; return 1; } return 0; } typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<int, pll> pip; const ll INF = 1ll << 29; const ll MOD = 1000000007; const double EPS = 1e-9; int C, N, M, S, D; vector<pii> g[100]; int d[11][100]; int main() { while (cin >> C >> N >> M >> S >> D, C || N || M || S || D) { S--; D--; REP(i, N) g[i].clear(); REP(i, M) { int a, b, f; scanf("%d %d %d", &a, &b, &f); a--; b--; g[a].push_back(pii(b, f)); g[b].push_back(pii(a, f)); } fill(d[0], d[C + 1], INF); priority_queue<pii, vector<pii>, greater<pii>> pq[11]; pq[0].push(pii(0, S)); d[0][S] = 0; REP(i, C + 1) { while (!pq[i].empty()) { pii now = pq[i].top(); pq[i].pop(); int u = now.se, cost = now.fi; REP(i, g[u].size()) { int v = g[u][i].fi, c = g[u][i].se; if (i < C && chmin(d[i + 1][v], cost + c / 2)) pq[i + 1].push(pii(cost + c / 2, v)); if (chmin(d[i][v], cost + c)) pq[i].push(pii(cost + c, v)); } } } int ans = INF; REP(i, C + 1) chmin(ans, d[i][D]); cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(v) (v).begin(), (v).end() #define fi first #define se second template <typename A, typename B> inline bool chmax(A &a, B b) { if (a < b) { a = b; return 1; } return 0; } template <typename A, typename B> inline bool chmin(A &a, B b) { if (a > b) { a = b; return 1; } return 0; } typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<int, pll> pip; const ll INF = 1ll << 29; const ll MOD = 1000000007; const double EPS = 1e-9; int C, N, M, S, D; vector<pii> g[100]; int d[11][100]; int main() { while (cin >> C >> N >> M >> S >> D, C || N || M || S || D) { S--; D--; REP(i, N) g[i].clear(); REP(i, M) { int a, b, f; scanf("%d %d %d", &a, &b, &f); a--; b--; g[a].push_back(pii(b, f)); g[b].push_back(pii(a, f)); } fill(d[0], d[C + 1], INF); priority_queue<pii, vector<pii>, greater<pii>> pq[11]; pq[0].push(pii(0, S)); d[0][S] = 0; REP(i, C + 1) { while (!pq[i].empty()) { pii now = pq[i].top(); pq[i].pop(); int u = now.se, cost = now.fi; REP(j, g[u].size()) { int v = g[u][j].fi, c = g[u][j].se; if (i < C && chmin(d[i + 1][v], cost + c / 2)) pq[i + 1].push(pii(cost + c / 2, v)); if (chmin(d[i][v], cost + c)) pq[i].push(pii(cost + c, v)); } } } int ans = INF; REP(i, C + 1) chmin(ans, d[i][D]); cout << ans << endl; } return 0; }
replace
64
66
64
66
0
p00212
C++
Runtime Error
#include <cstdio> #include <iostream> #include <queue> #include <vector> #define reep(i, n, m) for (int i = (n); i < (m); i++) #define rep(i, n) reep(i, 0, n) using namespace std; class Edge { public: int to; int cost; Edge(int to, int cost) : to(to), cost(cost) {} }; class Node { public: int distance; int num; int ticket; Node() {} Node(int distance, int num, int ticket) : distance(distance), num(num), ticket(ticket) {} bool operator<(const Node &b) const { return distance > b.distance; } }; vector<Edge> edge[101]; bool flg[101][11]; int c, n, m, s, d; int solve() { priority_queue<Node> Q; Q.push(Node(0, s, 0)); int ans = 1000000; while (!Q.empty()) { Node node = Q.top(); Q.pop(); if (flg[node.num][node.ticket]) { continue; } flg[node.num][node.ticket] = true; // printf("num->%d cost->%d // ticket->%d\n",node.num,node.distance,node.ticket); if (node.num == d) { ans = min(ans, node.distance); } rep(i, edge[node.num].size()) { Edge e = edge[node.num][i]; int cost = node.distance + e.cost; Q.push(Node(cost, e.to, node.ticket)); if (node.ticket < c) { int cost = node.distance + e.cost / 2; Q.push(Node(cost, e.to, node.ticket + 1)); } } } return solve(); } void init() { rep(i, 101) { edge[i].clear(); rep(k, 11) { flg[i][k] = 0; } } } int main() { while (cin >> c >> n >> m >> s >> d, !m == 0) { init(); rep(i, m) { int a, b, c; cin >> a >> b >> c; edge[a].push_back(Edge(b, c)); edge[b].push_back(Edge(a, c)); } cout << solve() << endl; } }
#include <cstdio> #include <iostream> #include <queue> #include <vector> #define reep(i, n, m) for (int i = (n); i < (m); i++) #define rep(i, n) reep(i, 0, n) using namespace std; class Edge { public: int to; int cost; Edge(int to, int cost) : to(to), cost(cost) {} }; class Node { public: int distance; int num; int ticket; Node() {} Node(int distance, int num, int ticket) : distance(distance), num(num), ticket(ticket) {} bool operator<(const Node &b) const { return distance > b.distance; } }; vector<Edge> edge[101]; bool flg[101][11]; int c, n, m, s, d; int solve() { priority_queue<Node> Q; Q.push(Node(0, s, 0)); int ans = 1000000; while (!Q.empty()) { Node node = Q.top(); Q.pop(); if (flg[node.num][node.ticket]) { continue; } flg[node.num][node.ticket] = true; // printf("num->%d cost->%d // ticket->%d\n",node.num,node.distance,node.ticket); if (node.num == d) { ans = min(ans, node.distance); } rep(i, edge[node.num].size()) { Edge e = edge[node.num][i]; int cost = node.distance + e.cost; Q.push(Node(cost, e.to, node.ticket)); if (node.ticket < c) { int cost = node.distance + e.cost / 2; Q.push(Node(cost, e.to, node.ticket + 1)); } } } return ans; } void init() { rep(i, 101) { edge[i].clear(); rep(k, 11) { flg[i][k] = 0; } } } int main() { while (cin >> c >> n >> m >> s >> d, !m == 0) { init(); rep(i, m) { int a, b, c; cin >> a >> b >> c; edge[a].push_back(Edge(b, c)); edge[b].push_back(Edge(a, c)); } cout << solve() << endl; } }
replace
61
62
61
62
-11
p00212
C++
Runtime Error
#include <bits/stdc++.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 mp(a, b) make_pair((a), (b)) #define pb(a) push_back((a)) #define all(x) (x).begin(), (x).end() #define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x)) #define fi first #define se second #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) void _dbg(string) { cout << endl; } template <class H, class... T> void _dbg(string s, H h, T... t) { int l = s.find(','); cout << s.substr(0, l) << " = " << h << ", "; _dbg(s.substr(l + 1), t...); } template <class T, class U> ostream &operator<<(ostream &o, const pair<T, U> &p) { o << "(" << p.fi << "," << p.se << ")"; return o; } template <class T> ostream &operator<<(ostream &o, const vector<T> &v) { o << "["; for (T t : v) { o << t << ","; } o << "]"; return o; } #define INF 1120000000 int main() { using P = pair<int, int>; int c, n, m, s, t; while (cin >> c >> n >> m >> s >> t, c | n | m) { s--; t--; vector<vector<P>> vec(n); rep(i, m) { int a, b, f; cin >> a >> b >> f; a--; b--; vec[a].pb(mp(f, b)); vec[b].pb(mp(f, a)); } vector<vector<int>> d(m, vector<int>(c + 1, INF)); priority_queue<P, vector<P>, greater<P>> pq; pq.push(mp(0, s * (c + 1))); d[s][c] = 0; while (!pq.empty()) { auto p = pq.top(); pq.pop(); int v = p.se / (c + 1); int k = p.se % (c + 1); if (d[v][k] < p.fi) continue; auto go = [&](int nv, int nk, int dist) { if (d[nv][nk] > dist) { pq.push(mp(dist, nv * (c + 1) + nk)); d[nv][nk] = dist; } }; for (auto to : vec[v]) { go(to.se, k, p.fi + to.fi); if (k < c) go(to.se, k + 1, p.fi + to.fi / 2); } } cout << *min_element(all(d[t])) << endl; } return 0; }
#include <bits/stdc++.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 mp(a, b) make_pair((a), (b)) #define pb(a) push_back((a)) #define all(x) (x).begin(), (x).end() #define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x)) #define fi first #define se second #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) void _dbg(string) { cout << endl; } template <class H, class... T> void _dbg(string s, H h, T... t) { int l = s.find(','); cout << s.substr(0, l) << " = " << h << ", "; _dbg(s.substr(l + 1), t...); } template <class T, class U> ostream &operator<<(ostream &o, const pair<T, U> &p) { o << "(" << p.fi << "," << p.se << ")"; return o; } template <class T> ostream &operator<<(ostream &o, const vector<T> &v) { o << "["; for (T t : v) { o << t << ","; } o << "]"; return o; } #define INF 1120000000 int main() { using P = pair<int, int>; int c, n, m, s, t; while (cin >> c >> n >> m >> s >> t, c | n | m) { s--; t--; vector<vector<P>> vec(n); rep(i, m) { int a, b, f; cin >> a >> b >> f; a--; b--; vec[a].pb(mp(f, b)); vec[b].pb(mp(f, a)); } vector<vector<int>> d(n, vector<int>(c + 1, INF)); priority_queue<P, vector<P>, greater<P>> pq; pq.push(mp(0, s * (c + 1))); d[s][c] = 0; while (!pq.empty()) { auto p = pq.top(); pq.pop(); int v = p.se / (c + 1); int k = p.se % (c + 1); if (d[v][k] < p.fi) continue; auto go = [&](int nv, int nk, int dist) { if (d[nv][nk] > dist) { pq.push(mp(dist, nv * (c + 1) + nk)); d[nv][nk] = dist; } }; for (auto to : vec[v]) { go(to.se, k, p.fi + to.fi); if (k < c) go(to.se, k + 1, p.fi + to.fi / 2); } } cout << *min_element(all(d[t])) << endl; } return 0; }
replace
49
50
49
50
0
p00212
C++
Runtime Error
// AOJ 0212 #define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <cstdio> #include <functional> #include <queue> #include <vector> #define rep(i, a) for (int i = 0; i != (a); ++i) typedef std::pair<int, int> P; typedef std::pair<P, int> PP; const int INF = 1 << 30; int c, n, m, s, d; std::vector<P> G[100]; int dist[100][11]; int dijkstra() { std::fill((int *)dist, (int *)(dist + 100), INF); dist[s][0] = 0; std::priority_queue<PP, std::vector<PP>, std::greater<PP>> pque; pque.push(PP(P(s, 0), 0)); while (!pque.empty()) { PP p = pque.top(); pque.pop(); int v = p.first.first, cost = p.first.second, tic = p.second; if (dist[v][tic] < cost) continue; rep(i, G[v].size()) { P &e = G[v][i]; if (tic < c && dist[e.first][tic + 1] > dist[v][tic] + e.second / 2) { dist[e.first][tic + 1] = dist[v][tic] + e.second / 2; pque.push(PP(P(e.first, dist[e.first][tic + 1]), tic + 1)); } if (dist[e.first][tic] > dist[v][tic] + e.second) { dist[e.first][tic] = dist[v][tic] + e.second; pque.push(PP(P(e.first, dist[e.first][tic]), tic)); } } } return *std::min_element(dist[d], dist[d] + 11); } int main() { while (scanf("%d%d%d%d%d", &c, &n, &m, &s, &d), c | n | m | s | d) { --s; --d; rep(i, 101) G[i].clear(); rep(i, m) { int a, b, f; scanf("%d%d%d", &a, &b, &f); --a; --b; G[a].push_back(P(b, f)); G[b].push_back(P(a, f)); } printf("%d\n", dijkstra()); } return 0; }
// AOJ 0212 #define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <cstdio> #include <functional> #include <queue> #include <vector> #define rep(i, a) for (int i = 0; i != (a); ++i) typedef std::pair<int, int> P; typedef std::pair<P, int> PP; const int INF = 1 << 30; int c, n, m, s, d; std::vector<P> G[100]; int dist[100][11]; int dijkstra() { std::fill((int *)dist, (int *)(dist + 100), INF); dist[s][0] = 0; std::priority_queue<PP, std::vector<PP>, std::greater<PP>> pque; pque.push(PP(P(s, 0), 0)); while (!pque.empty()) { PP p = pque.top(); pque.pop(); int v = p.first.first, cost = p.first.second, tic = p.second; if (dist[v][tic] < cost) continue; rep(i, G[v].size()) { P &e = G[v][i]; if (tic < c && dist[e.first][tic + 1] > dist[v][tic] + e.second / 2) { dist[e.first][tic + 1] = dist[v][tic] + e.second / 2; pque.push(PP(P(e.first, dist[e.first][tic + 1]), tic + 1)); } if (dist[e.first][tic] > dist[v][tic] + e.second) { dist[e.first][tic] = dist[v][tic] + e.second; pque.push(PP(P(e.first, dist[e.first][tic]), tic)); } } } return *std::min_element(dist[d], dist[d] + 11); } int main() { while (scanf("%d%d%d%d%d", &c, &n, &m, &s, &d), c | n | m | s | d) { --s; --d; rep(i, 100) G[i].clear(); rep(i, m) { int a, b, f; scanf("%d%d%d", &a, &b, &f); --a; --b; G[a].push_back(P(b, f)); G[b].push_back(P(a, f)); } printf("%d\n", dijkstra()); } return 0; }
replace
53
54
53
54
0
p00213
C++
Time Limit Exceeded
#include <iostream> #include <vector> using vec = std::vector<int>; using Land = std::vector<std::vector<int>>; int W, H, N; Land s; std::vector<Land> res; bool check(int h, int w, int a, int b, int num) { if (h + a > H || w + b > W) { return false; } for (int i = h; i < h + a; i++) { for (int j = w; j < w + b; j++) { if (s[i][j] != 0 && s[i][j] != num) { return false; } } } return true; } void paint(int h, int w, int a, int b, int num) { for (int i = h; i < h + a; i++) { for (int j = w; j < w + b; j++) { s[i][j] = num; } } } void dfs(int idx, const vec &b, const vec &k) { if (idx == N) { res.emplace_back(s); return; } for (int i = 1; i <= k[idx]; i++) { int j = k[idx] / i; if (i * j != k[idx]) continue; for (int h = 0; h < H; h++) { for (int w = 0; w < W; w++) { if (check(h, w, i, j, b[idx])) { auto tmp = s; paint(h, w, i, j, b[idx]); dfs(idx + 1, b, k); s = tmp; } } } } } void print(Land &l) { for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (j > 0) std::cout << " "; std::cout << l[i][j]; } std::cout << std::endl; } } void init() { s.assign(H, std::vector<int>(W)); res.clear(); } int main() { while (std::cin >> W >> H >> N, W) { init(); vec b(N), k(N); for (int i = 0; i < N; i++) { std::cin >> b[i] >> k[i]; } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { std::cin >> s[i][j]; } } dfs(0, b, k); if (res.size() != 1) { std::cout << "NA" << std::endl; } else { print(res[0]); } } return 0; }
#include <iostream> #include <vector> using vec = std::vector<int>; using Land = std::vector<std::vector<int>>; int W, H, N; Land s; std::vector<Land> res; bool check(int h, int w, int a, int b, int num) { if (h + a > H || w + b > W) { return false; } for (int i = h; i < h + a; i++) { for (int j = w; j < w + b; j++) { if (s[i][j] != 0 && s[i][j] != num) { return false; } } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (h <= i && i < h + a && w <= j && j < w + b) { continue; } if (s[i][j] == num) { return false; } } } return true; } void paint(int h, int w, int a, int b, int num) { for (int i = h; i < h + a; i++) { for (int j = w; j < w + b; j++) { s[i][j] = num; } } } void dfs(int idx, const vec &b, const vec &k) { if (idx == N) { res.emplace_back(s); return; } for (int i = 1; i <= k[idx]; i++) { int j = k[idx] / i; if (i * j != k[idx]) continue; for (int h = 0; h < H; h++) { for (int w = 0; w < W; w++) { if (check(h, w, i, j, b[idx])) { auto tmp = s; paint(h, w, i, j, b[idx]); dfs(idx + 1, b, k); s = tmp; } } } } } void print(Land &l) { for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (j > 0) std::cout << " "; std::cout << l[i][j]; } std::cout << std::endl; } } void init() { s.assign(H, std::vector<int>(W)); res.clear(); } int main() { while (std::cin >> W >> H >> N, W) { init(); vec b(N), k(N); for (int i = 0; i < N; i++) { std::cin >> b[i] >> k[i]; } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { std::cin >> s[i][j]; } } dfs(0, b, k); if (res.size() != 1) { std::cout << "NA" << std::endl; } else { print(res[0]); } } return 0; }
insert
21
21
21
33
TLE
p00213
C++
Runtime Error
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef istringstream ISS; typedef ostringstream OSS; typedef vector<string> VS; typedef int INT; typedef vector<INT> VI; typedef vector<VI> VVI; typedef pair<INT, INT> II; typedef vector<II> VII; template <class T> ostream &operator<<(ostream &os, vector<T> v) { for (typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i) { os << *it_i << ", "; } return os; } const int WH_SIZE = 11; const int SIZE = 16; int w, h; int n; int K[SIZE]; int S[WH_SIZE][WH_SIZE]; II T[11]; VVI M; VVI A; int cnt; void input() { for (int i = 0; i < n; ++i) { int b; cin >> b; b--; cin >> K[b]; } for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { int b; cin >> b; S[i][j] = b; if (b > 0) { b--; T[b] = II(i, j); } } } } void output() { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { cout << A[i][j]; if (j + 1 < w) cout << " "; } cout << endl; } } bool canPut(int k, int r0, int c0, int w0, int h0) { for (int i = 0; i < h0; ++i) { for (int j = 0; j < w0; ++j) { int r1 = r0 + i; int c1 = c0 + j; if (S[r1][c1] != k && S[r1][c1] != 0) return false; if (M[r1][c1] != 0) return false; } } return true; } void put(int k, int r0, int c0, int w0, int h0) { for (int i = 0; i < h0; ++i) { for (int j = 0; j < w0; ++j) { int r1 = r0 + i; int c1 = c0 + j; M[r1][c1] = k; } } } void solve(int depth) { if (depth >= n) { A = M; cnt++; return; } int r = T[depth].first; int c = T[depth].second; int k = K[depth]; for (int w0 = 1; w0 <= w; ++w0) { if (k % w0 != 0) continue; int h0 = k / w0; for (int r0 = max(0, r - h0 + 1); r0 + h0 - 1 < h; ++r0) { for (int c0 = max(0, c - w0 + 1); c0 + w0 - 1 < w; ++c0) { if (!(r0 <= r && r <= r0 + h0 - 1 && c0 <= c && c <= c0 + w0 - 1)) continue; if (!canPut(depth + 1, r0, c0, w0, h0)) continue; put(depth + 1, r0, c0, w0, h0); solve(depth + 1); if (cnt >= 2) return; put(0, r0, c0, w0, h0); } } } return; } int main() { while (cin >> w >> h >> n) { if (w == 0 && h == 0 && n == 0) break; M = VVI(h, VI(w, 0)); input(); cnt = 0; solve(0); if (cnt == 0 || cnt == 2) { cout << "NA" << endl; } else { output(); } } return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef istringstream ISS; typedef ostringstream OSS; typedef vector<string> VS; typedef int INT; typedef vector<INT> VI; typedef vector<VI> VVI; typedef pair<INT, INT> II; typedef vector<II> VII; template <class T> ostream &operator<<(ostream &os, vector<T> v) { for (typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i) { os << *it_i << ", "; } return os; } const int WH_SIZE = 11; const int SIZE = 16; int w, h; int n; int K[SIZE * 2]; int S[WH_SIZE * 2][WH_SIZE * 2]; II T[11 * 2]; VVI M; VVI A; int cnt; void input() { for (int i = 0; i < n; ++i) { int b; cin >> b; b--; cin >> K[b]; } for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { int b; cin >> b; S[i][j] = b; if (b > 0) { b--; T[b] = II(i, j); } } } } void output() { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { cout << A[i][j]; if (j + 1 < w) cout << " "; } cout << endl; } } bool canPut(int k, int r0, int c0, int w0, int h0) { for (int i = 0; i < h0; ++i) { for (int j = 0; j < w0; ++j) { int r1 = r0 + i; int c1 = c0 + j; if (S[r1][c1] != k && S[r1][c1] != 0) return false; if (M[r1][c1] != 0) return false; } } return true; } void put(int k, int r0, int c0, int w0, int h0) { for (int i = 0; i < h0; ++i) { for (int j = 0; j < w0; ++j) { int r1 = r0 + i; int c1 = c0 + j; M[r1][c1] = k; } } } void solve(int depth) { if (depth >= n) { A = M; cnt++; return; } int r = T[depth].first; int c = T[depth].second; int k = K[depth]; for (int w0 = 1; w0 <= w; ++w0) { if (k % w0 != 0) continue; int h0 = k / w0; for (int r0 = max(0, r - h0 + 1); r0 + h0 - 1 < h; ++r0) { for (int c0 = max(0, c - w0 + 1); c0 + w0 - 1 < w; ++c0) { if (!(r0 <= r && r <= r0 + h0 - 1 && c0 <= c && c <= c0 + w0 - 1)) continue; if (!canPut(depth + 1, r0, c0, w0, h0)) continue; put(depth + 1, r0, c0, w0, h0); solve(depth + 1); if (cnt >= 2) return; put(0, r0, c0, w0, h0); } } } return; } int main() { while (cin >> w >> h >> n) { if (w == 0 && h == 0 && n == 0) break; M = VVI(h, VI(w, 0)); input(); cnt = 0; solve(0); if (cnt == 0 || cnt == 2) { cout << "NA" << endl; } else { output(); } } return 0; }
replace
38
41
38
41
0
p00217
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <limits> #include <math.h> #include <sstream> #include <stdio.h> #include <string.h> #include <string> using namespace std; int main(void) { for (int u = 0; u < 50; u++) { long n; long id[51], a[51], b[51]; int p = -1; int q = 0; cin >> n; if (n == 0) { break; } for (long i = 0; i < n; i++) { cin >> id[i] >> a[i] >> b[i]; } for (long i = 0; i < n; i++) { if (a[i] + b[i] > q) { p = 0; p = id[i]; q = 0; q = a[i] + b[i]; } } cout << p << " " << q << endl; } }
#include <algorithm> #include <cmath> #include <iostream> #include <limits> #include <math.h> #include <sstream> #include <stdio.h> #include <string.h> #include <string> using namespace std; int main(void) { for (int u = 0; u < 50; u++) { long n; long id[10000], a[10000], b[10000]; int p = -1; int q = 0; cin >> n; if (n == 0) { break; } for (long i = 0; i < n; i++) { cin >> id[i] >> a[i] >> b[i]; } for (long i = 0; i < n; i++) { if (a[i] + b[i] > q) { p = 0; p = id[i]; q = 0; q = a[i] + b[i]; } } cout << p << " " << q << endl; } }
replace
15
16
15
16
0
p00217
C++
Runtime Error
#include <iostream> using namespace std; struct k { int bangou; int score; }; int main() { int n; int d1, d2, b; k id[1000]; while (cin >> n, n) { for (int i = 0; i < n; i++) { cin >> b >> d1 >> d2; id[i].bangou = b; id[i].score = (d1 + d2); } int max = 0; int ans_b; for (int i = 0; i < n; i++) { if (id[i].score > max) { max = id[i].score; ans_b = id[i].bangou; } } cout << ans_b << " " << max << endl; } return 0; }
#include <iostream> using namespace std; struct k { int bangou; int score; }; int main() { int n; int d1, d2, b; k id[10001]; while (cin >> n, n) { for (int i = 0; i < n; i++) { cin >> b >> d1 >> d2; id[i].bangou = b; id[i].score = (d1 + d2); } int max = 0; int ans_b; for (int i = 0; i < n; i++) { if (id[i].score > max) { max = id[i].score; ans_b = id[i].bangou; } } cout << ans_b << " " << max << endl; } return 0; }
replace
9
10
9
10
0
p00218
C++
Runtime Error
#include <iostream> #include <string> #include <vector> using namespace std; int main(void) { vector<vector<int>> p(10000, vector<int>(3)); vector<char> ans(10000); int n; while (1) { cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) { cin >> p[i][0] >> p[i][1] >> p[i][2]; if (p[i][0] == 100 || p[i][1] == 100 || p[i][2] == 100) ans[i] = 'A'; else if (90 * 2 <= p[i][0] + p[i][1]) ans[i] = 'A'; else if (80 * 3 <= p[i][0] + p[i][1] + p[i][2]) ans[i] = 'A'; else if (70 * 3 <= p[i][0] + p[i][1] + p[i][2]) ans[i] = 'B'; else if (50 * 3 <= p[i][0] + p[i][1] + p[i][2] && (80 <= p[i][0] || 80 <= p[i][1])) ans[i] = 'B'; else ans[i] = 'C'; } for (int i = 0; i < n; i++) cout << ans[i] << endl; p.clear(); ans.clear(); } return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; int main(void) { vector<vector<int>> p(10000, vector<int>(3)); vector<char> ans(10000); int n; while (1) { cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) { cin >> p[i][0] >> p[i][1] >> p[i][2]; if (p[i][0] == 100 || p[i][1] == 100 || p[i][2] == 100) ans[i] = 'A'; else if (90 * 2 <= p[i][0] + p[i][1]) ans[i] = 'A'; else if (80 * 3 <= p[i][0] + p[i][1] + p[i][2]) ans[i] = 'A'; else if (70 * 3 <= p[i][0] + p[i][1] + p[i][2]) ans[i] = 'B'; else if (50 * 3 <= p[i][0] + p[i][1] + p[i][2] && (80 <= p[i][0] || 80 <= p[i][1])) ans[i] = 'B'; else ans[i] = 'C'; } for (int i = 0; i < n; i++) cout << ans[i] << endl; } return 0; }
delete
30
32
30
30
0
p00218
C++
Time Limit Exceeded
#include <stdio.h> int main(void) { int number, pm, pe, pj, temp; while (1) { scanf("%d", &number); for (int i = number; i > 0; i--) { scanf("%d %d %d", &pm, &pe, &pj); if (pm == 100 || pe == 100 || pj == 100 || (pe + pm) / 2 >= 90 || (pe + pm + pj) / 3 >= 80) { printf("A\n"); } else if ((pe + pm + pj) / 3 >= 70 || ((pe + pm + pj) / 3 >= 50 && (pe >= 80 || pm >= 80))) { printf("B\n"); } else { printf("C\n"); } } } return 0; }
#include <stdio.h> int main(void) { int number, pm, pe, pj, temp; while (1) { scanf("%d", &number); if (number == 0) { break; } for (int i = number; i > 0; i--) { scanf("%d %d %d", &pm, &pe, &pj); if (pm == 100 || pe == 100 || pj == 100 || (pe + pm) / 2 >= 90 || (pe + pm + pj) / 3 >= 80) { printf("A\n"); } else if ((pe + pm + pj) / 3 >= 70 || ((pe + pm + pj) / 3 >= 50 && (pe >= 80 || pm >= 80))) { printf("B\n"); } else { printf("C\n"); } } } return 0; }
insert
6
6
6
9
TLE
p00219
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n, t; while (cin >> n, n) { int c[10]; fill(c, c + 10, 0); for (int i = 0; i < n; i++) cin >> t, c[t]++; for (int i = 0; i < n; i++) { if (!c[i]) cout << '-'; else for (int j = 0; j < c[i]; j++) cout << '*'; cout << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { int n, t; while (cin >> n, n) { int c[10]; fill(c, c + 10, 0); for (int i = 0; i < n; i++) cin >> t, c[t]++; for (int i = 0; i < 10; i++) { if (!c[i]) cout << '-'; else for (int j = 0; j < c[i]; j++) cout << '*'; cout << endl; } } }
replace
9
10
9
10
TLE
p00219
C++
Time Limit Exceeded
#include <cstdio> #include <iostream> using namespace std; int main() { int n; int buy; int ice[10]; while (scanf("%d", &n), n != 0) { for (int i = 0; i < 10; i++) { ice[i] = 0; } for (int i = 0; i < n; i++) { scanf("%d", &buy); ice[buy]++; } for (int i = 0; i < n; i++) { for (int j = 0; j < ice[i]; j++) { if (ice[i] == 0) { cout << "-" << endl; break; } else { cout << "*"; } } cout << endl; } } return (0); }
#include <cstdio> #include <iostream> using namespace std; int main() { int n; int buy; int ice[10]; while (scanf("%d", &n), n != 0) { for (int i = 0; i < 10; i++) { ice[i] = 0; } for (int i = 0; i < n; i++) { scanf("%d", &buy); ice[buy]++; } for (int i = 0; i < 10; i++) { for (int j = 0; j < ice[i]; j++) { if (ice[i] == 0) { cout << "-" << endl; break; } else { cout << "*"; } } cout << endl; } } return (0); }
replace
19
20
19
20
TLE
p00219
C++
Time Limit Exceeded
#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; //// < "D:\D_Download\Visual Studio ///2015\Projects\programing_contest_c++\Debug\a.txt" int main() { while (1) { int N; cin >> N; vector<int> nums(10000); for (int i = 0; i < N; ++i) { int a; cin >> a; nums[a]++; } for (int i = 0; i < 10; ++i) { if (nums[i]) { for (int j = 0; j < nums[i]; ++j) { cout << '*'; } } else { cout << '-'; } cout << 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; //// < "D:\D_Download\Visual Studio ///2015\Projects\programing_contest_c++\Debug\a.txt" int main() { while (1) { int N; cin >> N; if (!N) break; vector<int> nums(10000); for (int i = 0; i < N; ++i) { int a; cin >> a; nums[a]++; } for (int i = 0; i < 10; ++i) { if (nums[i]) { for (int j = 0; j < nums[i]; ++j) { cout << '*'; } } else { cout << '-'; } cout << endl; } } return 0; }
insert
15
15
15
17
TLE
p00219
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cstdio> #include <cstdlib> #include <iostream> #include <iterator> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <string> #include <time.h> #include <vector> using namespace std; #define FOR(I, F, N) for (int I = F; I < (int)(N); I++) #define rep(i, n) FOR(i, 0, n) #define FIN(V) cout << V << endl #define pb push_back #define INF (1 << 30) typedef pair<int, int> P; typedef long long ll; typedef priority_queue<int> pq; int StrToInt(string); string IntToStr(int); int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; int main(void) { int n; while (cin >> n) { if (n == 0) break; int menu[10]; fill_n(menu, n, 0); rep(i, n) { int a; cin >> a; menu[a]++; } rep(i, 10) { if (menu[i] == 0) { cout << "-" << endl; } else { rep(j, menu[i]) { cout << "*"; } cout << endl; } } } return 0; } int StrToInt(string s) { stringstream ss; ss << s; int val; ss >> val; return val; } string IntToStr(int i) { stringstream ss; ss << i; return ss.str(); }
#include <algorithm> #include <bitset> #include <cstdio> #include <cstdlib> #include <iostream> #include <iterator> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <string> #include <time.h> #include <vector> using namespace std; #define FOR(I, F, N) for (int I = F; I < (int)(N); I++) #define rep(i, n) FOR(i, 0, n) #define FIN(V) cout << V << endl #define pb push_back #define INF (1 << 30) typedef pair<int, int> P; typedef long long ll; typedef priority_queue<int> pq; int StrToInt(string); string IntToStr(int); int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; int main(void) { int n; while (cin >> n) { if (n == 0) break; int menu[15]; fill_n(menu, 15, 0); rep(i, n) { int a; cin >> a; menu[a]++; } rep(i, 10) { if (menu[i] == 0) { cout << "-" << endl; } else { rep(j, menu[i]) { cout << "*"; } cout << endl; } } } return 0; } int StrToInt(string s) { stringstream ss; ss << s; int val; ss >> val; return val; } string IntToStr(int i) { stringstream ss; ss << i; return ss.str(); }
replace
33
35
33
35
-6
*** stack smashing detected ***: terminated
p00219
C++
Time Limit Exceeded
#include <iostream> #include <map> #include <string> #include <vector> using namespace std; int main(void) { int a, n; while (1) { map<int, string> str; cin >> n; for (a = 0; a < n; a++) { int b; cin >> b; str[b].push_back('*'); } for (a = 0; a <= 9; a++) { if (str.size() > 0) cout << str[a] << endl; else cout << "-" << endl; } } return 0; }
#include <iostream> #include <map> #include <string> #include <vector> using namespace std; int main(void) { int a, n; while (1) { map<int, string> str; cin >> n; if (n == 0) break; for (a = 0; a < n; a++) { int b; cin >> b; str[b].push_back('*'); } for (a = 0; a <= 9; a++) { if (str.size() > 0) cout << str[a] << endl; else cout << "-" << endl; } } return 0; }
insert
12
12
12
14
TLE
p00219
C++
Runtime Error
#include <iostream> #include <string> using namespace std; int main() { int n, a; cin >> n; int b[10] = {}; while (cin >> a) { b[a] += 1; } for (int i = 0; i < 10; i++) { if (b[i] > 0) { string s(b[i], '*'); cout << s << endl; } else { cout << '-' << endl; } } return 0; }
#include <iostream> #include <string> using namespace std; int main() { int n, a; while (cin >> n, n) { int b[10] = {}; for (int i = 0; i < n; i++) { cin >> a; b[a] += 1; } for (int i = 0; i < 10; i++) { if (b[i] > 0) { string s(b[i], '*'); cout << s << endl; } else if (b[i] <= 0) { cout << '-' << endl; } } } return 0; }
replace
7
18
7
20
0
p00220
C++
Runtime Error
#include <cstdlib> #include <iostream> #include <string> using namespace std; int main() { char c; while (1) { string str; long long a, b; getline(cin, str); if (str[0] == '-') { break; } a = b = 0; int i = 0; while (str[i] != '.') { a *= 10; a += str[i] - '0'; i++; } i++; int d = 1000; while (i < str.size()) { b += d * (str[i] - '0'); d /= 10; i++; } a *= 16; b *= 16; a += b / 10000; if ((~(0xfff) & a) || (b % 10000)) cout << "NA" << endl; else { for (int i = 0; i < 12; i++) { if (i == 8) cout << "."; cout << !!(a & (0x1 << (11 - i))); } cout << endl; } } }
#include <cstdlib> #include <iostream> #include <string> using namespace std; int main() { char c; while (1) { string str; long long a, b; getline(cin, str); if (str[0] == '-') { break; } a = b = 0; int i = 0; while (str[i] != '.' && i < str.size()) { a *= 10; a += str[i] - '0'; i++; } i++; int d = 1000; while (i < str.size()) { b += d * (str[i] - '0'); d /= 10; i++; } a *= 16; b *= 16; a += b / 10000; if ((~(0xfff) & a) || (b % 10000)) cout << "NA" << endl; else { for (int i = 0; i < 12; i++) { if (i == 8) cout << "."; cout << !!(a & (0x1 << (11 - i))); } cout << endl; } } }
replace
16
17
16
17
0
p00221
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int M, N; while (cin >> M >> N, M != 0 && N != 0) { bool flag[1005] = {}; int jb = 0; int cnt = 1; int d = 0; bool lock = false; while (cnt <= N) { string S; cin >> S; while (!lock) { if (S == "FizzBuzz") { if (cnt % 15 != 0) { flag[jb] = true; d++; } } else if (S == "Fizz") { if (cnt % 3 != 0 || cnt % 15 == 0) { flag[jb] = true; d++; } } else if (S == "Buzz") { if (cnt % 5 != 0 || cnt % 15 == 0) { flag[jb] = true; d++; } } else { int val = atoi(S.c_str()); if (val != cnt || cnt % 3 == 0 || cnt % 5 == 0) { flag[jb] = true; d++; } } if (d == M - 1) { lock = true; } (jb += 1) %= M; while (flag[jb]) { (jb += 1) %= M; } } cnt++; } vector<int> ans; for (int i = 0; i < M; i++) { if (!flag[i]) { ans.push_back(i + 1); } } cout << ans[0]; for (int i = 1; i < ans.size(); i++) { cout << " " << ans[i]; } cout << endl; } return (0); }
#include <bits/stdc++.h> using namespace std; int main() { int M, N; while (cin >> M >> N, M != 0 && N != 0) { bool flag[1005] = {}; int jb = 0; int cnt = 1; int d = 0; bool lock = false; while (cnt <= N) { string S; cin >> S; if (!lock) { if (S == "FizzBuzz") { if (cnt % 15 != 0) { flag[jb] = true; d++; } } else if (S == "Fizz") { if (cnt % 3 != 0 || cnt % 15 == 0) { flag[jb] = true; d++; } } else if (S == "Buzz") { if (cnt % 5 != 0 || cnt % 15 == 0) { flag[jb] = true; d++; } } else { int val = atoi(S.c_str()); if (val != cnt || cnt % 3 == 0 || cnt % 5 == 0) { flag[jb] = true; d++; } } if (d == M - 1) { lock = true; } (jb += 1) %= M; while (flag[jb]) { (jb += 1) %= M; } } cnt++; } vector<int> ans; for (int i = 0; i < M; i++) { if (!flag[i]) { ans.push_back(i + 1); } } cout << ans[0]; for (int i = 1; i < ans.size(); i++) { cout << " " << ans[i]; } cout << endl; } return (0); }
replace
15
16
15
16
TLE
p00221
C++
Runtime Error
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <string> using namespace std; int main() { int n, m, j, suu; while ((cin >> m >> n) && m != 0 || n != 0) { int check[101] = {0}; for (int i = 1; i <= m; i++) check[i] = i; j = 1; suu = m; for (int i = 1; i <= n; i++) { string str; cin >> str; if (suu > 1) { if (i % 15 == 0) { if (str != "FizzBuzz") { // cout << "1.." << j << endl; check[j] = 0; suu--; } } else if (i % 3 == 0) { if (str != "Fizz") { // cout << "2.." << j << endl; check[j] = 0; suu--; } } else if (i % 5 == 0) { if (str != "Buzz") { // cout << "3.." << j << endl; check[j] = 0; suu--; } } else { char buf[10] = {NULL}; // buf2[10] = { NULL }; sprintf(buf, "%d", i); // cout << "str?????????" << str << endl; // cout <<"buf?????????"<< buf << endl; // snprintf(buf, 10, "%s", str); if (str != buf) { // cout << "kita"; check[j] = 0; suu--; } } j++; int x; if (suu > 1) for (x = j; check[x] == 0;) { x++; if (x > m) x = 1; } j = x; } } int las; for (int i = m; i > 0; i--) if (check[i] != 0) { las = i; break; } // cout << "las?????????" << las << endl; for (int i = 1; i <= m; i++) { if (check[i] != 0) { cout << i; if (i < las) cout << " "; } } cout << endl; } }
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <string> using namespace std; int main() { int n, m, j, suu; while ((cin >> m >> n) && m != 0 || n != 0) { int check[1001] = {0}; for (int i = 1; i <= m; i++) check[i] = i; j = 1; suu = m; for (int i = 1; i <= n; i++) { string str; cin >> str; if (suu > 1) { if (i % 15 == 0) { if (str != "FizzBuzz") { // cout << "1.." << j << endl; check[j] = 0; suu--; } } else if (i % 3 == 0) { if (str != "Fizz") { // cout << "2.." << j << endl; check[j] = 0; suu--; } } else if (i % 5 == 0) { if (str != "Buzz") { // cout << "3.." << j << endl; check[j] = 0; suu--; } } else { char buf[10] = {NULL}; // buf2[10] = { NULL }; sprintf(buf, "%d", i); // cout << "str?????????" << str << endl; // cout <<"buf?????????"<< buf << endl; // snprintf(buf, 10, "%s", str); if (str != buf) { // cout << "kita"; check[j] = 0; suu--; } } j++; int x; if (suu > 1) for (x = j; check[x] == 0;) { x++; if (x > m) x = 1; } j = x; } } int las; for (int i = m; i > 0; i--) if (check[i] != 0) { las = i; break; } // cout << "las?????????" << las << endl; for (int i = 1; i <= m; i++) { if (check[i] != 0) { cout << i; if (i < las) cout << " "; } } cout << endl; } }
replace
9
10
9
10
0
p00221
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define all(v) (v).begin(), (v).end() #define rev(s) (s).rbegin(), (s).rend() #define MP make_pair #define X first #define Y second using namespace std; typedef long long ll; typedef pair<int, int> P; bool check(string s, int cnt) { if (cnt % 15 == 0) return s == "FizzBuzz"; if (cnt % 5 == 0) return s == "Buzz"; if (cnt % 3 == 0) return s == "Fizz"; rep(i, s.size()) { if (s[i] < '0' || s[i] > '9') return false; } return atoi(s.c_str()) == cnt; } int main() { int m, n; while (cin >> m >> n, m | n) { vector<int> player(m); rep(i, m) player[i] = i; int p = 0; FOR(i, 1, n + 1) { string s; cin >> s; if (player.size() == 1) continue; if (!check(s, i)) player.erase(player.begin() + p); else p = (p + 1) % player.size(); } rep(i, player.size()) { cout << (i ? " " : "") << player[i] + 1; } cout << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define all(v) (v).begin(), (v).end() #define rev(s) (s).rbegin(), (s).rend() #define MP make_pair #define X first #define Y second using namespace std; typedef long long ll; typedef pair<int, int> P; bool check(string s, int cnt) { if (cnt % 15 == 0) return s == "FizzBuzz"; if (cnt % 5 == 0) return s == "Buzz"; if (cnt % 3 == 0) return s == "Fizz"; rep(i, s.size()) { if (s[i] < '0' || s[i] > '9') return false; } return atoi(s.c_str()) == cnt; } int main() { int m, n; while (cin >> m >> n, m | n) { vector<int> player(m); rep(i, m) player[i] = i; int p = 0; FOR(i, 1, n + 1) { string s; cin >> s; if (player.size() == 1) continue; if (!check(s, i)) player.erase(player.begin() + p); else p++; p %= player.size(); } rep(i, player.size()) { cout << (i ? " " : "") << player[i] + 1; } cout << endl; } return 0; }
replace
56
57
56
58
0
p00221
C++
Runtime Error
#include <list> #include <stdio.h> #include <string.h> int main() { using namespace std; int i, n, m, ps; char opn[10], ans[10]; bool ff, bf; list<int> player; list<int>::iterator it; do { scanf("%d %d", &m, &n); if (!m && !n) break; for (i = 0; i < m; i++) player.push_back(i + 1); for (i = 0, it = player.begin(), ps = m; i < n; i++) { scanf("%s", opn); if (ps < 2) continue; ff = (i + 1) % 3 == 0, bf = (i + 1) % 5 == 0; if (ff || bf) { sprintf(ans, "%s%s", ff ? "Fizz" : "", bf ? "Buzz" : ""); } else { sprintf(ans, "%d", i + 1); } if (strcmp(ans, opn)) { it = player.erase(it); ps--; } else { it++; } if (it == player.end()) it = player.begin(); } it = player.begin(); while (it != player.end()) { printf("%d", *it); it++; if (it != player.end()) printf(" "); } printf("\n"); player.clear(); } while (1); return 0; }
#include <list> #include <stdio.h> #include <string.h> int main() { using namespace std; int i, n, m, ps; char opn[20], ans[20]; bool ff, bf; list<int> player; list<int>::iterator it; do { scanf("%d %d", &m, &n); if (!m && !n) break; for (i = 0; i < m; i++) player.push_back(i + 1); for (i = 0, it = player.begin(), ps = m; i < n; i++) { scanf("%s", opn); if (ps < 2) continue; ff = (i + 1) % 3 == 0, bf = (i + 1) % 5 == 0; if (ff || bf) { sprintf(ans, "%s%s", ff ? "Fizz" : "", bf ? "Buzz" : ""); } else { sprintf(ans, "%d", i + 1); } if (strcmp(ans, opn)) { it = player.erase(it); ps--; } else { it++; } if (it == player.end()) it = player.begin(); } it = player.begin(); while (it != player.end()) { printf("%d", *it); it++; if (it != player.end()) printf(" "); } printf("\n"); player.clear(); } while (1); return 0; }
replace
7
8
7
8
0
p00221
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> using namespace std; string to_s(int n) { string s; while (n) { s += n % 10 + '0'; n /= 10; } reverse(s.begin(), s.end()); return s; } string FizzBuzz(int n) { if (n % 3 == 0 && n % 5 == 0) return "FizzBuzz"; if (n % 3 == 0) return "Fizz"; if (n % 5 == 0) return "Buzz"; return to_s(n); } int main() { int m, n; bool f[1000]; while (cin >> m >> n, m | n) { fill_n(f, m, true); string s; int rest = m; int num = 1, a = 0; for (int i = 0; i < n; i++) { cin >> s; if (rest == 1) break; while (!f[a]) { a = (a + 1) % m; } if (s != FizzBuzz(num)) { f[a] = false; rest--; } a = (a + 1) % m; num++; } bool first = true; for (int i = 0; i < m; i++) { if (!f[i]) continue; if (!first) cout << ' '; first = false; cout << i + 1; } cout << endl; } return 0; }
#include <algorithm> #include <iostream> #include <string> using namespace std; string to_s(int n) { string s; while (n) { s += n % 10 + '0'; n /= 10; } reverse(s.begin(), s.end()); return s; } string FizzBuzz(int n) { if (n % 3 == 0 && n % 5 == 0) return "FizzBuzz"; if (n % 3 == 0) return "Fizz"; if (n % 5 == 0) return "Buzz"; return to_s(n); } int main() { int m, n; bool f[1000]; while (cin >> m >> n, m | n) { fill_n(f, m, true); string s; int rest = m; int num = 1, a = 0; for (int i = 0; i < n; i++) { cin >> s; if (rest == 1) continue; while (!f[a]) { a = (a + 1) % m; } if (s != FizzBuzz(num)) { f[a] = false; rest--; } a = (a + 1) % m; num++; } bool first = true; for (int i = 0; i < m; i++) { if (!f[i]) continue; if (!first) cout << ' '; first = false; cout << i + 1; } cout << endl; } return 0; }
replace
35
36
35
36
0
p00222
C++
Time Limit Exceeded
#include <iostream> using namespace std; const int nmax = 10000000; char nums[nmax]; int main() { int n; for (int i = 2; i < nmax; i++) { nums[i] = '1'; } for (int i = 2; i * i < nmax; i++) { for (int k = 2; k * i < nmax; k++) { nums[i * k] = '0'; } } while (cin >> n) { if (n == 0) break; int i = n; while (nums[i] == '0' || nums[i - 2] == '0' || nums[i - 6] == '0' || nums[i - 8] == '0') i--; cout << i << endl; } return 0; }
#include <iostream> using namespace std; const int nmax = 10000000; char nums[nmax]; int main() { int n; for (int i = 2; i < nmax; i++) { nums[i] = '1'; } for (int i = 2; i * i < nmax; i++) { if (nums[i] == '1') { for (int k = 2; k * i < nmax; k++) { nums[i * k] = '0'; } } } while (cin >> n) { if (n == 0) break; int i = n; while (nums[i] == '0' || nums[i - 2] == '0' || nums[i - 6] == '0' || nums[i - 8] == '0') i--; cout << i << endl; } return 0; }
replace
12
14
12
16
TLE
p00222
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define dump(x) cerr << #x << " = " << (x) << endl #define rep(i, n) for (int i = 0; i < (n); i++) #define all(a) (a).begin(), (a).end() #define pb push_back bool era[10000001] = {false}; void makeERA() { for (int i = 2; i < 1000001; i++) { if (era[i] != true) continue; for (int j = 2;; j++) { if (i * j <= 1000000) era[i * j] = false; else break; } } era[2] = true; } int main() { for (int i = 0; i < 10000001; i++) era[i] = true; era[0] = false; era[1] = false; makeERA(); int n; while (cin >> n) { if (n == 0) break; if (n % 2 == 0) n--; for (int i = n; i >= 8; i -= 2) { if (era[i - 8] && era[i - 6] && !era[i - 4] && era[i - 2] && era[i]) { cout << i << endl; goto next; } } next:; } return 0; }
#include "bits/stdc++.h" using namespace std; typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define dump(x) cerr << #x << " = " << (x) << endl #define rep(i, n) for (int i = 0; i < (n); i++) #define all(a) (a).begin(), (a).end() #define pb push_back bool era[10000001] = {false}; void makeERA() { for (int i = 2; i < 1000001; i++) { if (era[i] != true) continue; for (int j = 2; i * j <= 10000000; j++) { era[i * j] = false; } } era[2] = true; } int main() { for (int i = 0; i < 10000001; i++) era[i] = true; era[0] = false; era[1] = false; makeERA(); int n; while (cin >> n) { if (n == 0) break; if (n % 2 == 0) n--; for (int i = n; i >= 8; i -= 2) { if (era[i - 8] && era[i - 6] && !era[i - 4] && era[i - 2] && era[i]) { cout << i << endl; goto next; } } next:; } return 0; }
replace
20
25
20
22
TLE
p00222
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; #define REP(i, j) for (int i = 0; i < j; i++) const int N = 10010; bool isprime[N]; int makePrime() { REP(i, N) isprime[i] = true; isprime[0] = false; isprime[1] = false; for (int i = 2; (i * i) <= N; i++) { if (!isprime[i]) continue; for (int j = i + i; j < N; j += i) isprime[j] = false; } } int main() { makePrime(); int n; while (cin >> n && n) { for (int i = n; i >= 8; i--) { if (isprime[i] && isprime[i - 2] && isprime[i - 6] && isprime[i - 8]) { cout << i << endl; break; } } } return 0; }
#include <iostream> #include <vector> using namespace std; #define REP(i, j) for (int i = 0; i < j; i++) const int N = 10000000; bool isprime[N]; int makePrime() { REP(i, N) isprime[i] = true; isprime[0] = false; isprime[1] = false; for (int i = 2; (i * i) <= N; i++) { if (!isprime[i]) continue; for (int j = i + i; j < N; j += i) isprime[j] = false; } } int main() { makePrime(); int n; while (cin >> n && n) { for (int i = n; i >= 8; i--) { if (isprime[i] && isprime[i - 2] && isprime[i - 6] && isprime[i - 8]) { cout << i << endl; break; } } } return 0; }
replace
4
5
4
5
0
p00222
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int F[11451419]; #define EPS 1e-14 bool prime(int p) { if (F[p] == 0) return false; if (F[p] == 1) return true; for (int i = 2; i * i <= p; i++) { if (F[i] == 0) continue; if (p % i == 0) { F[p] = 0; return false; } } F[p] = 1; return true; } int main() { for (int i = 1; i <= 10000000; i++) F[i] = -1; while (true) { int N; scanf("%d", &N); if (N == 0) break; if (N >= 13 && N <= 18) { printf("13\n"); continue; } while (true) { if (N % 10 != 9) { N--; continue; } if (prime(N) && prime(N - 2) && prime(N - 6) && prime(N - 8)) { printf("%d\n", N); break; } else N -= 10; } } return 0; }
#include <bits/stdc++.h> using namespace std; int F[11451419]; #define EPS 1e-14 bool prime(int p) { if (F[p] == 0) return false; if (F[p] == 1) return true; for (int i = 2; i * i <= p; i++) { if (F[i] == 0) continue; if (p % i == 0) { F[p] = 0; return false; } } F[p] = 1; return true; } int main() { for (int i = 1; i <= 10000000; i++) F[i] = -1; while (true) { int N; scanf("%d", &N); if (N == 0) break; if (N >= 13 && N <= 18) { printf("13\n"); continue; } while (true) { if (N % 10 != 9) { N--; continue; } if (N % 3 != 1) { N--; continue; } if (prime(N) && prime(N - 2) && prime(N - 6) && prime(N - 8)) { printf("%d\n", N); break; } else N -= 10; } } return 0; }
insert
38
38
38
42
TLE
p00222
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 fi first #define se second using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; int prime[10000011]; bool is_prime[10000011]; int MAX[10000011]; void sieve(int n) { int p = 0; rep(i, 0, n + 1) is_prime[i] = true; is_prime[0] = is_prime[1] = false; rep(i, 2, n + 1) { if (is_prime[i]) { prime[p++] = i; for (int j = i * 2; j <= n; j += i) { is_prime[j] = false; } } } } signed main() { int n; sieve(10000001); MAX[9] = 0; rep(i, 10, 10000001) { MAX[i] = MAX[i - 1]; if (is_prime[i] && is_prime[i - 2] && is_prime[i - 6] && is_prime[i - 8]) { MAX[i] = i; } } /*rep(i, 10, 30) { cout << i << " " << MAX[i] <<" " << is_prime[i]<< endl; }*/ while (cin >> n && n) { cout << MAX[n] << endl; } }
#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 fi first #define se second using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; int prime[10000011]; bool is_prime[10000011]; int MAX[10000011]; void sieve(int n) { int p = 0; rep(i, 0, n + 1) is_prime[i] = true; is_prime[0] = is_prime[1] = false; rep(i, 2, n + 1) { if (is_prime[i]) { prime[p++] = i; for (int j = i * 2; j <= n; j += i) { is_prime[j] = false; } } } } signed main() { int n; sieve(10000001); MAX[9] = 0; rep(i, 10, 10000001) { MAX[i] = MAX[i - 1]; if (is_prime[i] && is_prime[i - 2] && is_prime[i - 6] && is_prime[i - 8]) { MAX[i] = i; } } /*rep(i, 10, 30) { cout << i << " " << MAX[i] <<" " << is_prime[i]<< endl; }*/ while (cin >> n && n) { cout << MAX[n] << endl; } }
replace
5
6
5
6
MLE
p00222
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; inline bool isPrime(int n) { int sq = sqrt(n); for (int i = 3; i <= sq; i += 2) { if (n % i == 0) return false; } return true; } int main() { int n; while (cin >> n) { if (n == 0) break; int ans = -1; if (n % 10 == 9) n++; else n = (int)(n / 10) * 10; for (int i = n; i > 10; i -= 10) { if (isPrime(i - 9)) { if (isPrime(i - 7)) { if (isPrime(i - 3)) { if (isPrime(i - 1)) { ans = i - 1; break; } } } } } if (ans == -1) ans = 13; cout << ans << endl; } return 0; }
#include "bits/stdc++.h" using namespace std; inline bool isPrime(int n) { int sq = sqrt(n); for (int i = 3; i <= sq; i += 2) { if (n % i == 0) return false; } return true; } int main() { int n; while (cin >> n) { if (n == 0) break; int ans = -1; if (n % 10 == 9) n++; else n = (int)(n / 10) * 10; for (int i = n; i > 10; i -= 10) { if (isPrime(i - 9) && isPrime(i - 7) && isPrime(i - 3) && isPrime(i - 1)) { ans = i - 1; break; } } if (ans == -1) ans = 13; cout << ans << endl; } return 0; }
replace
27
36
27
31
TLE
p00222
C++
Time Limit Exceeded
#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; //// < "D:\D_Download\Visual Studio ///2015\Projects\programing_contest_c++\Debug\a.txt" long long int powmod(long long int a, long long int b, const int mod) { assert(b >= 0); if (b == 0) return 1; if (b == 1) return a; long long int ans = 1; long long int aa = powmod(a, b / 2, mod); ans *= aa * aa; ans %= mod; if (b % 2) ans *= a; ans %= mod; return ans; } const int k = 20; //?´???°?????? bool mirror_rabin(const long long int n) { if (n == 2) return true; if (n == 1 || (n & 1) == 0) return true; int s = 0; int d = n - 1; while (!(d % 2)) { s++; d /= 2; } for (int i = 0; i < k; ++i) { const long long int a = rand() % (n - 1) + 1; long long int t = d; long long int y = powmod(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = (y * y) % n; t <<= 1; } if (y != n - 1 && (t & 1) == 0) return false; } return true; } bool isso(long long int a) { return mirror_rabin(a); if (a == 1 || a == 0) return false; for (long long int i = 2; i * i <= a; ++i) { if ((a % i)) { } else { return false; } } return true; } int main() { while (1) { int N; cin >> N; if (!N) break; int num = N; if (num < 19) { cout << 13 << endl; } else { while (num % 30 != 19) num--; while (1) { if (mirror_rabin(num) && mirror_rabin(num - 2) && mirror_rabin(num - 6) && mirror_rabin(num - 8)) { cout << num << endl; break; } num -= 30; } } } 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; //// < "D:\D_Download\Visual Studio ///2015\Projects\programing_contest_c++\Debug\a.txt" long long int powmod(long long int a, long long int b, const int mod) { assert(b >= 0); if (b == 0) return 1; if (b == 1) return a; long long int ans = 1; long long int aa = powmod(a, b / 2, mod); ans *= aa * aa; ans %= mod; if (b % 2) ans *= a; ans %= mod; return ans; } const int k = 12; //?´???°?????? bool mirror_rabin(const long long int n) { if (n == 2) return true; if (n == 1 || (n & 1) == 0) return true; int s = 0; int d = n - 1; while (!(d % 2)) { s++; d /= 2; } for (int i = 0; i < k; ++i) { const long long int a = rand() % (n - 1) + 1; long long int t = d; long long int y = powmod(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = (y * y) % n; t <<= 1; } if (y != n - 1 && (t & 1) == 0) return false; } return true; } bool isso(long long int a) { return mirror_rabin(a); if (a == 1 || a == 0) return false; for (long long int i = 2; i * i <= a; ++i) { if ((a % i)) { } else { return false; } } return true; } int main() { while (1) { int N; cin >> N; if (!N) break; int num = N; if (num < 19) { cout << 13 << endl; } else { while (num % 30 != 19) num--; while (1) { if (mirror_rabin(num) && mirror_rabin(num - 2) && mirror_rabin(num - 6) && mirror_rabin(num - 8)) { cout << num << endl; break; } num -= 30; } } } return 0; }
replace
28
29
28
29
TLE
p00222
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; const int SIZE = 10000000; bool isprime[SIZE + 1]; vector<int> prime; void make_prime() { for (int i = 2; i <= SIZE; i++) { isprime[i] = true; } isprime[0] = false; isprime[1] = false; for (int i = 2; i <= SIZE; i++) { if (isprime[i] == false) continue; for (int j = i * 2; j <= SIZE; j += i) { isprime[j] = false; } prime.push_back(i); } } vector<int> yotugo; void make_list() { for (int i = 0; i <= (prime.size() - 4); i++) { if (prime[i + 1] == (prime[i] + 2) && prime[i + 2] == (prime[i] + 6) && prime[i + 3] == (prime[i] + 8)) { yotugo.push_back(prime[i + 3]); } } } int check(int n) { int i; for (i = 0; yotugo[i] <= n; i++) ; return yotugo[i - 1]; } int main() { make_prime(); make_list(); int n; while (cin >> n) { if (n == 0) break; cout << check(n) << endl; } return 0; }
#include <iostream> #include <vector> using namespace std; const int SIZE = 10000000; bool isprime[SIZE + 1]; vector<int> prime; void make_prime() { for (int i = 2; i <= SIZE; i++) { isprime[i] = true; } isprime[0] = false; isprime[1] = false; for (int i = 2; i <= SIZE; i++) { if (isprime[i] == false) continue; for (int j = i * 2; j <= SIZE; j += i) { isprime[j] = false; } prime.push_back(i); } } vector<int> yotugo; void make_list() { for (int i = 0; i <= (prime.size() - 4); i++) { if (prime[i + 1] == (prime[i] + 2) && prime[i + 2] == (prime[i] + 6) && prime[i + 3] == (prime[i] + 8)) { yotugo.push_back(prime[i + 3]); } } } int check(int n) { int i; for (i = 0; yotugo[i] <= n && i < yotugo.size(); i++) ; return yotugo[i - 1]; } int main() { make_prime(); make_list(); int n; while (cin >> n) { if (n == 0) break; cout << check(n) << endl; } return 0; }
replace
34
35
34
35
0
p00222
C++
Memory Limit Exceeded
#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; ll prime[10000001]; void Eratosthenes(int n) { rep(i, n) prime[i] = true; prime[1] = false; REP(i, 2, (int)sqrt(n)) { if (prime[i]) { for (int j = 0; i * (j + 2) < n; j++) { prime[i * (j + 2)] = 0; } } } } int main() { Eratosthenes(10000001); int n; while (cin >> n && n) { ll ans = 0; for (int i = n; i >= 0; i--) { if (prime[i] && prime[i - 2] && prime[i - 6] && prime[i - 8]) { ans = i; break; } } cout << ans << endl; } }
#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; bool prime[10000001]; void Eratosthenes(int n) { rep(i, n) prime[i] = true; prime[1] = false; REP(i, 2, (int)sqrt(n)) { if (prime[i]) { for (int j = 0; i * (j + 2) < n; j++) { prime[i * (j + 2)] = 0; } } } } int main() { Eratosthenes(10000001); int n; while (cin >> n && n) { ll ans = 0; for (int i = n; i >= 0; i--) { if (prime[i] && prime[i - 2] && prime[i - 6] && prime[i - 8]) { ans = i; break; } } cout << ans << endl; } }
replace
12
13
12
13
MLE
p00222
C++
Runtime Error
#include <iostream> using namespace std; const int MAX_N = 1000100; bool prime[MAX_N]; int num[MAX_N]; int main() { prime[0] = prime[1] = false; for (int i = 2; i < MAX_N; i++) { prime[i] = true; } for (int i = 2; i * i < MAX_N; i++) { if (prime[i]) { for (int j = i * i; j < MAX_N; j += i) prime[j] = false; } } int cnt = 0, tmp = MAX_N; int n, ans; while (true) { cin >> n; if (n == 0) break; for (int i = n - 8; i >= 0; i--) { if (prime[i] && prime[i + 2] && prime[i + 6] && prime[i + 8]) { cout << i + 8 << endl; break; } } } return 0; }
#include <iostream> using namespace std; const int MAX_N = 10001000; bool prime[MAX_N]; int num[MAX_N]; int main() { prime[0] = prime[1] = false; for (int i = 2; i < MAX_N; i++) { prime[i] = true; } for (int i = 2; i * i < MAX_N; i++) { if (prime[i]) { for (int j = i * i; j < MAX_N; j += i) prime[j] = false; } } int cnt = 0, tmp = MAX_N; int n, ans; while (true) { cin >> n; if (n == 0) break; for (int i = n - 8; i >= 0; i--) { if (prime[i] && prime[i + 2] && prime[i + 6] && prime[i + 8]) { cout << i + 8 << endl; break; } } } return 0; }
replace
4
5
4
5
0
p00222
C++
Runtime Error
#include <cstdio> #include <iostream> #include <string> #define N 10000001 using namespace std; int main() { int i, j, n, a[N]; for (i = 2; i < N; i++) a[i] = 1; for (i = 2; i * 2 <= N; i++) { if (a[i] == 1) { for (j = i * 2; j <= N; j += i) a[j] = 0; } } while (1) { cin >> n; if (n == 0) break; int ans = 0; for (i = n; i >= 13; i--) { if (a[i] == 1 && a[i - 2] == 1 && a[i - 6] == 1 && a[i - 8] == 1) { ans = i; break; } } cout << ans << endl; } return 0; }
#include <cstdio> #include <iostream> #include <string> #define N 10000001 using namespace std; int main() { int i, j, n; bool a[N]; for (i = 2; i < N; i++) a[i] = 1; for (i = 2; i * 2 <= N; i++) { if (a[i] == 1) { for (j = i * 2; j <= N; j += i) a[j] = 0; } } while (1) { cin >> n; if (n == 0) break; int ans = 0; for (i = n; i >= 13; i--) { if (a[i] == 1 && a[i - 2] == 1 && a[i - 6] == 1 && a[i - 8] == 1) { ans = i; break; } } cout << ans << endl; } return 0; }
replace
7
8
7
9
-11
p00222
C++
Memory Limit Exceeded
#include <cstdio> int prmt[10000005] = {}; int rest[10000005] = {}; int main(void) { int n, i, j; prmt[0] = -1; prmt[1] = -1; for (i = 2; i <= 10000000; i++) { if (prmt[i] == -1) continue; prmt[i] = 1; j = i * 2; while (j <= 10000000) { prmt[j] = -1; j += i; } } j = 13; for (i = 13; i <= 10000000; i++) { if (prmt[i] == 1 && prmt[i - 2] == 1 && prmt[i - 6] == 1 && prmt[i - 8] == 1) j = i; rest[i] = j; } while (1) { scanf("%d", &n); if (n == 0) break; printf("%d\n", rest[n]); } return 0; }
#include <cstdio> char prmt[10000005] = {}; int rest[10000005] = {}; int main(void) { int n, i, j; prmt[0] = -1; prmt[1] = -1; for (i = 2; i <= 10000000; i++) { if (prmt[i] == -1) continue; prmt[i] = 1; j = i * 2; while (j <= 10000000) { prmt[j] = -1; j += i; } } j = 13; for (i = 13; i <= 10000000; i++) { if (prmt[i] == 1 && prmt[i - 2] == 1 && prmt[i - 6] == 1 && prmt[i - 8] == 1) j = i; rest[i] = j; } while (1) { scanf("%d", &n); if (n == 0) break; printf("%d\n", rest[n]); } return 0; }
replace
2
3
2
3
MLE