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
p00712
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <vector> using namespace std; int gcd(int p, int q) { if (p < q) { int t = p; p = q; q = t; } while (q != 0) { int t = p; p = q; q = t % q; } return p; } int pow2(int a, int b) { int e = a, p = 1; while (b != 0) { while ((b & 1) == 0) { b >>= 1; e *= e; } p *= e; b >>= 1, e *= e; } return p; } struct Frac { int p, q; Frac() {} Frac(int p, int q) { int d = gcd(p, q); p /= d, q /= d; this->p = p, this->q = q; } Frac(int n) : p(n), q(1) {} Frac operator+(const Frac rhs) const { return Frac(p * rhs.q + q * rhs.p, q * rhs.q); } bool operator<(const Frac rhs) const { return (p * rhs.q - q * rhs.p < 0); } bool operator==(const Frac rhs) const { return (!(*this < rhs) && !(rhs < *this)); } int gauss() { if (p % q == 0) { return p; } return (p / q) + 1; } }; int P, Q, A, N; int ans(Frac tar, int m, int prod, int rest) { // printf("tar = %d/%d, m = %d, prod = %d, rest = %d\n", // tar.p, tar.q, m, prod, rest); if (tar < 0) { return 0; } if (prod > A) { return 0; } if (tar == 0) { // printf("Found\n"); return 1; } if (rest == 0) { return 0; } int min_q; for (min_q = m;; min_q++) { if (Frac(1, min_q) < tar || Frac(1, min_q) == tar) { break; } } int sum = 0; for (int i = min_q;; i++) { if (pow2(i, min(Frac(tar.p * i, tar.q).gauss(), rest)) > A) { break; } if (Frac(rest, i) < tar) { break; } sum += ans(tar + Frac(-1, i), i, prod * i, rest - 1); } return sum; } void solve() { printf("%d\n", ans(Frac(P, Q), 1, 1, N)); } int main() { while (scanf("%d %d %d %d", &P, &Q, &A, &N) == 4) { if (P == 0 && Q == 0 && A == 0 && N == 0) { break; } solve(); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <vector> using namespace std; int gcd(int p, int q) { if (p < q) { int t = p; p = q; q = t; } while (q != 0) { int t = p; p = q; q = t % q; } return p; } int pow2(int a, int b) { int e = a, p = 1; while (b != 0) { while ((b & 1) == 0) { b >>= 1; e *= e; } p *= e; b >>= 1, e *= e; } return p; } struct Frac { int p, q; Frac() {} Frac(int p, int q) { int d = gcd(p, q); p /= d, q /= d; this->p = p, this->q = q; } Frac(int n) : p(n), q(1) {} Frac operator+(const Frac rhs) const { return Frac(p * rhs.q + q * rhs.p, q * rhs.q); } bool operator<(const Frac rhs) const { return (p * rhs.q - q * rhs.p < 0); } bool operator==(const Frac rhs) const { return (!(*this < rhs) && !(rhs < *this)); } int gauss() { if (p % q == 0) { return p; } return (p / q) + 1; } }; int P, Q, A, N; int ans(Frac tar, int m, int prod, int rest) { // printf("tar = %d/%d, m = %d, prod = %d, rest = %d\n", // tar.p, tar.q, m, prod, rest); if (tar < 0) { return 0; } if (prod > A) { return 0; } if (tar == 0) { // printf("Found\n"); return 1; } if (rest == 0) { return 0; } int min_q; for (min_q = m;; min_q++) { if (Frac(1, min_q) < tar || Frac(1, min_q) == tar) { break; } } int sum = 0; for (int i = min_q;; i++) { if (prod * pow2(i, min(Frac(tar.p * i, tar.q).gauss(), rest)) > A) { break; } if (Frac(rest, i) < tar) { break; } sum += ans(tar + Frac(-1, i), i, prod * i, rest - 1); } return sum; } void solve() { printf("%d\n", ans(Frac(P, Q), 1, 1, N)); } int main() { while (scanf("%d %d %d %d", &P, &Q, &A, &N) == 4) { if (P == 0 && Q == 0 && A == 0 && N == 0) { break; } solve(); } return 0; }
replace
85
86
85
86
TLE
p00712
C++
Time Limit Exceeded
#include <iostream> using namespace std; int p, q, a, n, answer; void search(int pp, int qq, int start, int count) { if (count == n) return; for (int i = start; i <= a; ++i) { int ppp = pp * i + qq; int qqq = qq * i; if (qqq > a) return; if (!(qqq % q) && !(ppp % p) && qqq / q == ppp / p) ++answer; else search(ppp, qqq, i, count + 1); } return; } int gcd(int a, int b) { int c; while (b) { c = a % b; a = b; b = c; } return a; } int main(void) { while (cin >> p >> q >> a >> n, p | q | a | n) { int gg = gcd(p, q); p /= gg; q /= gg; answer = 0; search(0, 1, 1, 0); cout << answer << endl; } return 0; }
#include <iostream> using namespace std; int p, q, a, n, answer; void search(int pp, int qq, int start, int count) { if (count == n) return; for (int i = start; i <= a; ++i) { int ppp = pp * i + qq; int qqq = qq * i; if (qqq > a) return; if (ppp / qqq > p / q) continue; if (!(qqq % q) && !(ppp % p) && qqq / q == ppp / p) ++answer; else search(ppp, qqq, i, count + 1); } return; } int gcd(int a, int b) { int c; while (b) { c = a % b; a = b; b = c; } return a; } int main(void) { while (cin >> p >> q >> a >> n, p | q | a | n) { int gg = gcd(p, q); p /= gg; q /= gg; answer = 0; search(0, 1, 1, 0); cout << answer << endl; } return 0; }
insert
14
14
14
16
TLE
p00712
C++
Time Limit Exceeded
#include <cmath> #include <iostream> using namespace std; int p, q, a, n; double EPS = 1e-9; int solve(double sum, int mul, int prev, int num) { if (num > n) return 0; if (mul > a) return 0; if (fabs(sum - (double)p / (double)q) < EPS) return 1; if (sum > (double)p / (double)q) return 0; int ret = 0; for (int i = prev; i <= 800; i++) { ret += solve(sum + 1.0 / (double)i, mul * i, i, num + 1); } return ret; } int main() { while (1) { cin >> p >> q >> a >> n; if (p == 0 && q == 0 && a == 0 && n == 0) break; cout << solve(0.0, 1, 1, 0) << endl; } return 0; }
#include <cmath> #include <iostream> using namespace std; int p, q, a, n; double EPS = 1e-9; int solve(double sum, int mul, int prev, int num) { if (num > n) return 0; if (mul > a) return 0; if (fabs(sum - (double)p / (double)q) < EPS) return 1; if (sum > (double)p / (double)q) return 0; int ret = 0; for (int i = prev; mul * i <= a; i++) { ret += solve(sum + 1.0 / (double)i, mul * i, i, num + 1); } return ret; } int main() { while (1) { cin >> p >> q >> a >> n; if (p == 0 && q == 0 && a == 0 && n == 0) break; cout << solve(0.0, 1, 1, 0) << endl; } return 0; }
replace
20
21
20
21
TLE
p00712
C++
Time Limit Exceeded
#include <stdio.h> int dfs(int p, int q, int a, int n, int pre) { if (p == 0) return 1; if (n == 0) return 0; int res = 0; for (int i = pre; i <= 12000; i++) { if ((a / i) == 0) break; res += dfs(i * p - q, i * q, a / i, n - 1, i); } return res; } int main() { int p, q, a, n; while (scanf("%d %d %d %d", &p, &q, &a, &n), p) { printf("%d\n", dfs(p, q, a, n, 1)); } }
#include <stdio.h> int dfs(int p, int q, int a, int n, int pre) { if (p == 0) return 1; if (n == 0) return 0; int res = 0; for (int i = pre; i <= 6000; i++) { if ((a / i) == 0) break; res += dfs(i * p - q, i * q, a / i, n - 1, i); } return res; } int main() { int p, q, a, n; while (scanf("%d %d %d %d", &p, &q, &a, &n), p) { printf("%d\n", dfs(p, q, a, n, 1)); } }
replace
8
9
8
9
TLE
p00712
C++
Time Limit Exceeded
#include <iostream> using namespace std; typedef long long ll; ll p, q, a, n, an; ll gcd(ll a, ll b) { if (b) return gcd(b, a % b); return a; } void dfs(ll d, ll s, ll t, ll k, ll j) { if (d > n || s * q > p * k) return; if (s * q == p * k) { an++; return; } for (int i = j; t * i <= a; i++) { ll cd = gcd(k, i); dfs(d + 1, s * i / cd + k / cd, t * i, k * i / cd, i); } } int main() { while (cin >> p >> q >> a >> n && n) { an = 0; dfs(0, 0, 1, 1, 1); cout << an << endl; } return 0; }
#include <iostream> using namespace std; typedef long long ll; ll p, q, a, n, an; ll gcd(ll a, ll b) { if (b) return gcd(b, a % b); return a; } void dfs(ll d, ll s, ll t, ll k, ll j) { if (d > n || s * q > p * k) return; if (s * q == p * k) { an++; return; } for (int i = j; t * i <= a; i++) { // ll cd=gcd(k,i); dfs(d + 1, s * i + k, t * i, k * i, i); } } int main() { while (cin >> p >> q >> a >> n && n) { an = 0; dfs(0, 0, 1, 1, 1); cout << an << endl; } return 0; }
replace
19
21
19
21
TLE
p00712
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; #define ISEQ(c) (c).begin(), (c).end() typedef long long ll; typedef pair<int, int> P; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int calc(int p, int q, int a, int n, int d, int num, int prod) { if (p == 0) return 1; if (num == n) return 0; int res = 0; int g = gcd(p, q); p /= g; q /= g; for (int i = d; prod * i <= a; i++) { if (p * i - q < 0) continue; res += calc(p * i - q, i * q, a, n, i, num + 1, prod * i); } return res; } int main() { while (true) { int p, q, a, n; scanf("%d%d%d%d", &p, &q, &a, &n); if (p == 0 and q == 0 and a == 0 and n == 0) break; printf("%d\n", calc(p, q, a, n, 1, 0, 1)); } }
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; #define ISEQ(c) (c).begin(), (c).end() typedef long long ll; typedef pair<int, int> P; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int calc(int p, int q, int a, int n, int d, int num, int prod) { if (p == 0) return 1; if (num == n) return 0; int res = 0; int g = gcd(p, q); p /= g; q /= g; for (int i = d; prod * i <= a; i++) { if (p * i - q < 0) continue; if (p * i - q * (n - num) > 0) break; res += calc(p * i - q, i * q, a, n, i, num + 1, prod * i); } return res; } int main() { while (true) { int p, q, a, n; scanf("%d%d%d%d", &p, &q, &a, &n); if (p == 0 and q == 0 and a == 0 and n == 0) break; printf("%d\n", calc(p, q, a, n, 1, 0, 1)); } }
insert
34
34
34
36
TLE
p00712
C++
Time Limit Exceeded
#include <iostream> #include <set> #include <vector> using namespace std; int p, q, n, a; int ans; int rec(int sum_u, int sum_l, int nn, int k) { if (sum_l > a || nn == 0) return -1; if (sum_u * q == sum_l * p) { ans++; return 0; } nn--; for (int x = k;; x++) { if (rec(sum_u * x + sum_l, sum_l * x, nn, x) < 0) break; } return 0; } int main() { while (cin >> p >> q >> a >> n, p) { ans = 0; rec(0, 1, n + 1, 1); cout << ans << endl; } return 0; }
#include <iostream> #include <set> #include <vector> using namespace std; int p, q, n, a; int ans; int rec(int sum_u, int sum_l, int nn, int k) { if (sum_l > a || nn == 0) return -1; if (sum_u * q == sum_l * p) { ans++; return 0; } if (p / double(q) < sum_u / double(sum_l)) return 0; nn--; for (int x = k;; x++) { if (rec(sum_u * x + sum_l, sum_l * x, nn, x) < 0) break; } return 0; } int main() { while (cin >> p >> q >> a >> n, p) { ans = 0; rec(0, 1, n + 1, 1); cout << ans << endl; } return 0; }
insert
17
17
17
19
TLE
p00712
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <vector> using namespace std; int ans = 0; int p, q, a, n; void dfs(int num, int seki, int deno, int nume, int value) { if (nume > 0) { int g = __gcd(deno, nume); deno /= g; nume /= g; } if (p * deno == q * nume) { ans++; } if (p * deno <= q * nume) { return; } if (num == n) return; for (int i = value; i * seki <= a; i++) { dfs(num + 1, seki * i, deno * i, nume * i + deno, i); } } int main() { while (cin >> p >> q >> a >> n && q && p) { ans = 0; dfs(0, 1, 1, 0, 1); cout << ans << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int ans = 0; int p, q, a, n; void dfs(int num, int seki, int deno, int nume, int value) { if (nume > 0) { int g = __gcd(deno, nume); deno /= g; nume /= g; } if ((nume / (double)deno) + (double)(n - num) / (double)value < (double)p / (double)q - 1e-7) return; if (p * deno == q * nume) { ans++; } if (p * deno <= q * nume) { return; } if (num == n) return; for (int i = value; i * seki <= a; i++) { dfs(num + 1, seki * i, deno * i, nume * i + deno, i); } } int main() { while (cin >> p >> q >> a >> n && q && p) { ans = 0; dfs(0, 1, 1, 0, 1); cout << ans << endl; } return 0; }
insert
13
13
13
16
TLE
p00712
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <cxxabi.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; #define rep(i, n) range(i, 0, n) #define range(i, a, n) for (int i = a; i < n; i++) #define all(a) a.begin(), a.end() #define LINF ((ll)1ll < 60) #define INF ((int)1 << 30) #define EPS (1e-9) #define MOD (1000000007) template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) { os << "[" << p.first << ", " << p.second << "]"; return os; }; template <class S> auto &operator<<(ostream &os, vector<S> t) { bool b = 1; for (auto s : t) os << (exchange(b, 0) || strlen(abi::__cxa_demangle(typeid(S).name(), 0, 0, 0)) > 20 ? "" : " ") << s; return os << endl; } constexpr int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } constexpr int lcm(int a, int b) { return a * b / gcd(a, b); } int ma; int solve(int p, int q, int r, int a, int n) { int ans = 0; if (p == 0) return 1; if (n == 0) return 0; for (int nq = r; a * nq <= ma; nq++) { if (p * nq > n * q) break; // if(p*nq>=q){ int P = p * nq - q; int Q = q * nq; ans += solve(P / gcd(P, Q), Q / gcd(P, Q), nq, a * nq, n - 1); //} } return ans; } int main() { cin.tie(0); ios::sync_with_stdio(false); int p, q, n; while (cin >> p >> q >> ma >> n, p) { cout << solve(p, q, 1, 1, n) << endl; } return 0; }
#include <bits/stdc++.h> #include <cxxabi.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; #define rep(i, n) range(i, 0, n) #define range(i, a, n) for (int i = a; i < n; i++) #define all(a) a.begin(), a.end() #define LINF ((ll)1ll < 60) #define INF ((int)1 << 30) #define EPS (1e-9) #define MOD (1000000007) template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) { os << "[" << p.first << ", " << p.second << "]"; return os; }; template <class S> auto &operator<<(ostream &os, vector<S> t) { bool b = 1; for (auto s : t) os << (exchange(b, 0) || strlen(abi::__cxa_demangle(typeid(S).name(), 0, 0, 0)) > 20 ? "" : " ") << s; return os << endl; } constexpr int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } constexpr int lcm(int a, int b) { return a * b / gcd(a, b); } int ma; int solve(int p, int q, int r, int a, int n) { int ans = 0; if (p == 0) return 1; if (n == 0) return 0; for (int nq = r; a * nq <= ma; nq++) { if (p * nq > n * q) break; if (p * nq >= q) { int P = p * nq - q; int Q = q * nq; ans += solve(P / gcd(P, Q), Q / gcd(P, Q), nq, a * nq, n - 1); } } return ans; } int main() { cin.tie(0); ios::sync_with_stdio(false); int p, q, n; while (cin >> p >> q >> ma >> n, p) { cout << solve(p, q, 1, 1, n) << endl; } return 0; }
replace
43
48
43
48
TLE
p00712
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) const double EPS = 1e-10; const double PI = acos(-1.0); int dfs(int p, int q, int a, int n, int bunbo, int bunsi, int cnt, int prev) { if (cnt == n) { return (q * bunsi == p * bunbo); } int ret = (q * bunsi == p * bunbo); FOR(i, prev, a + 1) { int new_bunbo = bunbo * i; int new_bunsi = bunsi * i + bunbo; if (new_bunbo <= a) { ret += dfs(p, q, a, n, new_bunbo, new_bunsi, cnt + 1, i); } } return ret; } int main() { int p, q, a, n; while (cin >> p >> q >> a >> n, p | q | a | n) { cout << dfs(p, q, a, n, 1, 0, 0, 1) << endl; } }
#include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) const double EPS = 1e-10; const double PI = acos(-1.0); int dfs(int p, int q, int a, int n, int bunbo, int bunsi, int cnt, int prev) { if (cnt == n) { return (q * bunsi == p * bunbo); } int ret = (q * bunsi == p * bunbo); FOR(i, prev, a + 1) { int new_bunbo = bunbo * i; int new_bunsi = bunsi * i + bunbo; if (new_bunbo > a) break; ret += dfs(p, q, a, n, new_bunbo, new_bunsi, cnt + 1, i); } return ret; } int main() { int p, q, a, n; while (cin >> p >> q >> a >> n, p | q | a | n) { cout << dfs(p, q, a, n, 1, 0, 0, 1) << endl; } }
replace
54
57
54
57
TLE
p00712
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int p, q, a, n; int dfs(int A, int B, int C, int D, int MAX) { int res = 0; if (B * p == A * q) { // cout<<A<<' '<<B<<' '<<C<<' '<<D<<' '<<MAX<<endl; return 1; } if (D == n) return 0; for (int i = MAX; i * C <= a; i++) { int t = __gcd(B, i); int bb = B * i / t; int aa = bb / B * A + bb / i; // cout<<A<<' '<<B<<' '<<i<<' '<<aa<<' '<<bb<<endl; res += dfs(aa, bb, C * i, D + 1, i); } return res; } signed main() { while (cin >> p >> q >> a >> n, n) { cout << dfs(0, 1, 1, 0, 1) << endl; } }
#include <bits/stdc++.h> using namespace std; int p, q, a, n; int dfs(int A, int B, int C, int D, int MAX) { int res = 0; if (B * p == A * q) { // cout<<A<<' '<<B<<' '<<C<<' '<<D<<' '<<MAX<<endl; return 1; } if (B * p < A * q) return 0; if (D == n) return 0; for (int i = MAX; i * C <= a; i++) { int t = __gcd(B, i); int bb = B * i / t; int aa = bb / B * A + bb / i; // cout<<A<<' '<<B<<' '<<i<<' '<<aa<<' '<<bb<<endl; res += dfs(aa, bb, C * i, D + 1, i); } return res; } signed main() { while (cin >> p >> q >> a >> n, n) { cout << dfs(0, 1, 1, 0, 1) << endl; } }
insert
13
13
13
17
TLE
p00712
C++
Time Limit Exceeded
#include <cstdio> int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int p, q, a, n, cnt; void dfs(int v, int l, int r, int now) { if (v != 0) { int d = gcd(r, l); if (l / d == p && r / d == q) { cnt++; return; } } if (v < n) { for (int i = a / r; i >= now; i--) { if ((double)p / q < (double)(l * i + r) / (r * i)) return; dfs(v + 1, l * i + r, r * i, i); } } } int main(void) { while (1) { scanf("%d %d %d %d", &p, &q, &a, &n); if (p + q + a + n == 0) break; int d = gcd(p, q); p /= d; q /= d; cnt = 0; dfs(0, 0, 1, 1); printf("%d\n", cnt); } return 0; }
#include <cstdio> int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int p, q, a, n, cnt; void dfs(int v, int l, int r, int now) { double dsum = (double)l / r; dsum += (double)(n - v + 1) / now; if ((double)p / q > dsum) return; if (v != 0) { int d = gcd(r, l); if (l / d == p && r / d == q) { cnt++; return; } } if (v < n) { for (int i = a / r; i >= now; i--) { if ((double)p / q < (double)(l * i + r) / (r * i)) return; dfs(v + 1, l * i + r, r * i, i); } } } int main(void) { while (1) { scanf("%d %d %d %d", &p, &q, &a, &n); if (p + q + a + n == 0) break; int d = gcd(p, q); p /= d; q /= d; cnt = 0; dfs(0, 0, 1, 1); printf("%d\n", cnt); } return 0; }
insert
11
11
11
15
TLE
p00712
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, a, b) for (int i = (a); i < (b); ++i) using pii = pair<int, int>; using vi = vector<int>; using vvi = vector<vector<int>>; int p, q, a, n; map<vector<int>, int> dp; int gcd(int a, int b) { return a ? gcd(b % a, a) : b; } int lcm(int a, int b) { return a / gcd(a, b) * b; } int dfs(int x, int y, int z, int w, int last) { vi arg = {x, y, z, w, last}; // if(dp.count(arg))return dp[arg]; if (z == n) { if (x == 0) return 1; else return 0; } if (x < 0) return 0; if (x == 0) return 1; // x/y - (n-z)*(1/last)>0 if (x * last > (n - z) * y) return 0; int ret = 0; for (int i = last; i * w <= a; i++) { // x/y - 1/i int bunbo = lcm(y, i); int bunshi = bunbo / y * x - bunbo / i * 1; int g = gcd(bunbo, bunshi); bunbo /= g; bunshi /= g; ret += dfs(bunshi, bunbo, z + 1, i * w, i); } // return dp[arg]=ret; return ret; } signed main() { while (cin >> p >> q >> a >> n, p or q or a or n) { dp.clear(); int g = gcd(p, q); p /= g; q /= g; cout << dfs(p, q, 0, 1, 1) << endl; } }
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, a, b) for (int i = (a); i < (b); ++i) using pii = pair<int, int>; using vi = vector<int>; using vvi = vector<vector<int>>; int p, q, a, n; map<vector<int>, int> dp; int gcd(int a, int b) { return a ? gcd(b % a, a) : b; } int lcm(int a, int b) { return a / gcd(a, b) * b; } int dfs(int x, int y, int z, int w, int last) { vi arg = {x, y, z, w, last}; // if(dp.count(arg))return dp[arg]; if (z == n) { if (x == 0) return 1; else return 0; } if (x < 0) return 0; if (x == 0) return 1; // x/y - (n-z)*(1/last)>0 if (x * last > (n - z) * y) return 0; int ret = 0; for (int i = last; i * w <= a; i++) { // x/y - 1/i // int bunbo=lcm(y,i); // int bunshi=bunbo/y*x - bunbo/i*1; // int g=gcd(bunbo,bunshi); // bunbo/=g; // bunshi/=g; ret += dfs(x * i - y, y * i, z + 1, i * w, i); } // return dp[arg]=ret; return ret; } signed main() { while (cin >> p >> q >> a >> n, p or q or a or n) { dp.clear(); int g = gcd(p, q); p /= g; q /= g; cout << dfs(p, q, 0, 1, 1) << endl; } }
replace
34
40
34
41
TLE
p00712
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) REP(i, 0, n) #define REP(i, s, e) for (int i = (s); i < (int)(e); i++) #define pb push_back #define all(r) (r).begin(), (r).end() #define rall(r) (r).rbegin(), (r).rend() #define fi first #define se second typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; const ll MOD = 1e9 + 7; double EPS = 1e-8; int P, Q, A, N; int calc(int p, int q, int n, int a, int t) { if (p * Q == P * q) return 1; if (n == N) return 0; if (p * Q > P * q) return 0; int ret = 0; for (int i = t; i * a <= A; i++) { int np = p * i + q, nq = i * q; int g = __gcd(np, nq); np /= g; nq /= g; ret += calc(np, nq, n + 1, i * a, i); } return ret; } int main() { // while(cin >> P >> Q >> A >> N) { while (scanf("%d %d %d %d", &P, &Q, &A, &N) != EOF) { if (P == 0 && Q == 0 && A == 0 && N == 0) break; // cout << calc(0, 1, 0, 1, 1) << endl; printf("%d\n", calc(0, 1, 0, 1, 1)); } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) REP(i, 0, n) #define REP(i, s, e) for (int i = (s); i < (int)(e); i++) #define pb push_back #define all(r) (r).begin(), (r).end() #define rall(r) (r).rbegin(), (r).rend() #define fi first #define se second typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; const ll MOD = 1e9 + 7; double EPS = 1e-8; int P, Q, A, N; int calc(int p, int q, int n, int a, int t) { if (p * Q == P * q) return 1; if (n == N) return 0; if (p * Q > P * q) return 0; int ret = 0; for (int i = t; i * a <= A; i++) { int np = p * i + q, nq = i * q; // int g = __gcd(np, nq); // np /= g; // nq /= g; ret += calc(np, nq, n + 1, i * a, i); } return ret; } int main() { // while(cin >> P >> Q >> A >> N) { while (scanf("%d %d %d %d", &P, &Q, &A, &N) != EOF) { if (P == 0 && Q == 0 && A == 0 && N == 0) break; // cout << calc(0, 1, 0, 1, 1) << endl; printf("%d\n", calc(0, 1, 0, 1, 1)); } }
replace
35
38
35
38
TLE
p00712
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define INF 1000000000 #define EPS 1e-9 #define PI acos(-1) typedef long long ll; #define MAX_P 800 #define MAX_Q 800 #define MAX_A 12000 #define MAX_N 7 int p, q, a, n; vector<int> ans_list; //********************************************************* // 最大公約数(Greatest Common Divisor)を返す。 // 引数に0がある場合は0を返す。 //********************************************************* int gcd(int m, int n) { // 引数に0がある場合は0を返す if ((0 == m) || (0 == n)) return 0; // ユークリッドの方法 while (m != n) { if (m > n) m = m - n; else n = n - m; } return m; } // gcd //********************************************************* // 最小公倍数(Least Common Multiple)を返す。 // 引数に0がある場合は0を返す。 //********************************************************* int lcm(int m, int n) { // 引数に0がある場合は0を返す if ((0 == m) || (0 == n)) return 0; return ((m / gcd(m, n)) * n); // lcm = m * n / gcd(m,n) } // lcm // solve(現在の分解数, 現在の分数の分子, 現在の分数の分母, 分母の積, // 前の分母の値) int solve(int num, int P, int Q, int seki, int pre) { int ans = 0; if (num > n) return 0; if (seki > a) return 0; if (P * q == Q * p && P != 0 && Q != 0) return 1; if ((double)p / q < (double)P / Q) return 0; for (int i = pre; i * seki <= a; i++) { if (num == 0) ans += solve(1, 1, i, i, i); else { int tmp = lcm(Q, i); ans += solve(num + 1, P * tmp / Q + 1 * tmp / i, tmp, seki * i, i); } } return ans; } int main() { while (true) { cin >> p >> q >> a >> n; if (p == 0 && q == 0 && a == 0 && n == 0) break; ans_list.push_back(solve(0, 0, 0, 1, 1)); } for (int i = 0; i < ans_list.size(); i++) { cout << ans_list[i] << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define INF 1000000000 #define EPS 1e-9 #define PI acos(-1) typedef long long ll; #define MAX_P 800 #define MAX_Q 800 #define MAX_A 12000 #define MAX_N 7 int p, q, a, n; vector<int> ans_list; //********************************************************* // 最大公約数(Greatest Common Divisor)を返す。 // 引数に0がある場合は0を返す。 //********************************************************* int gcd(int m, int n) { // 引数に0がある場合は0を返す if ((0 == m) || (0 == n)) return 0; // ユークリッドの方法 while (m != n) { if (m > n) m = m - n; else n = n - m; } return m; } // gcd //********************************************************* // 最小公倍数(Least Common Multiple)を返す。 // 引数に0がある場合は0を返す。 //********************************************************* int lcm(int m, int n) { // 引数に0がある場合は0を返す if ((0 == m) || (0 == n)) return 0; return ((m / gcd(m, n)) * n); // lcm = m * n / gcd(m,n) } // lcm // solve(現在の分解数, 現在の分数の分子, 現在の分数の分母, 分母の積, // 前の分母の値) int solve(int num, int P, int Q, int seki, int pre) { int ans = 0; if (num > n) return 0; if (seki > a) return 0; if (P * q == Q * p && P != 0 && Q != 0) return 1; if ((double)p / q < (double)P / Q) return 0; for (int i = pre; i * seki <= a; i++) { if (num == 0) ans += solve(1, 1, i, i, i); else { ans += solve(num + 1, P * i + Q, Q * i, seki * i, i); } } return ans; } int main() { while (true) { cin >> p >> q >> a >> n; if (p == 0 && q == 0 && a == 0 && n == 0) break; ans_list.push_back(solve(0, 0, 0, 1, 1)); } for (int i = 0; i < ans_list.size(); i++) { cout << ans_list[i] << endl; } return 0; }
replace
90
92
90
91
TLE
p00712
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; #ifdef _DEBUG #include "dump.hpp" #else #define dump(...) #endif // #define int long long #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) #define all(c) begin(c), end(c) const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = (int)(1e9) + 7; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } int lcm(int x, int y) { return x * y / gcd(x, y); } int p, q, a, n; using T = tuple<int, int, int, int, int>; int f(int x, int y, int z, int c, int a_) { if (c == n) return 0; if (x * z > (n - c) * y) return 0; int ret = 0; rep(i, z, a + 1) { if (i * a_ > a) break; if (x * i > y) { int nx = x * i - y, ny = y * i; int g = gcd(nx, ny); nx /= g; ny /= g; ret += f(nx, ny, i, c + 1, i * a_); } else if (x * i == y) { ret += 1; } } return ret; } signed main() { cin.tie(0); ios::sync_with_stdio(false); for (; cin >> p >> q >> a >> n && p;) { cout << f(p, q, 1, 0, 1) << endl; } return 0; }
#include "bits/stdc++.h" using namespace std; #ifdef _DEBUG #include "dump.hpp" #else #define dump(...) #endif // #define int long long #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) #define all(c) begin(c), end(c) const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = (int)(1e9) + 7; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } int lcm(int x, int y) { return x * y / gcd(x, y); } int p, q, a, n; using T = tuple<int, int, int, int, int>; int f(int x, int y, int z, int c, int a_) { if (c == n - 1 && x != 1) return 0; if (c == n) return 0; if (x * z > (n - c) * y) return 0; int ret = 0; rep(i, z, a + 1) { if (i * a_ > a) break; if (x * i > y) { int nx = x * i - y, ny = y * i; int g = gcd(nx, ny); nx /= g; ny /= g; ret += f(nx, ny, i, c + 1, i * a_); } else if (x * i == y) { ret += 1; } } return ret; } signed main() { cin.tie(0); ios::sync_with_stdio(false); for (; cin >> p >> q >> a >> n && p;) { cout << f(p, q, 1, 0, 1) << endl; } return 0; }
insert
36
36
36
38
TLE
p00712
C++
Time Limit Exceeded
#include <iostream> using namespace std; int GCD(int m, int n) { if (n == 0) return m; return GCD(n, m % n); } int dfs(int p, int q, int a, int n, int temp) { if (a == 0 || n == 0) return 0; int res = 0; int gcd = GCD(p, q); p /= gcd; q /= gcd; for (int i = max((q + p - 1) / p, temp); i <= a; i++) { if (p == 1 && q == i) { res++; } else res += dfs(p * i - q, q * i, a / i, n - 1, i); } return res; } int main() { int p, q, a, n; while (cin >> p >> q >> a >> n, p || q || a || n) { cout << dfs(p, q, a, n, 1) << endl; } return 0; }
#include <iostream> using namespace std; int GCD(int m, int n) { if (n == 0) return m; return GCD(n, m % n); } int dfs(int p, int q, int a, int n, int temp) { if (a < temp || n == 0) return 0; int res = 0; int gcd = GCD(p, q); p /= gcd; q /= gcd; for (int i = max((q + p - 1) / p, temp); i <= a; i++) { if (p == 1 && q == i) { res++; } else res += dfs(p * i - q, q * i, a / i, n - 1, i); } return res; } int main() { int p, q, a, n; while (cin >> p >> q >> a >> n, p || q || a || n) { cout << dfs(p, q, a, n, 1) << endl; } return 0; }
replace
11
12
11
12
TLE
p00712
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define mind(a, b) (a > b ? b : a) #define maxd(a, b) (a > b ? a : b) #define absd(x) (x < 0 ? -(x) : x) #define pow2(x) ((x) * (x)) #define rep(i, n) for (int i = 0; i < n; ++i) #define repr(i, n) for (int i = n - 1; i >= 0; --i) #define repl(i, s, n) for (int i = s; i <= n; ++i) #define replr(i, s, n) for (int i = n; i >= s; --i) #define repf(i, s, n, j) for (int i = s; i <= n; i += j) #define repe(e, obj) for (auto e : obj) #define SP << " " << #define COL << " : " << #define COM << ", " << #define ARR << " -> " << #define PNT(STR) cout << STR << endl #define POS(X, Y) "(" << X << ", " << Y << ")" #define DEB(A) " (" << #A << ") " << A #define DEBREP(i, n, val) \ for (int i = 0; i < n; ++i) \ cout << val << " "; \ cout << endl #define ALL(V) (V).begin(), (V).end() #define INF 1000000007 #define INFLL 10000000000000000007LL #define EPS 1e-9 typedef unsigned int uint; typedef unsigned long ulong; typedef unsigned long long ull; typedef long long ll; typedef long double ld; typedef pair<int, int> P; // typedef pair<ll, ll> P; typedef pair<P, int> PI; typedef pair<int, P> IP; typedef pair<P, P> PP; typedef priority_queue<P, vector<P>, greater<P>> pvqueue; ll gcd(ll m, ll n) { if (m < n) return gcd(n, m); ll r = m % n; return r ? gcd(n, r) : n; } int p, q, a, n; // p/q = 1/a{0} + ... + 1/a{c-1} + x/y // where P_{i=0..c-1}a{i} = s int dfs(ll x, ll y, ll s, ll c, ll now) { int res = (x == 1 && now <= y && s * y <= a && c + 1 <= n); if (s * now > a || c >= n) return res; // use int nx = x * now - y, ny = y * now; if (nx != 0) { int g = gcd(nx, ny); nx /= g; ny /= g; int nxt = (ny + nx - 1) / nx; nxt = maxd(nxt, now); res += dfs(nx, ny, s * now, c + 1, nxt); } // next res += dfs(x, y, s, c, now + 1); return res; } int main() { while (cin >> p >> q >> a >> n && p + q + a + n) { int g = gcd(p, q); cout << dfs(p / g, q / g, 1, 0, maxd(1, (p + q - 1) / p)) << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define mind(a, b) (a > b ? b : a) #define maxd(a, b) (a > b ? a : b) #define absd(x) (x < 0 ? -(x) : x) #define pow2(x) ((x) * (x)) #define rep(i, n) for (int i = 0; i < n; ++i) #define repr(i, n) for (int i = n - 1; i >= 0; --i) #define repl(i, s, n) for (int i = s; i <= n; ++i) #define replr(i, s, n) for (int i = n; i >= s; --i) #define repf(i, s, n, j) for (int i = s; i <= n; i += j) #define repe(e, obj) for (auto e : obj) #define SP << " " << #define COL << " : " << #define COM << ", " << #define ARR << " -> " << #define PNT(STR) cout << STR << endl #define POS(X, Y) "(" << X << ", " << Y << ")" #define DEB(A) " (" << #A << ") " << A #define DEBREP(i, n, val) \ for (int i = 0; i < n; ++i) \ cout << val << " "; \ cout << endl #define ALL(V) (V).begin(), (V).end() #define INF 1000000007 #define INFLL 10000000000000000007LL #define EPS 1e-9 typedef unsigned int uint; typedef unsigned long ulong; typedef unsigned long long ull; typedef long long ll; typedef long double ld; typedef pair<int, int> P; // typedef pair<ll, ll> P; typedef pair<P, int> PI; typedef pair<int, P> IP; typedef pair<P, P> PP; typedef priority_queue<P, vector<P>, greater<P>> pvqueue; ll gcd(ll m, ll n) { if (m < n) return gcd(n, m); ll r = m % n; return r ? gcd(n, r) : n; } int p, q, a, n; // p/q = 1/a{0} + ... + 1/a{c-1} + x/y // where P_{i=0..c-1}a{i} = s int dfs(ll x, ll y, ll s, ll c, ll now) { int res = (x == 1 && now <= y && s * y <= a && c + 1 <= n); if (s * now > a || c >= n || x * now > y * (n - c)) return res; // use int nx = x * now - y, ny = y * now; if (nx != 0) { int g = gcd(nx, ny); nx /= g; ny /= g; int nxt = (ny + nx - 1) / nx; nxt = maxd(nxt, now); res += dfs(nx, ny, s * now, c + 1, nxt); } // next res += dfs(x, y, s, c, now + 1); return res; } int main() { while (cin >> p >> q >> a >> n && p + q + a + n) { int g = gcd(p, q); cout << dfs(p / g, q / g, 1, 0, maxd(1, (p + q - 1) / p)) << endl; } return 0; }
replace
65
66
65
66
TLE
p00712
C++
Time Limit Exceeded
#include <bits/stdc++.h> typedef long long ll; typedef unsigned long long ull; using namespace std; #define pb push_back int dy[] = {0, 0, 1, -1, 1, 1, -1, -1}; int dx[] = {1, -1, 0, 0, 1, -1, -1, 1}; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) #define mp make_pair #define fi first #define sc second ll p, q, a, n; typedef ll GCD_TYPE; GCD_TYPE gcd(GCD_TYPE a, GCD_TYPE b) { if (b == 0) return a; return gcd(b, a % b); } ll dfs(ll p, ll q, ll nowa, ll nown, ll j) { if (p <= 0) { return p == 0; } if (nown >= n) { return 0; } if (q * (n - nown) < p * j) { return 0; } ll g = gcd(p, q); p /= g; q /= g; ll ret = 0; for (ll i = j; i * nowa <= a; i++) { ll l = q * i / gcd(q, i); ret += dfs((p * l / q) - (l / i), l, nowa * i, nown + 1, i); } return ret; } int main() { while (1) { cin >> p >> q >> a >> n; if (p == 0) { return 0; } cout << dfs(p, q, 1, 0, 1) << endl; } return 0; }
#include <bits/stdc++.h> typedef int ll; typedef unsigned long long ull; using namespace std; #define pb push_back int dy[] = {0, 0, 1, -1, 1, 1, -1, -1}; int dx[] = {1, -1, 0, 0, 1, -1, -1, 1}; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) #define mp make_pair #define fi first #define sc second ll p, q, a, n; typedef ll GCD_TYPE; GCD_TYPE gcd(GCD_TYPE a, GCD_TYPE b) { if (b == 0) return a; return gcd(b, a % b); } ll dfs(ll p, ll q, ll nowa, ll nown, ll j) { if (p <= 0) { return p == 0; } if (nown >= n) { return 0; } if (q * (n - nown) < p * j) { return 0; } ll g = gcd(p, q); p /= g; q /= g; ll ret = 0; for (ll i = j; i * nowa <= a; i++) { ll l = q * i / gcd(q, i); ret += dfs((p * l / q) - (l / i), l, nowa * i, nown + 1, i); } return ret; } int main() { while (1) { cin >> p >> q >> a >> n; if (p == 0) { return 0; } cout << dfs(p, q, 1, 0, 1) << endl; } return 0; }
replace
1
2
1
2
TLE
p00712
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <queue> #include <sstream> #include <string> #include <vector> using namespace std; #define reps(i, f, n) for (int i = f; i < int(n); i++) #define rep(i, n) reps(i, 0, n) int cont; void saiki(double rest, int a, int n, int st, vector<int> &bin) { if (fabs(rest) < 0.0000001) { cont++; return; } if (bin.size() == n) return; int sum = 1; rep(i, bin.size()) sum *= bin[i]; for (int i = st;; i++) { if (sum * i <= a) { bin.push_back(i); saiki(rest - 1.0 / i, a, n, i, bin); bin.pop_back(); } else { break; } } } int main() { while (1) { int p, q, a, n; cin >> p >> q >> a >> n; cont = 0; if (n == 0) break; vector<int> bin; int st = 1; double rest = (double)p / q; saiki(rest, a, n, st, bin); printf("%d\n", cont); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <queue> #include <sstream> #include <string> #include <vector> using namespace std; #define reps(i, f, n) for (int i = f; i < int(n); i++) #define rep(i, n) reps(i, 0, n) int cont; void saiki(double rest, int a, int n, int st, vector<int> &bin) { if (fabs(rest) < 0.0000001) { cont++; return; } if (bin.size() == n) return; if (rest < 0) return; int sum = 1; rep(i, bin.size()) sum *= bin[i]; for (int i = st;; i++) { if (sum * i <= a) { bin.push_back(i); saiki(rest - 1.0 / i, a, n, i, bin); bin.pop_back(); } else { break; } } } int main() { while (1) { int p, q, a, n; cin >> p >> q >> a >> n; cont = 0; if (n == 0) break; vector<int> bin; int st = 1; double rest = (double)p / q; saiki(rest, a, n, st, bin); printf("%d\n", cont); } }
insert
23
23
23
25
TLE
p00712
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <climits> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #define rep(i, m, n) for (int i = int(m); i < int(n); i++) #define EACH(i, c) for (auto &(i) : c) #define all(c) begin(c), end(c) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort(begin(c), end(c)) #define pb emplace_back #define MP make_pair #define SZ(a) int((a).size()) // #define LOCAL 0 // #ifdef LOCAL // #define DEBUG(s) cout << (s) << endl // #define dump(x) cerr << #x << " = " << (x) << endl // #define BR cout << endl; // #else // #define DEBUG(s) do{}while(0) // #define dump(x) do{}while(0) // #define BR // #endif // 改造 typedef long long int ll; using namespace std; #define INF (1 << 30) - 1 #define INFl (ll)5e15 #define DEBUG 0 // デバッグする時1にしてね #define dump(x) cerr << #x << " = " << (x) << endl #define MOD 1000000007 // ここから編集する /** * p / q */ struct Bunsuu { using T = ll; T p, q; Bunsuu(T p_, T q_) : p(p_), q(q_) {} Bunsuu(pair<T, T> aPair) : p(aPair.first), q(aPair.second) {} Bunsuu operator+(Bunsuu right) { T bunbo = q * right.q; T bunshi = p * right.q + right.p * q; return Bunsuu(yakubun(bunshi, bunbo)); } Bunsuu operator-(Bunsuu right) { T bunbo = q * right.q; T bunshi = p * right.q - right.p * q; return Bunsuu(yakubun(bunshi, bunbo)); } Bunsuu operator*(Bunsuu right) { T bunbo = q * right.q; T bunshi = p * right.p; return Bunsuu(yakubun(bunshi, bunbo)); } Bunsuu operator/(Bunsuu right) { T bunbo = q * right.p; T bunshi = p * right.q; return Bunsuu(yakubun(bunshi, bunbo)); } void print() { cout << p << "/" << q << endl; } ll gcd(ll a, ll b) { a = abs(a); b = abs(b); if (a < b) swap(a, b); if (a % b == 0) return b; return gcd(b, a % b); } pair<T, T> yakubun(T p, T q) { if (p == 0) return pair<T, T>(0ll, 1ll); T gcd_v = gcd(p, q); p /= gcd_v; q /= gcd_v; return pair<T, T>(p, q); } double value() { return 1.0 * p / q; } }; ll dfs(Bunsuu bunsuu, Bunsuu a, int n, int low) { if (bunsuu.p == 0) { return 1; } if (bunsuu.value() < 0) return 0; if (n == 0) return 0; ll ret = 0; for (int i = low; i <= a.value(); i++) { Bunsuu tmp = Bunsuu(1, i); ret += dfs(bunsuu - tmp, a * tmp, n - 1, i); } return ret; } ll dfs(Bunsuu bunsuu, int a, int n) { return dfs(bunsuu, Bunsuu(a, 1), n, 1); } int main() { cin.tie(0); ios::sync_with_stdio(false); while (true) { int p, q, a, n; cin >> p >> q >> a >> n; if (p == 0) break; cout << dfs(Bunsuu(p, q), a, n) << endl; } return 0; }
#include <algorithm> #include <bitset> #include <climits> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #define rep(i, m, n) for (int i = int(m); i < int(n); i++) #define EACH(i, c) for (auto &(i) : c) #define all(c) begin(c), end(c) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort(begin(c), end(c)) #define pb emplace_back #define MP make_pair #define SZ(a) int((a).size()) // #define LOCAL 0 // #ifdef LOCAL // #define DEBUG(s) cout << (s) << endl // #define dump(x) cerr << #x << " = " << (x) << endl // #define BR cout << endl; // #else // #define DEBUG(s) do{}while(0) // #define dump(x) do{}while(0) // #define BR // #endif // 改造 typedef long long int ll; using namespace std; #define INF (1 << 30) - 1 #define INFl (ll)5e15 #define DEBUG 0 // デバッグする時1にしてね #define dump(x) cerr << #x << " = " << (x) << endl #define MOD 1000000007 // ここから編集する /** * p / q */ struct Bunsuu { using T = ll; T p, q; Bunsuu(T p_, T q_) : p(p_), q(q_) {} Bunsuu(pair<T, T> aPair) : p(aPair.first), q(aPair.second) {} Bunsuu operator+(Bunsuu right) { T bunbo = q * right.q; T bunshi = p * right.q + right.p * q; return Bunsuu(yakubun(bunshi, bunbo)); } Bunsuu operator-(Bunsuu right) { T bunbo = q * right.q; T bunshi = p * right.q - right.p * q; return Bunsuu(yakubun(bunshi, bunbo)); } Bunsuu operator*(Bunsuu right) { T bunbo = q * right.q; T bunshi = p * right.p; return Bunsuu(yakubun(bunshi, bunbo)); } Bunsuu operator/(Bunsuu right) { T bunbo = q * right.p; T bunshi = p * right.q; return Bunsuu(yakubun(bunshi, bunbo)); } void print() { cout << p << "/" << q << endl; } ll gcd(ll a, ll b) { a = abs(a); b = abs(b); if (a < b) swap(a, b); if (a % b == 0) return b; return gcd(b, a % b); } pair<T, T> yakubun(T p, T q) { if (p == 0) return pair<T, T>(0ll, 1ll); T gcd_v = gcd(p, q); p /= gcd_v; q /= gcd_v; return pair<T, T>(p, q); } double value() { return 1.0 * p / q; } }; ll dfs(Bunsuu bunsuu, Bunsuu a, int n, int low) { if (bunsuu.p == 0) { return 1; } if (bunsuu.value() < 0) return 0; if (n == 0) return 0; ll ret = 0; for (int i = low; i <= a.value(); i++) { Bunsuu tmp = Bunsuu(1, i); Bunsuu hantei = bunsuu - (tmp * Bunsuu(n, 1)); if (hantei.p > 0) { break; } ret += dfs(bunsuu - tmp, a * tmp, n - 1, i); } return ret; } ll dfs(Bunsuu bunsuu, int a, int n) { return dfs(bunsuu, Bunsuu(a, 1), n, 1); } int main() { cin.tie(0); ios::sync_with_stdio(false); while (true) { int p, q, a, n; cin >> p >> q >> a >> n; if (p == 0) break; cout << dfs(Bunsuu(p, q), a, n) << endl; } return 0; }
insert
136
136
136
140
TLE
p00712
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <sstream> #include <stack> #include <string.h> #include <string> #include <type_traits> #include <vector> using namespace std; #define ll long long int #define all(v) begin(v), end(v) #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++) // typedef vector<int> V; // typedef vector<VV> VVV; template <typename T> void remove(std::vector<T> &vector, unsigned int index) { vector.erase(vector.begin() + index); } int euclid(int a, int b) { int temp; if (a < b) { temp = a; a = b; b = temp; } if (b < 1) return -1; if (a % b == 0) return b; return euclid(b, a % b); } int P, Q, N, A, cot = 0; void dfs(int p, int q, int a, int n, int mini) { for (int i = mini; i * a <= A; i++) { if (n == N) break; if (((i * p - q) == 0)) { // cout << "N:" << n+1 << " mini:" << mini << " e:" << (i * p - q) << // endl; cot++; continue; } int e = euclid(i * p - q, i * q); if (i * q / e > 0) dfs((i * p - q) / e, i * q / e, i * a, n + 1, i); // cout << "N:" << n+1 << " mini:" << mini << " i:" << i <<" e:"<< (i * p - // q) / e <<" "<< i * q / e<< endl; } } int main() { cin >> P >> Q >> A >> N; while (P != 0) { cot = 0; dfs(P, Q, 1, 0, 1); cout << cot << endl; cin >> P >> Q >> A >> N; } }
#include <algorithm> #include <bitset> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <sstream> #include <stack> #include <string.h> #include <string> #include <type_traits> #include <vector> using namespace std; #define ll long long int #define all(v) begin(v), end(v) #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++) // typedef vector<int> V; // typedef vector<VV> VVV; template <typename T> void remove(std::vector<T> &vector, unsigned int index) { vector.erase(vector.begin() + index); } int euclid(int a, int b) { int temp; if (a < b) { temp = a; a = b; b = temp; } if (b < 1) return -1; if (a % b == 0) return b; return euclid(b, a % b); } int P, Q, N, A, cot = 0; void dfs(int p, int q, int a, int n, int mini) { for (int i = mini; i * a <= A; i++) { if (n == N) break; if (((i * p - q) == 0)) { // cout << "N:" << n+1 << " mini:" << mini << " e:" << (i * p - q) << // endl; cot++; continue; } if (i * p - q > 0) dfs((i * p - q), i * q, i * a, n + 1, i); // cout << "N:" << n+1 << " mini:" << mini << " i:" << i <<" e:"<< (i * p - // q) / e <<" "<< i * q / e<< endl; } } int main() { cin >> P >> Q >> A >> N; while (P != 0) { cot = 0; dfs(P, Q, 1, 0, 1); cout << cot << endl; cin >> P >> Q >> A >> N; } }
replace
59
62
59
61
TLE
p00712
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define INF 1.1e9 #define LINF 1.1e18 #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 pb push_back #define pf push_front #define fi first #define se second #define BIT(x, n) bitset<n>(x) #define PI 3.14159265358979323846 typedef long long ll; typedef pair<ll, int> P; typedef pair<ll, P> PP; //----------------------------------------------------------------------------- int p, q, a, n, num[7], ans; void dfs(int x, int y, int z) { int sum = 0; REP(i, x) sum += y / num[i]; if (p * y == sum * q) ans++; if (p * y < sum * q) return; if (x == n) return; FOR(i, z, a + 1) { if (i * y <= a) { num[x] = i; dfs(x + 1, y * i, i); } } } int main() { cin.tie(0); ios::sync_with_stdio(false); while (true) { cin >> p >> q >> a >> n; if (p == 0 && q == 0) break; ans = 0; dfs(0, 1, 1); cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define INF 1.1e9 #define LINF 1.1e18 #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 pb push_back #define pf push_front #define fi first #define se second #define BIT(x, n) bitset<n>(x) #define PI 3.14159265358979323846 typedef long long ll; typedef pair<ll, int> P; typedef pair<ll, P> PP; //----------------------------------------------------------------------------- int p, q, a, n, num[7], ans; void dfs(int x, int y, int z) { int sum = 0; REP(i, x) sum += y / num[i]; if (p * y == sum * q) ans++; if (p * y < sum * q) return; if (x == n) return; FOR(i, z, a + 1) { if (i * y <= a) { num[x] = i; dfs(x + 1, y * i, i); } else break; } } int main() { cin.tie(0); ios::sync_with_stdio(false); while (true) { cin >> p >> q >> a >> n; if (p == 0 && q == 0) break; ans = 0; dfs(0, 1, 1); cout << ans << endl; } return 0; }
replace
36
37
36
38
TLE
p00712
C++
Time Limit Exceeded
#include <iostream> using namespace std; int P, Q, A, N; int P_, Q_; int gcd(int a, int b) { return a % b ? gcd(b, a % b) : b; } int solve(int a, int n, int sum, int b) { int res = 0; int div = gcd(sum, a); if (P_ == sum / div && Q_ == a / div) return 1; if (P_ * a < Q_ * sum) return 0; if (n >= N) return res; for (int i = b; a * i <= A; i++) res += solve(a * i, n + 1, sum * i + a, i); return res; } int main() { while (cin >> P >> Q >> A >> N, P | Q | A | N) { // cout << P << " " << Q << " " << A << " " << N << endl; int div = gcd(P, Q); // cout << div << endl; P_ = P / div; Q_ = Q / div; int res = 0; for (int i = 1; i <= A; i++) { int tmp; tmp = solve(i, 1, 1, i); if (tmp > 0) res += tmp; } cout << res << endl; } }
#include <iostream> using namespace std; int P, Q, A, N; int P_, Q_; int gcd(int a, int b) { return a % b ? gcd(b, a % b) : b; } int solve(int a, int n, int sum, int b) { int res = 0; /* int div = gcd(sum , a); if(P_ == sum / div && Q_ == a / div) return 1; // */ if (sum / P_ == a / Q_ && sum % P_ + a % Q_ == 0) return 1; if (P_ * a < Q_ * sum) return 0; if (n >= N) return res; for (int i = b; a * i <= A; i++) res += solve(a * i, n + 1, sum * i + a, i); return res; } int main() { while (cin >> P >> Q >> A >> N, P | Q | A | N) { // cout << P << " " << Q << " " << A << " " << N << endl; int div = gcd(P, Q); // cout << div << endl; P_ = P / div; Q_ = Q / div; int res = 0; for (int i = 1; i <= A; i++) { int tmp; tmp = solve(i, 1, 1, i); if (tmp > 0) res += tmp; } cout << res << endl; } }
replace
11
13
11
17
TLE
p00712
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep1(i, n) for (int i = 1; i <= (n); ++i) #define all(c) (c).begin(), (c).end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl; int p, q, a, n, ans; double eps = 1e-12; void dfs(int pro, int cnt, double sum, int mx) { if (abs(sum - (double)p / q) < eps) ans++; if (cnt == n) return; rep1(i, mx) { if (i * pro > a) break; dfs(i * pro, cnt + 1, sum + 1.0 / i, min(mx, i)); } } int main() { while (true) { cin >> p >> q >> a >> n; if (p == 0) break; ans = 0; dfs(1, 0, 0, a); cout << ans << endl; } }
#include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep1(i, n) for (int i = 1; i <= (n); ++i) #define all(c) (c).begin(), (c).end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl; int p, q, a, n, ans; double eps = 1e-12; void dfs(int pro, int cnt, double sum, int mx) { if (abs(sum - (double)p / q) < eps) ans++; if (sum + eps > (double)p / q) return; if (cnt == n) return; rep1(i, mx) { if (i * pro > a) break; dfs(i * pro, cnt + 1, sum + 1.0 / i, min(mx, i)); } } int main() { while (true) { cin >> p >> q >> a >> n; if (p == 0) break; ans = 0; dfs(1, 0, 0, a); cout << ans << endl; } }
insert
19
19
19
21
TLE
p00712
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long LL; static const double EPS = 1e-9; #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) struct flac { int p, q; flac(int p, int q) : p(p), q(q) {} }; flac sumf(flac f, flac g) { return flac(f.p * g.q + f.q * g.p, f.q * g.q); } bool operator==(flac f, flac g) { return (f.p * g.q == f.q * g.p); } bool operator<(flac f, flac g) { return (f.p * g.q < f.q * g.p); } int solve(flac sum, int &a, int n, flac &target, int bf) { if (sum == target) return 1; if (n == 0) return 0; int para = sum.q; int ret = 0; FOR(i, bf, a) { if (i * para > a) break; ret += solve(sumf(sum, flac(1, i)), a, n - 1, target, i); } return ret; } int main(void) { int p, q, a, n; while (cin >> p >> q >> a >> n) { if (!p && !q && !a && !n) break; flac tar = flac(p, q); cout << solve(flac(0, 1), a, n, tar, 1) << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long LL; static const double EPS = 1e-9; #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) struct flac { int p, q; flac(int p, int q) : p(p), q(q) {} }; flac sumf(flac f, flac g) { return flac(f.p * g.q + f.q * g.p, f.q * g.q); } bool operator==(flac f, flac g) { return (f.p * g.q == f.q * g.p); } bool operator<(flac f, flac g) { return (f.p * g.q < f.q * g.p); } int solve(flac sum, int &a, int n, flac &target, int bf) { if (sum == target) return 1; if (target < sum) return 0; if (n == 0) return 0; int para = sum.q; int ret = 0; FOR(i, bf, a) { if (i * para > a) break; ret += solve(sumf(sum, flac(1, i)), a, n - 1, target, i); } return ret; } int main(void) { int p, q, a, n; while (cin >> p >> q >> a >> n) { if (!p && !q && !a && !n) break; flac tar = flac(p, q); cout << solve(flac(0, 1), a, n, tar, 1) << endl; } return 0; }
insert
30
30
30
32
TLE
p00712
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; int memo[12001][8]; int b_pow(int a, int b) { if (memo[a][b]) return memo[a][b]; else if (b == 0) return 1; else if (b == 1) return a; else return memo[a][b] = b_pow(a, b / 2) * b_pow(a, (b + 1) / 2); } int ans, A; int dfs(int p, int q, int k, int a, int t) { if (a <= 0 || a > A) return 0; if (p == 0) return 1; if (t * a > A || k == 0 || p == 0) return 0; int ans = 0; for (int i = 0; i <= k; i++) { if (p * t >= i * q) { int w = p * t - i * q; int x = q * t; if (w != 0) { int g = __gcd(w, x); w /= g, x /= g; } ans += dfs(w, x, k - i, a * b_pow(t, i), t + 1); } else { break; } } return ans; } int main() { int a, b, c, d; while (cin >> a >> b >> c >> d, a || b || c || d) { A = c; cout << dfs(a, b, d, 1, 1) << endl; } }
#include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; int memo[12001][8]; int b_pow(int a, int b) { if (memo[a][b]) return memo[a][b]; else if (b == 0) return 1; else if (b == 1) return a; else return memo[a][b] = b_pow(a, b / 2) * b_pow(a, (b + 1) / 2); } int ans, A; int dfs(int p, int q, int k, int a, int t) { if (a <= 0 || a > A || p * t > k * q) return 0; if (p == 0) return 1; if (t * a > A || k == 0 || p == 0) return 0; int ans = 0; for (int i = 0; i <= k; i++) { if (p * t >= i * q) { int w = p * t - i * q; int x = q * t; if (w != 0) { int g = __gcd(w, x); w /= g, x /= g; } ans += dfs(w, x, k - i, a * b_pow(t, i), t + 1); } else { break; } } return ans; } int main() { int a, b, c, d; while (cin >> a >> b >> c >> d, a || b || c || d) { A = c; cout << dfs(a, b, d, 1, 1) << endl; } }
replace
19
20
19
20
TLE
p00712
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define LOG(...) printf(__VA_ARGS__) // #define LOG(...) #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) #define RSORT(c) sort((c).rbegin(), (c).rend()) #define CLR(a) memset((a), 0, sizeof(a)) typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; struct UnionFind { vector<int> v; UnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; } int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); } void unite(int x, int y) { v[find(x)] = find(y); } }; long long gcd(long long ag, long long bg) { if (ag < bg) swap(ag, bg); while (bg != 0) { ag %= bg; swap(ag, bg); } return ag; } long long p, q, a, n; long long dfs(long long bunsi, long long bunbo, long long mal, long long num, long long mi) { long long cnt = 0; if (num == 1 && bunsi != 1) return 0; if (bunsi == 1 && bunbo * mal <= a && bunbo >= mi) { cnt++; if (num <= 1) return 1; } if (bunsi == 0) return 0; if (mi * mal > a) return cnt; for (long long i = mi; i * mal <= a; i++) { if (bunsi * i < bunbo) continue; long long g = gcd(bunsi * i - bunbo, bunbo * i); cnt += dfs((bunsi * i - bunbo) / g, bunbo * i / g, mal * i, num - 1, i); } return cnt; } int main() { while (cin >> p >> q >> a >> n, p | q | a | n) { int g = gcd(p, q); cout << dfs(p / g, q / g, 1, n, 1) << endl; } }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define LOG(...) printf(__VA_ARGS__) // #define LOG(...) #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) #define RSORT(c) sort((c).rbegin(), (c).rend()) #define CLR(a) memset((a), 0, sizeof(a)) typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; struct UnionFind { vector<int> v; UnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; } int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); } void unite(int x, int y) { v[find(x)] = find(y); } }; long long gcd(long long ag, long long bg) { if (ag < bg) swap(ag, bg); while (bg != 0) { ag %= bg; swap(ag, bg); } return ag; } long long p, q, a, n; long long dfs(long long bunsi, long long bunbo, long long mal, long long num, long long mi) { long long cnt = 0; if (num == 1 && bunsi != 1) return 0; if (bunsi == 1 && bunbo * mal <= a && bunbo >= mi) { cnt++; if (num <= 1) return 1; } if (bunsi == 0) return 0; if (mi * mal > a) return cnt; for (long long i = mi; i * mal <= a; i++) { if (bunsi * i < bunbo) continue; if (num * bunbo < bunsi * i) break; long long g = gcd(bunsi * i - bunbo, bunbo * i); cnt += dfs((bunsi * i - bunbo) / g, bunbo * i / g, mal * i, num - 1, i); } return cnt; } int main() { while (cin >> p >> q >> a >> n, p | q | a | n) { int g = gcd(p, q); cout << dfs(p / g, q / g, 1, n, 1) << endl; } }
insert
83
83
83
85
TLE
p00713
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <vector> #define rep(n) for (int i = 0; i < n; i++) #define repp(j, n) for (int j = 0; j < n; j++) #define reppp(i, m, n) for (int i = m; i < n; i++) #define all(c) c.begin(), c.end() #define rall(c) c.rbegin(), c.rend() #define debug(x) cerr << #x << ": " << x << endl using namespace std; typedef long long ll; typedef pair<ll, ll> Pll; typedef pair<int, int> Pii; constexpr long double EPS = 10e-6; constexpr long double radius = 1.0 + EPS; constexpr long double diameter = 2.0 + EPS; class Point { public: long double x, y; Point(long double x = 0.0, long double y = 0.0) : x(x), y(y) {} Point operator+(Point p) const { return Point(x + p.x, y + p.y); } Point operator-(Point p) const { return Point(x - p.x, y - p.y); } Point operator*(long double a) const { return Point(x * a, y * a); } Point operator/(long double a) const { return Point(x / a, y / a); } long double norm() const { return x * x + y * y; } long double dot(Point p) const { return x * p.x + y * p.y; } }; int main() { int n; while (scanf("%d", &n) && n) { vector<Point> points(n); rep(n) scanf("%Lf %Lf", &points[i].x, &points[i].y); int ans = 1, tmp; Point mid, midv, center; long double c; rep(n) { repp(j, n) { if (j == i) continue; mid = points[j] - points[i]; if (mid.norm() > diameter * diameter) continue; mid = mid * 0.5; midv = Point(mid.y, -mid.x); c = sqrt((1.0 - mid.norm()) / midv.norm()); center = points[i] + mid + midv * c; tmp = 0; for (Point p : points) { Point v = p - center; if (v.norm() <= radius) tmp++; } ans = max(tmp, ans); } } printf("%d\n", ans); debug(ans); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <vector> #define rep(n) for (int i = 0; i < n; i++) #define repp(j, n) for (int j = 0; j < n; j++) #define reppp(i, m, n) for (int i = m; i < n; i++) #define all(c) c.begin(), c.end() #define rall(c) c.rbegin(), c.rend() #define debug(x) cerr << #x << ": " << x << endl using namespace std; typedef long long ll; typedef pair<ll, ll> Pll; typedef pair<int, int> Pii; constexpr long double EPS = 10e-6; constexpr long double radius = 1.0 + EPS; constexpr long double diameter = 2.0 + EPS; class Point { public: long double x, y; Point(long double x = 0.0, long double y = 0.0) : x(x), y(y) {} Point operator+(Point p) const { return Point(x + p.x, y + p.y); } Point operator-(Point p) const { return Point(x - p.x, y - p.y); } Point operator*(long double a) const { return Point(x * a, y * a); } Point operator/(long double a) const { return Point(x / a, y / a); } long double norm() const { return x * x + y * y; } long double dot(Point p) const { return x * p.x + y * p.y; } }; int main() { int n; while (scanf("%d", &n) && n) { vector<Point> points(n); rep(n) scanf("%Lf %Lf", &points[i].x, &points[i].y); int ans = 1, tmp; Point mid, midv, center; long double c; rep(n) { repp(j, n) { if (j == i) continue; mid = points[j] - points[i]; if (mid.norm() > diameter * diameter) continue; mid = mid * 0.5; midv = Point(mid.y, -mid.x); c = sqrt((1.0 - mid.norm()) / midv.norm()); center = points[i] + mid + midv * c; tmp = 0; for (Point p : points) { Point v = p - center; if (v.norm() <= radius) tmp++; } ans = max(tmp, ans); } } printf("%d\n", ans); } }
delete
65
66
65
65
0
ans: 2 ans: 5 ans: 5 ans: 11
p00713
C++
Runtime Error
#include <algorithm>/*{{{*/ #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; static const double PI(3.14159265358979323846); static const double EPS(1e-10); typedef long long int ll; typedef unsigned long long int ull; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef complex<double> P; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } #define FOR(i, b, e) for (typeof(b) i = (b); assert((i) <= (e)), i != (e); ++i) #define REP(i, n) FOR(i, 0, n) #define OPOVER(_op, _type) inline bool operator _op(const _type &t) const #define arrsz(a) (sizeof(a) / sizeof(a[0])) #define F first #define S second #define MP(a, b) make_pair(a, b) #define SZ(a) ((ll)a.size()) #define PB(e) push_back(e) #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define BIT(n) (1ULL << (n)) #define BITOF(n, m) ((n) >> (m)&1) #define RANGE(a, b, c) ((a) <= (b) && (b) <= (c)) #define DEBUG_ON #ifdef DEBUG_ON #define dprt(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__) #define darr(a) \ copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " ")); \ cerr << endl #define darr_range(a, f, t) \ copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " ")); \ cerr << endl #define dvec(v) \ copy(ALL(v), ostream_iterator<int>(cerr, " ")); \ cerr << endl #define darr2(a, n, m) \ FOR(i, 0, (n)) { darr_range((a)[i], 0, (m)); } #define dvec2(v) \ FOR(i, 0, SZ(v)) { dvec((v)[i]); } #define WAIT() \ { \ string _wait_; \ cerr << "(hit return to continue)" << endl; \ getline(cin, _wait_); \ } #else #define dprt(fmt, ...) #define darr(a) #define darr_range(a, f, t) #define dvec(v) #define darr2(a, n, m) #define dvec2(v) #define WAIT() #endif /*}}}*/ int N; vector<P> points; P middle_point(P a, P b) { return a + (b - a) / 2.0; } // 点pを中心としてr(radian)回転 p(0,0)で原点を中心として回転 P rotate(P t, P p, double r) { // double r=radians(angle); return (t - p) * P(cos(r), sin(r)) + p; } // 2円 |x-a|=raと|x-b|=rbの交点計算 bool intersection_c_c(P a, double ra, P b, double rb, P ans[]) { double di = abs(a - b); if (di > ra + rb || di < abs(ra - rb)) return false; double t = (ra * ra - rb * rb + di * di) / (di + di); double rd = acos(t / ra); P dv = (b - a) / abs(b - a); P g1 = rotate(dv, P(0, 0), rd); P g2 = rotate(dv, P(0, 0), -rd); ans[0] = a + g1 * ra; ans[1] = a + g2 * ra; return true; } int main(int argc, char const *argv[]) { while (cin >> N, N) { int ans = 1; points.clear(); REP(i, N) { double x, y; cin >> x >> y; points.PB(P(x, y)); } REP(i, N) { FOR(j, i + 1, N) { P p1 = points[i], p2 = points[j]; dprt("(%lf, %lf) and (%lf, %lf)\n", p1.real(), p1.imag(), p2.real(), p2.imag()); P centers[2]; if (intersection_c_c(p1, 1, p2, 1, centers)) { REP(k, 2) { dprt(" center: (%lf, %lf)\n", centers[k].real(), centers[k].imag()); int cnt = 0; REP(l, N) { if (abs(centers[k] - points[l]) < 1.0 + EPS) { ++cnt; } } ans = max(ans, cnt); } } } } cout << ans << endl; } return 0; }
#include <algorithm>/*{{{*/ #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; static const double PI(3.14159265358979323846); static const double EPS(1e-10); typedef long long int ll; typedef unsigned long long int ull; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef complex<double> P; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } #define FOR(i, b, e) for (typeof(b) i = (b); assert((i) <= (e)), i != (e); ++i) #define REP(i, n) FOR(i, 0, n) #define OPOVER(_op, _type) inline bool operator _op(const _type &t) const #define arrsz(a) (sizeof(a) / sizeof(a[0])) #define F first #define S second #define MP(a, b) make_pair(a, b) #define SZ(a) ((ll)a.size()) #define PB(e) push_back(e) #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define BIT(n) (1ULL << (n)) #define BITOF(n, m) ((n) >> (m)&1) #define RANGE(a, b, c) ((a) <= (b) && (b) <= (c)) #define DEBUG_ON_ #ifdef DEBUG_ON #define dprt(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__) #define darr(a) \ copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " ")); \ cerr << endl #define darr_range(a, f, t) \ copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " ")); \ cerr << endl #define dvec(v) \ copy(ALL(v), ostream_iterator<int>(cerr, " ")); \ cerr << endl #define darr2(a, n, m) \ FOR(i, 0, (n)) { darr_range((a)[i], 0, (m)); } #define dvec2(v) \ FOR(i, 0, SZ(v)) { dvec((v)[i]); } #define WAIT() \ { \ string _wait_; \ cerr << "(hit return to continue)" << endl; \ getline(cin, _wait_); \ } #else #define dprt(fmt, ...) #define darr(a) #define darr_range(a, f, t) #define dvec(v) #define darr2(a, n, m) #define dvec2(v) #define WAIT() #endif /*}}}*/ int N; vector<P> points; P middle_point(P a, P b) { return a + (b - a) / 2.0; } // 点pを中心としてr(radian)回転 p(0,0)で原点を中心として回転 P rotate(P t, P p, double r) { // double r=radians(angle); return (t - p) * P(cos(r), sin(r)) + p; } // 2円 |x-a|=raと|x-b|=rbの交点計算 bool intersection_c_c(P a, double ra, P b, double rb, P ans[]) { double di = abs(a - b); if (di > ra + rb || di < abs(ra - rb)) return false; double t = (ra * ra - rb * rb + di * di) / (di + di); double rd = acos(t / ra); P dv = (b - a) / abs(b - a); P g1 = rotate(dv, P(0, 0), rd); P g2 = rotate(dv, P(0, 0), -rd); ans[0] = a + g1 * ra; ans[1] = a + g2 * ra; return true; } int main(int argc, char const *argv[]) { while (cin >> N, N) { int ans = 1; points.clear(); REP(i, N) { double x, y; cin >> x >> y; points.PB(P(x, y)); } REP(i, N) { FOR(j, i + 1, N) { P p1 = points[i], p2 = points[j]; dprt("(%lf, %lf) and (%lf, %lf)\n", p1.real(), p1.imag(), p2.real(), p2.imag()); P centers[2]; if (intersection_c_c(p1, 1, p2, 1, centers)) { REP(k, 2) { dprt(" center: (%lf, %lf)\n", centers[k].real(), centers[k].imag()); int cnt = 0; REP(l, N) { if (abs(centers[k] - points[l]) < 1.0 + EPS) { ++cnt; } } ans = max(ans, cnt); } } } } cout << ans << endl; } return 0; }
replace
70
71
70
71
0
(6.476340, 7.696280) and (5.168280, 4.799150) (6.476340, 7.696280) and (6.695330, 6.203780) center: (7.235478, 7.045350) center: (5.936192, 6.854710) (5.168280, 4.799150) and (6.695330, 6.203780) (7.152960, 4.083280) and (6.508270, 2.694660) center: (7.414234, 3.118015) center: (6.246996, 3.659925) (7.152960, 4.083280) and (5.912190, 3.866610) center: (6.666198, 3.209745) center: (6.398952, 4.740145) (7.152960, 4.083280) and (5.298530, 4.160970) center: (6.210153, 3.749942) center: (6.241337, 4.494308) (7.152960, 4.083280) and (6.108380, 3.460390) center: (7.037255, 3.089996) center: (6.224085, 4.453674) (7.152960, 4.083280) and (6.340600, 2.415990) center: (7.083209, 3.085716) center: (6.410351, 3.413554) (6.508270, 2.694660) and (5.912190, 3.866610) center: (5.538586, 2.939022) center: (6.881874, 3.622248) (6.508270, 2.694660) and (5.298530, 4.160970) center: (5.663636, 3.230004) center: (6.143164, 3.625626) (6.508270, 2.694660) and (6.108380, 3.460390) center: (5.508870, 2.660023) center: (7.107780, 3.495027) (6.508270, 2.694660) and (6.340600, 2.415990) center: (7.269888, 2.046633) center: (5.578982, 3.064017) (5.912190, 3.866610) and (5.298530, 4.160970) center: (5.198677, 3.165968) center: (6.012043, 4.861612) (5.912190, 3.866610) and (6.108380, 3.460390) center: (6.887558, 4.087192) center: (5.133012, 3.239808) (5.912190, 3.866610) and (6.340600, 2.415990) center: (6.753853, 3.326606) center: (5.498937, 2.955994) (5.298530, 4.160970) and (6.108380, 3.460390) center: (6.256022, 4.449431) center: (5.150888, 3.171929) (5.298530, 4.160970) and (6.340600, 2.415990) (6.108380, 3.460390) and (6.340600, 2.415990) center: (7.049231, 3.121569) center: (5.399749, 2.754811) (7.906500, 4.017460) and (4.109980, 4.183540) (7.906500, 4.017460) and (4.672890, 4.018870) (7.906500, 4.017460) and (6.338850, 4.283880) center: (7.021054, 3.552718) center: (7.224296, 4.748622) (7.906500, 4.017460) and (4.981060, 3.827280) (7.906500, 4.017460) and (5.123790, 5.164730) (7.906500, 4.017460) and (7.846640, 4.676930) center: (6.936834, 4.261895) center: (8.816306, 4.432495) (7.906500, 4.017460) and (4.027760, 3.879900) (4.109980, 4.183540) and (4.672890, 4.018870) center: (4.659858, 5.018785) center: (4.123012, 3.183625) (4.109980, 4.183540) and (6.338850, 4.283880) (4.109980, 4.183540) and (4.981060, 3.827280) center: (4.879541, 4.822114) center: (4.211499, 3.188706) (4.109980, 4.183540) and (5.123790, 5.164730) center: (4.123965, 5.183442) center: (5.109805, 4.164828) (4.109980, 4.183540) and (7.846640, 4.676930) (4.109980, 4.183540) and (4.027760, 3.879900) center: (5.022095, 3.773605) center: (3.115645, 4.289835) (4.672890, 4.018870) and (6.338850, 4.283880) center: (5.421476, 4.681907) center: (5.590264, 3.620843) (4.672890, 4.018870) and (4.981060, 3.827280) center: (5.346196, 4.758234) center: (4.307754, 3.087916) (4.672890, 4.018870) and (5.123790, 5.164730) center: (4.165081, 4.880340) center: (5.631599, 4.303260) (4.672890, 4.018870) and (7.846640, 4.676930) (4.672890, 4.018870) and (4.027760, 3.879900) center: (4.549114, 3.026560) center: (4.151536, 4.872210) (6.338850, 4.283880) and (4.981060, 3.827280) center: (5.882386, 3.394138) center: (5.437524, 4.717022) (6.338850, 4.283880) and (5.123790, 5.164730) center: (5.343349, 4.189131) center: (6.119291, 5.259479) (6.338850, 4.283880) and (7.846640, 4.676930) center: (6.934606, 5.087045) center: (7.250884, 3.873765) (6.338850, 4.283880) and (4.027760, 3.879900) (4.981060, 3.827280) and (5.123790, 5.164730) center: (4.316527, 4.574539) center: (5.788323, 4.417471) (4.981060, 3.827280) and (7.846640, 4.676930) (4.981060, 3.827280) and (4.027760, 3.879900) center: (4.455982, 2.976226) center: (4.552838, 4.730954) (5.123790, 5.164730) and (7.846640, 4.676930) (5.123790, 5.164730) and (4.027760, 3.879900) center: (4.983337, 4.174643) center: (4.168213, 4.869987) (7.846640, 4.676930) and (4.027760, 3.879900) (6.651280, 5.474900) and (6.427430, 6.261890) center: (5.661681, 5.618751) center: (7.417029, 6.118039) (6.651280, 5.474900) and (6.358640, 4.616110) center: (7.348514, 4.758057) center: (5.661406, 5.332953) (6.651280, 5.474900) and (6.590200, 4.542280) center: (7.502944, 4.950812) center: (5.738536, 5.066368) (6.651280, 5.474900) and (4.439670, 5.700590) (6.651280, 5.474900) and (4.382260, 5.705360) (6.651280, 5.474900) and (5.507550, 6.181630) center: (5.690249, 5.198461) center: (6.468581, 6.458069) (6.651280, 5.474900) and (7.419710, 6.136680) center: (6.473041, 6.458887) center: (7.597949, 5.152693) (6.651280, 5.474900) and (6.719360, 3.044960) (6.651280, 5.474900) and (5.618320, 4.238570) center: (6.589527, 4.476809) center: (5.680073, 5.236661) (6.651280, 5.474900) and (5.994240, 4.293280) center: (6.966789, 4.525977) center: (5.678731, 5.242203) (6.651280, 5.474900) and (5.609610, 4.329980) center: (6.598851, 4.476275) center: (5.662039, 5.328605) (6.651280, 5.474900) and (6.822420, 5.796830) center: (5.868660, 6.097400) center: (7.605040, 5.174330) (6.651280, 5.474900) and (5.446930, 3.827240) (6.651280, 5.474900) and (6.709060, 3.657360) center: (7.096256, 4.579357) center: (6.264084, 4.552903) (6.651280, 5.474900) and (7.890870, 5.680000) center: (7.144070, 6.345048) center: (7.398080, 4.809852) (6.651280, 5.474900) and (6.233000, 4.595300) center: (7.230904, 4.660016) center: (5.653376, 5.410184) (6.651280, 5.474900) and (5.924010, 4.923290) center: (6.825344, 4.490166) center: (5.749946, 5.908024) (6.651280, 5.474900) and (6.241680, 3.813890) center: (6.949409, 4.520374) center: (5.943551, 4.768416) (6.651280, 5.474900) and (6.226710, 3.622100) center: (6.742133, 4.479036) center: (6.135857, 4.617964) (6.427430, 6.261890) and (6.358640, 4.616110) center: (6.959699, 5.415315) center: (5.826371, 5.462685) (6.427430, 6.261890) and (6.590200, 4.542280) center: (7.010667, 5.449588) center: (6.006963, 5.354582) (6.427430, 6.261890) and (4.439670, 5.700590) (6.427430, 6.261890) and (4.382260, 5.705360) (6.427430, 6.261890) and (5.507550, 6.181630) center: (6.044592, 5.338075) center: (5.890388, 7.105445) (6.427430, 6.261890) and (7.419710, 6.136680) center: (7.031984, 7.058454) center: (6.815156, 5.340116) (6.427430, 6.261890) and (6.719360, 3.044960) (6.427430, 6.261890) and (5.618320, 4.238570) (6.427430, 6.261890) and (5.994240, 4.293280) (6.427430, 6.261890) and (5.609610, 4.329980) (6.427430, 6.261890) and (6.822420, 5.796830) center: (7.350779, 6.645851) center: (5.899071, 5.412869) (6.427430, 6.261890) and (5.446930, 3.827240) (6.427430, 6.261890) and (6.709060, 3.657360) (6.427430, 6.261890) and (7.890870, 5.680000) center: (7.386895, 6.543718) center: (6.931405, 5.398172) (6.427430, 6.261890) and (6.233000, 4.595300) center: (6.870763, 5.365533) center: (5.789667, 5.491657) (6.427430, 6.261890) and (5.924010, 4.923290) center: (6.830034, 5.346516) center: (5.521406, 5.838664) (6.427430, 6.261890) and (6.241680, 3.813890) (6.427430, 6.261890) and (6.226710, 3.622100) (6.358640, 4.616110) and (6.590200, 4.542280) center: (6.775939, 5.524879) center: (6.172901, 3.633511) (6.358640, 4.616110) and (4.439670, 5.700590) (6.358640, 4.616110) and (4.382260, 5.705360) (6.358640, 4.616110) and (5.507550, 6.181630) center: (5.534148, 5.181984) center: (6.332042, 5.615756) (6.358640, 4.616110) and (7.419710, 6.136680) center: (6.581784, 5.590895) center: (7.196566, 5.161895) (6.358640, 4.616110) and (6.719360, 3.044960) center: (7.115888, 3.962983) center: (5.962112, 3.698087) (6.358640, 4.616110) and (5.618320, 4.238570) center: (6.401709, 3.617038) center: (5.575251, 5.237642) (6.358640, 4.616110) and (5.994240, 4.293280) center: (6.819617, 3.728698) center: (5.533263, 5.180692) (6.358640, 4.616110) and (5.609610, 4.329980) center: (6.311042, 3.617243) center: (5.657208, 5.328847) (6.358640, 4.616110) and (6.822420, 5.796830) center: (5.870939, 5.489121) center: (7.310121, 4.923819) (6.358640, 4.616110) and (5.446930, 3.827240) center: (6.424860, 3.618305) center: (5.380710, 4.825045) (6.358640, 4.616110) and (6.709060, 3.657360) center: (7.341535, 4.431941) center: (5.726165, 3.841529) (6.358640, 4.616110) and (7.890870, 5.680000) center: (6.919036, 5.444335) center: (7.330474, 4.851775) (6.358640, 4.616110) and (6.233000, 4.595300) center: (6.458894, 3.621148) center: (6.132746, 5.590262) (6.358640, 4.616110) and (5.924010, 4.923290) center: (5.584974, 3.982517) center: (6.697676, 5.556883) (6.358640, 4.616110) and (6.241680, 3.813890) center: (7.204758, 4.083114) center: (5.395562, 4.346886) (6.358640, 4.616110) and (6.226710, 3.622100) center: (7.150390, 4.005265) center: (5.434960, 4.232945) (6.590200, 4.542280) and (4.439670, 5.700590) (6.590200, 4.542280) and (4.382260, 5.705360) (6.590200, 4.542280) and (5.507550, 6.181630) center: (5.892540, 5.258709) center: (6.205210, 5.465201) (6.590200, 4.542280) and (7.419710, 6.136680) center: (6.615782, 5.541953) center: (7.394128, 5.137007) (6.590200, 4.542280) and (6.719360, 3.044960) center: (7.312140, 3.850324) center: (5.997420, 3.736916) (6.590200, 4.542280) and (5.618320, 4.238570) center: (6.360983, 3.568905) center: (5.847537, 5.211945) (6.590200, 4.542280) and (5.994240, 4.293280) center: (6.657080, 3.544519) center: (5.927360, 5.291041) (6.590200, 4.542280) and (5.609610, 4.329980) center: (6.282953, 3.590650) center: (5.916857, 5.281610) (6.590200, 4.542280) and (6.822420, 5.796830) center: (5.949079, 5.309720) center: (7.463541, 5.029390) (6.590200, 4.542280) and (5.446930, 3.827240) center: (6.410176, 3.558618) center: (5.626954, 4.810902) (6.590200, 4.542280) and (6.709060, 3.657360) center: (7.536483, 4.218940) center: (5.762777, 3.980700) (6.590200, 4.542280) and (7.890870, 5.680000) center: (6.909071, 5.490078) center: (7.571999, 4.732202) (6.590200, 4.542280) and (6.233000, 4.595300) center: (6.267189, 3.595885) center: (6.556011, 5.541695) (6.590200, 4.542280) and (5.924010, 4.923290) center: (5.798647, 3.931179) center: (6.715563, 5.534391) (6.590200, 4.542280) and (6.241680, 3.813890) center: (7.241209, 3.783210) center: (5.590671, 4.572960) (6.590200, 4.542280) and (6.226710, 3.622100) center: (7.216748, 3.762897) center: (5.600162, 4.401483) (4.439670, 5.700590) and (4.382260, 5.705360) center: (4.328198, 4.706822) center: (4.493732, 6.699128) (4.439670, 5.700590) and (5.507550, 6.181630) center: (4.640688, 6.680178) center: (5.306532, 5.202042) (4.439670, 5.700590) and (7.419710, 6.136680) (4.439670, 5.700590) and (6.719360, 3.044960) (4.439670, 5.700590) and (5.618320, 4.238570) center: (5.296787, 5.185468) center: (4.761203, 4.753692) (4.439670, 5.700590) and (5.994240, 4.293280) (4.439670, 5.700590) and (5.609610, 4.329980) center: (5.354571, 5.296911) center: (4.694709, 4.733659) (4.439670, 5.700590) and (6.822420, 5.796830) (4.439670, 5.700590) and (5.446930, 3.827240) (4.439670, 5.700590) and (6.709060, 3.657360) (4.439670, 5.700590) and (7.890870, 5.680000) (4.439670, 5.700590) and (6.233000, 4.595300) (4.439670, 5.700590) and (5.924010, 4.923290) center: (5.435142, 5.795648) center: (4.928538, 4.828232) (4.439670, 5.700590) and (6.241680, 3.813890) (4.439670, 5.700590) and (6.226710, 3.622100) (4.382260, 5.705360) and (5.507550, 6.181630) center: (4.636341, 6.672543) center: (5.253469, 5.214447) (4.382260, 5.705360) and (7.419710, 6.136680) (4.382260, 5.705360) and (6.719360, 3.044960) (4.382260, 5.705360) and (5.618320, 4.238570) center: (5.216807, 5.154423) center: (4.783773, 4.789507) (4.382260, 5.705360) and (5.994240, 4.293280) (4.382260, 5.705360) and (5.609610, 4.329980) center: (5.285372, 5.275956) center: (4.706498, 4.759384) (4.382260, 5.705360) and (6.822420, 5.796830) (4.382260, 5.705360) and (5.446930, 3.827240) (4.382260, 5.705360) and (6.709060, 3.657360) (4.382260, 5.705360) and (7.890870, 5.680000) (4.382260, 5.705360) and (6.233000, 4.595300) (4.382260, 5.705360) and (5.924010, 4.923290) center: (5.380611, 5.762765) center: (4.925659, 4.865885) (4.382260, 5.705360) and (6.241680, 3.813890) (4.382260, 5.705360) and (6.226710, 3.622100) (5.507550, 6.181630) and (7.419710, 6.136680) center: (6.470498, 6.451317) center: (6.456762, 5.866993) (5.507550, 6.181630) and (6.719360, 3.044960) (5.507550, 6.181630) and (5.618320, 4.238570) center: (5.792913, 5.223211) center: (5.332957, 5.196989) (5.507550, 6.181630) and (5.994240, 4.293280) center: (5.965941, 5.292880) center: (5.535849, 5.182030) (5.507550, 6.181630) and (5.609610, 4.329980) center: (5.932504, 5.276415) center: (5.184656, 5.235195) (5.507550, 6.181630) and (6.822420, 5.796830) center: (6.369610, 6.688437) center: (5.960360, 5.290023) (5.507550, 6.181630) and (5.446930, 3.827240) (5.507550, 6.181630) and (6.709060, 3.657360) (5.507550, 6.181630) and (7.890870, 5.680000) (5.507550, 6.181630) and (6.233000, 4.595300) center: (6.315164, 5.591919) center: (5.425386, 5.185011) (5.507550, 6.181630) and (5.924010, 4.923290) center: (6.426712, 5.787750) center: (5.004848, 5.317170) (5.507550, 6.181630) and (6.241680, 3.813890) (5.507550, 6.181630) and (6.226710, 3.622100) (7.419710, 6.136680) and (6.719360, 3.044960) (7.419710, 6.136680) and (5.618320, 4.238570) (7.419710, 6.136680) and (5.994240, 4.293280) (7.419710, 6.136680) and (5.609610, 4.329980) (7.419710, 6.136680) and (6.822420, 5.796830) center: (7.585493, 5.150518) center: (6.656637, 6.782992) (7.419710, 6.136680) and (5.446930, 3.827240) (7.419710, 6.136680) and (6.709060, 3.657360) (7.419710, 6.136680) and (7.890870, 5.680000) center: (8.312754, 6.586650) center: (6.997826, 5.230030) (7.419710, 6.136680) and (6.233000, 4.595300) center: (7.010428, 5.224272) center: (6.642282, 5.507708) (7.419710, 6.136680) and (5.924010, 4.923290) center: (6.841661, 5.320678) center: (6.502059, 5.739292) (7.419710, 6.136680) and (6.241680, 3.813890) (7.419710, 6.136680) and (6.226710, 3.622100) (6.719360, 3.044960) and (5.618320, 4.238570) center: (5.739771, 3.245973) center: (6.597909, 4.037557) (6.719360, 3.044960) and (5.994240, 4.293280) center: (5.758357, 3.321498) center: (6.955243, 4.016742) (6.719360, 3.044960) and (5.609610, 4.329980) center: (5.764510, 3.342050) center: (6.564460, 4.032890) (6.719360, 3.044960) and (6.822420, 5.796830) (6.719360, 3.044960) and (5.446930, 3.827240) center: (5.734858, 2.869588) center: (6.431432, 4.002612) (6.719360, 3.044960) and (6.709060, 3.657360) center: (5.762391, 3.335151) center: (7.666029, 3.367169) (6.719360, 3.044960) and (7.890870, 5.680000) (6.719360, 3.044960) and (6.233000, 4.595300) center: (5.919840, 3.645600) center: (7.032520, 3.994660) (6.719360, 3.044960) and (5.924010, 4.923290) (6.719360, 3.044960) and (6.241680, 3.813890) center: (5.723072, 2.958878) center: (7.237968, 3.899972) (6.719360, 3.044960) and (6.226710, 3.622100) center: (5.769319, 2.732834) center: (7.176751, 3.934226) (5.618320, 4.238570) and (5.994240, 4.293280) center: (5.664883, 5.237485) center: (5.947677, 3.294365) (5.618320, 4.238570) and (5.609610, 4.329980) center: (4.619524, 4.189520) center: (6.608406, 4.379030) (5.618320, 4.238570) and (6.822420, 5.796830) center: (6.082195, 5.124471) center: (6.358545, 4.910929) (5.618320, 4.238570) and (5.446930, 3.827240) center: (6.432497, 3.657953) center: (4.632753, 4.407857) (5.618320, 4.238570) and (6.709060, 3.657360) center: (6.533413, 4.641813) center: (5.793967, 3.254117) (5.618320, 4.238570) and (7.890870, 5.680000) (5.618320, 4.238570) and (6.233000, 4.595300) center: (5.456475, 5.225386) center: (6.394845, 3.608484) (5.618320, 4.238570) and (5.924010, 4.923290) center: (4.924643, 4.958856) center: (6.617687, 4.203004) (5.618320, 4.238570) and (6.241680, 3.813890) center: (6.451455, 4.791640) center: (5.408545, 3.260820) (5.618320, 4.238570) and (6.226710, 3.622100) center: (6.564066, 4.563477) center: (5.280964, 3.297193) (5.994240, 4.293280) and (5.609610, 4.329980) center: (5.708729, 3.334904) center: (5.895121, 5.288356) (5.994240, 4.293280) and (6.822420, 5.796830) center: (5.958820, 5.292653) center: (6.857840, 4.797457) (5.994240, 4.293280) and (5.446930, 3.827240) center: (6.325577, 3.349767) center: (5.115593, 4.770753) (5.994240, 4.293280) and (6.709060, 3.657360) center: (6.935335, 4.631424) center: (5.767965, 3.319216) (5.994240, 4.293280) and (7.890870, 5.680000) (5.994240, 4.293280) and (6.233000, 4.595300) center: (5.343817, 5.052853) center: (6.883423, 3.835727) (5.994240, 4.293280) and (5.924010, 4.923290) center: (5.016523, 4.503209) center: (6.901727, 4.713361) (5.994240, 4.293280) and (6.241680, 3.813890) center: (6.973633, 4.495246) center: (5.262287, 3.611924) (5.994240, 4.293280) and (6.226710, 3.622100) center: (6.993801, 4.263639) center: (5.227149, 3.651741) (5.609610, 4.329980) and (6.822420, 5.796830) center: (5.979274, 5.259146) center: (6.452756, 4.867664) (5.609610, 4.329980) and (5.446930, 3.827240) center: (6.445892, 3.781680) center: (4.610648, 4.375540) (5.609610, 4.329980) and (6.709060, 3.657360) center: (6.558382, 4.645943) center: (5.760288, 3.341397) (5.609610, 4.329980) and (7.890870, 5.680000) (5.609610, 4.329980) and (6.233000, 4.595300) center: (5.552844, 5.328368) center: (6.289766, 3.596912) (5.609610, 4.329980) and (5.924010, 4.923290) center: (4.934490, 5.067688) center: (6.599130, 4.185582) (5.609610, 4.329980) and (6.241680, 3.813890) center: (6.503070, 4.779123) center: (5.348220, 3.364747) (5.609610, 4.329980) and (6.226710, 3.622100) center: (6.583683, 4.556215) center: (5.252637, 3.395865) (6.822420, 5.796830) and (5.446930, 3.827240) (6.822420, 5.796830) and (6.709060, 3.657360) (6.822420, 5.796830) and (7.890870, 5.680000) center: (7.448312, 6.576740) center: (7.264978, 4.900090) (6.822420, 5.796830) and (6.233000, 4.595300) center: (7.194877, 4.868781) center: (5.860543, 5.523349) (6.822420, 5.796830) and (5.924010, 4.923290) center: (6.916538, 4.801269) center: (5.829892, 5.918851) (6.822420, 5.796830) and (6.241680, 3.813890) (6.822420, 5.796830) and (6.226710, 3.622100) (5.446930, 3.827240) and (6.709060, 3.657360) center: (6.180851, 4.506475) center: (5.975139, 2.978125) (5.446930, 3.827240) and (7.890870, 5.680000) (5.446930, 3.827240) and (6.233000, 4.595300) center: (5.256069, 4.808857) center: (6.423861, 3.613683) (5.446930, 3.827240) and (5.924010, 4.923290) center: (4.950361, 4.695237) center: (6.420579, 4.055293) (5.446930, 3.827240) and (6.241680, 3.813890) center: (5.859717, 4.738068) center: (5.828893, 2.903062) (5.446930, 3.827240) and (6.226710, 3.622100) center: (6.069645, 4.609688) center: (5.603995, 2.839652) (6.709060, 3.657360) and (7.890870, 5.680000) (6.709060, 3.657360) and (6.233000, 4.595300) center: (5.712596, 3.741380) center: (7.229464, 4.511280) (6.709060, 3.657360) and (5.924010, 4.923290) center: (5.749436, 3.938646) center: (6.883634, 4.642004) (6.709060, 3.657360) and (6.241680, 3.813890) center: (6.167593, 2.816638) center: (6.783147, 4.654612) (6.709060, 3.657360) and (6.226710, 3.622100) center: (6.538627, 2.671991) center: (6.397143, 4.607469) (7.890870, 5.680000) and (6.233000, 4.595300) center: (7.136851, 5.023147) center: (6.987019, 5.252153) (7.890870, 5.680000) and (5.924010, 4.923290) (7.890870, 5.680000) and (6.241680, 3.813890) (7.890870, 5.680000) and (6.226710, 3.622100) (6.233000, 4.595300) and (5.924010, 4.923290) center: (5.369345, 4.091216) center: (6.787665, 5.427374) (6.233000, 4.595300) and (6.241680, 3.813890) center: (7.157789, 4.214819) center: (5.316891, 4.194371) (6.233000, 4.595300) and (6.226710, 3.622100) center: (7.103456, 4.103054) center: (5.356254, 4.114346) (5.924010, 4.923290) and (6.241680, 3.813890) center: (6.868038, 4.593425) center: (5.297652, 4.143755) (5.924010, 4.923290) and (6.226710, 3.622100) center: (6.800195, 4.441316) center: (5.350525, 4.104074) (6.241680, 3.813890) and (6.226710, 3.622100) center: (7.226540, 3.640538) center: (5.241850, 3.795452)
p00714
C++
Time Limit Exceeded
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; class Cell { public: int parent; int brother; bool has_child; int water; int capacity; int height; int start_x; int end_x; Cell(int _h, int _sx, int _ex) : height(_h), start_x(_sx), end_x(_ex), parent(-1), brother(-1), water(0), has_child(false) { capacity = height * (end_x - start_x) * 30; } void pour_water(int _water) { water += _water; } double compute_water_level() { return (double)water / ((double)(end_x - start_x) * 30.0); } bool is_filled() { return (capacity <= water); } bool operator<(const Cell &c) const { return start_x < c.start_x; } bool operator<(const int _x) const { return start_x < _x; } }; struct Board { int x; int height; Board(int _x, int _h) : x(_x), height(_h) {} bool operator<(const Board &b) const { return x < b.x; } bool operator>(const Board &b) const { return x > b.x; } }; struct Faucet { int x; int cm3_per_second; Faucet(int _x, int _c) : x(_x), cm3_per_second(_c) {} Faucet() : x(0), cm3_per_second(0) {} bool operator<(const Faucet &f) const { return (x < f.x); } bool operator>(const Faucet &f) const { return (x > f.x); } bool operator==(const Faucet &f) const { return (x == f.x); } bool operator<(const int _x) const { return (x < _x); } bool operator>(const int _x) const { return (x > _x); } bool operator==(const int _x) const { return (x == _x); } }; int main() { int total_data_sets; while (~scanf("%d", &total_data_sets)) { for (int data_set_idx = 0; data_set_idx < total_data_sets; data_set_idx++) { vector<Cell> cells; vector<Board> boards; int total_boards; scanf("%d", &total_boards); boards.push_back(Board(0, 50)); for (int board_idx = 0; board_idx < total_boards; board_idx++) { int x, height; scanf("%d %d", &x, &height); boards.push_back(Board(x, height)); } boards.push_back(Board(100, 50)); sort(boards.begin(), boards.end()); for (int src_idx = 0; src_idx < boards.size(); src_idx++) { if (boards[src_idx].x == 100) continue; for (int target_idx = src_idx + 1; target_idx < boards.size(); target_idx++) { if (boards[target_idx].height < boards[src_idx].height) continue; else if (boards[target_idx].height >= boards[src_idx].height) { cells.push_back(Cell(boards[src_idx].height, boards[src_idx].x, boards[target_idx].x)); break; } } for (int target_idx = src_idx - 1; target_idx >= 0; target_idx--) { if (boards[target_idx].height < boards[src_idx].height) continue; else if (boards[target_idx].height > boards[src_idx].height) { cells.push_back(Cell(boards[src_idx].height, boards[target_idx].x, boards[src_idx].x)); break; } } } // set brother for (int src_idx = 0; src_idx < cells.size(); src_idx++) { int height = cells[src_idx].height; int target_wall[101]; memset(target_wall, 0, sizeof(target_wall)); for (int target_idx = 0; target_idx < cells.size(); target_idx++) { if (target_idx == src_idx) continue; if (cells[src_idx].end_x == cells[target_idx].start_x) { target_wall[cells[target_idx].start_x] = max(cells[target_idx].height, target_wall[cells[target_idx].start_x]); } if (cells[src_idx].start_x == cells[target_idx].end_x) { target_wall[cells[target_idx].end_x] = max( cells[target_idx].height, target_wall[cells[target_idx].end_x]); } } int diff = INF; for (int target_idx = 0; target_idx < cells.size(); target_idx++) { if (src_idx == target_idx) continue; if (cells[src_idx].end_x == cells[target_idx].start_x && height == target_wall[cells[target_idx].start_x] && diff > abs(cells[src_idx].end_x - cells[target_idx].end_x)) { diff = abs(cells[src_idx].end_x - cells[target_idx].end_x); cells[src_idx].brother = target_idx; } if (cells[src_idx].start_x == cells[target_idx].end_x && height == target_wall[cells[target_idx].end_x] && diff > abs(cells[src_idx].start_x - cells[target_idx].start_x)) { diff = abs(cells[src_idx].start_x - cells[target_idx].start_x); cells[src_idx].brother = target_idx; } } } // set parent for (int src_idx = 0; src_idx < cells.size(); src_idx++) { int min_height = INF; int lower_height = cells[src_idx].height; for (int target_idx = 0; target_idx < cells.size(); target_idx++) { if (src_idx == target_idx) continue; if (cells[src_idx].start_x >= cells[target_idx].start_x && cells[src_idx].end_x <= cells[target_idx].end_x && lower_height <= cells[target_idx].height && min_height > cells[target_idx].height) { min_height = cells[target_idx].height; cells[src_idx].parent = target_idx; } } if (cells[src_idx].parent != -1) { cells[cells[src_idx].parent].has_child = true; } } vector<Faucet> faucets; int total_faucets; scanf("%d", &total_faucets); for (int foucet_idx = 0; foucet_idx < total_faucets; foucet_idx++) { int x, cm3_per_second; scanf("%d %d", &x, &cm3_per_second); faucets.push_back(Faucet(x, cm3_per_second)); } sort(faucets.begin(), faucets.end()); int total_observation_times; scanf("%d", &total_observation_times); vector<Cell> store = cells; for (int observation_time_idx = 0; observation_time_idx < total_observation_times; observation_time_idx++) { int x, time; scanf("%d %d", &x, &time); for (int cell_idx = 0; cell_idx < cells.size(); cell_idx++) { for (int faucet_idx = 0; faucet_idx < faucets.size(); faucet_idx++) { // int faucet_idx = //lower_bound(faucets.begin(),faucets.end(),cells[cell_idx].start_x) //- faucets.begin(); if (faucet_idx == faucets.size()) continue; if (faucets[faucet_idx].x < cells[cell_idx].start_x) continue; if (faucets[faucet_idx].x > cells[cell_idx].end_x) continue; if (cells[cell_idx].has_child) continue; cells[cell_idx].pour_water(faucets[faucet_idx].cm3_per_second * time); } } bool is_erased[1001]; memset(is_erased, false, sizeof(is_erased)); for (int round = 0; round < 1000000; round++) { for (int src_idx = 0; src_idx < cells.size(); src_idx++) { if (is_erased[src_idx]) continue; if (!cells[src_idx].is_filled()) continue; int freq[1001]; memset(freq, 0, sizeof(freq)); int limit = cells.size() * 3; int cell_idx = src_idx; bool isok = true; while (limit-- > 0) { if (!cells[cell_idx].is_filled()) break; freq[cell_idx]++; if (freq[cell_idx] >= 3) isok = false; int brother = cells[cell_idx].brother; if (brother == -1) break; cells[brother].water += (cells[cell_idx].water - cells[cell_idx].capacity); cells[cell_idx].water = cells[cell_idx].capacity; cell_idx = brother; } if (isok) continue; int sx = INF; int ex = 0; int water = 0; for (int cell_idx = 0; cell_idx < cells.size(); cell_idx++) { if (is_erased[cell_idx]) continue; if (freq[cell_idx] >= 3) { sx = min(sx, cells[cell_idx].start_x); ex = max(ex, cells[cell_idx].end_x); water += cells[cell_idx].water; cells[cell_idx].water = 0; is_erased[cell_idx] = true; } } int new_cell_idx = 0; for (int cell_idx = 0; cell_idx < cells.size(); cell_idx++) { if (is_erased[cell_idx]) continue; if (sx == cells[cell_idx].start_x && ex == cells[cell_idx].end_x) { cells[cell_idx].water = water; new_cell_idx = cell_idx; break; } } for (int cell_idx = 0; cell_idx < cells.size(); cell_idx++) { if (is_erased[cell_idx]) continue; if (cells[cell_idx].brother != -1 && is_erased[cells[cell_idx].brother]) { cells[cell_idx].brother = new_cell_idx; } } } } double res = 0.0; for (int cell_idx = 0; cell_idx < cells.size(); cell_idx++) { if (cells[cell_idx].start_x <= x && x <= cells[cell_idx].end_x) { res = max(cells[cell_idx].compute_water_level(), res); } } printf("%lf\n", res >= 50.0 ? 50.0 : res); cells = store; } } } }
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; class Cell { public: int parent; int brother; bool has_child; int water; int capacity; int height; int start_x; int end_x; Cell(int _h, int _sx, int _ex) : height(_h), start_x(_sx), end_x(_ex), parent(-1), brother(-1), water(0), has_child(false) { capacity = height * (end_x - start_x) * 30; } void pour_water(int _water) { water += _water; } double compute_water_level() { return (double)water / ((double)(end_x - start_x) * 30.0); } bool is_filled() { return (capacity <= water); } bool operator<(const Cell &c) const { return start_x < c.start_x; } bool operator<(const int _x) const { return start_x < _x; } }; struct Board { int x; int height; Board(int _x, int _h) : x(_x), height(_h) {} bool operator<(const Board &b) const { return x < b.x; } bool operator>(const Board &b) const { return x > b.x; } }; struct Faucet { int x; int cm3_per_second; Faucet(int _x, int _c) : x(_x), cm3_per_second(_c) {} Faucet() : x(0), cm3_per_second(0) {} bool operator<(const Faucet &f) const { return (x < f.x); } bool operator>(const Faucet &f) const { return (x > f.x); } bool operator==(const Faucet &f) const { return (x == f.x); } bool operator<(const int _x) const { return (x < _x); } bool operator>(const int _x) const { return (x > _x); } bool operator==(const int _x) const { return (x == _x); } }; int main() { int total_data_sets; while (~scanf("%d", &total_data_sets)) { for (int data_set_idx = 0; data_set_idx < total_data_sets; data_set_idx++) { vector<Cell> cells; vector<Board> boards; int total_boards; scanf("%d", &total_boards); boards.push_back(Board(0, 50)); for (int board_idx = 0; board_idx < total_boards; board_idx++) { int x, height; scanf("%d %d", &x, &height); boards.push_back(Board(x, height)); } boards.push_back(Board(100, 50)); sort(boards.begin(), boards.end()); for (int src_idx = 0; src_idx < boards.size(); src_idx++) { if (boards[src_idx].x == 100) continue; for (int target_idx = src_idx + 1; target_idx < boards.size(); target_idx++) { if (boards[target_idx].height < boards[src_idx].height) continue; else if (boards[target_idx].height >= boards[src_idx].height) { cells.push_back(Cell(boards[src_idx].height, boards[src_idx].x, boards[target_idx].x)); break; } } for (int target_idx = src_idx - 1; target_idx >= 0; target_idx--) { if (boards[target_idx].height < boards[src_idx].height) continue; else if (boards[target_idx].height > boards[src_idx].height) { cells.push_back(Cell(boards[src_idx].height, boards[target_idx].x, boards[src_idx].x)); break; } } } // set brother for (int src_idx = 0; src_idx < cells.size(); src_idx++) { int height = cells[src_idx].height; int target_wall[101]; memset(target_wall, 0, sizeof(target_wall)); for (int target_idx = 0; target_idx < cells.size(); target_idx++) { if (target_idx == src_idx) continue; if (cells[src_idx].end_x == cells[target_idx].start_x) { target_wall[cells[target_idx].start_x] = max(cells[target_idx].height, target_wall[cells[target_idx].start_x]); } if (cells[src_idx].start_x == cells[target_idx].end_x) { target_wall[cells[target_idx].end_x] = max( cells[target_idx].height, target_wall[cells[target_idx].end_x]); } } int diff = INF; for (int target_idx = 0; target_idx < cells.size(); target_idx++) { if (src_idx == target_idx) continue; if (cells[src_idx].end_x == cells[target_idx].start_x && height == target_wall[cells[target_idx].start_x] && diff > abs(cells[src_idx].end_x - cells[target_idx].end_x)) { diff = abs(cells[src_idx].end_x - cells[target_idx].end_x); cells[src_idx].brother = target_idx; } if (cells[src_idx].start_x == cells[target_idx].end_x && height == target_wall[cells[target_idx].end_x] && diff > abs(cells[src_idx].start_x - cells[target_idx].start_x)) { diff = abs(cells[src_idx].start_x - cells[target_idx].start_x); cells[src_idx].brother = target_idx; } } } // set parent for (int src_idx = 0; src_idx < cells.size(); src_idx++) { int min_height = INF; int lower_height = cells[src_idx].height; for (int target_idx = 0; target_idx < cells.size(); target_idx++) { if (src_idx == target_idx) continue; if (cells[src_idx].start_x >= cells[target_idx].start_x && cells[src_idx].end_x <= cells[target_idx].end_x && lower_height <= cells[target_idx].height && min_height > cells[target_idx].height) { min_height = cells[target_idx].height; cells[src_idx].parent = target_idx; } } if (cells[src_idx].parent != -1) { cells[cells[src_idx].parent].has_child = true; } } vector<Faucet> faucets; int total_faucets; scanf("%d", &total_faucets); for (int foucet_idx = 0; foucet_idx < total_faucets; foucet_idx++) { int x, cm3_per_second; scanf("%d %d", &x, &cm3_per_second); faucets.push_back(Faucet(x, cm3_per_second)); } sort(faucets.begin(), faucets.end()); int total_observation_times; scanf("%d", &total_observation_times); vector<Cell> store = cells; for (int observation_time_idx = 0; observation_time_idx < total_observation_times; observation_time_idx++) { int x, time; scanf("%d %d", &x, &time); for (int cell_idx = 0; cell_idx < cells.size(); cell_idx++) { for (int faucet_idx = 0; faucet_idx < faucets.size(); faucet_idx++) { // int faucet_idx = //lower_bound(faucets.begin(),faucets.end(),cells[cell_idx].start_x) //- faucets.begin(); if (faucet_idx == faucets.size()) continue; if (faucets[faucet_idx].x < cells[cell_idx].start_x) continue; if (faucets[faucet_idx].x > cells[cell_idx].end_x) continue; if (cells[cell_idx].has_child) continue; cells[cell_idx].pour_water(faucets[faucet_idx].cm3_per_second * time); } } bool is_erased[1001]; memset(is_erased, false, sizeof(is_erased)); for (int round = 0; round < 10000; round++) { for (int src_idx = 0; src_idx < cells.size(); src_idx++) { if (is_erased[src_idx]) continue; if (!cells[src_idx].is_filled()) continue; int freq[1001]; memset(freq, 0, sizeof(freq)); int limit = cells.size() * 3; int cell_idx = src_idx; bool isok = true; while (limit-- > 0) { if (!cells[cell_idx].is_filled()) break; freq[cell_idx]++; if (freq[cell_idx] >= 3) isok = false; int brother = cells[cell_idx].brother; if (brother == -1) break; cells[brother].water += (cells[cell_idx].water - cells[cell_idx].capacity); cells[cell_idx].water = cells[cell_idx].capacity; cell_idx = brother; } if (isok) continue; int sx = INF; int ex = 0; int water = 0; for (int cell_idx = 0; cell_idx < cells.size(); cell_idx++) { if (is_erased[cell_idx]) continue; if (freq[cell_idx] >= 3) { sx = min(sx, cells[cell_idx].start_x); ex = max(ex, cells[cell_idx].end_x); water += cells[cell_idx].water; cells[cell_idx].water = 0; is_erased[cell_idx] = true; } } int new_cell_idx = 0; for (int cell_idx = 0; cell_idx < cells.size(); cell_idx++) { if (is_erased[cell_idx]) continue; if (sx == cells[cell_idx].start_x && ex == cells[cell_idx].end_x) { cells[cell_idx].water = water; new_cell_idx = cell_idx; break; } } for (int cell_idx = 0; cell_idx < cells.size(); cell_idx++) { if (is_erased[cell_idx]) continue; if (cells[cell_idx].brother != -1 && is_erased[cells[cell_idx].brother]) { cells[cell_idx].brother = new_cell_idx; } } } } double res = 0.0; for (int cell_idx = 0; cell_idx < cells.size(); cell_idx++) { if (cells[cell_idx].start_x <= x && x <= cells[cell_idx].end_x) { res = max(cells[cell_idx].compute_water_level(), res); } } printf("%lf\n", res >= 50.0 ? 50.0 : res); cells = store; } } } }
replace
237
238
237
238
TLE
p00715
C++
Runtime Error
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<int> vi; typedef vector<vi> vvi; #define all(c) (c).begin(), (c).end() #define loop(i, a, b) for (int i = a; i < int(b); i++) #define rep(i, b) loop(i, 0, b) #define each(e, c) for (auto &e : c) #define iter(it, c) for (auto it = c.begin(); it != c.end(); ++it) ll const inf = 1 << 28; map<string, int> rtoi; int d[256][256]; int V; void solve() { rep(i, V) rep(j, V) { bool C = false, DE = true; if (d[i][j] == 1 || d[j][i] == 1) continue; rep(k, V) { if (k == i || k == j) continue; if ((d[i][k] == 1 || d[k][i] == 1) && (d[j][k] == 1 || d[k][j] == 1)) C = true; if ((d[i][k] == 1 && d[k][j] == 1) || (d[k][i] == 1 && d[j][k] == 1)) DE = false; } if (C && DE) d[i][j] = d[j][i] = 0; } rep(k, V) rep(i, V) rep(j, V) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } bool ask(string const &p, string const &q) { int pi = inf, qi = inf; if (rtoi.count(p) == 1 && rtoi.count(q) == 1) { pi = rtoi[p], qi = rtoi[q]; } return max(pi, qi) != inf && d[pi][qi] != inf && d[pi][qi] & 1; } int main() { int n; while (cin >> n, n) { rtoi.clear(); rep(i, 256) rep(j, 256) d[i][j] = inf; rep(i, n) { string s; cin >> s; int x = s.find('-'); string p = s.substr(0, x); string q = s.substr(x + 1); if (rtoi.count(p) == 0) rtoi[p] = V++; if (rtoi.count(q) == 0) rtoi[q] = V++; int ip = rtoi[p], iq = rtoi[q]; d[ip][iq] = 1; } solve(); cout << V << endl; int m; cin >> m; rep(i, m) { string s; cin >> s; int x = s.find('-'); string p = s.substr(0, x), q = s.substr(x + 1); puts(ask(p, q) ? "YES" : "NO"); } } }
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<int> vi; typedef vector<vi> vvi; #define all(c) (c).begin(), (c).end() #define loop(i, a, b) for (int i = a; i < int(b); i++) #define rep(i, b) loop(i, 0, b) #define each(e, c) for (auto &e : c) #define iter(it, c) for (auto it = c.begin(); it != c.end(); ++it) ll const inf = 1 << 28; map<string, int> rtoi; int d[256][256]; int V; void solve() { rep(i, V) rep(j, V) { bool C = false, DE = true; if (d[i][j] == 1 || d[j][i] == 1) continue; rep(k, V) { if (k == i || k == j) continue; if ((d[i][k] == 1 || d[k][i] == 1) && (d[j][k] == 1 || d[k][j] == 1)) C = true; if ((d[i][k] == 1 && d[k][j] == 1) || (d[k][i] == 1 && d[j][k] == 1)) DE = false; } if (C && DE) d[i][j] = d[j][i] = 0; } rep(k, V) rep(i, V) rep(j, V) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } bool ask(string const &p, string const &q) { int pi = inf, qi = inf; if (rtoi.count(p) == 1 && rtoi.count(q) == 1) { pi = rtoi[p], qi = rtoi[q]; } return max(pi, qi) != inf && d[pi][qi] != inf && d[pi][qi] & 1; } int main() { int n; while (cin >> n, n) { V = 0; rtoi.clear(); rep(i, 256) rep(j, 256) d[i][j] = inf; rep(i, n) { string s; cin >> s; int x = s.find('-'); string p = s.substr(0, x); string q = s.substr(x + 1); if (rtoi.count(p) == 0) rtoi[p] = V++; if (rtoi.count(q) == 0) rtoi[q] = V++; int ip = rtoi[p], iq = rtoi[q]; d[ip][iq] = 1; } solve(); cout << V << endl; int m; cin >> m; rep(i, m) { string s; cin >> s; int x = s.find('-'); string p = s.substr(0, x), q = s.substr(x + 1); puts(ask(p, q) ? "YES" : "NO"); } } }
insert
63
63
63
64
0
p00715
C++
Runtime Error
#include <algorithm> #include <cfloat> #include <cmath> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <vector> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; struct Info { char name[20]; }; int N, table[200][200], index; Info info[200]; bool strCmp(char *base, char *comp) { int length1, length2; for (length1 = 0; base[length1] != '\0'; length1++) ; for (length2 = 0; comp[length2] != '\0'; length2++) ; if (length1 != length2) return false; for (int i = 0; base[i] != '\0'; i++) { if (base[i] != comp[i]) return false; } return true; } void strcpy(char *to, char *str) { for (int i = 0; str[i] != '\0'; i++) { to[i] = str[i]; to[i + 1] = '\0'; } } bool check(int A, int B) { bool FLG = false; for (int k = 0; k < index; k++) { if (k == A || k == B) continue; if ((table[k][A] == 1 && table[k][B] == 1) || (table[A][k] == 1 && table[B][k] == 1)) { FLG = true; } else if ((table[k][A] == 1 && table[B][k] == 1) || (table[A][k] == 1 && table[k][B] == 1)) { return false; } } return FLG; } void func() { for (int i = 0; i < 200; i++) { for (int k = 0; k < 200; k++) { table[i][k] = BIG_NUM; } } index = 0; int work_index, left_id, right_id, start; char buf[20], work[20]; bool FLG; for (int i = 0; i < N; i++) { scanf("%s", buf); for (work_index = 0; buf[work_index] != '-'; work_index++) { work[work_index] = buf[work_index]; } work[work_index] = '\0'; FLG = false; for (int k = 0; k < index; k++) { if (strCmp(info[k].name, work)) { left_id = k; FLG = true; break; } } if (!FLG) { left_id = index; strcpy(info[index].name, work); index++; } start = work_index + 1; for (work_index = 0; buf[start + work_index] != '\0'; work_index++) { work[work_index] = buf[start + work_index]; } work[work_index] = '\0'; FLG = false; for (int k = 0; k < index; k++) { if (strCmp(info[k].name, work)) { right_id = k; FLG = true; break; } } if (!FLG) { right_id = index; strcpy(info[index].name, work); index++; } table[left_id][right_id] = 1; } for (int i = 0; i < index - 1; i++) { for (int k = i + 1; k < index; k++) { if (check(i, k)) { table[i][k] = 2; table[k][i] = 2; } } } for (int mid = 0; mid < index; mid++) { for (int start = 0; start < index; start++) { if (table[start][mid] == BIG_NUM) continue; for (int goal = 0; goal < index; goal++) { if (table[mid][goal] == BIG_NUM) continue; table[start][goal] = min(table[start][goal], table[start][mid] + table[mid][goal]); } } } printf("%d\n", index); int M; scanf("%d", &M); for (int loop = 0; loop < M; loop++) { scanf("%s", buf); for (work_index = 0; buf[work_index] != '-'; work_index++) { work[work_index] = buf[work_index]; } work[work_index] = '\0'; FLG = false; for (int k = 0; k < index; k++) { if (strCmp(info[k].name, work)) { left_id = k; FLG = true; break; } } if (!FLG) { printf("NO\n"); continue; } start = work_index + 1; for (work_index = 0; buf[start + work_index] != '\0'; work_index++) { work[work_index] = buf[start + work_index]; } work[work_index] = '\0'; FLG = false; for (int k = 0; k < index; k++) { if (strCmp(info[k].name, work)) { right_id = k; FLG = true; break; } } if (!FLG) { printf("NO\n"); continue; } if (table[left_id][right_id] % 2 == 1) { printf("YES\n"); } else { printf("NO\n"); } } } int main() { while (true) { scanf("%d", &N); if (N == 0) break; func(); } return 0; }
#include <algorithm> #include <cfloat> #include <cmath> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <vector> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; struct Info { char name[20]; }; int N, table[200][200], index; Info info[200]; bool strCmp(char *base, char *comp) { int length1, length2; for (length1 = 0; base[length1] != '\0'; length1++) ; for (length2 = 0; comp[length2] != '\0'; length2++) ; if (length1 != length2) return false; for (int i = 0; base[i] != '\0'; i++) { if (base[i] != comp[i]) return false; } return true; } void strcpy(char *to, char *str) { for (int i = 0; str[i] != '\0'; i++) { to[i] = str[i]; to[i + 1] = '\0'; } } bool check(int A, int B) { bool FLG = false; for (int k = 0; k < index; k++) { if (k == A || k == B) continue; if ((table[k][A] == 1 && table[k][B] == 1) || (table[A][k] == 1 && table[B][k] == 1)) { FLG = true; } else if ((table[k][A] == 1 && table[B][k] == 1) || (table[A][k] == 1 && table[k][B] == 1)) { return false; } } return FLG; } void func() { for (int i = 0; i < 200; i++) { for (int k = 0; k < 200; k++) { table[i][k] = BIG_NUM; } } index = 0; int work_index, left_id, right_id, start; char buf[40], work[20]; bool FLG; for (int i = 0; i < N; i++) { scanf("%s", buf); for (work_index = 0; buf[work_index] != '-'; work_index++) { work[work_index] = buf[work_index]; } work[work_index] = '\0'; FLG = false; for (int k = 0; k < index; k++) { if (strCmp(info[k].name, work)) { left_id = k; FLG = true; break; } } if (!FLG) { left_id = index; strcpy(info[index].name, work); index++; } start = work_index + 1; for (work_index = 0; buf[start + work_index] != '\0'; work_index++) { work[work_index] = buf[start + work_index]; } work[work_index] = '\0'; FLG = false; for (int k = 0; k < index; k++) { if (strCmp(info[k].name, work)) { right_id = k; FLG = true; break; } } if (!FLG) { right_id = index; strcpy(info[index].name, work); index++; } table[left_id][right_id] = 1; } for (int i = 0; i < index - 1; i++) { for (int k = i + 1; k < index; k++) { if (check(i, k)) { table[i][k] = 2; table[k][i] = 2; } } } for (int mid = 0; mid < index; mid++) { for (int start = 0; start < index; start++) { if (table[start][mid] == BIG_NUM) continue; for (int goal = 0; goal < index; goal++) { if (table[mid][goal] == BIG_NUM) continue; table[start][goal] = min(table[start][goal], table[start][mid] + table[mid][goal]); } } } printf("%d\n", index); int M; scanf("%d", &M); for (int loop = 0; loop < M; loop++) { scanf("%s", buf); for (work_index = 0; buf[work_index] != '-'; work_index++) { work[work_index] = buf[work_index]; } work[work_index] = '\0'; FLG = false; for (int k = 0; k < index; k++) { if (strCmp(info[k].name, work)) { left_id = k; FLG = true; break; } } if (!FLG) { printf("NO\n"); continue; } start = work_index + 1; for (work_index = 0; buf[start + work_index] != '\0'; work_index++) { work[work_index] = buf[start + work_index]; } work[work_index] = '\0'; FLG = false; for (int k = 0; k < index; k++) { if (strCmp(info[k].name, work)) { right_id = k; FLG = true; break; } } if (!FLG) { printf("NO\n"); continue; } if (table[left_id][right_id] % 2 == 1) { printf("YES\n"); } else { printf("NO\n"); } } } int main() { while (true) { scanf("%d", &N); if (N == 0) break; func(); } return 0; }
replace
79
80
79
80
0
p00715
C++
Runtime Error
#include <algorithm> #include <map> #include <stdio.h> #include <string> #include <vector> using namespace std; char in[50]; char L[50]; char R[50]; int p[500]; int q[500]; int UF[500]; int FIND(int a) { if (UF[a] < 0) return a; return UF[a] = FIND(UF[a]); } void UNION(int a, int b) { a = FIND(a); b = FIND(b); if (a == b) return; UF[a] += UF[b]; UF[b] = a; } int g[310][310]; int g2[310][310]; int main() { int a; while (scanf("%d", &a), a) { map<string, int> m; int n = 0; for (int i = 0; i < 500; i++) UF[i] = -1; for (int i = 0; i < a; i++) { scanf("%s", in); int at = 0; int j; for (j = 0; in[j] != '-'; j++) { L[at] = in[j]; at++; } L[at] = 0; at = 0; j++; for (; in[j]; j++) { R[at] = in[j]; at++; } R[at] = 0; // printf("%s %s\n",L,R); string l = L; string r = R; if (m.count(l)) p[i] = m[l]; else { m[l] = n; p[i] = n++; } if (m.count(r)) { q[i] = m[r]; } else { m[r] = n; q[i] = n++; } } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) g[i][j] = 0; for (int i = 0; i < a; i++) g[p[i]][q[i]] = 1; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) g2[i][j] = 0; for (int i = 0; i < n; i++) g2[i][i] = 1; for (int i = 0; i < a; i++) g2[p[i]][q[i]] = 1; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { bool ok1 = false; bool ok2 = true; for (int k = 0; k < n; k++) { if (g[i][k] && g[j][k]) ok1 = true; if (g[k][i] && g[k][j]) ok1 = true; if (g[i][k] && g[k][j]) ok2 = false; if (g[j][k] && g[k][i]) ok2 = false; } if (ok1 && ok2) g2[i][j] = g2[j][i] = 1; } } for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) g2[i][j] |= g2[i][k] & g2[k][j]; printf("%d\n", n); for (int i = 0; i < a; i++) { UNION(p[i] * 2, q[i] * 2 + 1); UNION(p[i] * 2 + 1, q[i] * 2); } int b; scanf("%d", &b); for (int i = 0; i < b; i++) { scanf("%s", in); int at = 0; int j; for (j = 0; in[j] != '-'; j++) { L[at] = in[j]; at++; } L[at] = 0; at = 0; j++; for (; in[j]; j++) { R[at] = in[j]; at++; } R[at] = 0; string l = L; string r = R; if (!m.count(l) || !m.count(r)) { printf("NO\n"); continue; } int P = m[l]; int Q = m[r]; if (FIND(P * 2) != FIND(Q * 2 + 1) || FIND(P * 2 + 1) != FIND(Q * 2)) { printf("NO\n"); continue; } if (g2[P][Q]) printf("YES\n"); else printf("NO\n"); } } }
#include <algorithm> #include <map> #include <stdio.h> #include <string> #include <vector> using namespace std; char in[50]; char L[50]; char R[50]; int p[5100]; int q[5100]; int UF[500]; int FIND(int a) { if (UF[a] < 0) return a; return UF[a] = FIND(UF[a]); } void UNION(int a, int b) { a = FIND(a); b = FIND(b); if (a == b) return; UF[a] += UF[b]; UF[b] = a; } int g[310][310]; int g2[310][310]; int main() { int a; while (scanf("%d", &a), a) { map<string, int> m; int n = 0; for (int i = 0; i < 500; i++) UF[i] = -1; for (int i = 0; i < a; i++) { scanf("%s", in); int at = 0; int j; for (j = 0; in[j] != '-'; j++) { L[at] = in[j]; at++; } L[at] = 0; at = 0; j++; for (; in[j]; j++) { R[at] = in[j]; at++; } R[at] = 0; // printf("%s %s\n",L,R); string l = L; string r = R; if (m.count(l)) p[i] = m[l]; else { m[l] = n; p[i] = n++; } if (m.count(r)) { q[i] = m[r]; } else { m[r] = n; q[i] = n++; } } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) g[i][j] = 0; for (int i = 0; i < a; i++) g[p[i]][q[i]] = 1; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) g2[i][j] = 0; for (int i = 0; i < n; i++) g2[i][i] = 1; for (int i = 0; i < a; i++) g2[p[i]][q[i]] = 1; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { bool ok1 = false; bool ok2 = true; for (int k = 0; k < n; k++) { if (g[i][k] && g[j][k]) ok1 = true; if (g[k][i] && g[k][j]) ok1 = true; if (g[i][k] && g[k][j]) ok2 = false; if (g[j][k] && g[k][i]) ok2 = false; } if (ok1 && ok2) g2[i][j] = g2[j][i] = 1; } } for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) g2[i][j] |= g2[i][k] & g2[k][j]; printf("%d\n", n); for (int i = 0; i < a; i++) { UNION(p[i] * 2, q[i] * 2 + 1); UNION(p[i] * 2 + 1, q[i] * 2); } int b; scanf("%d", &b); for (int i = 0; i < b; i++) { scanf("%s", in); int at = 0; int j; for (j = 0; in[j] != '-'; j++) { L[at] = in[j]; at++; } L[at] = 0; at = 0; j++; for (; in[j]; j++) { R[at] = in[j]; at++; } R[at] = 0; string l = L; string r = R; if (!m.count(l) || !m.count(r)) { printf("NO\n"); continue; } int P = m[l]; int Q = m[r]; if (FIND(P * 2) != FIND(Q * 2 + 1) || FIND(P * 2 + 1) != FIND(Q * 2)) { printf("NO\n"); continue; } if (g2[P][Q]) printf("YES\n"); else printf("NO\n"); } } }
replace
9
11
9
11
0
p00717
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long // <-----!!!!!!!!!!!!!!!!!!! #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define rrep2(i, a, b) for (int i = (a)-1; i >= b; i--) #define chmin(a, b) (a) = min((a), (b)); #define chmax(a, b) (a) = max((a), (b)); #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define printV(v) \ cerr << (#v) << ":"; \ for (auto(x) : (v)) { \ cerr << " " << (x); \ } \ cerr << endl; #define printVS(vs) \ cerr << (#vs) << ":" << endl; \ for (auto(s) : (vs)) { \ cerr << (s) << endl; \ } #define printVV(vv) \ cerr << (#vv) << ":" << endl; \ for (auto(v) : (vv)) { \ for (auto(x) : (v)) { \ cerr << " " << (x); \ } \ cerr << endl; \ } #define printP(p) cerr << (#p) << (p).first << " " << (p).second << endl; #define printVP(vp) \ cerr << (#vp) << ":" << endl; \ for (auto(p) : (vp)) { \ cerr << (p).first << " " << (p).second << endl; \ } inline void output() { cerr << endl; } template <typename First, typename... Rest> inline void output(const First &first, const Rest &...rest) { cerr << first << " "; output(rest...); } using ll = long long; using Pii = pair<int, int>; using TUPLE = tuple<int, int, int>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; const int inf = 1ll << 60; const int mod = 1e9 + 7; using Graph = vector<vector<int>>; int n, m0, m; vector<Pii> vp0, vp; void trans() { int dx = vp[0].first - vp0[0].first; int dy = vp[0].second - vp0[0].second; for (auto &p : vp0) { p = Pii(p.first + dx, p.second + dy); } } void rot() { for (auto &p : vp0) { p = Pii(p.second, -p.first); } } bool check() { rep(k, 4) { trans(); if (vp0 == vp) return true; reverse(all(vp0)); trans(); if (vp0 == vp) return true; rot(); } return false; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n; while (cin >> n, n) { cin >> m0; vp0.clear(); vp0.resize(m0); rep(i, m0) cin >> vp0[i].first >> vp0[i].second; rep(i, n) { cin >> m; vp.clear(); vp.resize(m); rep(i, m0) cin >> vp[i].first >> vp[i].second; if (check()) cout << i + 1 << endl; } cout << "+++++" << endl; } }
#include <bits/stdc++.h> using namespace std; #define int long long // <-----!!!!!!!!!!!!!!!!!!! #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define rrep2(i, a, b) for (int i = (a)-1; i >= b; i--) #define chmin(a, b) (a) = min((a), (b)); #define chmax(a, b) (a) = max((a), (b)); #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define printV(v) \ cerr << (#v) << ":"; \ for (auto(x) : (v)) { \ cerr << " " << (x); \ } \ cerr << endl; #define printVS(vs) \ cerr << (#vs) << ":" << endl; \ for (auto(s) : (vs)) { \ cerr << (s) << endl; \ } #define printVV(vv) \ cerr << (#vv) << ":" << endl; \ for (auto(v) : (vv)) { \ for (auto(x) : (v)) { \ cerr << " " << (x); \ } \ cerr << endl; \ } #define printP(p) cerr << (#p) << (p).first << " " << (p).second << endl; #define printVP(vp) \ cerr << (#vp) << ":" << endl; \ for (auto(p) : (vp)) { \ cerr << (p).first << " " << (p).second << endl; \ } inline void output() { cerr << endl; } template <typename First, typename... Rest> inline void output(const First &first, const Rest &...rest) { cerr << first << " "; output(rest...); } using ll = long long; using Pii = pair<int, int>; using TUPLE = tuple<int, int, int>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; const int inf = 1ll << 60; const int mod = 1e9 + 7; using Graph = vector<vector<int>>; int n, m0, m; vector<Pii> vp0, vp; void trans() { int dx = vp[0].first - vp0[0].first; int dy = vp[0].second - vp0[0].second; for (auto &p : vp0) { p = Pii(p.first + dx, p.second + dy); } } void rot() { for (auto &p : vp0) { p = Pii(p.second, -p.first); } } bool check() { rep(k, 4) { trans(); if (vp0 == vp) return true; reverse(all(vp0)); trans(); if (vp0 == vp) return true; rot(); } return false; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n; while (cin >> n, n) { cin >> m0; vp0.clear(); vp0.resize(m0); rep(i, m0) cin >> vp0[i].first >> vp0[i].second; rep(i, n) { cin >> m; vp.clear(); vp.resize(m); rep(i, m) cin >> vp[i].first >> vp[i].second; if (check()) cout << i + 1 << endl; } cout << "+++++" << endl; } }
replace
101
102
101
102
0
p00717
C++
Runtime Error
#include "bits/stdc++.h" #include <fstream> #include <sys/timeb.h> using namespace std; #define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repl(i, 0, n) #define replrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--) #define reprev(i, n) replrev(i, 0, n) #define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++) #define all(a) a.begin(), a.end() #define mp make_pair #define mt make_tuple #define INF 2000000000 #define INFL 1000000000000000000LL #define EPS 1e-9 #define MOD 1000000007 #define PI 3.1415926536 #define RMAX 4294967295 typedef long long ll; typedef pair<int, int> P; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<bool> vb; typedef vector<char> vc; typedef vector<string> vs; typedef vector<double> vd; typedef vector<P> vP; typedef vector<vector<int>> vvi; typedef vector<vector<bool>> vvb; typedef vector<vector<ll>> vvll; typedef vector<vector<char>> vvc; typedef vector<vector<double>> vvd; typedef vector<vector<P>> vvP; typedef priority_queue<int, vector<int>, greater<int>> pqli; typedef priority_queue<ll, vector<ll>, greater<ll>> pqlll; typedef priority_queue<P, vector<P>, greater<P>> pqlP; typedef pair<int, pair<int, int>> Edge; typedef vector<Edge> vE; typedef priority_queue<Edge, vector<Edge>, greater<Edge>> pqlE; int main() { std::ifstream in("1136-input.txt"); std::cin.rdbuf(in.rdbuf()); while (true) { int N; cin >> N; if (N == 0) break; int n; vP L[4]; cin >> n; rep(i, 4) { L[i] = vP(n); } rep(i, n) { int x, y; cin >> x >> y; L[0][i] = mp(x, y); } rep(i, n) { repl(j, 1, 4) { // [0 -1][x] // [1 0][y] L[j][i] = mp(L[j - 1][i].second, -L[j - 1][i].first); } } rep(i, N) { cin >> n; vP l(n); rep(i, n) { int x, y; cin >> x >> y; l[i] = mp(x, y); } if (n != L[0].size()) continue; rep(j, 4) { bool flag = true; rep(i, n - 1) { if (L[j][i + 1].first - L[j][i].first != l[i + 1].first - l[i].first || L[j][i + 1].second - L[j][i].second != l[i + 1].second - l[i].second) { flag = false; break; } } if (flag) { cout << i + 1 << endl; break; } flag = true; rep(i, n - 1) { if (L[j][n - 2 - i].first - L[j][n - 1 - i].first != l[i + 1].first - l[i].first || L[j][n - 2 - i].second - L[j][n - 1 - i].second != l[i + 1].second - l[i].second) { flag = false; break; } } if (flag) { cout << i + 1 << endl; break; } } } cout << "+++++" << endl; } return 0; }
#include "bits/stdc++.h" #include <fstream> #include <sys/timeb.h> using namespace std; #define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repl(i, 0, n) #define replrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--) #define reprev(i, n) replrev(i, 0, n) #define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++) #define all(a) a.begin(), a.end() #define mp make_pair #define mt make_tuple #define INF 2000000000 #define INFL 1000000000000000000LL #define EPS 1e-9 #define MOD 1000000007 #define PI 3.1415926536 #define RMAX 4294967295 typedef long long ll; typedef pair<int, int> P; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<bool> vb; typedef vector<char> vc; typedef vector<string> vs; typedef vector<double> vd; typedef vector<P> vP; typedef vector<vector<int>> vvi; typedef vector<vector<bool>> vvb; typedef vector<vector<ll>> vvll; typedef vector<vector<char>> vvc; typedef vector<vector<double>> vvd; typedef vector<vector<P>> vvP; typedef priority_queue<int, vector<int>, greater<int>> pqli; typedef priority_queue<ll, vector<ll>, greater<ll>> pqlll; typedef priority_queue<P, vector<P>, greater<P>> pqlP; typedef pair<int, pair<int, int>> Edge; typedef vector<Edge> vE; typedef priority_queue<Edge, vector<Edge>, greater<Edge>> pqlE; int main() { while (true) { int N; cin >> N; if (N == 0) break; int n; vP L[4]; cin >> n; rep(i, 4) { L[i] = vP(n); } rep(i, n) { int x, y; cin >> x >> y; L[0][i] = mp(x, y); } rep(i, n) { repl(j, 1, 4) { // [0 -1][x] // [1 0][y] L[j][i] = mp(L[j - 1][i].second, -L[j - 1][i].first); } } rep(i, N) { cin >> n; vP l(n); rep(i, n) { int x, y; cin >> x >> y; l[i] = mp(x, y); } if (n != L[0].size()) continue; rep(j, 4) { bool flag = true; rep(i, n - 1) { if (L[j][i + 1].first - L[j][i].first != l[i + 1].first - l[i].first || L[j][i + 1].second - L[j][i].second != l[i + 1].second - l[i].second) { flag = false; break; } } if (flag) { cout << i + 1 << endl; break; } flag = true; rep(i, n - 1) { if (L[j][n - 2 - i].first - L[j][n - 1 - i].first != l[i + 1].first - l[i].first || L[j][n - 2 - i].second - L[j][n - 1 - i].second != l[i + 1].second - l[i].second) { flag = false; break; } } if (flag) { cout << i + 1 << endl; break; } } } cout << "+++++" << endl; } return 0; }
delete
44
47
44
44
TLE
p00717
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; int main() { int n, m, x, y; while (true) { cin >> n; if (n == 0) { break; } vector<vector<pair<long double, long double>>> L(2 * n + 2); long double theta, theta2, x4, y4; for (int i = 0; i <= n; i++) { cin >> m; for (int j = 0; j < m; j++) { cin >> x >> y; L[2 * i].push_back(make_pair(x, y)); } L[2 * i + 1] = L[2 * i]; reverse(L[2 * i + 1].begin(), L[2 * i + 1].end()); } for (int i = 0; i < 2 * n + 2; i++) { int zx = -L[i][0].first; int zy = -L[i][0].second; for (int j = 0; j < L[i].size(); j++) { L[i][j].first += zx; L[i][j].second += zy; } theta = -atan2l(L[i][1].second, L[i][1].first); for (int j = 1; j < m; j++) { theta2 = atan2l(L[i][j].second, L[i][j].first) + theta; x4 = cosl(theta2) * sqrtl(L[i][j].first * L[i][j].first + L[i][j].second * L[i][j].second); y4 = sinl(theta2) * sqrtl(L[i][j].first * L[i][j].first + L[i][j].second * L[i][j].second); L[i][j].first = x4; L[i][j].second = y4; } } for (int i = 1; i <= n; i++) { bool ok1 = (L[0].size() == L[2 * i].size()); bool ok2 = (L[0].size() == L[2 * i + 1].size()); bool ok3 = (L[1].size() == L[2 * i].size()); bool ok4 = (L[1].size() == L[2 * i + 1].size()); for (int j = 0; j < min(L[0].size(), L[2 * i].size()); j++) { if (!(-1e-6 < L[2 * i][j].first - L[0][j].first && L[2 * i][j].first - L[0][j].first < 1e-6 && -1e-6 < L[2 * i][j].second - L[0][j].second && L[2 * i][j].second - L[0][j].second < 1e-6)) { ok1 = false; } } for (int j = 0; j < min(L[0].size(), L[2 * i + 1].size()); j++) { if (!(-1e-6 < L[2 * i + 1][j].first - L[0][j].first && L[2 * i + 1][j].first - L[0][j].first < 1e-6 && -1e-6 < L[2 * i + 1][j].second - L[0][j].second && L[2 * i + 1][j].second - L[0][j].second < 1e-6)) { ok2 = false; } } for (int j = 0; j < min(L[1].size(), L[2 * i].size()); j++) { if (!(-1e-6 < L[2 * i][j].first - L[1][j].first && L[2 * i][j].first - L[1][j].first < 1e-6 && -1e-6 < L[2 * i][j].second - L[1][j].second && L[2 * i][j].second - L[1][j].second < 1e-6)) { ok3 = false; } } for (int j = 0; j < min(L[1].size(), L[2 * i + 1].size()); j++) { if (!(-1e-6 < L[2 * i + 1][j].first - L[1][j].first && L[2 * i + 1][j].first - L[1][j].first < 1e-6 && -1e-6 < L[2 * i + 1][j].second - L[1][j].second && L[2 * i + 1][j].second - L[1][j].second < 1e-6)) { ok4 = false; } } if (ok1 || ok2 || ok3 || ok4) { cout << i << endl; } } cout << "+++++" << endl; } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; int main() { int n, m, x, y; while (true) { cin >> n; if (n == 0) { break; } vector<vector<pair<long double, long double>>> L(2 * n + 2); long double theta, theta2, x4, y4; for (int i = 0; i <= n; i++) { cin >> m; for (int j = 0; j < m; j++) { cin >> x >> y; L[2 * i].push_back(make_pair(x, y)); } L[2 * i + 1] = L[2 * i]; reverse(L[2 * i + 1].begin(), L[2 * i + 1].end()); } for (int i = 0; i < 2 * n + 2; i++) { int zx = -L[i][0].first; int zy = -L[i][0].second; for (int j = 0; j < L[i].size(); j++) { L[i][j].first += zx; L[i][j].second += zy; } theta = -atan2l(L[i][1].second, L[i][1].first); for (int j = 1; j < L[i].size(); j++) { theta2 = atan2l(L[i][j].second, L[i][j].first) + theta; x4 = cosl(theta2) * sqrtl(L[i][j].first * L[i][j].first + L[i][j].second * L[i][j].second); y4 = sinl(theta2) * sqrtl(L[i][j].first * L[i][j].first + L[i][j].second * L[i][j].second); L[i][j].first = x4; L[i][j].second = y4; } } for (int i = 1; i <= n; i++) { bool ok1 = (L[0].size() == L[2 * i].size()); bool ok2 = (L[0].size() == L[2 * i + 1].size()); bool ok3 = (L[1].size() == L[2 * i].size()); bool ok4 = (L[1].size() == L[2 * i + 1].size()); for (int j = 0; j < min(L[0].size(), L[2 * i].size()); j++) { if (!(-1e-6 < L[2 * i][j].first - L[0][j].first && L[2 * i][j].first - L[0][j].first < 1e-6 && -1e-6 < L[2 * i][j].second - L[0][j].second && L[2 * i][j].second - L[0][j].second < 1e-6)) { ok1 = false; } } for (int j = 0; j < min(L[0].size(), L[2 * i + 1].size()); j++) { if (!(-1e-6 < L[2 * i + 1][j].first - L[0][j].first && L[2 * i + 1][j].first - L[0][j].first < 1e-6 && -1e-6 < L[2 * i + 1][j].second - L[0][j].second && L[2 * i + 1][j].second - L[0][j].second < 1e-6)) { ok2 = false; } } for (int j = 0; j < min(L[1].size(), L[2 * i].size()); j++) { if (!(-1e-6 < L[2 * i][j].first - L[1][j].first && L[2 * i][j].first - L[1][j].first < 1e-6 && -1e-6 < L[2 * i][j].second - L[1][j].second && L[2 * i][j].second - L[1][j].second < 1e-6)) { ok3 = false; } } for (int j = 0; j < min(L[1].size(), L[2 * i + 1].size()); j++) { if (!(-1e-6 < L[2 * i + 1][j].first - L[1][j].first && L[2 * i + 1][j].first - L[1][j].first < 1e-6 && -1e-6 < L[2 * i + 1][j].second - L[1][j].second && L[2 * i + 1][j].second - L[1][j].second < 1e-6)) { ok4 = false; } } if (ok1 || ok2 || ok3 || ok4) { cout << i << endl; } } cout << "+++++" << endl; } return 0; }
replace
45
46
45
46
0
p00717
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; typedef vector<pair<int, int>> VP; int main(void) { while (1) { int n; cin >> n; if (n == 0) { break; } // make base int m; cin >> m; VP base(m); VP base2(m); VP base3(m); VP base4(m); for (int j = 0; j < m; j++) { int first; int second; cin >> first >> second; base[j] = make_pair(first, second); } int a = base[0].first; int b = base[0].second; for (int j = 0; j < m; j++) { base[j].first -= a; base[j].second -= b; } for (int j = 0; j < m; j++) { base2[j] = make_pair(-base[j].second, base[j].first); base3[j] = make_pair(-base[j].first, -base[j].second); base4[j] = make_pair(base[j].second, -base[j].first); } for (int i = 0; i < n; i++) { int temp_m; cin >> temp_m; VP chal(temp_m); VP chal2(temp_m); for (int j = 0; j < temp_m; j++) { int first; int second; cin >> first >> second; chal[j] = make_pair(first, second); chal2[m - 1 - j] = make_pair(first, second); } if (temp_m != m) { continue; } int a2 = chal[0].first; int b2 = chal[0].second; for (int j = 0; j < m; j++) { chal[j].first -= a2; chal[j].second -= b2; } int a3 = chal2[0].first; int b3 = chal2[0].second; for (int j = 0; j < m; j++) { chal2[j].first -= a3; chal2[j].second -= b3; } bool st1 = true; bool st2 = true; bool st3 = true; bool st4 = true; bool st12 = true; bool st22 = true; bool st32 = true; bool st42 = true; for (int j = 0; j < m; j++) { if (!(chal[j].first == base[j].first && chal[j].second == base[j].second)) { st1 = false; } if (!(chal[j].first == base2[j].first && chal[j].second == base2[j].second)) { st2 = false; } if (!(chal[j].first == base3[j].first && chal[j].second == base3[j].second)) { st3 = false; } if (!(chal[j].first == base4[j].first && chal[j].second == base4[j].second)) { st4 = false; } if (!(chal2[j].first == base[j].first && chal2[j].second == base[j].second)) { st12 = false; } if (!(chal2[j].first == base2[j].first && chal2[j].second == base2[j].second)) { st22 = false; } if (!(chal2[j].first == base3[j].first && chal2[j].second == base3[j].second)) { st32 = false; } if (!(chal2[j].first == base4[j].first && chal2[j].second == base4[j].second)) { st42 = false; } } if (st1 == true || st2 == true || st3 == true || st4 == true || st12 == true || st22 == true || st32 == true || st42 == true) { cout << i + 1 << endl; } } cout << "+++++" << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; typedef vector<pair<int, int>> VP; int main(void) { while (1) { int n; cin >> n; if (n == 0) { break; } // make base int m; cin >> m; VP base(m); VP base2(m); VP base3(m); VP base4(m); for (int j = 0; j < m; j++) { int first; int second; cin >> first >> second; base[j] = make_pair(first, second); } int a = base[0].first; int b = base[0].second; for (int j = 0; j < m; j++) { base[j].first -= a; base[j].second -= b; } for (int j = 0; j < m; j++) { base2[j] = make_pair(-base[j].second, base[j].first); base3[j] = make_pair(-base[j].first, -base[j].second); base4[j] = make_pair(base[j].second, -base[j].first); } for (int i = 0; i < n; i++) { int temp_m; cin >> temp_m; VP chal(temp_m); VP chal2(temp_m); for (int j = 0; j < temp_m; j++) { int first; int second; cin >> first >> second; chal[j] = make_pair(first, second); chal2[temp_m - 1 - j] = make_pair(first, second); } if (temp_m != m) { continue; } int a2 = chal[0].first; int b2 = chal[0].second; for (int j = 0; j < m; j++) { chal[j].first -= a2; chal[j].second -= b2; } int a3 = chal2[0].first; int b3 = chal2[0].second; for (int j = 0; j < m; j++) { chal2[j].first -= a3; chal2[j].second -= b3; } bool st1 = true; bool st2 = true; bool st3 = true; bool st4 = true; bool st12 = true; bool st22 = true; bool st32 = true; bool st42 = true; for (int j = 0; j < m; j++) { if (!(chal[j].first == base[j].first && chal[j].second == base[j].second)) { st1 = false; } if (!(chal[j].first == base2[j].first && chal[j].second == base2[j].second)) { st2 = false; } if (!(chal[j].first == base3[j].first && chal[j].second == base3[j].second)) { st3 = false; } if (!(chal[j].first == base4[j].first && chal[j].second == base4[j].second)) { st4 = false; } if (!(chal2[j].first == base[j].first && chal2[j].second == base[j].second)) { st12 = false; } if (!(chal2[j].first == base2[j].first && chal2[j].second == base2[j].second)) { st22 = false; } if (!(chal2[j].first == base3[j].first && chal2[j].second == base3[j].second)) { st32 = false; } if (!(chal2[j].first == base4[j].first && chal2[j].second == base4[j].second)) { st42 = false; } } if (st1 == true || st2 == true || st3 == true || st4 == true || st12 == true || st22 == true || st32 == true || st42 == true) { cout << i + 1 << endl; } } cout << "+++++" << endl; } return 0; }
replace
51
52
51
52
0
p00718
C++
Runtime Error
#include <iostream> #include <string> #include <vector> using namespace std; int main(void) { int n; vector<string> s(510); vector<string> t(510); int dp[510] = {}; cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i] >> t[i]; } for (int i = 1; i <= n; i++) { int a, b, c, d; for (int p = 0; p <= s[i].size() - 1; p++) { if (p == 0) { switch (s[i][p]) { case 'm': dp[i] += 1000; break; case 'c': dp[i] += 100; break; case 'x': dp[i] += 10; break; case 'i': dp[i] += 1; break; } } else { switch (s[i][p]) { case 'm': if ('2' <= s[i][p - 1] && s[i][p - 1] <= '9') { dp[i] += 1000 * (s[i][p - 1] - '0'); } else { dp[i] += 1000; } break; case 'c': if ('2' <= s[i][p - 1] && s[i][p - 1] <= '9') { dp[i] += 100 * (s[i][p - 1] - '0'); } else { dp[i] += 100; } break; case 'x': if ('2' <= s[i][p - 1] && s[i][p - 1] <= '9') { dp[i] += 10 * (s[i][p - 1] - '0'); } else { dp[i] += 10; } break; case 'i': if ('2' <= s[i][p - 1] && s[i][p - 1] <= '9') { dp[i] += (s[i][p - 1] - '0'); } else { dp[i] += 1; } break; } } } for (int p = 0; p <= t[i].size() - 1; p++) { if (p == 0) { switch (t[i][p]) { case 'm': dp[i] += 1000; break; case 'c': dp[i] += 100; break; case 'x': dp[i] += 10; break; case 'i': dp[i] += 1; break; } } else { switch (t[i][p]) { case 'm': if ('2' <= t[i][p - 1] && t[i][p - 1] <= '9') { dp[i] += 1000 * (t[i][p - 1] - '0'); } else { dp[i] += 1000; } break; case 'c': if ('2' <= t[i][p - 1] && t[i][p - 1] <= '9') { dp[i] += 100 * (t[i][p - 1] - '0'); } else { dp[i] += 100; } break; case 'x': if ('2' <= t[i][p - 1] && t[i][p - 1] <= '9') { dp[i] += 10 * (t[i][p - 1] - '0'); } else { dp[i] += 10; } break; case 'i': if ('2' <= t[i][p - 1] && t[i][p - 1] <= '9') { dp[i] += (t[i][p - 1] - '0'); } else { dp[i] += 1; } break; } } } a = dp[i] / 1000; b = (dp[i] - 1000 * a) / 100; c = (dp[i] - 1000 * a - 100 * b) / 10; d = (dp[i] - 1000 * a - 100 * b - 10 * c); if (a != 0) { if (a == 1) { cout << 'm'; } else { cout << a << 'm'; } } if (b != 0) { if (b == 1) { cout << 'c'; } else { cout << b << 'c'; } } if (c != 0) { if (c == 1) { cout << 'x'; } else { cout << c << 'x'; } } if (d != 0) { if (d == 1) { cout << 'i'; } else { cout << d << 'i'; } } cout << endl; } return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; int main(void) { int n; vector<string> s(10000); vector<string> t(10000); int dp[10000] = {}; cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i] >> t[i]; } for (int i = 1; i <= n; i++) { int a, b, c, d; for (int p = 0; p <= s[i].size() - 1; p++) { if (p == 0) { switch (s[i][p]) { case 'm': dp[i] += 1000; break; case 'c': dp[i] += 100; break; case 'x': dp[i] += 10; break; case 'i': dp[i] += 1; break; } } else { switch (s[i][p]) { case 'm': if ('2' <= s[i][p - 1] && s[i][p - 1] <= '9') { dp[i] += 1000 * (s[i][p - 1] - '0'); } else { dp[i] += 1000; } break; case 'c': if ('2' <= s[i][p - 1] && s[i][p - 1] <= '9') { dp[i] += 100 * (s[i][p - 1] - '0'); } else { dp[i] += 100; } break; case 'x': if ('2' <= s[i][p - 1] && s[i][p - 1] <= '9') { dp[i] += 10 * (s[i][p - 1] - '0'); } else { dp[i] += 10; } break; case 'i': if ('2' <= s[i][p - 1] && s[i][p - 1] <= '9') { dp[i] += (s[i][p - 1] - '0'); } else { dp[i] += 1; } break; } } } for (int p = 0; p <= t[i].size() - 1; p++) { if (p == 0) { switch (t[i][p]) { case 'm': dp[i] += 1000; break; case 'c': dp[i] += 100; break; case 'x': dp[i] += 10; break; case 'i': dp[i] += 1; break; } } else { switch (t[i][p]) { case 'm': if ('2' <= t[i][p - 1] && t[i][p - 1] <= '9') { dp[i] += 1000 * (t[i][p - 1] - '0'); } else { dp[i] += 1000; } break; case 'c': if ('2' <= t[i][p - 1] && t[i][p - 1] <= '9') { dp[i] += 100 * (t[i][p - 1] - '0'); } else { dp[i] += 100; } break; case 'x': if ('2' <= t[i][p - 1] && t[i][p - 1] <= '9') { dp[i] += 10 * (t[i][p - 1] - '0'); } else { dp[i] += 10; } break; case 'i': if ('2' <= t[i][p - 1] && t[i][p - 1] <= '9') { dp[i] += (t[i][p - 1] - '0'); } else { dp[i] += 1; } break; } } } a = dp[i] / 1000; b = (dp[i] - 1000 * a) / 100; c = (dp[i] - 1000 * a - 100 * b) / 10; d = (dp[i] - 1000 * a - 100 * b - 10 * c); if (a != 0) { if (a == 1) { cout << 'm'; } else { cout << a << 'm'; } } if (b != 0) { if (b == 1) { cout << 'c'; } else { cout << b << 'c'; } } if (c != 0) { if (c == 1) { cout << 'x'; } else { cout << c << 'x'; } } if (d != 0) { if (d == 1) { cout << 'i'; } else { cout << d << 'i'; } } cout << endl; } return 0; }
replace
8
11
8
11
0
p00719
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; #define dump(o) \ { cerr << #o << " " << o << endl; } #define dumpc(o) \ { \ cerr << #o; \ for (auto e : (o)) \ cerr << " " << e; \ cerr << endl; \ } #define MOD 1000000007 #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL //??¶?´?????????£???????????? int,double:INF ll:INFL #define INFG INF using Weight = double; // int; class Edge { public: int s; // source int d; // destination Weight w; // weight Edge(int s = 0, int d = 0, Weight w = INFG) : s(s), d(d), w(w){}; bool operator<(const Edge &e) const { return w < e.w; } }; using Edges = vector<Edge>; //???????????\: g[u].push_back(Edge(u, v, c)); //????????¨???????????????????????°??????????????\????????????????????? using Graph = vector<Edges>; using Array = vector<Weight>; using Matrix = vector<Array>; vector<int> ticket; double cost[30][1 << 8]; int n, m, p, a, b; void solve(const Graph &g, int s) { fill(cost[0], cost[30], INFG); typedef tuple<Weight, int, int> State; //?§?????????????????°?????????? ??? used queue<State> Q; cost[s][0] = 0; Q.push(State(0, s, 0)); //?§???? while (!Q.empty()) { Weight d; int v; int used; tie(d, v, used) = Q.front(); Q.pop(); if (cost[v][used] > d) continue; for (auto &e : g[v]) { //??£??\????????????????????¨???????????? for (int i = 0; i < n; i++) { if ((used & (1 << i)) == (1 << i)) continue; int dused = used | (1 << i); if (cost[e.d][dused] > cost[v][used] + e.w / ticket[i]) { cost[e.d][dused] = cost[v][used] + e.w / ticket[i]; Q.push(State(cost[e.d][dused], e.d, dused)); } } } } } int main() { /* n ??¬????????° m ????????° p ????????° a ????????° b ????????° t ??¬?????° x y ?????? z ?????¢ */ cout << fixed << setprecision(4); for (; scanf("%d%d%d%d%d", &n, &m, &p, &a, &b), n;) { ticket.resize(n); --a; --b; for (int i = 0; i < n; i++) scanf("%d", &ticket[i]); Graph g(m); for (int i = 0; i < p; i++) { int x, y, z; scanf("%d%d%d", &x, &y, &z); --x; --y; g[x].push_back(Edge(x, y, z)); g[y].push_back(Edge(y, x, z)); } if (p == 0) { cout << "Impossible" << endl; continue; } solve(g, a); double ans = INFG; dumpc(cost[b]); for (auto &e : cost[b]) { ans = min(ans, e); } if (ans == INFG) cout << "Impossible" << endl; else cout << ans << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; #define dump(o) \ { cerr << #o << " " << o << endl; } #define dumpc(o) \ { \ cerr << #o; \ for (auto e : (o)) \ cerr << " " << e; \ cerr << endl; \ } #define MOD 1000000007 #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL //??¶?´?????????£???????????? int,double:INF ll:INFL #define INFG INF using Weight = double; // int; class Edge { public: int s; // source int d; // destination Weight w; // weight Edge(int s = 0, int d = 0, Weight w = INFG) : s(s), d(d), w(w){}; bool operator<(const Edge &e) const { return w < e.w; } }; using Edges = vector<Edge>; //???????????\: g[u].push_back(Edge(u, v, c)); //????????¨???????????????????????°??????????????\????????????????????? using Graph = vector<Edges>; using Array = vector<Weight>; using Matrix = vector<Array>; vector<int> ticket; double cost[30][1 << 8]; int n, m, p, a, b; void solve(const Graph &g, int s) { fill(cost[0], cost[30], INFG); typedef tuple<Weight, int, int> State; //?§?????????????????°?????????? ??? used queue<State> Q; cost[s][0] = 0; Q.push(State(0, s, 0)); //?§???? while (!Q.empty()) { Weight d; int v; int used; tie(d, v, used) = Q.front(); Q.pop(); if (cost[v][used] > d) continue; for (auto &e : g[v]) { //??£??\????????????????????¨???????????? for (int i = 0; i < n; i++) { if ((used & (1 << i)) == (1 << i)) continue; int dused = used | (1 << i); if (cost[e.d][dused] > cost[v][used] + e.w / ticket[i]) { cost[e.d][dused] = cost[v][used] + e.w / ticket[i]; Q.push(State(cost[e.d][dused], e.d, dused)); } } } } } int main() { /* n ??¬????????° m ????????° p ????????° a ????????° b ????????° t ??¬?????° x y ?????? z ?????¢ */ cout << fixed << setprecision(4); for (; scanf("%d%d%d%d%d", &n, &m, &p, &a, &b), n;) { ticket.resize(n); --a; --b; for (int i = 0; i < n; i++) scanf("%d", &ticket[i]); Graph g(m); for (int i = 0; i < p; i++) { int x, y, z; scanf("%d%d%d", &x, &y, &z); --x; --y; g[x].push_back(Edge(x, y, z)); g[y].push_back(Edge(y, x, z)); } if (p == 0) { cout << "Impossible" << endl; continue; } solve(g, a); double ans = INFG; // dumpc(cost[b]); for (auto &e : cost[b]) { ans = min(ans, e); } if (ans == INFG) cout << "Impossible" << endl; else cout << ans << endl; } return 0; }
replace
123
124
123
124
0
cost[b] 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 30 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 cost[b] 1.06111e+09 1.06111e+09 1.06111e+09 3.66667 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 cost[b] 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 1.06111e+09 cost[b] 1.06111e+09 49.5 14.1429 7.92857 99 25.5 9.42857 8.78571 12.375 7.125 6.05357 5.08929 8.625 8.375 6.58929 6.58929 24.75 12.75 7.17857 6.03571 14.25 11.25 7.53571 7.25 6.375 5.625 4.33929 4.08929 7.125 7.125 5.58929 8.83929 19.8 10.5 7.02857 5.78571 12 10.1 7.28571 7 6.225 5.375 4.18929 3.88929 6.875 6.875 5.38929 8.68929 9.75 7.35 5.03571 4.5 8.85 7.65 6 9.53571 4.625 4.375 3.13929 6.43929 5.875 9.125 7.93929 7.63929 16.5 9 6.92857 5.61905 10.5 9.33333 7.11905 6.83333 6.125 5.20833 4.08929 3.75595 6.70833 6.70833 5.25595 8.58929 8.25 6.58333 4.86905 4.33333 8.08333 7.41667 5.83333 9.36905 4.45833 4.20833 3.00595 6.33929 5.70833 8.95833 7.83929 7.50595 8.1 6.33333 4.71905 4.13333 7.83333 7.16667 5.63333 9.21905 4.30833 4.00833 2.85595 6.18929 5.50833 8.80833 7.68929 7.35595 5.58333 4.66667 3.38333 6.96905 6.16667 10.0833 8.46905 7.88333 3.25833 6.55833 5.43929 5.10595 8.05833 7.75833 6.60595 9.93929 33 16.5 7.42857 6.45238 18 13.1667 7.95238 7.66667 6.625 6.04167 4.58929 4.42262 7.54167 7.54167 5.92262 9.08929 12.25 8.91667 5.53571 5.08333 10.4167 8.41667 6.58333 10.0357 5.125 4.95833 3.58929 6.83929 6.45833 9.625 8.33929 8.08929 10 7.76667 5.28571 4.83333 9.26667 8.06667 6.33333 9.78571 4.875 4.70833 3.38929 6.68929 6.20833 9.375 8.18929 7.88929 6.85 5.48333 4 7.53571 6.98333 11.35 9.03571 8.5 3.875 7.125 5.93929 5.63929 8.625 8.375 7.13929 10.4393 8.5 7 5.11905 4.66667 8.5 7.83333 6.16667 9.61905 4.70833 4.54167 3.25595 6.58929 6.04167 9.20833 8.08929 7.75595 6.08333 5.25 3.83333 7.36905 6.75 10.5833 8.86905 8.33333 3.70833 6.95833 5.83929 5.50595 8.45833 8.20833 7.00595 10.3393 5.83333 5 3.63333 7.21905 6.5 10.3333 8.71905 8.13333 3.50833 6.80833 5.68929 5.35595 8.30833 8.00833 6.85595 10.1893 4.16667 8.08333 6.46905 5.88333 9.58333 8.66667 7.38333 10.969 6.05833 5.75833 4.60595 7.93929 7.25833 10.5583 9.43929 9.10595
p00719
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> #pragma warning(disable : 4996) #define INF 1000000000.0 using namespace std; int n, m, p, a, b, x, y, z, t[8]; double dp[128][30]; vector<pair<int, int>> G[30]; int main() { while (true) { scanf("%d", &n); scanf("%d", &m); scanf("%d", &p); scanf("%d", &a); scanf("%d", &b); if (n == 0 && m == 0 && p == 0 && a == 0 && b == 0) break; for (int i = 0; i < m; i++) G[i].clear(); for (int i = 0; i < n; i++) scanf("%d", &t[i]); for (int i = 0; i < p; i++) { scanf("%d", &x); scanf("%d", &y); scanf("%d", &z); G[x - 1].push_back(make_pair(y - 1, z)); G[y - 1].push_back(make_pair(x - 1, z)); } for (int i = 0; i < (1 << n); i++) { for (int j = 0; j < m; j++) { dp[i][j] = INF; } } dp[0][a - 1] = 0.0; for (int i = 1; i < (1 << n); i++) { for (int j = 0; j < n; j++) { if (i & (1 << j)) { for (int k = 0; k < m; k++) { for (int l = 0; l < G[k].size(); l++) { int node = G[k][l].first; double dist = G[k][l].second * 1.0 / t[j]; dp[i][node] = min(dp[i][node], dp[i - (1 << j)][k] + dist); } } } } } double ret = INF; for (int i = 0; i < (1 << n); i++) { ret = min(ret, dp[i][b - 1]); } if (ret != INF) { printf("%.12f\n", ret); } else { printf("Impossible\n"); } } return 0; }
#include <algorithm> #include <iostream> #include <vector> #pragma warning(disable : 4996) #define INF 1000000000.0 using namespace std; int n, m, p, a, b, x, y, z, t[8]; double dp[256][30]; vector<pair<int, int>> G[30]; int main() { while (true) { scanf("%d", &n); scanf("%d", &m); scanf("%d", &p); scanf("%d", &a); scanf("%d", &b); if (n == 0 && m == 0 && p == 0 && a == 0 && b == 0) break; for (int i = 0; i < m; i++) G[i].clear(); for (int i = 0; i < n; i++) scanf("%d", &t[i]); for (int i = 0; i < p; i++) { scanf("%d", &x); scanf("%d", &y); scanf("%d", &z); G[x - 1].push_back(make_pair(y - 1, z)); G[y - 1].push_back(make_pair(x - 1, z)); } for (int i = 0; i < (1 << n); i++) { for (int j = 0; j < m; j++) { dp[i][j] = INF; } } dp[0][a - 1] = 0.0; for (int i = 1; i < (1 << n); i++) { for (int j = 0; j < n; j++) { if (i & (1 << j)) { for (int k = 0; k < m; k++) { for (int l = 0; l < G[k].size(); l++) { int node = G[k][l].first; double dist = G[k][l].second * 1.0 / t[j]; dp[i][node] = min(dp[i][node], dp[i - (1 << j)][k] + dist); } } } } } double ret = INF; for (int i = 0; i < (1 << n); i++) { ret = min(ret, dp[i][b - 1]); } if (ret != INF) { printf("%.12f\n", ret); } else { printf("Impossible\n"); } } return 0; }
replace
12
13
12
13
-11
p00719
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef pair<int, double> P; int main() { int n, m, p, a, b; while (cin >> n >> m >> p >> a >> b, n) { a--, b--; double t[8], tp; vector<P> e[31]; for (int i = 0; i < n; i++) cin >> t[i]; for (int j = 0, x, y; j < p; j++) { cin >> x >> y >> tp; x--, y--; e[x].push_back(P(y, tp)), e[y].push_back(P(x, tp)); } double dp[1 << 8][30]; for (int i = 0; i < 1 << n; i++) for (int j = 0; j < m; j++) dp[i][j] = 1e4; dp[0][a] = 0; for (int i = 0; i < 1 << n; i++) for (int j = 0; j < m; j++) for (int k = 0; k < n; k++) if (!(i & 1 << k)) for (int l = 0; l < e[k].size(); l++) dp[i + (1 << k)][e[j][l].first] = min(dp[i + (1 << k)][e[j][l].first], dp[i][j] + e[j][l].second / t[k]); double ans = 1e4; for (int i = 0; i < 1 << n; i++) ans = min(ans, dp[i][b]); if (ans == 1e4) cout << "Impossible" << endl; else cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, double> P; int main() { int n, m, p, a, b; while (cin >> n >> m >> p >> a >> b, n) { a--, b--; double t[8], tp; vector<P> e[31]; for (int i = 0; i < n; i++) cin >> t[i]; for (int j = 0, x, y; j < p; j++) { cin >> x >> y >> tp; x--, y--; e[x].push_back(P(y, tp)), e[y].push_back(P(x, tp)); } double dp[1 << 8][30]; for (int i = 0; i < 1 << n; i++) for (int j = 0; j < m; j++) dp[i][j] = 1e4; dp[0][a] = 0; for (int i = 0; i < 1 << n; i++) for (int j = 0; j < m; j++) for (int k = 0; k < n; k++) if (!(i & 1 << k)) for (int l = 0; l < e[j].size(); l++) dp[i + (1 << k)][e[j][l].first] = min(dp[i + (1 << k)][e[j][l].first], dp[i][j] + e[j][l].second / t[k]); double ans = 1e4; for (int i = 0; i < 1 << n; i++) ans = min(ans, dp[i][b]); if (ans == 1e4) cout << "Impossible" << endl; else cout << ans << endl; } return 0; }
replace
29
30
29
30
0
p00719
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> #define INF 1000000000 #define LINF 9000000000000000000 #define mod 1000000007 #define rep(i, n) for (int i = 0; i < int(n); i++) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define REP(i, a, b) for (int i = (a); i < int(b); i++) #define all(x) (x).begin(), x.end() #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<long long> vll; typedef pair<int, int> pi; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; bool debug = false; /*---------------------------------------------------*/ typedef pair<double, pair<int, int>> type; void solve(int n, int m, int p, int a, int b) { a--; b--; vector<int> t(m); for (int i = 0; i < n; i++) { cin >> t[i]; } int dist[105][105]; for (int i = 0; i < 105; i++) { for (int j = 0; j < 105; j++) dist[i][j] = -1; } vector<int> x(p), y(p), z(p); for (int i = 0; i < p; i++) { cin >> x[i] >> y[i] >> z[i]; x[i]--; y[i]--; dist[x[i]][y[i]] = dist[y[i]][x[i]] = z[i]; } double dp[105][1 << 8]; for (int i = 0; i < 105; i++) { for (int j = 0; j < (1 << 8); j++) dp[i][j] = 1e12; } dp[a][0] = 0; // cout<<1<<endl; // first -> コスト | second.first -> 現在地 second.second -> bit mask priority_queue<type, vector<type>, greater<type>> que; // cout<<1<<endl; que.push(mp(0, mp(a, 0))); // cout<<1<<endl; while (que.size()) { type top = que.top(); que.pop(); double cost = top.first; int now = top.second.first, now_mask = top.second.second; if (cost > dp[now][now_mask]) continue; for (int i = 0; i < m; i++) if (dist[now][i] != -1) { for (int j = 0; j < n; j++) { int next_mask = now_mask; if (next_mask & (1 << j)) continue; next_mask |= (1 << j); double next_cost = cost + double(dist[now][i]) / t[j]; if (dp[i][next_mask] > next_cost) { dp[i][next_mask] = next_cost; que.push(mp(next_cost, mp(i, next_mask))); } } } } double ans = 1e12; for (int i = 0; i < (1 << 8); i++) { ans = min(ans, dp[b][i]); } if (ans == 1e12) printf("Impossible\n"); else printf("%.8f\n", ans); } int main() { int n, m, p, a, b; while (cin >> n >> m >> p >> a >> b) { if (!(n | m | p | a | b)) break; solve(n, m, p, a, b); } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> #define INF 1000000000 #define LINF 9000000000000000000 #define mod 1000000007 #define rep(i, n) for (int i = 0; i < int(n); i++) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define REP(i, a, b) for (int i = (a); i < int(b); i++) #define all(x) (x).begin(), x.end() #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<long long> vll; typedef pair<int, int> pi; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; bool debug = false; /*---------------------------------------------------*/ typedef pair<double, pair<int, int>> type; void solve(int n, int m, int p, int a, int b) { a--; b--; vector<int> t(n); for (int i = 0; i < n; i++) { cin >> t[i]; } int dist[105][105]; for (int i = 0; i < 105; i++) { for (int j = 0; j < 105; j++) dist[i][j] = -1; } vector<int> x(p), y(p), z(p); for (int i = 0; i < p; i++) { cin >> x[i] >> y[i] >> z[i]; x[i]--; y[i]--; dist[x[i]][y[i]] = dist[y[i]][x[i]] = z[i]; } double dp[105][1 << 8]; for (int i = 0; i < 105; i++) { for (int j = 0; j < (1 << 8); j++) dp[i][j] = 1e12; } dp[a][0] = 0; // cout<<1<<endl; // first -> コスト | second.first -> 現在地 second.second -> bit mask priority_queue<type, vector<type>, greater<type>> que; // cout<<1<<endl; que.push(mp(0, mp(a, 0))); // cout<<1<<endl; while (que.size()) { type top = que.top(); que.pop(); double cost = top.first; int now = top.second.first, now_mask = top.second.second; if (cost > dp[now][now_mask]) continue; for (int i = 0; i < m; i++) if (dist[now][i] != -1) { for (int j = 0; j < n; j++) { int next_mask = now_mask; if (next_mask & (1 << j)) continue; next_mask |= (1 << j); double next_cost = cost + double(dist[now][i]) / t[j]; if (dp[i][next_mask] > next_cost) { dp[i][next_mask] = next_cost; que.push(mp(next_cost, mp(i, next_mask))); } } } } double ans = 1e12; for (int i = 0; i < (1 << 8); i++) { ans = min(ans, dp[b][i]); } if (ans == 1e12) printf("Impossible\n"); else printf("%.8f\n", ans); } int main() { int n, m, p, a, b; while (cin >> n >> m >> p >> a >> b) { if (!(n | m | p | a | b)) break; solve(n, m, p, a, b); } return 0; }
replace
54
55
54
55
-6
munmap_chunk(): invalid pointer
p00721
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; int w, h; vector<string> field; int dist(pair<int, int> a, pair<int, int> b) { queue<pair<pair<int, int>, int>> bfs; set<pair<int, int>> already; bfs.push(make_pair(a, 0)); while (!bfs.empty()) { pair<int, int> pos; int cost; tie(pos, cost) = bfs.front(); bfs.pop(); int y, x; tie(y, x) = pos; if (y < 0 || h <= y || x < 0 || w <= h) continue; if (field[y][x] == 'x') continue; if (!already.insert(pos).second) continue; if (pos == b) return cost; int dy[4] = {0, 0, -1, 1}, dx[4] = {-1, 1, 0, 0}; for (int i = 0; i < 4; ++i) bfs.push(make_pair(make_pair(y + dy[i], x + dx[i]), cost + 1)); } return 1048576; } int solve() { cin >> w >> h; if (!w && !h) return 0; field.resize(h); for (int i = 0; i < h; ++i) cin >> field[i]; pair<int, int> robot; vector<pair<int, int>> dirty; for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) { if (field[i][j] == 'o') robot = make_pair(i, j); if (field[i][j] == '*') dirty.push_back(make_pair(i, j)); } // init bitdp[state][lastpos] vector<vector<int>> bitdp; bitdp.assign(1 << dirty.size(), vector<int>()); for (int i = 0; i < (1 << dirty.size()); ++i) bitdp[i].assign(dirty.size(), 1048576); for (int i = 0; i < dirty.size(); ++i) { bitdp[1 << i][i] = dist(robot, dirty[i]); if (bitdp[1 << i][i] > 8192) return -1; } vector<vector<int>> ecost; ecost.assign(dirty.size(), vector<int>()); for (int i = 0; i < dirty.size(); ++i) ecost[i].assign(dirty.size(), 1048576); for (int i = 0; i < dirty.size(); ++i) for (int j = 0; j < dirty.size(); ++j) ecost[i][j] = dist(dirty[i], dirty[j]); for (int i = 0; i < (1 << dirty.size()); ++i) for (int j = 0; j < dirty.size(); ++j) { if (i & (1 << j)) continue; for (int k = 0; k < dirty.size(); ++k) if (i & (1 << k)) bitdp[i | (1 << j)][j] = min(bitdp[i | (1 << j)][j], bitdp[i][k] + ecost[k][j]); } int answer = 1048576; for (int i = 0; i < dirty.size(); ++i) answer = min(answer, bitdp[(1 << dirty.size()) - 1][i]); if (answer > 65536) answer = -1; return answer; } int main() { for (;;) { int answer = solve(); if (answer) cout << answer << endl; else break; } return 0; }
#include <bits/stdc++.h> using namespace std; int w, h; vector<string> field; int dist(pair<int, int> a, pair<int, int> b) { queue<pair<pair<int, int>, int>> bfs; set<pair<int, int>> already; bfs.push(make_pair(a, 0)); while (!bfs.empty()) { pair<int, int> pos; int cost; tie(pos, cost) = bfs.front(); bfs.pop(); int y, x; tie(y, x) = pos; if (y < 0 || h <= y || x < 0 || w <= x) continue; if (field[y][x] == 'x') continue; if (!already.insert(pos).second) continue; if (pos == b) return cost; int dy[4] = {0, 0, -1, 1}, dx[4] = {-1, 1, 0, 0}; for (int i = 0; i < 4; ++i) bfs.push(make_pair(make_pair(y + dy[i], x + dx[i]), cost + 1)); } return 1048576; } int solve() { cin >> w >> h; if (!w && !h) return 0; field.resize(h); for (int i = 0; i < h; ++i) cin >> field[i]; pair<int, int> robot; vector<pair<int, int>> dirty; for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) { if (field[i][j] == 'o') robot = make_pair(i, j); if (field[i][j] == '*') dirty.push_back(make_pair(i, j)); } // init bitdp[state][lastpos] vector<vector<int>> bitdp; bitdp.assign(1 << dirty.size(), vector<int>()); for (int i = 0; i < (1 << dirty.size()); ++i) bitdp[i].assign(dirty.size(), 1048576); for (int i = 0; i < dirty.size(); ++i) { bitdp[1 << i][i] = dist(robot, dirty[i]); if (bitdp[1 << i][i] > 8192) return -1; } vector<vector<int>> ecost; ecost.assign(dirty.size(), vector<int>()); for (int i = 0; i < dirty.size(); ++i) ecost[i].assign(dirty.size(), 1048576); for (int i = 0; i < dirty.size(); ++i) for (int j = 0; j < dirty.size(); ++j) ecost[i][j] = dist(dirty[i], dirty[j]); for (int i = 0; i < (1 << dirty.size()); ++i) for (int j = 0; j < dirty.size(); ++j) { if (i & (1 << j)) continue; for (int k = 0; k < dirty.size(); ++k) if (i & (1 << k)) bitdp[i | (1 << j)][j] = min(bitdp[i | (1 << j)][j], bitdp[i][k] + ecost[k][j]); } int answer = 1048576; for (int i = 0; i < dirty.size(); ++i) answer = min(answer, bitdp[(1 << dirty.size()) - 1][i]); if (answer > 65536) answer = -1; return answer; } int main() { for (;;) { int answer = solve(); if (answer) cout << answer << endl; else break; } return 0; }
replace
15
16
15
16
MLE
p00721
C++
Memory Limit Exceeded
#include <algorithm> #include <chrono> #include <functional> #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; #define INF 1e9 //???????????? class Timer { chrono::high_resolution_clock::time_point start, end; double limit; public: Timer() { start = chrono::high_resolution_clock::now(); } Timer(double l) { start = chrono::high_resolution_clock::now(); limit = l; } double getTime() { end = chrono::high_resolution_clock::now(); return chrono::duration<double>(end - start).count(); } bool Over() { if (getTime() > limit) { return true; } return false; } void setLimit(double l) { limit = l; } void setStart() { start = chrono::high_resolution_clock::now(); } }; struct POINT { int x, y; POINT() {} POINT(int x_, int y_) { x = x_, y = y_; } bool operator<(const POINT &r) const { return x + y < r.x + r.y; } POINT operator+(const POINT &r) const { return POINT(x + r.x, y + r.y); } }; struct STATUS { int dist; int id; vector<bool> used; STATUS() {} STATUS(int dist_, int id_, vector<bool> used_) { dist = dist_; id = id_; used = used_; } bool operator>(const STATUS &r) const { return dist > r.dist; } }; int W, H; vector<vector<int>> MAP; int status[20][20]; int cost[20][20]; void dijkstra(POINT p) { int v[] = {-1, 0, 1, 0}; int h[] = {0, 1, 0, -1}; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { status[i][j] = 0; cost[i][j] = INF; } } cost[p.x][p.y] = 0; status[p.x][p.y] = 1; priority_queue<pair<int, POINT>, vector<pair<int, POINT>>, greater<pair<int, POINT>>> U; U.push(make_pair(cost[p.x][p.y], p)); while (!U.empty()) { //??¢?´¢???????????£??????????????§?????????????°??????? POINT u = U.top().second; U.pop(); if (status[u.x][u.y] == -1) continue; status[u.x][u.y] = -1; //?????????????°????????????¢??? for (int i = 0; i < 4; i++) { POINT np = u + POINT(v[i], h[i]); if (np.x < 0 || np.x >= H || np.y < 0 || np.y >= W) continue; if (MAP[np.x][np.y] == 1) continue; if (status[np.x][np.y] != -1) { if (cost[np.x][np.y] > cost[u.x][u.y] + 1) { cost[np.x][np.y] = cost[u.x][u.y] + 1; status[np.x][np.y] = 1; U.push(make_pair(cost[np.x][np.y], np)); } } } } } int main() { while (1) { cin >> W >> H; if (W == 0 && H == 0) break; MAP.assign(H, vector<int>(W, 0)); vector<POINT> pos; int cnt = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { char c; cin >> c; if (c == '*') { POINT p = POINT(i, j); pos.push_back(p); cnt++; } else if (c == 'x') MAP[i][j] = 1; else if (c == 'o') { POINT sp = POINT(i, j); pos.insert(pos.begin(), sp); cnt++; } } } bool fin = false; vector<vector<int>> dist(cnt); for (int i = 0; i < cnt; i++) { dist[i].resize(cnt); dijkstra(pos[i]); for (int j = 1; j < cnt; j++) { int distance = cost[pos[j].x][pos[j].y]; if (distance == INF) fin = true; dist[i][j] = distance; } } if (fin) { cout << -1 << endl; continue; } Timer tmr(0.5); vector<priority_queue<STATUS, vector<STATUS>, greater<STATUS>>> STAT(cnt); STAT[0].push(STATUS(0, 0, vector<bool>(cnt))); while (1) { if (tmr.Over()) break; for (int i = 0; i < cnt - 1; i++) { if (STAT[i].empty()) continue; STATUS st = STAT[i].top(); STAT[i].pop(); for (int j = 1; j < cnt; j++) { if (st.used[j]) continue; st.used[j] = true; STAT[i + 1].push(STATUS(st.dist + dist[st.id][j], j, st.used)); st.used[j] = false; } } } STATUS last = STAT[cnt - 1].top(); cout << last.dist << endl; } return 0; }
#include <algorithm> #include <chrono> #include <functional> #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; #define INF 1e9 //???????????? class Timer { chrono::high_resolution_clock::time_point start, end; double limit; public: Timer() { start = chrono::high_resolution_clock::now(); } Timer(double l) { start = chrono::high_resolution_clock::now(); limit = l; } double getTime() { end = chrono::high_resolution_clock::now(); return chrono::duration<double>(end - start).count(); } bool Over() { if (getTime() > limit) { return true; } return false; } void setLimit(double l) { limit = l; } void setStart() { start = chrono::high_resolution_clock::now(); } }; struct POINT { int x, y; POINT() {} POINT(int x_, int y_) { x = x_, y = y_; } bool operator<(const POINT &r) const { return x + y < r.x + r.y; } POINT operator+(const POINT &r) const { return POINT(x + r.x, y + r.y); } }; struct STATUS { int dist; int id; vector<bool> used; STATUS() {} STATUS(int dist_, int id_, vector<bool> used_) { dist = dist_; id = id_; used = used_; } bool operator>(const STATUS &r) const { return dist > r.dist; } }; int W, H; vector<vector<int>> MAP; int status[20][20]; int cost[20][20]; void dijkstra(POINT p) { int v[] = {-1, 0, 1, 0}; int h[] = {0, 1, 0, -1}; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { status[i][j] = 0; cost[i][j] = INF; } } cost[p.x][p.y] = 0; status[p.x][p.y] = 1; priority_queue<pair<int, POINT>, vector<pair<int, POINT>>, greater<pair<int, POINT>>> U; U.push(make_pair(cost[p.x][p.y], p)); while (!U.empty()) { //??¢?´¢???????????£??????????????§?????????????°??????? POINT u = U.top().second; U.pop(); if (status[u.x][u.y] == -1) continue; status[u.x][u.y] = -1; //?????????????°????????????¢??? for (int i = 0; i < 4; i++) { POINT np = u + POINT(v[i], h[i]); if (np.x < 0 || np.x >= H || np.y < 0 || np.y >= W) continue; if (MAP[np.x][np.y] == 1) continue; if (status[np.x][np.y] != -1) { if (cost[np.x][np.y] > cost[u.x][u.y] + 1) { cost[np.x][np.y] = cost[u.x][u.y] + 1; status[np.x][np.y] = 1; U.push(make_pair(cost[np.x][np.y], np)); } } } } } int main() { while (1) { cin >> W >> H; if (W == 0 && H == 0) break; MAP.assign(H, vector<int>(W, 0)); vector<POINT> pos; int cnt = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { char c; cin >> c; if (c == '*') { POINT p = POINT(i, j); pos.push_back(p); cnt++; } else if (c == 'x') MAP[i][j] = 1; else if (c == 'o') { POINT sp = POINT(i, j); pos.insert(pos.begin(), sp); cnt++; } } } bool fin = false; vector<vector<int>> dist(cnt); for (int i = 0; i < cnt; i++) { dist[i].resize(cnt); dijkstra(pos[i]); for (int j = 1; j < cnt; j++) { int distance = cost[pos[j].x][pos[j].y]; if (distance == INF) fin = true; dist[i][j] = distance; } } if (fin) { cout << -1 << endl; continue; } Timer tmr(0.016); vector<priority_queue<STATUS, vector<STATUS>, greater<STATUS>>> STAT(cnt); STAT[0].push(STATUS(0, 0, vector<bool>(cnt))); while (1) { if (tmr.Over()) break; for (int i = 0; i < cnt - 1; i++) { if (STAT[i].empty()) continue; STATUS st = STAT[i].top(); STAT[i].pop(); for (int j = 1; j < cnt; j++) { if (st.used[j]) continue; st.used[j] = true; STAT[i + 1].push(STATUS(st.dist + dist[st.id][j], j, st.used)); st.used[j] = false; } } } STATUS last = STAT[cnt - 1].top(); cout << last.dist << endl; } return 0; }
replace
163
164
163
164
MLE
p00721
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iostream> #include <math.h> #include <queue> #include <set> #include <stack> #include <string.h> #include <string> using namespace std; #define FOR(i, n) for (int i = 0; i < n; i++) #define bit(i) static_cast<bitset<8>>(i) typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef vector<string> VS; typedef pair<int, int> PII; typedef pair<long long, long long> PLL; typedef queue<int> QI; typedef priority_queue<int> maxpq; typedef priority_queue<int, vector<int>, greater<int>> minpq; struct edge { int to, cost; }; void begin() { cin.tie(0); ios::sync_with_stdio(false); }; int gcd(int a, int b) { if (a % b == 0) { return (b); } else { return (gcd(b, a % b)); } }; int lcm(int m, int n) { if ((0 == m) || (0 == n)) { return 0; } return ((m / gcd(m, n)) * n); }; unsigned long long comb(long n, long m) { unsigned long long c = 1; m = (n - m < m ? n - m : m); for (long ns = n - m + 1, ms = 1; ms <= m; ns++, ms++) { c *= ns; c /= ms; } return c; }; void cp(int a1[], int a2[], int l) { FOR(i, l) a2[i] = a1[i]; }; double sq(double d) { return d * d; }; int sq(int i) { return i * i; }; double sqdist(int x1, int y1, int x2, int y2) { double dx = x1 - x2, dy = y1 - y2; return dx * dx + dy * dy; }; // ワーシャルフロイド法 void warshallFloyd(int d[100][100], int n) { for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (d[i][k] != -1 && d[k][j] != -1) { if (d[i][j] == -1) { d[i][j] = d[i][k] + d[k][j]; } else { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } }; // d:隣接行列 n:グラフのサイズ s:始点 dist:始点からの距離が入る配列 void dijkstra(int d[1000][1000], int n, int s, int dist[1000]) { FOR(i, n) dist[i] = -1; dist[s] = 0; priority_queue<PII, vector<PII>, greater<PII>> q; q.push(PII(0, s)); while (!q.empty()) { PII p = q.top(); q.pop(); int i = p.second; if (dist[i] < p.first) continue; for (int j = 0; j < n; j++) { if (d[i][j] == -1) continue; if (dist[j] == -1) { dist[j] = dist[i] + d[i][j]; q.push(PII(dist[j], j)); } else if (dist[j] > dist[i] + d[i][j]) { dist[j] = dist[i] + d[i][j]; q.push(PII(dist[j], j)); } } } }; // 線分の交差判定 bool isCross(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { // 並行な場合 int m = (x2 - x1) * (y4 - y3) - (y2 - y1) * (x4 - x3); if (m == 0) return false; // 媒介変数の値が0より大きく1以下なら交差する、これは問題の状況によって変わるかも。 double r = (double)((y4 - y3) * (x3 - x1) - (x4 - x3) * (y3 - y1)) / m; double s = (double)((y2 - y1) * (x3 - x1) - (x2 - x1) * (y3 - y1)) / m; return (0 < r && r <= 1 && 0 < s && s <= 1); }; // 外積の計算 AB CD の内積を求める int crossProdct(int ax, int ay, int bx, int by, int cx, int cy, int dx, int dy) { int abx = bx - ax; int aby = by - ay; int cdx = dx - cx; int cdy = dy - cy; return abx * cdy - cdx * aby; }; double crossProdct(double ax, double ay, double bx, double by, double cx, double cy, double dx, double dy) { double abx = bx - ax; double aby = by - ay; double cdx = dx - cx; double cdy = dy - cy; return abx * cdy - cdx * aby; }; double innerProduct(double ax, double ay, double bx, double by, double cx, double cy, double dx, double dy) { double abx = bx - ax; double aby = by - ay; double cdx = dx - cx; double cdy = dy - cy; return abx * cdx + aby * cdy; }; // 三角形の内部判定 ABCの中にPがあるか判定 bool inTriangle(int ax, int ay, int bx, int by, int cx, int cy, int px, int py) { int c1 = crossProdct(ax, ay, bx, by, bx, by, px, py); int c2 = crossProdct(bx, by, cx, cy, cx, cy, px, py); int c3 = crossProdct(cx, cy, ax, ay, ax, ay, px, py); return (c1 > 0 && c2 > 0 && c3 > 0) || (c1 < 0 && c2 < 0 && c3 < 0); }; bool inTriangle(double ax, double ay, double bx, double by, double cx, double cy, double px, double py) { double c1 = crossProdct(ax, ay, bx, by, bx, by, px, py); double c2 = crossProdct(bx, by, cx, cy, cx, cy, px, py); double c3 = crossProdct(cx, cy, ax, ay, ax, ay, px, py); return (c1 > 0 && c2 > 0 && c3 > 0) || (c1 < 0 && c2 < 0 && c3 < 0); }; // 三角形の外心 void circumcenter(double ax, double ay, double bx, double by, double cx, double cy, double res[3]) { // AB AC を求める double abx = bx - ax; double aby = by - ay; double acx = cx - ax; double acy = cy - ay; double m = abx * acy - acx * aby; // 分母 m = 0 となるのは3点が一直線になるとき double s = (abx * acx + aby * acy - sq(acx) - sq(acy)) / m; // 媒介変数s res[0] = abx / 2 + s * aby / 2; res[1] = aby / 2 - s * abx / 2; // cout << res[0] << " " << res[1] << endl; res[2] = sqrt(sqdist(0, 0, res[0], res[1])); res[0] += ax; res[1] += ay; }; /** * start * @author yoshikyoto */ int w, h; string tile[20]; int dist[20][20]; int g[11][11]; int sx[11], sy[11]; int dp[1024][11]; int vy[4] = {-1, 1, 0, 0}, vx[4] = {0, 0, -1, 1}; int dcount = 1; int p[11]; void dfs(int hist, int curr) { for (int next = 1; next < dcount; next++) { if (hist & p[next]) continue; // 既に訪れている // 訪れていない int next_hist = hist | p[next]; int cost = dp[hist][curr] + g[curr][next]; if (cost < dp[next_hist][next] || dp[next_hist][next] == -1) { dp[next_hist][next] = cost; dfs(next_hist, next); } } }; void solve() { FOR(i, h) cin >> tile[i]; // 配列をなめて汚れたタイルの位置を獲得、汚れに番号をつける FOR(i, h) FOR(j, w) { if (tile[i][j] == '*') { sy[dcount] = i; sx[dcount] = j; tile[i][j] = (char)('0' + dcount); // 汚れに番号を付ける dcount++; } else if (tile[i][j] == 'o') { // 自分の位置 sy[0] = i; sx[0] = j; tile[i][j] = '0'; } } // グラフの初期化 FOR(i, dcount) FOR(j, dcount) g[i][j] = -1; // FOR(i, h) cout << tile[i] << endl; // まずはbfsしてグラフを生成する FOR(i, dcount) { FOR(k, h) FOR(l, w) dist[k][l] = -1; dist[sy[i]][sx[i]] = 0; QI yq, xq; yq.push(sy[i]); xq.push(sx[i]); int checked = 1; while (checked < dcount && !yq.empty()) { int y = yq.front(); yq.pop(); int x = xq.front(); xq.pop(); // 上下左右を見る FOR(j, 4) { int ny = y + vy[j], nx = x + vx[j]; // nexty, nextx; if (dist[ny][nx] > -1) continue; // 既に訪れた場合 if (ny < 0 || ny > h - 1 || nx < 0 || nx > w - 1) continue; // 部屋を出てしまう場合 int dnum = (int)tile[ny][nx] - '0'; if (tile[ny][nx] != 'x') { // 障害物でない場合 yq.push(ny); xq.push(nx); dist[ny][nx] = dist[y][x] + 1; // グラフに追加したい場 if (0 <= dnum && dnum < dcount) { g[i][dnum] = dist[ny][nx]; checked++; } } } } } /* FOR(i, dcount){ FOR(j, dcount){ cout << g[i][j] << " "; } cout << endl; } */ // 行けないところがあったらダメ for (int i = 1; i < dcount; i++) if (g[0][i] == -1) { cout << -1 << endl; return; } // メモ化再帰 FOR(i, 1024) FOR(j, 11) dp[i][j] = -1; FOR(i, dcount) p[i] = (int)pow(2, i); dp[1][0] = 0; dfs(1, 0); int final = 1; for (int i = 1; i < dcount; i++) final += p[i]; int ans = dp[final][1]; for (int i = 2; i < dcount; i++) { ans = min(ans, dp[final][i]); } cout << ans << endl; }; int main(int argc, const char *argv[]) { begin(); while (true) { cin >> w >> h; if (w == 0 && h == 0) break; solve(); } }
#include <algorithm> #include <cstdio> #include <iostream> #include <math.h> #include <queue> #include <set> #include <stack> #include <string.h> #include <string> using namespace std; #define FOR(i, n) for (int i = 0; i < n; i++) #define bit(i) static_cast<bitset<8>>(i) typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef vector<string> VS; typedef pair<int, int> PII; typedef pair<long long, long long> PLL; typedef queue<int> QI; typedef priority_queue<int> maxpq; typedef priority_queue<int, vector<int>, greater<int>> minpq; struct edge { int to, cost; }; void begin() { cin.tie(0); ios::sync_with_stdio(false); }; int gcd(int a, int b) { if (a % b == 0) { return (b); } else { return (gcd(b, a % b)); } }; int lcm(int m, int n) { if ((0 == m) || (0 == n)) { return 0; } return ((m / gcd(m, n)) * n); }; unsigned long long comb(long n, long m) { unsigned long long c = 1; m = (n - m < m ? n - m : m); for (long ns = n - m + 1, ms = 1; ms <= m; ns++, ms++) { c *= ns; c /= ms; } return c; }; void cp(int a1[], int a2[], int l) { FOR(i, l) a2[i] = a1[i]; }; double sq(double d) { return d * d; }; int sq(int i) { return i * i; }; double sqdist(int x1, int y1, int x2, int y2) { double dx = x1 - x2, dy = y1 - y2; return dx * dx + dy * dy; }; // ワーシャルフロイド法 void warshallFloyd(int d[100][100], int n) { for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (d[i][k] != -1 && d[k][j] != -1) { if (d[i][j] == -1) { d[i][j] = d[i][k] + d[k][j]; } else { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } }; // d:隣接行列 n:グラフのサイズ s:始点 dist:始点からの距離が入る配列 void dijkstra(int d[1000][1000], int n, int s, int dist[1000]) { FOR(i, n) dist[i] = -1; dist[s] = 0; priority_queue<PII, vector<PII>, greater<PII>> q; q.push(PII(0, s)); while (!q.empty()) { PII p = q.top(); q.pop(); int i = p.second; if (dist[i] < p.first) continue; for (int j = 0; j < n; j++) { if (d[i][j] == -1) continue; if (dist[j] == -1) { dist[j] = dist[i] + d[i][j]; q.push(PII(dist[j], j)); } else if (dist[j] > dist[i] + d[i][j]) { dist[j] = dist[i] + d[i][j]; q.push(PII(dist[j], j)); } } } }; // 線分の交差判定 bool isCross(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { // 並行な場合 int m = (x2 - x1) * (y4 - y3) - (y2 - y1) * (x4 - x3); if (m == 0) return false; // 媒介変数の値が0より大きく1以下なら交差する、これは問題の状況によって変わるかも。 double r = (double)((y4 - y3) * (x3 - x1) - (x4 - x3) * (y3 - y1)) / m; double s = (double)((y2 - y1) * (x3 - x1) - (x2 - x1) * (y3 - y1)) / m; return (0 < r && r <= 1 && 0 < s && s <= 1); }; // 外積の計算 AB CD の内積を求める int crossProdct(int ax, int ay, int bx, int by, int cx, int cy, int dx, int dy) { int abx = bx - ax; int aby = by - ay; int cdx = dx - cx; int cdy = dy - cy; return abx * cdy - cdx * aby; }; double crossProdct(double ax, double ay, double bx, double by, double cx, double cy, double dx, double dy) { double abx = bx - ax; double aby = by - ay; double cdx = dx - cx; double cdy = dy - cy; return abx * cdy - cdx * aby; }; double innerProduct(double ax, double ay, double bx, double by, double cx, double cy, double dx, double dy) { double abx = bx - ax; double aby = by - ay; double cdx = dx - cx; double cdy = dy - cy; return abx * cdx + aby * cdy; }; // 三角形の内部判定 ABCの中にPがあるか判定 bool inTriangle(int ax, int ay, int bx, int by, int cx, int cy, int px, int py) { int c1 = crossProdct(ax, ay, bx, by, bx, by, px, py); int c2 = crossProdct(bx, by, cx, cy, cx, cy, px, py); int c3 = crossProdct(cx, cy, ax, ay, ax, ay, px, py); return (c1 > 0 && c2 > 0 && c3 > 0) || (c1 < 0 && c2 < 0 && c3 < 0); }; bool inTriangle(double ax, double ay, double bx, double by, double cx, double cy, double px, double py) { double c1 = crossProdct(ax, ay, bx, by, bx, by, px, py); double c2 = crossProdct(bx, by, cx, cy, cx, cy, px, py); double c3 = crossProdct(cx, cy, ax, ay, ax, ay, px, py); return (c1 > 0 && c2 > 0 && c3 > 0) || (c1 < 0 && c2 < 0 && c3 < 0); }; // 三角形の外心 void circumcenter(double ax, double ay, double bx, double by, double cx, double cy, double res[3]) { // AB AC を求める double abx = bx - ax; double aby = by - ay; double acx = cx - ax; double acy = cy - ay; double m = abx * acy - acx * aby; // 分母 m = 0 となるのは3点が一直線になるとき double s = (abx * acx + aby * acy - sq(acx) - sq(acy)) / m; // 媒介変数s res[0] = abx / 2 + s * aby / 2; res[1] = aby / 2 - s * abx / 2; // cout << res[0] << " " << res[1] << endl; res[2] = sqrt(sqdist(0, 0, res[0], res[1])); res[0] += ax; res[1] += ay; }; /** * start * @author yoshikyoto */ int w, h; string tile[20]; int dist[20][20]; int g[11][11]; int sx[11], sy[11]; int dp[1024][11]; int vy[4] = {-1, 1, 0, 0}, vx[4] = {0, 0, -1, 1}; int dcount = 1; int p[11]; void dfs(int hist, int curr) { for (int next = 1; next < dcount; next++) { if (hist & p[next]) continue; // 既に訪れている // 訪れていない int next_hist = hist | p[next]; int cost = dp[hist][curr] + g[curr][next]; if (cost < dp[next_hist][next] || dp[next_hist][next] == -1) { dp[next_hist][next] = cost; dfs(next_hist, next); } } }; void solve() { FOR(i, h) cin >> tile[i]; // 配列をなめて汚れたタイルの位置を獲得、汚れに番号をつける FOR(i, h) FOR(j, w) { if (tile[i][j] == '*') { sy[dcount] = i; sx[dcount] = j; tile[i][j] = (char)('0' + dcount); // 汚れに番号を付ける dcount++; } else if (tile[i][j] == 'o') { // 自分の位置 sy[0] = i; sx[0] = j; tile[i][j] = '0'; } } // グラフの初期化 FOR(i, dcount) FOR(j, dcount) g[i][j] = -1; // FOR(i, h) cout << tile[i] << endl; // まずはbfsしてグラフを生成する FOR(i, dcount) { FOR(k, h) FOR(l, w) dist[k][l] = -1; dist[sy[i]][sx[i]] = 0; QI yq, xq; yq.push(sy[i]); xq.push(sx[i]); int checked = 1; while (checked < dcount && !yq.empty()) { int y = yq.front(); yq.pop(); int x = xq.front(); xq.pop(); // 上下左右を見る FOR(j, 4) { int ny = y + vy[j], nx = x + vx[j]; // nexty, nextx; if (dist[ny][nx] > -1) continue; // 既に訪れた場合 if (ny < 0 || ny > h - 1 || nx < 0 || nx > w - 1) continue; // 部屋を出てしまう場合 int dnum = (int)tile[ny][nx] - '0'; if (tile[ny][nx] != 'x') { // 障害物でない場合 yq.push(ny); xq.push(nx); dist[ny][nx] = dist[y][x] + 1; // グラフに追加したい場 if (0 <= dnum && dnum < dcount) { g[i][dnum] = dist[ny][nx]; checked++; } } } } } /* FOR(i, dcount){ FOR(j, dcount){ cout << g[i][j] << " "; } cout << endl; } */ // 行けないところがあったらダメ for (int i = 1; i < dcount; i++) if (g[0][i] == -1) { cout << -1 << endl; return; } // メモ化再帰 FOR(i, 1024) FOR(j, 11) dp[i][j] = -1; FOR(i, dcount) p[i] = (int)pow(2, i); dp[1][0] = 0; dfs(1, 0); int final = 1; for (int i = 1; i < dcount; i++) final += p[i]; int ans = dp[final][1]; for (int i = 2; i < dcount; i++) { ans = min(ans, dp[final][i]); } cout << ans << endl; }; int main(int argc, const char *argv[]) { begin(); while (true) { cin >> w >> h; if (w == 0 && h == 0) break; dcount = 1; solve(); } }
insert
302
302
302
303
0
p00721
C++
Runtime Error
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <string> #include <utility> #include <vector> using namespace std; #define ALL(g) (g).begin(), (g).end() #define REP(i, x, n) for (int i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) #define F(i, j, k) fill(i[0], i[0] + j * j, k) #define P(p) cout << (p) << endl; #define EXIST(s, e) ((s).find(e) != (s).end()) #define INF 1 << 25 #define pb push_back typedef vector<int> vi; typedef vector<long long> vl; typedef vector<double> vd; typedef pair<int, int> pii; typedef pair<long, long> pll; typedef long long ll; using namespace std; struct robot { int x, y, cost, dust; bool operator>(const robot &r) const { return cost > r.cost; } }; int main() { int w, h; while (cin >> w >> h, w | h) { // input vector<string> v; rep(j, h) { string input; cin >> input; v.pb(input); } // dust map int dust[20][20]; fill_n((int *)dust, 20 * 20, 0); int bit = 1; // start & goal int sx, sy; int goal = 0; rep(j, h) { rep(i, w) { if (v[j][i] == 'o') { sx = i; sy = j; } if (v[j][i] == '*') { dust[j][i] = bit; goal += bit; bit *= 2; } } } // bfs int check[20][20][1 << 10]; fill_n((int *)check, 20 * 20 * (1 << 10), INT_MAX); int cost = -1; priority_queue<robot, vector<robot>, greater<robot>> q; q.push((robot){sx, sy, 0, 0}); while (!q.empty()) { robot p = q.top(); q.pop(); // goal if (p.dust == goal) { cost = p.cost; break; } // up if (p.y - 1 >= 0 && v[p.y - 1][p.x] != 'x') { if (v[p.y - 1][p.x] == '*' && (p.dust & dust[p.y - 1][p.x]) != dust[p.y - 1][p.x]) { if (check[p.y - 1][p.x][p.dust + dust[p.y - 1][p.x]] > p.cost + 1) { q.push( (robot){p.x, p.y - 1, p.cost + 1, p.dust + dust[p.y - 1][p.x]}); check[p.y - 1][p.x][p.dust + dust[p.y - 1][p.x]] = p.cost + 1; } } else { if (check[p.y - 1][p.x][p.dust] > p.cost + 1) { q.push((robot){p.x, p.y - 1, p.cost + 1, p.dust}); check[p.y - 1][p.x][p.dust] = p.cost + 1; } } } // down if (p.y + 1 < h && v[p.y + 1][p.x] != 'x') { if (v[p.y + 1][p.x] == '*' && (p.dust & dust[p.y + 1][p.x]) != dust[p.y + 1][p.x]) { if (check[p.y + 1][p.x][p.dust + dust[p.y + 1][p.x]] > p.cost + 1) { q.push( (robot){p.x, p.y + 1, p.cost + 1, p.dust + dust[p.y + 1][p.x]}); check[p.y + 1][p.x][p.dust + dust[p.y + 1][p.x]] = p.cost + 1; } } else { if (check[p.y + 1][p.x][p.dust] > p.cost + 1) { q.push((robot){p.x, p.y + 1, p.cost + 1, p.dust}); check[p.y + 1][p.x][p.dust] = p.cost + 1; } } } // right if (p.x + 1 < w && v[p.y][p.x + 1] != 'x') { if (v[p.y][p.x + 1] == '*' && (p.dust & dust[p.y][p.x + 1]) != dust[p.y][p.x + 1]) { if (check[p.y][p.x + 1][p.dust + dust[p.y][p.x + 1]] > p.cost + 1) { q.push( (robot){p.x + 1, p.y, p.cost + 1, p.dust + dust[p.y][p.x + 1]}); check[p.y][p.x + 1][p.dust + dust[p.y][p.x + 1]] = p.cost + 1; } } else { if (check[p.y][p.x + 1][p.dust] > p.cost + 1) { q.push((robot){p.x + 1, p.y, p.cost + 1, p.dust}); check[p.y][p.x + 1][p.dust] = p.cost + 1; } } } // left if (p.x - 1 >= 0 && v[p.y][p.x - 1] != 'x') { if (v[p.y][p.x - 1] == '*' && (p.dust & dust[p.y][p.x - 1]) != dust[p.y][p.x - 1]) { if (check[p.y][p.x - 1][p.dust + dust[p.y][p.x - 1]] > p.cost + 1) { q.push( (robot){p.x - 1, p.y, p.cost + 1, p.dust + dust[p.y][p.x - 1]}); check[p.y][p.x - 1][p.dust + dust[p.y][p.x - 11]] = p.cost + 1; } } else { if (check[p.y][p.x - 1][p.dust] > p.cost + 1) { q.push((robot){p.x - 1, p.y, p.cost + 1, p.dust}); check[p.y][p.x - 1][p.dust] = p.cost + 1; } } } } cout << cost << endl; } return 0; }
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <string> #include <utility> #include <vector> using namespace std; #define ALL(g) (g).begin(), (g).end() #define REP(i, x, n) for (int i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) #define F(i, j, k) fill(i[0], i[0] + j * j, k) #define P(p) cout << (p) << endl; #define EXIST(s, e) ((s).find(e) != (s).end()) #define INF 1 << 25 #define pb push_back typedef vector<int> vi; typedef vector<long long> vl; typedef vector<double> vd; typedef pair<int, int> pii; typedef pair<long, long> pll; typedef long long ll; using namespace std; struct robot { int x, y, cost, dust; bool operator>(const robot &r) const { return cost > r.cost; } }; int main() { int w, h; while (cin >> w >> h, w | h) { // input vector<string> v; rep(j, h) { string input; cin >> input; v.pb(input); } // dust map int dust[20][20]; fill_n((int *)dust, 20 * 20, 0); int bit = 1; // start & goal int sx, sy; int goal = 0; rep(j, h) { rep(i, w) { if (v[j][i] == 'o') { sx = i; sy = j; } if (v[j][i] == '*') { dust[j][i] = bit; goal += bit; bit *= 2; } } } // bfs int check[20][20][1 << 10]; fill_n((int *)check, 20 * 20 * (1 << 10), INT_MAX); int cost = -1; priority_queue<robot, vector<robot>, greater<robot>> q; q.push((robot){sx, sy, 0, 0}); while (!q.empty()) { robot p = q.top(); q.pop(); // goal if (p.dust == goal) { cost = p.cost; break; } // up if (p.y - 1 >= 0 && v[p.y - 1][p.x] != 'x') { if (v[p.y - 1][p.x] == '*' && (p.dust & dust[p.y - 1][p.x]) != dust[p.y - 1][p.x]) { if (check[p.y - 1][p.x][p.dust + dust[p.y - 1][p.x]] > p.cost + 1) { q.push( (robot){p.x, p.y - 1, p.cost + 1, p.dust + dust[p.y - 1][p.x]}); check[p.y - 1][p.x][p.dust + dust[p.y - 1][p.x]] = p.cost + 1; } } else { if (check[p.y - 1][p.x][p.dust] > p.cost + 1) { q.push((robot){p.x, p.y - 1, p.cost + 1, p.dust}); check[p.y - 1][p.x][p.dust] = p.cost + 1; } } } // down if (p.y + 1 < h && v[p.y + 1][p.x] != 'x') { if (v[p.y + 1][p.x] == '*' && (p.dust & dust[p.y + 1][p.x]) != dust[p.y + 1][p.x]) { if (check[p.y + 1][p.x][p.dust + dust[p.y + 1][p.x]] > p.cost + 1) { q.push( (robot){p.x, p.y + 1, p.cost + 1, p.dust + dust[p.y + 1][p.x]}); check[p.y + 1][p.x][p.dust + dust[p.y + 1][p.x]] = p.cost + 1; } } else { if (check[p.y + 1][p.x][p.dust] > p.cost + 1) { q.push((robot){p.x, p.y + 1, p.cost + 1, p.dust}); check[p.y + 1][p.x][p.dust] = p.cost + 1; } } } // right if (p.x + 1 < w && v[p.y][p.x + 1] != 'x') { if (v[p.y][p.x + 1] == '*' && (p.dust & dust[p.y][p.x + 1]) != dust[p.y][p.x + 1]) { if (check[p.y][p.x + 1][p.dust + dust[p.y][p.x + 1]] > p.cost + 1) { q.push( (robot){p.x + 1, p.y, p.cost + 1, p.dust + dust[p.y][p.x + 1]}); check[p.y][p.x + 1][p.dust + dust[p.y][p.x + 1]] = p.cost + 1; } } else { if (check[p.y][p.x + 1][p.dust] > p.cost + 1) { q.push((robot){p.x + 1, p.y, p.cost + 1, p.dust}); check[p.y][p.x + 1][p.dust] = p.cost + 1; } } } // left if (p.x - 1 >= 0 && v[p.y][p.x - 1] != 'x') { if (v[p.y][p.x - 1] == '*' && (p.dust & dust[p.y][p.x - 1]) != dust[p.y][p.x - 1]) { if (check[p.y][p.x - 1][p.dust + dust[p.y][p.x - 1]] > p.cost + 1) { q.push( (robot){p.x - 1, p.y, p.cost + 1, p.dust + dust[p.y][p.x - 1]}); check[p.y][p.x - 1][p.dust + dust[p.y][p.x - 1]] = p.cost + 1; } } else { if (check[p.y][p.x - 1][p.dust] > p.cost + 1) { q.push((robot){p.x - 1, p.y, p.cost + 1, p.dust}); check[p.y][p.x - 1][p.dust] = p.cost + 1; } } } } cout << cost << endl; } return 0; }
replace
149
150
149
150
0
p00721
C++
Runtime Error
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include "bits/stdc++.h" #define REP(i, a, b) for (i = a; i < b; ++i) #define rep(i, n) REP(i, 0, n) #define ll long long #define ull unsigned ll typedef long double ld; #define ALL(a) (a).begin(), (a).end() #define ifnot(a) if (not a) #define dump(x) cerr << #x << " = " << (x) << endl using namespace std; void reader(int &a) { scanf("%d", &a); } void reader(double &a) { scanf("%lf", &a); } void reader(char a[]) { scanf("%s", a); } void reader(char &a) { scanf(" %c", &a); } void reader(ll &a) { scanf("%lld", &a); } void reader(ull &a) { scanf("%llu", &a); } // void reader(string& a){cin >> a;}; template <class T, class U> void reader(T &t, U &u) { reader(t); reader(u); } template <class T, class U, class V> void reader(T &t, U &u, V &v) { reader(t); reader(u); reader(v); } void writer(const int a, char c) { printf("%d", a); putchar(c); } void writer(const ll a, char c) { printf("%lld", a); putchar(c); } void writer(const ull a, char c) { printf("%llu", a); putchar(c); } void writer(const double a, char c) { printf("%.20lf", a); putchar(c); } void writer(const char a[]) { printf("%s", a); }; void writer(const char a[], char c) { printf("%s", a); putchar(c); }; void writer(const char a, char c) { putchar(a); putchar(c); }; template <class T> void writerLn(T t) { writer(t, '\n'); } template <class T, class U> void writerLn(T t, U u) { writer(t, ' '); writer(u, '\n'); } template <class T, class U, class V> void writerLn(T t, U u, V v) { writer(t, ' '); writer(u, ' '); writer(v, '\n'); } template <class T> void writerArr(T x[], int n) { int i; if (!n) { putchar('\n'); return; } rep(i, n - 1) writer(x[i], ' '); writer(x[n - 1], '\n'); } template <class T> void writerVec(vector<T> x) { int n = x.size(); int i; if (!n) { putchar('\n'); return; } rep(i, n - 1) writer(x[i], ' '); writer(x[n - 1], '\n'); } vector<string> split(const string &str, char sep) { vector<string> v; stringstream ss(str); string buffer; while (getline(ss, buffer, sep)) { v.push_back(buffer); } return v; } // #define int ll bool test = 0; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; #define INF (1 << 28) ull mod = (int)1e9 + 7; //..................... #define MAX 22 struct Node { int y, x; }; int w, h; char c[MAX][MAX]; int dm[MAX][MAX]; vector<Node> nodes; int start; bool create_distace_matrix() { int i, j, k; int y, x; int cf[MAX][MAX]; rep(i, nodes.size()) { rep(y, h) rep(x, w) { if (c[y][x] == 'x') cf[y][x] = -2; else cf[y][x] = -1; } cf[nodes[i].y][nodes[i].x] = 0; bool changed = true; int current = 0; for (; changed; current += 1) { changed = false; rep(y, h) rep(x, w) { if (cf[y][x] == current) { rep(j, 4) { int ny = y + dy[j]; int nx = x + dx[j]; if (ny < 0 || h <= ny) continue; if (nx < 0 || w <= nx) continue; if (cf[ny][nx] == -1) cf[ny][nx] = current + 1; changed = true; } } } } rep(j, nodes.size()) { int distance = cf[nodes[j].y][nodes[j].x]; if (distance < 0) return false; dm[i][j] = distance; } } return true; } bool done[MAX]; int ans; void rec(int n, int idx, int sum) { int i; if (sum >= ans) return; if (n == nodes.size()) { if (sum < ans) ans = sum; return; } rep(i, nodes.size()) { if (done[i]) continue; done[i] = true; rec(n + 1, i, sum + dm[idx][i]); done[i] = false; } } signed main(void) { int i, j, k, l; while (1) { cin >> w >> h; if (w == 0) return 0; rep(i, h) reader(c[i]); nodes.clear(); rep(i, h) rep(j, w) { if (c[i][j] == 'o') start = nodes.size(); if (c[i][j] == 'o' || c[i][j] == '*') { nodes.push_back({i, j}); } } if (!create_distace_matrix()) { cout << -1 << endl; cerr << "????????????" << endl; } ans = INT_MAX; done[start] = true; rec(1, start, 0); cout << ans << endl; done[start] = false; } return 0; }
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include "bits/stdc++.h" #define REP(i, a, b) for (i = a; i < b; ++i) #define rep(i, n) REP(i, 0, n) #define ll long long #define ull unsigned ll typedef long double ld; #define ALL(a) (a).begin(), (a).end() #define ifnot(a) if (not a) #define dump(x) cerr << #x << " = " << (x) << endl using namespace std; void reader(int &a) { scanf("%d", &a); } void reader(double &a) { scanf("%lf", &a); } void reader(char a[]) { scanf("%s", a); } void reader(char &a) { scanf(" %c", &a); } void reader(ll &a) { scanf("%lld", &a); } void reader(ull &a) { scanf("%llu", &a); } // void reader(string& a){cin >> a;}; template <class T, class U> void reader(T &t, U &u) { reader(t); reader(u); } template <class T, class U, class V> void reader(T &t, U &u, V &v) { reader(t); reader(u); reader(v); } void writer(const int a, char c) { printf("%d", a); putchar(c); } void writer(const ll a, char c) { printf("%lld", a); putchar(c); } void writer(const ull a, char c) { printf("%llu", a); putchar(c); } void writer(const double a, char c) { printf("%.20lf", a); putchar(c); } void writer(const char a[]) { printf("%s", a); }; void writer(const char a[], char c) { printf("%s", a); putchar(c); }; void writer(const char a, char c) { putchar(a); putchar(c); }; template <class T> void writerLn(T t) { writer(t, '\n'); } template <class T, class U> void writerLn(T t, U u) { writer(t, ' '); writer(u, '\n'); } template <class T, class U, class V> void writerLn(T t, U u, V v) { writer(t, ' '); writer(u, ' '); writer(v, '\n'); } template <class T> void writerArr(T x[], int n) { int i; if (!n) { putchar('\n'); return; } rep(i, n - 1) writer(x[i], ' '); writer(x[n - 1], '\n'); } template <class T> void writerVec(vector<T> x) { int n = x.size(); int i; if (!n) { putchar('\n'); return; } rep(i, n - 1) writer(x[i], ' '); writer(x[n - 1], '\n'); } vector<string> split(const string &str, char sep) { vector<string> v; stringstream ss(str); string buffer; while (getline(ss, buffer, sep)) { v.push_back(buffer); } return v; } // #define int ll bool test = 0; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; #define INF (1 << 28) ull mod = (int)1e9 + 7; //..................... #define MAX 22 struct Node { int y, x; }; int w, h; char c[MAX][MAX]; int dm[MAX][MAX]; vector<Node> nodes; int start; bool create_distace_matrix() { int i, j, k; int y, x; int cf[MAX][MAX]; rep(i, nodes.size()) { rep(y, h) rep(x, w) { if (c[y][x] == 'x') cf[y][x] = -2; else cf[y][x] = -1; } cf[nodes[i].y][nodes[i].x] = 0; bool changed = true; int current = 0; for (; changed; current += 1) { changed = false; rep(y, h) rep(x, w) { if (cf[y][x] == current) { rep(j, 4) { int ny = y + dy[j]; int nx = x + dx[j]; if (ny < 0 || h <= ny) continue; if (nx < 0 || w <= nx) continue; if (cf[ny][nx] == -1) cf[ny][nx] = current + 1; changed = true; } } } } rep(j, nodes.size()) { int distance = cf[nodes[j].y][nodes[j].x]; if (distance < 0) return false; dm[i][j] = distance; } } return true; } bool done[MAX]; int ans; void rec(int n, int idx, int sum) { int i; if (sum >= ans) return; if (n == nodes.size()) { if (sum < ans) ans = sum; return; } rep(i, nodes.size()) { if (done[i]) continue; done[i] = true; rec(n + 1, i, sum + dm[idx][i]); done[i] = false; } } signed main(void) { int i, j, k, l; while (1) { cin >> w >> h; if (w == 0) return 0; rep(i, h) reader(c[i]); nodes.clear(); rep(i, h) rep(j, w) { if (c[i][j] == 'o') start = nodes.size(); if (c[i][j] == 'o' || c[i][j] == '*') { nodes.push_back({i, j}); } } if (!create_distace_matrix()) { cout << -1 << endl; continue; } ans = INT_MAX; done[start] = true; rec(1, start, 0); cout << ans << endl; done[start] = false; } return 0; }
replace
190
191
190
191
0
????????????
p00722
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; const int MAX_SIZE = 100000; int prime[MAX_SIZE]; bool isPrime[MAX_SIZE]; int p; int main() { fill(isPrime, isPrime + MAX_SIZE, true); p = 0; isPrime[0] = isPrime[1] = false; for (int i = 2; i <= MAX_SIZE; i++) { if (isPrime[i]) { prime[p++] = i; } else continue; for (int j = 2 * i; j <= MAX_SIZE; j += i) { isPrime[j] = false; } } int a, d, n; while (cin >> a >> d >> n && !(a == 0 && d == 0 && n == 0)) { int cnt = 0; for (int i = 0;; i++) { int num = a + i * d; if (isPrime[num]) { cnt++; } if (cnt == n) { cout << num << endl; break; } } } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; const int MAX_SIZE = 1000000; int prime[MAX_SIZE]; bool isPrime[MAX_SIZE]; int p; int main() { fill(isPrime, isPrime + MAX_SIZE, true); p = 0; isPrime[0] = isPrime[1] = false; for (int i = 2; i <= MAX_SIZE; i++) { if (isPrime[i]) { prime[p++] = i; } else continue; for (int j = 2 * i; j <= MAX_SIZE; j += i) { isPrime[j] = false; } } int a, d, n; while (cin >> a >> d >> n && !(a == 0 && d == 0 && n == 0)) { int cnt = 0; for (int i = 0;; i++) { int num = a + i * d; if (isPrime[num]) { cnt++; } if (cnt == n) { cout << num << endl; break; } } } return 0; }
replace
7
8
7
8
-11
p00722
C++
Memory Limit Exceeded
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <iostream> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define vi vector<int> #define vvi vector<vector<int>> #define ll long long int #define vl vector<ll> #define vvl vector<vector<ll>> #define ld long double #define INF 1e9 #define EPS 0.0000000001 #define rep(i, n) for (int i = 0; i < n; i++) #define CC puts("-------ok--------"); #define all(in) in.begin(), in.end() #define bv vector<bool> using namespace std; typedef pair<int, int> PA; using namespace std; #define MAX 99999999 int main() { // ?´???°??????sqrt(max)??§?????? vector<bool> check(MAX + 1); rep(i, MAX + 1) check[i] = true; vector<int> Primenumber(MAX + 1, 0); // Primearray int counter = 0; // Primearray counter; for (int i = 2; i < MAX + 1; i++) { if (check[i]) { for (int j = 2; i * j < MAX; j++) check[i * j] = false; //?´???°??????Primearray???????´??????????????????°?????¨???false??? Primenumber[counter] = i; counter++; } } int a, d, n; while (cin >> a >> d >> n, a + d + n) { int count = 0; int ans = 0; while (true) { if (check[a] && a != 1) count++; if (count == n) { ans = a; break; } a += d; } cout << ans << endl; } }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <iostream> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define vi vector<int> #define vvi vector<vector<int>> #define ll long long int #define vl vector<ll> #define vvl vector<vector<ll>> #define ld long double #define INF 1e9 #define EPS 0.0000000001 #define rep(i, n) for (int i = 0; i < n; i++) #define CC puts("-------ok--------"); #define all(in) in.begin(), in.end() #define bv vector<bool> using namespace std; typedef pair<int, int> PA; using namespace std; #define MAX 999999 int main() { // ?´???°??????sqrt(max)??§?????? vector<bool> check(MAX + 1); rep(i, MAX + 1) check[i] = true; vector<int> Primenumber(MAX + 1, 0); // Primearray int counter = 0; // Primearray counter; for (int i = 2; i < MAX + 1; i++) { if (check[i]) { for (int j = 2; i * j < MAX; j++) check[i * j] = false; //?´???°??????Primearray???????´??????????????????°?????¨???false??? Primenumber[counter] = i; counter++; } } int a, d, n; while (cin >> a >> d >> n, a + d + n) { int count = 0; int ans = 0; while (true) { if (check[a] && a != 1) count++; if (count == n) { ans = a; break; } a += d; } cout << ans << endl; } }
replace
28
29
28
29
MLE
p00722
C++
Time Limit Exceeded
#include <iostream> using namespace std; int main(void) { for (;;) { int a, d, n; cin >> a >> d >> n; if (a == 0 && d == 0 && n == 0) { return 0; } int ans = -1; for (int i = 0; ((a + d * i <= 1000000) && (ans == -1)); i++) { bool prime = true; for (int j = 2; j <= 10000; j++) { if ((a + d * i) % j == 0) { prime = false; } } if (prime) { if (n == 1) { ans = a + d * i; } else { n--; } } } cout << ans << endl; } }
#include <iostream> using namespace std; int main(void) { for (;;) { int a, d, n; cin >> a >> d >> n; if (a == 0 && d == 0 && n == 0) { return 0; } int ans = -1; for (int i = 0; ((a + d * i <= 1000000) && (ans == -1)); i++) { bool prime = true; if (a + d * i == 1) { prime = false; } else { for (int j = 2; j * j <= a + d * i; j++) { if ((a + d * i) % j == 0) { prime = false; } } } if (prime) { if (n == 1) { ans = a + d * i; } else { n--; } } } cout << ans << endl; } }
replace
12
15
12
19
TLE
p00722
C++
Time Limit Exceeded
#include <algorithm> #include <fstream> #include <iostream> #include <list> #include <queue> #include <stdio.h> #include <string> #include <vector> #define REP(i, n) for (int i = 0; i < n; ++i) #define LL long long using namespace std; #define int LL bool func(int x) { if (x < 2) return false; for (int i = 2; i * i <= x; ++i) { if (x % i == 0) return false; } return true; } signed main() { // ifstream cin("Ain.txt"); // ofstream cout("Aout.txt"); int a, d, n; do { cin >> a >> d >> n; a -= d; while (n) { a += d; if (func(a)) n--; } cout << a << endl; } while (!(a && d && n)); }
#include <algorithm> #include <fstream> #include <iostream> #include <list> #include <queue> #include <stdio.h> #include <string> #include <vector> #define REP(i, n) for (int i = 0; i < n; ++i) #define LL long long using namespace std; #define int LL bool func(int x) { if (x < 2) return false; for (int i = 2; i * i <= x; ++i) { if (x % i == 0) return false; } return true; } signed main() { // ifstream cin("Ain.txt"); // ofstream cout("Aout.txt"); int a, d, n; do { cin >> a >> d >> n; if (!(a && d && n)) break; a -= d; while (n) { a += d; if (func(a)) n--; } cout << a << endl; } while (!(a && d && n)); }
insert
36
36
36
39
TLE
p00722
C++
Time Limit Exceeded
#include <math.h> #include <stdio.h> int main(); bool prime_chk(int num); int main() { int a[100] = {0}; int d[100] = {0}; int n[100] = {0}; int result[100] = {0}; int n_series; int s_series; int nth_prime_num; int i, i_dataset; bool prime_flg; // データ入力 i_dataset = 0; while (1) { scanf("%d %d %d", &a[i_dataset], &d[i_dataset], &n[i_dataset]); if (a[i_dataset] == 0 && d[i_dataset] == 0 && n[i_dataset] == 0) break; // 0 0 0が入力されると処理終了 else i_dataset++; // データ数を記録 } // 計算開始 for (i = 0; i < i_dataset; i++) { nth_prime_num = 0; n_series = 1; while (1) { if (n_series == 1) s_series = a[i]; else s_series = a[i] + (n_series - 1) * d[i]; prime_flg = prime_chk(s_series); if (prime_flg == true) nth_prime_num++; if (nth_prime_num == n[i]) { result[i] = s_series; break; } n_series++; } } for (i = 0; i < i_dataset; i++) { printf("%d\n", result[i]); // 結果出力 } return 0; } bool prime_chk(int num) { if (num < 2) return false; else if (num == 2) return true; else if (num % 2 == 0) return false; // 偶数はあらかじめ除く double sqrt_num = sqrt(num); for (int i = 3; i <= sqrt_num; i += 2) { if (num % i == 0) { // 素数ではない return false; } } // 素数である return true; }
#include <math.h> #include <stdio.h> int main(); bool prime_chk(int num); int main() { int a[1000] = {0}; int d[1000] = {0}; int n[1000] = {0}; int result[1000] = {0}; int n_series; int s_series; int nth_prime_num; int i, i_dataset; bool prime_flg; // データ入力 i_dataset = 0; while (1) { scanf("%d %d %d", &a[i_dataset], &d[i_dataset], &n[i_dataset]); if (a[i_dataset] == 0 && d[i_dataset] == 0 && n[i_dataset] == 0) break; // 0 0 0が入力されると処理終了 else i_dataset++; // データ数を記録 } // 計算開始 for (i = 0; i < i_dataset; i++) { nth_prime_num = 0; n_series = 1; while (1) { if (n_series == 1) s_series = a[i]; else s_series = a[i] + (n_series - 1) * d[i]; prime_flg = prime_chk(s_series); if (prime_flg == true) nth_prime_num++; if (nth_prime_num == n[i]) { result[i] = s_series; break; } n_series++; } } for (i = 0; i < i_dataset; i++) { printf("%d\n", result[i]); // 結果出力 } return 0; } bool prime_chk(int num) { if (num < 2) return false; else if (num == 2) return true; else if (num % 2 == 0) return false; // 偶数はあらかじめ除く double sqrt_num = sqrt(num); for (int i = 3; i <= sqrt_num; i += 2) { if (num % i == 0) { // 素数ではない return false; } } // 素数である return true; }
replace
8
12
8
12
TLE
p00722
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define FOR(i, s, n) for (int i = s; i < (int)n; i++) #define per(i, n) for (int i = n; i >= 0; i--) #define ROF(i, s, n) for (int i = s; i >= (int)n; i--) #define FORIT(i, A) for (auto i : A) #define PRINT(x) cout << (x) << "\n" #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define MP make_pair #define EACH(i, n) \ for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i) #define SZ(a) int((a).size()) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) #define CLR(a) memset((a), 0, sizeof(a)) #define dump(x) cout << #x << " = " << (x) << "\n"; #define debug(x) \ cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << "\n"; #define sq(n) (n) * (n) typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; typedef vector<LL> VLL; typedef vector<VLL> VVLL; typedef unsigned int uint; typedef unsigned long long ull; typedef priority_queue<int> maxpq; typedef priority_queue<int, vector<int>, greater<int>> minpq; static const double EPS = 1e-10; static const double PI = acos(-1.0); static const int mod = 10007; static const int INF = 1 << 25; static const LL LL_INF = 1152921504606846976; static const int dx[] = {-1, 0, 1, 0, 1, -1, 1, -1}; static const int dy[] = {0, -1, 0, 1, 1, 1, -1, -1}; int a, b, n; VI pm; int main() { pm.push_back(2); for (int i = 3; i < 10000000; i += 2) { bool can_add = true; for (int j = 0; j < pm.size() && pm[j] * pm[j] <= i; j++) { if (!(i % pm[j])) { can_add = false; break; } } if (can_add) { pm.push_back(i); } } while (~scanf("%d %d %d", &a, &b, &n) && a && b && n) { while (n) { n -= binary_search(pm.begin(), pm.end(), a); if (!n) { printf("%d\n", a); break; } a += b; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, s, n) for (int i = s; i < (int)n; i++) #define per(i, n) for (int i = n; i >= 0; i--) #define ROF(i, s, n) for (int i = s; i >= (int)n; i--) #define FORIT(i, A) for (auto i : A) #define PRINT(x) cout << (x) << "\n" #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define MP make_pair #define EACH(i, n) \ for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i) #define SZ(a) int((a).size()) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) #define CLR(a) memset((a), 0, sizeof(a)) #define dump(x) cout << #x << " = " << (x) << "\n"; #define debug(x) \ cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << "\n"; #define sq(n) (n) * (n) typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; typedef vector<LL> VLL; typedef vector<VLL> VVLL; typedef unsigned int uint; typedef unsigned long long ull; typedef priority_queue<int> maxpq; typedef priority_queue<int, vector<int>, greater<int>> minpq; static const double EPS = 1e-10; static const double PI = acos(-1.0); static const int mod = 10007; static const int INF = 1 << 25; static const LL LL_INF = 1152921504606846976; static const int dx[] = {-1, 0, 1, 0, 1, -1, 1, -1}; static const int dy[] = {0, -1, 0, 1, 1, 1, -1, -1}; int a, b, n; VI pm; int main() { pm.push_back(2); for (int i = 3; i < 9000000; i += 2) { bool can_add = true; for (int j = 0; j < pm.size() && pm[j] * pm[j] <= i; j++) { if (!(i % pm[j])) { can_add = false; break; } } if (can_add) { pm.push_back(i); } } while (~scanf("%d %d %d", &a, &b, &n) && a && b && n) { while (n) { n -= binary_search(pm.begin(), pm.end(), a); if (!n) { printf("%d\n", a); break; } a += b; } } return 0; }
replace
45
46
45
46
TLE
p00722
C++
Runtime Error
#include <algorithm> #include <cmath> #include <functional> #include <iostream> #include <queue> #include <set> #include <stack> #include <string.h> #include <string> using namespace std; #define INF 1 << 21 #define MOD 1000000007 #define MAX 1000000 int main() { int p[MAX]; memset(p, 1, sizeof(p)); p[0] = 0; p[1] = 0; for (int i = 2; i <= sqrt(MAX) + 1; i++) { if (p[i] == 1) { for (int j = 2 * i; j <= MAX; j += i) { p[j] = 0; } } } int a, d, n; while (1) { cin >> a >> d >> n; if (a == 0 && d == 0 && n == 0) return 0; while (1) { if (p[a] == 1) n--; if (n == 0) { cout << a << endl; break; } a += d; } } }
#include <algorithm> #include <cmath> #include <functional> #include <iostream> #include <queue> #include <set> #include <stack> #include <string.h> #include <string> using namespace std; #define INF 1 << 21 #define MOD 1000000007 #define MAX 1000000 int main() { char p[MAX]; memset(p, 1, sizeof(p)); p[0] = 0; p[1] = 0; for (int i = 2; i <= sqrt(MAX) + 1; i++) { if (p[i] == 1) { for (int j = 2 * i; j <= MAX; j += i) { p[j] = 0; } } } int a, d, n; while (1) { cin >> a >> d >> n; if (a == 0 && d == 0 && n == 0) return 0; while (1) { if (p[a] == 1) n--; if (n == 0) { cout << a << endl; break; } a += d; } } }
replace
16
17
16
17
-11
p00722
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int main(int argc, char *argv[]) { const int N = 1000001; vector<bool> prime(N, true); prime[0] = false; prime[1] = false; for (int i = 2; i < N; i++) { if (prime[i]) { for (int j = i + i; j < N; j += i) { prime[j] = false; } } } for (;;) { int a, d, n; cin >> a >> d >> n; if (a == 0) break; for (;;) { while (!prime[a]) { a += d; } if (n == 0) break; a += d; } cout << a << endl; } return 0; }
#include <iostream> #include <vector> using namespace std; int main(int argc, char *argv[]) { const int N = 1000001; vector<bool> prime(N, true); prime[0] = false; prime[1] = false; for (int i = 2; i < N; i++) { if (prime[i]) { for (int j = i + i; j < N; j += i) { prime[j] = false; } } } for (;;) { int a, d, n; cin >> a >> d >> n; if (a == 0) break; for (;;) { while (!prime[a]) { a += d; } n--; if (n == 0) break; a += d; } cout << a << endl; } return 0; }
insert
24
24
24
25
-11
p00722
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int a, b, n; while (cin >> a >> b >> n && n) { int count = 0, flag = 0; for (int i = a; i < 1000000; i += b) { for (int j = 2; j < i; j++) { if (i % j == 0 && i != j) { flag++; break; } } if (!flag && i >= 2) count++; if (count == n) { cout << i << endl; break; } flag = 0; } } }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, n; while (cin >> a >> b >> n && n) { int count = 0, flag = 0; for (int i = a; i <= 1000000; i += b) { for (int j = 2; j <= sqrt(i); j++) { if (i % j == 0 && i != j) { flag++; break; } } if (!flag && i >= 2) count++; if (count == n) { cout << i << endl; break; } flag = 0; } } }
replace
9
11
9
11
TLE
p00722
C++
Time Limit Exceeded
#include <iostream> #include <vector> using namespace std; bool isPrime(int x) { if (x == 1) { return false; } if (x == 2) { return true; } if (x % 2 == 0) { return false; } for (int i = 3; i < x; i += 2) { if (x % i == 0) { return false; } } return true; } int main() { while (1) { int a, d, n; cin >> a >> d >> n; if (a == 0 && d == 0 && n == 0) { break; } int count = 0; for (int i = a; count < n; i += d) { if (isPrime(i)) { ++count; if (count == n) { cout << i << endl; break; } } } } return 0; }
#include <iostream> #include <vector> using namespace std; bool isPrime(int x) { if (x == 1) { return false; } if (x == 2) { return true; } if (x % 2 == 0) { return false; } for (int i = 3; i * i <= x; i += 2) { if (x % i == 0) { return false; } } return true; } int main() { while (1) { int a, d, n; cin >> a >> d >> n; if (a == 0 && d == 0 && n == 0) { break; } int count = 0; for (int i = a; count < n; i += d) { if (isPrime(i)) { ++count; if (count == n) { cout << i << endl; break; } } } } return 0; }
replace
16
17
16
17
TLE
p00722
C++
Runtime Error
#include <iostream> using namespace std; int main() { int a[100000] = {0, 0, 1, 1}, n, k = 0; for (int i = 4; i < 100000; i++) { for (int j = 2; j * j <= i; j++) { if (i % j == 0) { k++; break; } } if (!k) a[i]++; k = 0; } int b, c, d, x, s, q; while (cin >> b >> c >> d, b != 0 && c != 0 && d != 0) { s = x = q = 0; while (1) { x = b + c * s; if (a[x] == 1) q++; if (q == d) break; s++; } cout << x << endl; } return 0; }
#include <iostream> using namespace std; int main() { int a[1000000] = {0, 0, 1, 1}, n, k = 0; for (int i = 4; i < 1000000; i++) { for (int j = 2; j * j <= i; j++) { if (i % j == 0) { k++; break; } } if (!k) a[i]++; k = 0; } int b, c, d, x, s, q; while (cin >> b >> c >> d, b != 0 && c != 0 && d != 0) { s = x = q = 0; while (1) { x = b + c * s; if (a[x] == 1) q++; if (q == d) break; s++; } cout << x << endl; } return 0; }
replace
3
5
3
5
-11
p00722
C++
Time Limit Exceeded
// #define _GRIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define int long long vector<int> sieve_of_eratosthenes(int n) { vector<int> primes(n); for (int i = 2; i < n; ++i) primes[i] = i; for (int i = 2; i * i < n; ++i) if (primes[i]) for (int j = i * i; j < n; j += i) primes[j] = 0; primes.erase(remove(primes.begin(), primes.end(), 0), primes.end()); return primes; } signed main() { int a, d, n; vector<int> primes = sieve_of_eratosthenes(1000001); while (cin >> a >> d >> n, a) { int cnt = 0; int ans = 0; for (int i = a;; i += d) { if (find(primes.begin(), primes.end(), i) != primes.end()) cnt++; if (cnt == n) { ans = i; break; } } cout << ans << endl; } return 0; }
// #define _GRIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define int long long vector<int> sieve_of_eratosthenes(int n) { vector<int> primes(n); for (int i = 2; i < n; ++i) primes[i] = i; for (int i = 2; i * i < n; ++i) if (primes[i]) for (int j = i * i; j < n; j += i) primes[j] = 0; primes.erase(remove(primes.begin(), primes.end(), 0), primes.end()); return primes; } signed main() { int a, d, n; vector<int> primes = sieve_of_eratosthenes(1000001); while (cin >> a >> d >> n, a) { int cnt = 0; int ans = 0; for (int i = a;; i += d) { if (*lower_bound(primes.begin(), primes.end(), i) == i) cnt++; if (cnt == n) { ans = i; break; } } cout << ans << endl; } return 0; }
replace
26
27
26
27
TLE
p00722
C++
Time Limit Exceeded
#include <cstdio> bool isprime(int n, int k = 2) { if (n == 1) { return false; } if (n < k * k) { return true; } if (n % k == 0) { return false; } return isprime(n, k + 1); } int main() { int a, d, n, c; while (true) { scanf("%d%d%d", &n, &d, &a); if (n == 0) { break; } c = 0; while (c < n) { if (isprime(a)) { c++; } a += d; } printf("%d\n", a - d); } }
#include <cstdio> bool isprime(int n, int k = 2) { if (n == 1) { return false; } if (n < k * k) { return true; } if (n % k == 0) { return false; } return isprime(n, k + 1); } int main() { int a, d, n, c; while (true) { scanf("%d%d%d", &a, &d, &n); if (n == 0) { break; } c = 0; while (c < n) { if (isprime(a)) { c++; } a += d; } printf("%d\n", a - d); } }
replace
16
17
16
17
TLE
p00722
C++
Time Limit Exceeded
#include <algorithm> #include <math.h> #include <stdio.h> #include <string.h> #define MAXNUM 1000000 using namespace std; int main(void) { int flag[MAXNUM + 1], a, d, n, i, j, count; fill(flag, flag + MAXNUM, 1); flag[0] = flag[1] = 0; for (i = 2; i <= sqrt((double)MAXNUM); i++) { if (flag[i]) { for (j = i * 2; j <= MAXNUM; j += i) flag[j] = 0; } } while (1) { scanf("%d%d%d", &a, &d, &n); i = count = 0; while (1) { if (flag[a + d * i]) { count++; if (count == n) break; } i++; } printf("%d\n", a + d * i); } return 0; }
#include <algorithm> #include <math.h> #include <stdio.h> #include <string.h> #define MAXNUM 1000000 using namespace std; int main(void) { int flag[MAXNUM + 1], a, d, n, i, j, count; fill(flag, flag + MAXNUM, 1); flag[0] = flag[1] = 0; for (i = 2; i <= sqrt((double)MAXNUM); i++) { if (flag[i]) { for (j = i * 2; j <= MAXNUM; j += i) flag[j] = 0; } } while (1) { scanf("%d%d%d", &a, &d, &n); if (a == 0 && d == 0 && n == 0) break; i = count = 0; while (1) { if (flag[a + d * i]) { count++; if (count == n) break; } i++; } printf("%d\n", a + d * i); } return 0; }
insert
20
20
20
22
TLE
p00722
C++
Runtime Error
#include <cmath> #include <iostream> #include <vector> using std::cin; using std::cout; using std::endl; bool is_prime(int n) { if (n < 2) { return false; } if (n == 2 || n == 3) { return true; } for (int i = 3; i < n; i = i + 2) { if (n % i == 0) { return false; } } return true; } int main(void) { int N = 1000000; int a, d, n; cin >> a; cin >> d; cin >> n; std::vector<bool> prime(N); for (int i = 0; i < N + 1; i++) { prime[i] = true; } prime[0] = false; prime[1] = false; for (int i = 2; i <= sqrt(N); i++) { if (is_prime(i)) { for (int j = 2; i * j < N + 1; j++) { prime[i * j] = false; } } } while (a != 0 || d != 0 || n != 0) { bool find = false; int count = 0; int b; for (int i = 0; !find; i++) { b = a + (d * i); if (prime[b]) { count++; } if (count == n) { cout << b << endl; find = true; } } cin >> a; cin >> d; cin >> n; } return 0; }
#include <cmath> #include <iostream> #include <vector> using std::cin; using std::cout; using std::endl; bool is_prime(int n) { if (n < 2) { return false; } if (n == 2 || n == 3) { return true; } for (int i = 3; i < n; i = i + 2) { if (n % i == 0) { return false; } } return true; } int main(void) { int N = 1100000; int a, d, n; cin >> a; cin >> d; cin >> n; std::vector<bool> prime(N); for (int i = 0; i < N + 1; i++) { prime[i] = true; } prime[0] = false; prime[1] = false; for (int i = 2; i <= sqrt(N); i++) { if (is_prime(i)) { for (int j = 2; i * j < N + 1; j++) { prime[i * j] = false; } } } while (a != 0 || d != 0 || n != 0) { bool find = false; int count = 0; int b; for (int i = 0; !find; i++) { b = a + (d * i); if (prime[b]) { count++; } if (count == n) { cout << b << endl; find = true; } } cin >> a; cin >> d; cin >> n; } return 0; }
replace
24
25
24
25
0
p00722
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int a = 0, d = 0, n = 0; int sum = 0; while (1) { int count = 0; cin >> a >> d >> n; if (a == 0 && d == 0 && n == 0) break; sum = a; while (1) { if (sum == 2) { count++; } else if (sum > 2) { bool flag = true; for (int i = 3; i < sum; i += 2) { if (sum % i == 0) { flag = false; break; } } if (flag) count++; } if (count == n) break; sum += d; } cout << sum << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a = 0, d = 0, n = 0; int sum = 0; while (1) { int count = 0; cin >> a >> d >> n; if (a == 0 && d == 0 && n == 0) break; sum = a; while (1) { if (sum == 2) { count++; } else if (sum > 2) { bool flag = true; if (sum % 2 == 0) flag = false; for (int i = 3; i <= sqrt(sum); i += 2) { if (sum % i == 0) { flag = false; break; } } if (flag) count++; } if (count == n) break; sum += d; } cout << sum << endl; } return 0; }
replace
18
19
18
21
TLE
p00722
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <iterator> using namespace std; struct Fast { Fast() { std::cin.tie(0); ios::sync_with_stdio(false); } } fast; /* cpp template {{{ */ /* short */ #define pb push_back #define eb emplace_back #define mp make_pair #define Fi first #define Se second #define ALL(v) begin(v), end(v) #define RALL(v) (v).rbegin(), (v).rend() #define X real() #define Y imag() /* REPmacro */ #define REPS(i, a, n) for (ll i = (a); i < (ll)(n); ++i) #define rep(i, n) REPS(i, 0, n) #define REP1(i, n) for (ll i = 1; i <= (ll)(n); ++i) #define RREP(i, n) REPS(i, 1, n + 1) #define DEPS(i, a, n) for (ll i = (a); i >= (ll)(n); --i) #define DEP(i, n) DEPS(i, n, 0) #define EACH(i, n) for (auto &&i : n) /* debug */ #define debug(x) \ cerr << x << " " \ << "(L:" << __LINE__ << ")" << '\n'; /* alias */ using ll = long long; using ull = unsigned long long; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using pii = pair<int, int>; using D = double; using P = complex<D>; using vs = vector<string>; template <typename T> using PQ = priority_queue<T>; template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>; /* const */ const int INF = 1001001001; const ll LINF = 1001001001001001001ll; const int MOD = 1e9 + 7; const D EPS = 1e-9; const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1}; /* func */ inline bool inside(int y, int x, int H, int W) { return y >= 0 && x >= 0 && y < H && x < W; } inline int in() { int x; cin >> x; return x; } inline ll IN() { ll x; cin >> x; return x; } inline vs split(const string &t, char c) { vs v; stringstream s(t); string b; while (getline(s, b, c)) v.eb(b); return v; } template <typename T> inline bool chmin(T &a, const T &b) { if (a > b) a = b; return a > b; } template <typename T> inline bool chmax(T &a, const T &b) { if (a < b) a = b; return a < b; } template <typename T, typename S> inline void print(const pair<T, S> &p) { cout << p.first << " " << p.second << endl; } template <typename T> inline void print(const T &x) { cout << x << '\n'; } template <typename T, typename S> inline void print(const vector<pair<T, S>> &v) { for (auto &&p : v) print(p); } template <typename T> inline void print(const vector<T> &v, string s = " ") { rep(i, v.size()) cout << v[i] << (i != (ll)v.size() - 1 ? s : "\n"); } // 素数判定 bool isPrime(int n) { if (n <= 1) return false; for (int i = 2; i < n / 2; ++i) { if (!(n % i)) return false; } return true; } signed main() { int a, d, n; vi ans; while (cin >> a >> d >> n, a || d || n) { int cnt = 0; while (1) { if (isPrime(a)) { ++cnt; if (cnt == n) break; } a += d; } ans.pb(a); } EACH(e, ans) print(e); return 0; }
#include <bits/stdc++.h> #include <iterator> using namespace std; struct Fast { Fast() { std::cin.tie(0); ios::sync_with_stdio(false); } } fast; /* cpp template {{{ */ /* short */ #define pb push_back #define eb emplace_back #define mp make_pair #define Fi first #define Se second #define ALL(v) begin(v), end(v) #define RALL(v) (v).rbegin(), (v).rend() #define X real() #define Y imag() /* REPmacro */ #define REPS(i, a, n) for (ll i = (a); i < (ll)(n); ++i) #define rep(i, n) REPS(i, 0, n) #define REP1(i, n) for (ll i = 1; i <= (ll)(n); ++i) #define RREP(i, n) REPS(i, 1, n + 1) #define DEPS(i, a, n) for (ll i = (a); i >= (ll)(n); --i) #define DEP(i, n) DEPS(i, n, 0) #define EACH(i, n) for (auto &&i : n) /* debug */ #define debug(x) \ cerr << x << " " \ << "(L:" << __LINE__ << ")" << '\n'; /* alias */ using ll = long long; using ull = unsigned long long; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using pii = pair<int, int>; using D = double; using P = complex<D>; using vs = vector<string>; template <typename T> using PQ = priority_queue<T>; template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>; /* const */ const int INF = 1001001001; const ll LINF = 1001001001001001001ll; const int MOD = 1e9 + 7; const D EPS = 1e-9; const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1}; /* func */ inline bool inside(int y, int x, int H, int W) { return y >= 0 && x >= 0 && y < H && x < W; } inline int in() { int x; cin >> x; return x; } inline ll IN() { ll x; cin >> x; return x; } inline vs split(const string &t, char c) { vs v; stringstream s(t); string b; while (getline(s, b, c)) v.eb(b); return v; } template <typename T> inline bool chmin(T &a, const T &b) { if (a > b) a = b; return a > b; } template <typename T> inline bool chmax(T &a, const T &b) { if (a < b) a = b; return a < b; } template <typename T, typename S> inline void print(const pair<T, S> &p) { cout << p.first << " " << p.second << endl; } template <typename T> inline void print(const T &x) { cout << x << '\n'; } template <typename T, typename S> inline void print(const vector<pair<T, S>> &v) { for (auto &&p : v) print(p); } template <typename T> inline void print(const vector<T> &v, string s = " ") { rep(i, v.size()) cout << v[i] << (i != (ll)v.size() - 1 ? s : "\n"); } // 素数判定 bool isPrime(int n) { if (n <= 1) return false; for (int i = 2; i <= sqrt(double(n)); ++i) { if (!(n % i)) return false; } return true; } signed main() { int a, d, n; vi ans; while (cin >> a >> d >> n, a || d || n) { int cnt = 0; while (1) { if (isPrime(a)) { ++cnt; if (cnt == n) break; } a += d; } ans.pb(a); } EACH(e, ans) print(e); return 0; }
replace
108
109
108
109
TLE
p00723
C++
Runtime Error
#include <algorithm> #include <iostream> #include <set> #include <string> using namespace std; int main(int argc, const char *argv[]) { int n; cin >> n; while (n--) { set<string> kinds; string str; cin >> str; for (int i = 1; i < str.size(); i++) { string s1 = str.substr(i), s2 = str.substr(0, i); string r1 = s1, r2 = s2; reverse(r1.begin(), r1.end()); reverse(r2.begin(), r2.end()); string s[8] = { s1 + s2, s1 + r2, r1 + s2, r1 + r2, s2 + s1, r2 + s1, s2 + r1, r2 + r1, }; for (int j = 0; j < 8; j++) kinds.insert(s[i]); } cout << kinds.size() << endl; } }
#include <algorithm> #include <iostream> #include <set> #include <string> using namespace std; int main(int argc, const char *argv[]) { int n; cin >> n; while (n--) { set<string> kinds; string str; cin >> str; for (int i = 1; i < str.size(); i++) { string s1 = str.substr(i), s2 = str.substr(0, i); string r1 = s1, r2 = s2; reverse(r1.begin(), r1.end()); reverse(r2.begin(), r2.end()); string s[8] = { s1 + s2, s1 + r2, r1 + s2, r1 + r2, s2 + s1, r2 + s1, s2 + r1, r2 + r1, }; for (int j = 0; j < 8; j++) kinds.insert(s[j]); } cout << kinds.size() << endl; } }
replace
25
26
25
26
0
p00723
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; #define ALL(c) (c).begin(), (c).end() int main() { int M; cin >> M; while (M--) { string s; cin >> s; vector<string> v; for (int i = 0; i < (int)s.length() - 1; i++) { string front = s.substr(0, i + 1); string back = s.substr(i + 1); v.push_back(front + back); v.push_back(back + front); reverse(ALL(front)); v.push_back(front + back); v.push_back(back + front); reverse(ALL(front)); reverse(ALL(back)); v.push_back(front + back); v.push_back(back + front); reverse(ALL(front)); v.push_back(front + back); v.push_back(back + front); } sort(ALL(v)); v.erase(unique(ALL(v)), v.end()); cerr << "-----" << endl; for (vector<string>::const_iterator iter = v.begin(); iter != v.end(); ++iter) cerr << *iter << endl; cerr << "-----" << endl; cout << v.size() << endl; } }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; #define ALL(c) (c).begin(), (c).end() int main() { int M; cin >> M; while (M--) { string s; cin >> s; vector<string> v; for (int i = 0; i < (int)s.length() - 1; i++) { string front = s.substr(0, i + 1); string back = s.substr(i + 1); v.push_back(front + back); v.push_back(back + front); reverse(ALL(front)); v.push_back(front + back); v.push_back(back + front); reverse(ALL(front)); reverse(ALL(back)); v.push_back(front + back); v.push_back(back + front); reverse(ALL(front)); v.push_back(front + back); v.push_back(back + front); } sort(ALL(v)); v.erase(unique(ALL(v)), v.end()); #if 0 cerr << "-----" << endl; for(vector<string>::const_iterator iter = v.begin(); iter != v.end(); ++iter) cerr << *iter << endl; cerr << "-----" << endl; #endif cout << v.size() << endl; } }
replace
38
43
38
44
0
----- aa ----- ----- aabb abab abba baab baba bbaa ----- ----- abcd abdc adcb bacd badc bcda cbad cdab cdba dabc dcab dcba ----- ----- abcde abced abedc aedcb bacde baedc bcdea cbade cbaed cdeab cdeba dcbae deabc decba eabcd edabc edcab edcba -----
p00723
C++
Time Limit Exceeded
#include <iostream> #define rep(i, n) for (int i = 0; i < n; i++) int main() { int n; char str[500][73]; std::cin >> n; int strsize; int moji; int ans; rep(h, n) { std::cin >> str[0]; strsize = 0; rep(i, 73) { if (str[0][i] == '\0') { strsize = i; break; } } rep(i, strsize - 1) { rep(j, 7) { switch (j) { case 0: rep(k, strsize) { if (k <= i) str[1 + j + 7 * i][k] = str[0][k]; else str[1 + j + 7 * i][k] = str[0][strsize + i - k]; } break; case 1: rep(k, strsize) { if (k <= i) str[1 + j + 7 * i][k] = str[0][i - k]; else str[1 + j + 7 * i][k] = str[0][k]; } break; case 2: rep(k, strsize) { if (k <= i) str[1 + j + 7 * i][k] = str[0][i - k]; else str[1 + j + 7 * i][k] = str[0][strsize + i - k]; } break; case 3: rep(k, strsize) { if (k <= i) str[1 + j + 7 * i][strsize - 1 - i + k] = str[0][k]; else str[1 + j + 7 * i][k - i - 1] = str[0][k]; } break; case 4: rep(k, strsize) { if (k <= i) str[1 + j + 7 * i][strsize - 1 - i + k] = str[0][k]; else str[1 + j + 7 * i][strsize - k - 1] = str[0][k]; } break; case 5: rep(k, strsize) { if (k <= i) str[1 + j + 7 * i][strsize - k - 1] = str[0][k]; else str[1 + j + 7 * i][k - i - 1] = str[0][k]; } break; case 6: rep(k, strsize) { if (k <= i) str[1 + j + 7 * i][strsize - k - 1] = str[0][k]; else str[1 + j + 7 * i][strsize - k - 1] = str[0][k]; } break; } } } rep(k, 7 * (strsize - 1) + 1) { if (str[k][0] == '0') continue; rep(i, 7 * (strsize - 1) + 1) { if (i == k || str[i][0] == '0') continue; moji = 0; rep(j, strsize) { if (str[i][j] == str[k][j]) moji++; } if (moji == strsize) str[i][0] = '0'; } } ans = 0; rep(i, (strsize - 1) * 7 + 1) { if (str[i][0] != '0') ans++; } std::cout << ans << std::endl; } return 0; }
#include <iostream> #define rep(i, n) for (int i = 0; i < n; i++) int main() { int n; char str[500][73]; std::cin >> n; int strsize; int moji; int ans; rep(h, n) { std::cin >> str[0]; strsize = 0; rep(i, 73) { if (str[0][i] == '\0') { strsize = i; break; } } rep(i, strsize - 1) { rep(j, 7) { switch (j) { case 0: rep(k, strsize) { if (k <= i) str[1 + j + 7 * i][k] = str[0][k]; else str[1 + j + 7 * i][k] = str[0][strsize + i - k]; } break; case 1: rep(k, strsize) { if (k <= i) str[1 + j + 7 * i][k] = str[0][i - k]; else str[1 + j + 7 * i][k] = str[0][k]; } break; case 2: rep(k, strsize) { if (k <= i) str[1 + j + 7 * i][k] = str[0][i - k]; else str[1 + j + 7 * i][k] = str[0][strsize + i - k]; } break; case 3: rep(k, strsize) { if (k <= i) str[1 + j + 7 * i][strsize - 1 - i + k] = str[0][k]; else str[1 + j + 7 * i][k - i - 1] = str[0][k]; } break; case 4: rep(k, strsize) { if (k <= i) str[1 + j + 7 * i][strsize - 1 - i + k] = str[0][k]; else str[1 + j + 7 * i][strsize - k - 1] = str[0][k]; } break; case 5: rep(k, strsize) { if (k <= i) str[1 + j + 7 * i][strsize - k - 1] = str[0][k]; else str[1 + j + 7 * i][k - i - 1] = str[0][k]; } break; case 6: rep(k, strsize) { if (k <= i) str[1 + j + 7 * i][strsize - k - 1] = str[0][k]; else str[1 + j + 7 * i][strsize - k - 1] = str[0][k]; } break; } } } rep(k, 7 * (strsize - 1) + 1) { if (str[k][0] == '0') continue; rep(i, 7 * (strsize - 1) + 1) { if (i == k || str[i][0] == '0') continue; moji = 0; rep(j, strsize) { if (str[i][j] != str[k][j]) break; else moji++; } if (moji == strsize) str[i][0] = '0'; } } ans = 0; rep(i, (strsize - 1) * 7 + 1) { if (str[i][0] != '0') ans++; } std::cout << ans << std::endl; } return 0; }
replace
90
91
90
93
TLE
p00723
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main(void) { vector<string> str; vector<string> ans; string s[120]; int n; cin >> n; for (int i = 0; i < n; i++) { cin >> s[i]; } for (int k = 0; k < n; k++) { for (unsigned int i = 0; i < s[k].size(); i++) { string str1(s[k].substr(0, i)); string str2(s[k].substr(i, s[k].size() - i)); str.push_back(str1 + str2); str.push_back(str2 + str1); reverse(str1.begin(), str1.end()); str.push_back(str1 + str2); str.push_back(str2 + str1); reverse(str1.begin(), str1.end()); reverse(str2.begin(), str2.end()); str.push_back(str1 + str2); str.push_back(str2 + str1); reverse(str1.begin(), str1.end()); str.push_back(str1 + str2); str.push_back(str2 + str1); } unsigned int n = 1; ans.push_back(s[k]); for (unsigned int i = 0; i < str.size(); i++) { bool f = true; for (unsigned int j = 0; j < ans.size(); j++) { if (str[i] == ans[j]) { f = false; break; } } if (f) { n++; ans.push_back(str[i]); } } cout << n << endl; } return (0); }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main(void) { vector<string> str; vector<string> ans; string s[120]; int n; cin >> n; for (int i = 0; i < n; i++) { cin >> s[i]; } for (int k = 0; k < n; k++) { str.clear(); ans.clear(); for (unsigned int i = 0; i < s[k].size(); i++) { string str1(s[k].substr(0, i)); string str2(s[k].substr(i, s[k].size() - i)); str.push_back(str1 + str2); str.push_back(str2 + str1); reverse(str1.begin(), str1.end()); str.push_back(str1 + str2); str.push_back(str2 + str1); reverse(str1.begin(), str1.end()); reverse(str2.begin(), str2.end()); str.push_back(str1 + str2); str.push_back(str2 + str1); reverse(str1.begin(), str1.end()); str.push_back(str1 + str2); str.push_back(str2 + str1); } unsigned int n = 1; ans.push_back(s[k]); for (unsigned int i = 0; i < str.size(); i++) { bool f = true; for (unsigned int j = 0; j < ans.size(); j++) { if (str[i] == ans[j]) { f = false; break; } } if (f) { n++; ans.push_back(str[i]); } } cout << n << endl; } return (0); }
insert
20
20
20
22
TLE
p00724
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <vector> using namespace std; typedef pair<int, int> pii; #define REP(i, x) for (int i = 0; i < (int)(x); i++) #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define ALL(container) container.begin(), container.end() #define X first #define Y second pii operator+(const pii &s, const pii &t) { return pii(s.first + t.first, s.second + t.second); } pii operator-(const pii &s, const pii &t) { return pii(s.first - t.first, s.second - t.second); } pii d[7] = {pii(0, -1), pii(1, -1), pii(1, 0), pii(0, 1), pii(-1, 1), pii(-1, 0), pii(0, 0)}; vector<vector<pii>> Body[9]; vector<vector<pii>> g[9]; inline int abs(const pii &d) { return ((d.X < 0) ^ (d.Y < 0)) ? max(abs(d.X), abs(d.Y)) : abs(d.X) + abs(d.Y); } inline int abs(const pii &p1, const pii &p2) { return abs(p1 - p2); } void enumerateState(vector<pii> &p) { Body[p.size()].push_back(p); if (p.size() == 8) return; const pii b = p.back(); p.emplace_back(); REP(i, 6) { const pii q = b + d[i]; p.back() = q; int f = 1; RREP(j, (int)p.size() - 2) if (abs(p[j], q) <= 1) f = 0; if (f) enumerateState(p); } p.pop_back(); } void makeGraph() { REPS(k, 8) { const int n = Body[k].size(); g[k].resize(n); REP(i, n) REP(j, i) REP(l, 7) { int f = 1, prev = l != 6; for (int q = 1; q < k && f; q++) { const int dist = abs(Body[k][j][q], Body[k][i][q] - d[l]); if (dist > 1 || (dist == 1 && prev)) f = 0; prev = dist; } if (f) { g[k][i].emplace_back(j, l); g[k][j].emplace_back(i, l == 6 ? l : (l + 3) % 6); } } } } struct State { pii pos; int id, cost; State(pii pos = pii(0, 0), int id = 0, int cost = 0) : pos(pos), id(id), cost(cost) {} }; int n, m, visited[60][60][4100], Rock[60][60]; pii goal; int main() { ios::sync_with_stdio(false); vector<pii> p(1, pii(0, 0)); enumerateState(p); makeGraph(); while (cin >> n, n) { vector<pii> body(n); REP(i, n) cin >> body[i].X >> body[i].Y; pii pos = pii(30, 30); memset(Rock, 0, sizeof(Rock)); memset(visited, 0, sizeof(visited)); cin >> m; REP(i, m) { pii t; cin >> t.X >> t.Y; t = t - body[0] + pos; if (abs(t, pos) > 25) continue; Rock[t.X][t.Y] = 1; } cin >> goal.X >> goal.Y; goal = goal - body[0] + pos; RREP(i, n) body[i] = body[i] - body[0]; int state = find(ALL(Body[n]), body) - Body[n].begin(); visited[pos.X][pos.Y][state] = 1; queue<State> que; que.emplace(pos, state, 0); int ans = 20; while (!que.empty()) { State s = que.front(); que.pop(); if (s.pos == goal) { ans = s.cost; break; } FOR(it, g[n][s.id]) { State t(s.pos + d[it->second], it->first, s.cost + 1); if (t.cost + abs(t.pos, goal) >= 20 || visited[t.pos.X][t.pos.Y][t.id]) continue; int f = 1; REP(i, n) if (Rock[t.pos.X + Body[n][t.id][i].X][t.pos.Y + Body[n][t.id][i].Y]) f = 0; if (f) que.push(t); } } cout << ans << endl; } return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <vector> using namespace std; typedef pair<int, int> pii; #define REP(i, x) for (int i = 0; i < (int)(x); i++) #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define ALL(container) container.begin(), container.end() #define X first #define Y second pii operator+(const pii &s, const pii &t) { return pii(s.first + t.first, s.second + t.second); } pii operator-(const pii &s, const pii &t) { return pii(s.first - t.first, s.second - t.second); } pii d[7] = {pii(0, -1), pii(1, -1), pii(1, 0), pii(0, 1), pii(-1, 1), pii(-1, 0), pii(0, 0)}; vector<vector<pii>> Body[9]; vector<vector<pii>> g[9]; inline int abs(const pii &d) { return ((d.X < 0) ^ (d.Y < 0)) ? max(abs(d.X), abs(d.Y)) : abs(d.X) + abs(d.Y); } inline int abs(const pii &p1, const pii &p2) { return abs(p1 - p2); } void enumerateState(vector<pii> &p) { Body[p.size()].push_back(p); if (p.size() == 8) return; const pii b = p.back(); p.emplace_back(); REP(i, 6) { const pii q = b + d[i]; p.back() = q; int f = 1; RREP(j, (int)p.size() - 2) if (abs(p[j], q) <= 1) f = 0; if (f) enumerateState(p); } p.pop_back(); } void makeGraph() { REPS(k, 8) { const int n = Body[k].size(); g[k].resize(n); REP(i, n) REP(j, i) REP(l, 7) { int f = 1, prev = l != 6; for (int q = 1; q < k && f; q++) { const int dist = abs(Body[k][j][q], Body[k][i][q] - d[l]); if (dist > 1 || (dist == 1 && prev)) f = 0; prev = dist; } if (f) { g[k][i].emplace_back(j, l); g[k][j].emplace_back(i, l == 6 ? l : (l + 3) % 6); } } } } struct State { pii pos; int id, cost; State(pii pos = pii(0, 0), int id = 0, int cost = 0) : pos(pos), id(id), cost(cost) {} }; int n, m, visited[60][60][4100], Rock[60][60]; pii goal; int main() { ios::sync_with_stdio(false); vector<pii> p(1, pii(0, 0)); enumerateState(p); makeGraph(); while (cin >> n, n) { vector<pii> body(n); REP(i, n) cin >> body[i].X >> body[i].Y; pii pos = pii(30, 30); memset(Rock, 0, sizeof(Rock)); memset(visited, 0, sizeof(visited)); cin >> m; REP(i, m) { pii t; cin >> t.X >> t.Y; t = t - body[0] + pos; if (abs(t, pos) > 25) continue; Rock[t.X][t.Y] = 1; } cin >> goal.X >> goal.Y; goal = goal - body[0] + pos; RREP(i, n) body[i] = body[i] - body[0]; int state = find(ALL(Body[n]), body) - Body[n].begin(); visited[pos.X][pos.Y][state] = 1; queue<State> que; que.emplace(pos, state, 0); int ans = 20; while (!que.empty()) { State s = que.front(); que.pop(); if (s.pos == goal) { ans = s.cost; break; } FOR(it, g[n][s.id]) { State t(s.pos + d[it->second], it->first, s.cost + 1); if (t.cost + abs(t.pos, goal) >= 20 || visited[t.pos.X][t.pos.Y][t.id]) continue; visited[t.pos.X][t.pos.Y][t.id] = 1; int f = 1; REP(i, n) if (Rock[t.pos.X + Body[n][t.id][i].X][t.pos.Y + Body[n][t.id][i].Y]) f = 0; if (f) que.push(t); } } cout << ans << endl; } return 0; }
insert
124
124
124
125
TLE
p00724
C++
Runtime Error
#include <cstdio> #include <cstdlib> #include <queue> #include <set> using namespace std; #define REP(i, n) for (int i = 0; i < n; ++i) #define FOR(i, a, b) for (int i = a; i < b; ++i) #define QCLR(q) \ while (!q.empty()) \ q.pop(); int dx[] = {0, 0, -1, 1, -1, 1}; int dy[] = {-1, 1, 0, -1, 1, 0}; int N, K, rx[100], ry[100], gx, gy; class Hex { public: int px[8], py[8]; Hex(int tpx[8], int tpy[8]) { REP(i, N) { px[i] = tpx[i]; py[i] = tpy[i]; } } bool operator<(const Hex &h) const { REP(i, N) { if (px[i] != h.px[i]) return px[i] < h.px[i]; if (py[i] != h.py[i]) return py[i] < h.py[i]; } return false; } }; class State { public: Hex h; int c; State(Hex h, int c) : h(h), c(c) {} }; set<Hex> s; queue<State> q; int dist60(int x1, int y1, int x2, int y2) { return (abs(x1 - x2) + abs(y1 - y2) + abs((x1 + y1) - (x2 + y2))) / 2; } bool crash(Hex &h, int p) { REP(i, K) if (h.px[p] == rx[i] && h.py[p] == ry[i]) return true; return false; } bool connect(Hex &h, int j) { REP(i, j) { if (h.px[i] == h.px[j] && h.py[i] == h.py[j]) return false; int cnt = 0; REP(k, 6) { int tx = h.px[i] + dx[k], ty = h.py[i] + dy[k]; if (h.px[j] == tx && h.py[j] == ty) { cnt++; break; } } if (abs(i - j) == 1 && cnt != 1) return false; if (abs(i - j) != 1 && cnt == 1) return false; } return true; } bool create(int p, Hex &h, int c, bool sk) { if (p == N) { if (!s.count(h)) { s.insert(h); q.push(State(h, c + 1)); return true; } return false; } int cnt = 0; if (connect(h, p)) create(p + 1, h, c, false); if (!sk) { REP(i, 6) { h.px[p] += dx[i], h.py[p] += dy[i]; if (crash(h, p) || !connect(h, p)) { h.px[p] -= dx[i], h.py[p] -= dy[i]; continue; } if (create(p + 1, h, c, true)) cnt++; h.px[p] -= dx[i], h.py[p] -= dy[i]; if (p == 0 || p == N - 1) { if (cnt == 2) break; } else { if (cnt == 1) break; } } } return false; } int bfs(int ix[8], int iy[8]) { q.push(State(Hex(ix, iy), 0)); while (1) { State s = q.front(); q.pop(); if (s.h.px[0] == gx && s.h.py[0] == gy) return s.c; int dist = dist60(gx, gy, s.h.px[0], s.h.py[0]); if (dist > 1) dist = (dist - 1) * 2 + 1; if (dist > 20 - s.c) continue; create(0, s.h, s.c, false); } return 0; } int main() { while (scanf("%d", &N), N) { QCLR(q); s.clear(); int px[8], py[8]; REP(i, N) scanf("%d %d", &px[i], &py[i]); scanf("%d", &K); REP(i, K) scanf("%d %d", &rx[i], &ry[i]); scanf("%d%d", &gx, &gy); printf("%d\n", bfs(px, py)); } }
#include <cstdio> #include <cstdlib> #include <queue> #include <set> using namespace std; #define REP(i, n) for (int i = 0; i < n; ++i) #define FOR(i, a, b) for (int i = a; i < b; ++i) #define QCLR(q) \ while (!q.empty()) \ q.pop(); int dx[] = {0, 0, -1, 1, -1, 1}; int dy[] = {-1, 1, 0, -1, 1, 0}; int N, K, rx[100], ry[100], gx, gy; class Hex { public: int px[8], py[8]; Hex(int tpx[8], int tpy[8]) { REP(i, N) { px[i] = tpx[i]; py[i] = tpy[i]; } } bool operator<(const Hex &h) const { REP(i, N) { if (px[i] != h.px[i]) return px[i] < h.px[i]; if (py[i] != h.py[i]) return py[i] < h.py[i]; } return false; } }; class State { public: Hex h; int c; State(Hex h, int c) : h(h), c(c) {} }; set<Hex> s; queue<State> q; int dist60(int x1, int y1, int x2, int y2) { return (abs(x1 - x2) + abs(y1 - y2) + abs((x1 + y1) - (x2 + y2))) / 2; } bool crash(Hex &h, int p) { REP(i, K) if (h.px[p] == rx[i] && h.py[p] == ry[i]) return true; return false; } bool connect(Hex &h, int j) { REP(i, j) { if (h.px[i] == h.px[j] && h.py[i] == h.py[j]) return false; int cnt = 0; REP(k, 6) { int tx = h.px[i] + dx[k], ty = h.py[i] + dy[k]; if (h.px[j] == tx && h.py[j] == ty) { cnt++; break; } } if (abs(i - j) == 1 && cnt != 1) return false; if (abs(i - j) != 1 && cnt == 1) return false; } return true; } bool create(int p, Hex &h, int c, bool sk) { if (p == N) { if (!s.count(h)) { s.insert(h); q.push(State(h, c + 1)); return true; } return false; } int cnt = 0; if (connect(h, p)) create(p + 1, h, c, false); if (!sk) { REP(i, 6) { h.px[p] += dx[i], h.py[p] += dy[i]; if (crash(h, p) || !connect(h, p)) { h.px[p] -= dx[i], h.py[p] -= dy[i]; continue; } if (create(p + 1, h, c, true)) cnt++; h.px[p] -= dx[i], h.py[p] -= dy[i]; if (p == 0 || p == N - 1) { if (cnt == 2) break; } else { if (cnt == 1) break; } } } return false; } int bfs(int ix[8], int iy[8]) { q.push(State(Hex(ix, iy), 0)); while (1) { State s = q.front(); q.pop(); if (s.h.px[0] == gx && s.h.py[0] == gy) return s.c; int dist = dist60(gx, gy, s.h.px[0], s.h.py[0]); if (dist > 1) dist = (dist - 1) * 2; if (dist > 20 - s.c) continue; create(0, s.h, s.c, false); } return 0; } int main() { while (scanf("%d", &N), N) { QCLR(q); s.clear(); int px[8], py[8]; REP(i, N) scanf("%d %d", &px[i], &py[i]); scanf("%d", &K); REP(i, K) scanf("%d %d", &rx[i], &ry[i]); scanf("%d%d", &gx, &gy); printf("%d\n", bfs(px, py)); } }
replace
129
130
129
130
TLE
p00724
C++
Runtime Error
#include <cmath> #include <cstdlib> #include <exception> #include <iostream> #include <map> #include <queue> #include <set> #include <utility> #include <vector> using namespace std; struct coordinate { int x, y; coordinate() { x = 0; y = 0; } coordinate(int X, int Y) { x = X; y = Y; } coordinate operator+(const int &a) const { // {{{ coordinate ret(x, y); if (a < 0 || 6 < a) throw exception(); switch (a) { case 0: break; case 1: ret.y--; break; case 2: ret.y--; ret.x++; break; case 3: ret.x++; break; case 4: ret.y++; break; case 5: ret.x--; ret.y++; break; case 6: ret.x--; } return ret; // }}} } int direction(coordinate a) { // {{{ int diff_x, diff_y; diff_x = a.x - x; diff_y = a.y - y; if (diff_x < -1 || diff_x > 1 || diff_y < -1 || diff_x > 1) throw exception(); if (diff_x == 0 && diff_y == 0) return 0; else if (diff_x == 0 && diff_y == -1) return 1; else if (diff_x == 1 && diff_y == -1) return 2; else if (diff_x == 1 && diff_y == 0) return 3; else if (diff_x == 0 && diff_y == 1) return 4; else if (diff_x == -1 && diff_y == 1) return 5; else if (diff_x == -1 && diff_y == 0) return 6; else throw exception(); // }}} } bool operator==(const coordinate a) const { return (x == a.x) && (y == a.y); } bool operator<(const coordinate a) const { if (x != a.x) return x < a.x; return y < a.y; } }; bool adjoin(coordinate a, coordinate b) { int x, y; x = a.x - b.x; y = a.y - b.y; if (x < -1 || 1 < x || y < -1 || 1 < y) return false; if ((x == 1 && y == 1) || (x == -1 && y == -1)) return false; return true; } struct snake { coordinate head; // head; int geometry; snake(){}; snake(coordinate h, int geo) { head = h; geometry = geo; } }; int encode_snake_geometory(vector<coordinate> v) { int ret = 0; for (int i = 0; i < v.size() - 1; i++) { ret |= v[i].direction(v[i + 1]) << (3 * i); } return ret; } vector<coordinate> decode_snake_geometory(int n, coordinate c) { vector<coordinate> ret; ret.push_back(c); for (int i = 0; (n >> (3 * i)) & 7; i++) { ret.push_back(ret[i] + ((n >> (3 * i)) & 7)); } return ret; } map<int, vector<pair<int, int>>> snake_transform; // first:geometory, second:(first:geometory, second:head // move) bool save(vector<coordinate> geometory) { for (int i = 0; i < geometory.size(); i++) { for (int j = i + 2; j < geometory.size(); j++) { /* try { geometory[i].direction(geometory[j]); return false; } catch(exception e) { } */ if (adjoin(geometory[i], geometory[j])) return false; } } return true; } vector<vector<coordinate>> next_geometory_result; vector<coordinate> next_geometory_search_table; int next_geometory_head_move; int next_geometory_from; void next_geometory(vector<coordinate> geometory, int indx, bool prev_move) { if (indx >= geometory.size()) { if (save(next_geometory_search_table)) { next_geometory_result.push_back(next_geometory_search_table); snake_transform[next_geometory_from].push_back( pair<int, int>(encode_snake_geometory(next_geometory_search_table), next_geometory_head_move)); } return; } if (!prev_move) { for (int i = 1; i <= 6; i++) { coordinate c = geometory[indx]; c = c + i; if (indx == 0) next_geometory_head_move = i; /* try { if(indx > 0) c.direction(geometory[indx - 1]); if(indx < geometory.size() - 1) c.direction(geometory[indx + 1]); next_geometory_search_table.push_back(c); next_geometory(geometory, indx + 1, true); next_geometory_search_table.pop_back(); } catch (exception e){ } */ bool flag0, flag1; flag0 = flag1 = true; if (indx > 0) { flag0 = adjoin(c, geometory[indx - 1]); } if (indx < geometory.size() - 1) { flag1 = adjoin(c, geometory[indx + 1]); } if (flag0 && flag1) { next_geometory_search_table.push_back(c); next_geometory(geometory, indx + 1, true); next_geometory_search_table.pop_back(); } } } if (indx == 0) next_geometory_head_move = 0; next_geometory_search_table.push_back(geometory[indx]); next_geometory(geometory, indx + 1, false); next_geometory_search_table.pop_back(); } void make_table(int n) { vector<coordinate> geometory; for (int i = 0; i < n; i++) { geometory.push_back(coordinate(0, i)); } queue<int> q; q.push(encode_snake_geometory(geometory)); while (!q.empty()) { int g; g = q.front(); q.pop(); if (snake_transform.find(g) == snake_transform.end()) { /* debug vector<coordinate> tmp; tmp = decode_snake_geometory(g, coordinate(0,0)); for(int i = 0; i < tmp.size(); i++) cout << "(" << tmp[i].x << ", " << tmp[i].y << ") "; cout << endl; debug */ next_geometory_from = g; next_geometory_result.clear(); next_geometory_search_table.clear(); next_geometory(decode_snake_geometory(g, coordinate(0, 0)), 0, false); for (int i = 0; i < next_geometory_result.size(); i++) { q.push(encode_snake_geometory(next_geometory_result[i])); } } } } /* input value */ int snake_nodes; vector<coordinate> snake; int rock_num; vector<coordinate> rock; coordinate scientist; /* input process */ bool input() { cin >> snake_nodes; if (!snake_nodes) return false; for (int i = 0; i < snake_nodes; i++) { int x, y; cin >> x >> y; snake.push_back(coordinate(x, y)); } cin >> rock_num; for (int i = 0; i < rock_num; i++) { int x, y; cin >> x >> y; rock.push_back(coordinate(x, y)); } int x, y; cin >> x >> y; scientist = coordinate(x, y); return true; } void init() { snake.clear(); rock.clear(); snake_transform.clear(); next_geometory_result.clear(); next_geometory_search_table.clear(); } int solve() { set<pair<int, coordinate>> moved; queue<pair<int, coordinate>> q; q.push(pair<int, coordinate>(encode_snake_geometory(snake), snake[0])); int count = 0; // debug int prev; while (true) { queue<pair<int, coordinate>> next_q; prev = 0; while (!q.empty()) { prev++; pair<int, coordinate> tmp; int g; vector<pair<int, int>> next; tmp = q.front(); q.pop(); if (tmp.second == scientist) return count; if (moved.find(tmp) != moved.end()) { continue; } if (abs(tmp.second.x - scientist.x) + abs(tmp.second.y - scientist.y) > 20 - count) { continue; } moved.insert(tmp); next = snake_transform[tmp.first]; for (int i = 0; i < next.size(); i++) { vector<coordinate> s; s = decode_snake_geometory(next[i].first, tmp.second + next[i].second); for (int j = 0; j < s.size(); j++) { for (int k = 0; k < rock.size(); k++) { if (s[j] == rock[k]) { goto fail0; } } } next_q.push(pair<int, coordinate>(encode_snake_geometory(s), s[0])); fail0:; } } if (prev == 0) exit(1); q = next_q; count++; } return -1; } int main() { init(); while (input()) { make_table(snake_nodes); cout << solve() << endl; init(); } }
#include <cmath> #include <cstdlib> #include <exception> #include <iostream> #include <map> #include <queue> #include <set> #include <utility> #include <vector> using namespace std; struct coordinate { int x, y; coordinate() { x = 0; y = 0; } coordinate(int X, int Y) { x = X; y = Y; } coordinate operator+(const int &a) const { // {{{ coordinate ret(x, y); if (a < 0 || 6 < a) throw exception(); switch (a) { case 0: break; case 1: ret.y--; break; case 2: ret.y--; ret.x++; break; case 3: ret.x++; break; case 4: ret.y++; break; case 5: ret.x--; ret.y++; break; case 6: ret.x--; } return ret; // }}} } int direction(coordinate a) { // {{{ int diff_x, diff_y; diff_x = a.x - x; diff_y = a.y - y; if (diff_x < -1 || diff_x > 1 || diff_y < -1 || diff_x > 1) throw exception(); if (diff_x == 0 && diff_y == 0) return 0; else if (diff_x == 0 && diff_y == -1) return 1; else if (diff_x == 1 && diff_y == -1) return 2; else if (diff_x == 1 && diff_y == 0) return 3; else if (diff_x == 0 && diff_y == 1) return 4; else if (diff_x == -1 && diff_y == 1) return 5; else if (diff_x == -1 && diff_y == 0) return 6; else throw exception(); // }}} } bool operator==(const coordinate a) const { return (x == a.x) && (y == a.y); } bool operator<(const coordinate a) const { if (x != a.x) return x < a.x; return y < a.y; } }; bool adjoin(coordinate a, coordinate b) { int x, y; x = a.x - b.x; y = a.y - b.y; if (x < -1 || 1 < x || y < -1 || 1 < y) return false; if ((x == 1 && y == 1) || (x == -1 && y == -1)) return false; return true; } struct snake { coordinate head; // head; int geometry; snake(){}; snake(coordinate h, int geo) { head = h; geometry = geo; } }; int encode_snake_geometory(vector<coordinate> v) { int ret = 0; for (int i = 0; i < v.size() - 1; i++) { ret |= v[i].direction(v[i + 1]) << (3 * i); } return ret; } vector<coordinate> decode_snake_geometory(int n, coordinate c) { vector<coordinate> ret; ret.push_back(c); for (int i = 0; (n >> (3 * i)) & 7; i++) { ret.push_back(ret[i] + ((n >> (3 * i)) & 7)); } return ret; } map<int, vector<pair<int, int>>> snake_transform; // first:geometory, second:(first:geometory, second:head // move) bool save(vector<coordinate> geometory) { for (int i = 0; i < geometory.size(); i++) { for (int j = i + 2; j < geometory.size(); j++) { /* try { geometory[i].direction(geometory[j]); return false; } catch(exception e) { } */ if (adjoin(geometory[i], geometory[j])) return false; } } return true; } vector<vector<coordinate>> next_geometory_result; vector<coordinate> next_geometory_search_table; int next_geometory_head_move; int next_geometory_from; void next_geometory(vector<coordinate> geometory, int indx, bool prev_move) { if (indx >= geometory.size()) { if (save(next_geometory_search_table)) { next_geometory_result.push_back(next_geometory_search_table); snake_transform[next_geometory_from].push_back( pair<int, int>(encode_snake_geometory(next_geometory_search_table), next_geometory_head_move)); } return; } if (!prev_move) { for (int i = 1; i <= 6; i++) { coordinate c = geometory[indx]; c = c + i; if (indx == 0) next_geometory_head_move = i; /* try { if(indx > 0) c.direction(geometory[indx - 1]); if(indx < geometory.size() - 1) c.direction(geometory[indx + 1]); next_geometory_search_table.push_back(c); next_geometory(geometory, indx + 1, true); next_geometory_search_table.pop_back(); } catch (exception e){ } */ bool flag0, flag1; flag0 = flag1 = true; if (indx > 0) { flag0 = adjoin(c, geometory[indx - 1]); } if (indx < geometory.size() - 1) { flag1 = adjoin(c, geometory[indx + 1]); } if (flag0 && flag1) { next_geometory_search_table.push_back(c); next_geometory(geometory, indx + 1, true); next_geometory_search_table.pop_back(); } } } if (indx == 0) next_geometory_head_move = 0; next_geometory_search_table.push_back(geometory[indx]); next_geometory(geometory, indx + 1, false); next_geometory_search_table.pop_back(); } void make_table(int n) { vector<coordinate> geometory; for (int i = 0; i < n; i++) { geometory.push_back(coordinate(0, i)); } queue<int> q; q.push(encode_snake_geometory(geometory)); while (!q.empty()) { int g; g = q.front(); q.pop(); if (snake_transform.find(g) == snake_transform.end()) { /* debug vector<coordinate> tmp; tmp = decode_snake_geometory(g, coordinate(0,0)); for(int i = 0; i < tmp.size(); i++) cout << "(" << tmp[i].x << ", " << tmp[i].y << ") "; cout << endl; debug */ next_geometory_from = g; next_geometory_result.clear(); next_geometory_search_table.clear(); next_geometory(decode_snake_geometory(g, coordinate(0, 0)), 0, false); for (int i = 0; i < next_geometory_result.size(); i++) { q.push(encode_snake_geometory(next_geometory_result[i])); } } } } /* input value */ int snake_nodes; vector<coordinate> snake; int rock_num; vector<coordinate> rock; coordinate scientist; /* input process */ bool input() { cin >> snake_nodes; if (!snake_nodes) return false; for (int i = 0; i < snake_nodes; i++) { int x, y; cin >> x >> y; snake.push_back(coordinate(x, y)); } cin >> rock_num; for (int i = 0; i < rock_num; i++) { int x, y; cin >> x >> y; rock.push_back(coordinate(x, y)); } int x, y; cin >> x >> y; scientist = coordinate(x, y); return true; } void init() { snake.clear(); rock.clear(); snake_transform.clear(); next_geometory_result.clear(); next_geometory_search_table.clear(); } int solve() { set<pair<int, coordinate>> moved; queue<pair<int, coordinate>> q; q.push(pair<int, coordinate>(encode_snake_geometory(snake), snake[0])); int count = 0; // debug int prev; while (true) { queue<pair<int, coordinate>> next_q; prev = 0; while (!q.empty()) { prev++; pair<int, coordinate> tmp; int g; vector<pair<int, int>> next; tmp = q.front(); q.pop(); if (tmp.second == scientist) return count; if (moved.find(tmp) != moved.end()) { continue; } moved.insert(tmp); next = snake_transform[tmp.first]; for (int i = 0; i < next.size(); i++) { vector<coordinate> s; s = decode_snake_geometory(next[i].first, tmp.second + next[i].second); for (int j = 0; j < s.size(); j++) { for (int k = 0; k < rock.size(); k++) { if (s[j] == rock[k]) { goto fail0; } } } next_q.push(pair<int, coordinate>(encode_snake_geometory(s), s[0])); fail0:; } } if (prev == 0) exit(1); q = next_q; count++; } return -1; } int main() { init(); while (input()) { make_table(snake_nodes); cout << solve() << endl; init(); } }
replace
304
308
304
305
TLE
p00725
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long int ll; const int INF = 1000000000; #define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rep(i, n) REP(i, 0, n) typedef vector<int> vec; typedef vector<vec> mat; typedef pair<int, int> pint; const int dx[4] = {0, 1, -1, 0}, dy[4] = {1, 0, 0, -1}; pint Start, Goal; int W, H; bool range_out(int x, int y) { return x < 0 || y < 0 || W <= x || H <= y; } int solve(mat fld, pint now, int turn) { if (turn > 10) return INF; int res = INF; // cout << "now" << now.first << ' ' << now.second << endl; rep(i, 4) { int nx = now.first + dx[i], ny = now.second + dy[i]; if (range_out(nx, ny) || fld[ny][nx] == 1) continue; mat temp = fld; bool out = false; while (true) { if (range_out(nx, ny)) { out = true; break; } if (fld[ny][nx] == 1) { temp[ny][nx] = 0; nx -= dx[i]; ny -= dy[i]; break; } if (fld[ny][nx] == 3) return turn + 1; nx += dx[i]; ny += dy[i]; } // cout << nx << ' ' << ny << endl; if (!out) res = min(res, solve(temp, pint(nx, ny), turn + 1)); } return res; } int main() { cin.tie(0); ios::sync_with_stdio(false); while (cin >> W >> H && W) { mat fld(H, vec(W)); rep(i, H) rep(j, W) { cin >> fld[i][j]; if (fld[i][j] == 2) Start = pint(j, i); if (fld[i][j] == 3) Goal = pint(j, i); } int ans = solve(fld, Start, 0); if (ans == INF || ans > 10) ans = -1; cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; const int INF = 1000000000; #define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rep(i, n) REP(i, 0, n) typedef vector<int> vec; typedef vector<vec> mat; typedef pair<int, int> pint; const int dx[4] = {0, 1, -1, 0}, dy[4] = {1, 0, 0, -1}; pint Start, Goal; int W, H; bool range_out(int x, int y) { return x < 0 || y < 0 || W <= x || H <= y; } int solve(mat fld, pint now, int turn) { if (turn >= 10) return INF; int res = INF; // cout << "now" << now.first << ' ' << now.second << endl; rep(i, 4) { int nx = now.first + dx[i], ny = now.second + dy[i]; if (range_out(nx, ny) || fld[ny][nx] == 1) continue; mat temp = fld; bool out = false; while (true) { if (range_out(nx, ny)) { out = true; break; } if (fld[ny][nx] == 1) { temp[ny][nx] = 0; nx -= dx[i]; ny -= dy[i]; break; } if (fld[ny][nx] == 3) return turn + 1; nx += dx[i]; ny += dy[i]; } // cout << nx << ' ' << ny << endl; if (!out) res = min(res, solve(temp, pint(nx, ny), turn + 1)); } return res; } int main() { cin.tie(0); ios::sync_with_stdio(false); while (cin >> W >> H && W) { mat fld(H, vec(W)); rep(i, H) rep(j, W) { cin >> fld[i][j]; if (fld[i][j] == 2) Start = pint(j, i); if (fld[i][j] == 3) Goal = pint(j, i); } int ans = solve(fld, Start, 0); if (ans == INF || ans > 10) ans = -1; cout << ans << endl; } return 0; }
replace
14
15
14
15
TLE
p00725
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> using namespace std; int w, h; int ans; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool canmove(int x, int y) { return (0 <= x && x < w && 0 <= y && y < h); } struct Map { int map[20][20]; int s, g; int cnt; int cur; Map(int m[20][20]) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { map[i][j] = m[i][j]; if (m[i][j] == 2) s = i * 32 + j; if (m[i][j] == 3) g = i * 32 + j; } } cnt = 0; cur = s; } Map(int m[20][20], int curling, int _cnt) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { map[i][j] = m[i][j]; if (m[i][j] == 2) s = i * 32 + j; if (m[i][j] == 3) g = i * 32 + j; } } cnt = _cnt; cur = curling; } Map move(int direc) { Map m(map, cur, cnt + 1); int y = cur / 32, x = cur % 32; if (canmove(x + dx[direc], y + dy[direc]) && map[y + dy[direc]][x + dx[direc]] == 1) { m.cnt = 100; return m; } int k = 1; while (true) { bool b = canmove(x + k * dx[direc], y + k * dy[direc]); int num = map[y + k * dy[direc]][x + k * dx[direc]]; if (b && num == 3) { m.cur = g; break; } else if (b && num != 1) k++; else if (b && num == 1) { m.map[y + k * dy[direc]][x + k * dx[direc]] = 0; k--; m.cur = (y + k * dy[direc]) * 32 + x + k * dx[direc]; break; } else { m.cnt = 100; break; } } // cout<<m.cnt<<endl; return m; } }; void dfs(Map m) { if (m.cnt > 10) return; if (m.cur == m.g) { ans = min(m.cnt, ans); return; } for (int i = 0; i < 4; i++) { dfs(m.move(i)); } } int main() { while (cin >> w >> h, w + h) { int map[20][20]; ans = 1000; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> map[i][j]; Map m(map); dfs(m); if (ans == 1000) cout << -1 << endl; else cout << ans << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; int w, h; int ans; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool canmove(int x, int y) { return (0 <= x && x < w && 0 <= y && y < h); } struct Map { int map[20][20]; int s, g; int cnt; int cur; Map(int m[20][20]) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { map[i][j] = m[i][j]; if (m[i][j] == 2) s = i * 32 + j; if (m[i][j] == 3) g = i * 32 + j; } } cnt = 0; cur = s; } Map(int m[20][20], int curling, int _cnt) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { map[i][j] = m[i][j]; if (m[i][j] == 2) s = i * 32 + j; if (m[i][j] == 3) g = i * 32 + j; } } cnt = _cnt; cur = curling; } Map move(int direc) { Map m(map, cur, cnt + 1); int y = cur / 32, x = cur % 32; if (canmove(x + dx[direc], y + dy[direc]) && map[y + dy[direc]][x + dx[direc]] == 1) { m.cnt = 100; return m; } int k = 1; while (true) { bool b = canmove(x + k * dx[direc], y + k * dy[direc]); int num = map[y + k * dy[direc]][x + k * dx[direc]]; if (b && num == 3) { m.cur = g; break; } else if (b && num != 1) k++; else if (b && num == 1) { m.map[y + k * dy[direc]][x + k * dx[direc]] = 0; k--; m.cur = (y + k * dy[direc]) * 32 + x + k * dx[direc]; break; } else { m.cnt = 100; break; } } // cout<<m.cnt<<endl; return m; } }; void dfs(Map m) { if (m.cnt > 10) return; if (m.cnt >= ans) return; if (m.cur == m.g) { ans = min(m.cnt, ans); return; } for (int i = 0; i < 4; i++) { dfs(m.move(i)); } } int main() { while (cin >> w >> h, w + h) { int map[20][20]; ans = 1000; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> map[i][j]; Map m(map); dfs(m); if (ans == 1000) cout << -1 << endl; else cout << ans << endl; } return 0; }
insert
77
77
77
79
TLE
p00725
C++
Time Limit Exceeded
#include <iostream> #define MAX 1000000000 using namespace std; int w, h; int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1}; int func(int y, int x, int now, int tab[][20]) { int ans = MAX, nx, ny; for (int i = 0; i < 4; i++) { nx = dx[i] + x; ny = dy[i] + y; if (nx >= 0 && nx < w && ny >= 0 && ny < h && tab[ny][nx] == 1) continue; while (nx >= 0 && nx < w && ny >= 0 && ny < h) { if (tab[ny][nx] == 1 || tab[ny][nx] == 3) break; nx += dx[i]; ny += dy[i]; } if (!(nx >= 0 && nx < w && ny >= 0 && ny < h)) { continue; } else if (tab[ny][nx] == 1) { tab[ny][nx] = 0; ans = min(ans, func(ny - dy[i], nx - dx[i], now + 1, tab)); tab[ny][nx] = 1; } else if (tab[ny][nx] == 3) { ans = min(ans, now); } } return ans; } int main() { int sy, sx; while (cin >> w >> h, w || h) { int tab[20][20]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { cin >> tab[i][j]; if (tab[i][j] == 2) { sy = i; sx = j; } } int ans = func(sy, sx, 1, tab); if (ans != MAX && ans < 11) cout << ans << endl; else cout << -1 << endl; } return 0; }
#include <iostream> #define MAX 1000000000 using namespace std; int w, h; int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1}; int func(int y, int x, int now, int tab[][20]) { if (now > 10) return MAX; int ans = MAX, nx, ny; for (int i = 0; i < 4; i++) { nx = dx[i] + x; ny = dy[i] + y; if (nx >= 0 && nx < w && ny >= 0 && ny < h && tab[ny][nx] == 1) continue; while (nx >= 0 && nx < w && ny >= 0 && ny < h) { if (tab[ny][nx] == 1 || tab[ny][nx] == 3) break; nx += dx[i]; ny += dy[i]; } if (!(nx >= 0 && nx < w && ny >= 0 && ny < h)) { continue; } else if (tab[ny][nx] == 1) { tab[ny][nx] = 0; ans = min(ans, func(ny - dy[i], nx - dx[i], now + 1, tab)); tab[ny][nx] = 1; } else if (tab[ny][nx] == 3) { ans = min(ans, now); } } return ans; } int main() { int sy, sx; while (cin >> w >> h, w || h) { int tab[20][20]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { cin >> tab[i][j]; if (tab[i][j] == 2) { sy = i; sx = j; } } int ans = func(sy, sx, 1, tab); if (ans != MAX && ans < 11) cout << ans << endl; else cout << -1 << endl; } return 0; }
insert
6
6
6
8
TLE
p00725
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> #include <vector> // 0 : space 1 : twall 2 : start 3 : goal 4 : wall int board[32][32]; int W, H; void init() { for (int i = 0; i < (int)32; ++i) { for (int j = 0; j < (int)32; ++j) { board[i][j] = 4; } } } void Load() { scanf("%d %d", &W, &H); for (int i = 0; i < (int)H; ++i) { for (int j = 0; j < (int)W; ++j) { scanf("%d", &board[i + 1][j + 1]); } } } int dr[4] = {1, 0, -1, 0}; int dc[4] = {0, 1, 0, -1}; class Solve_ { std::pair<int, int> start, end; int dist[32][32][4]; int limit; bool ok; public: int operator()() { init(); ok = false; int res = (1 << 29); for (limit = 1; limit < 441; ++limit) { dfs(start, 0); if (ok) { res = limit; break; } } if (res >= 11) res = -1; return res; } void dfs(std::pair<int, int> p, int d) { if (d >= limit) return; int r = p.first, c = p.second; if (board[r][c] == 3) return; for (int k = 0; k < 4; ++k) { int nr = r + dr[k], nc = c + dc[k]; if (board[nr][nc] == 1 or board[nr][nc] == 4) continue; if (nr == end.first and nc == end.second) { ok = true; return; } for (;;) { int nnr = nr + dr[k], nnc = nc + dc[k]; if (board[nnr][nnc] == 1 or board[nnr][nnc] == 4) break; nr = nnr; nc = nnc; if (nr == end.first and nc == end.second) { ok = true; return; } } if (board[nr + dr[k]][nc + dc[k]] == 4) continue; // if( dist[nr][nc][k] <= d + 1 ) continue; dist[nr][nc][k] = d + 1; bool b = false; if (board[nr + dr[k]][nc + dc[k]] == 1) { b = true; } if (b) { board[nr + dr[k]][nc + dc[k]] = 0; } dfs(std::pair<int, int>(nr, nc), d + 1); if (b) { board[nr + dr[k]][nc + dc[k]] = 1; } if (ok) break; } } void init() { for (int i = 1; i <= H; ++i) { for (int j = 1; j <= W; ++j) { if (board[i][j] == 2) { start = std::pair<int, int>(i, j); } if (board[i][j] == 3) { end = std::pair<int, int>(i, j); } } } for (int i = 0; i < 32; ++i) { for (int j = 0; j < 32; ++j) { for (int k = 0; k < 4; ++k) { dist[i][j][k] = (1 << 29); } } } } }; Solve_ Solve; int main() { for (;;) { init(); Load(); if (W == 0 and H == 0) break; int res = Solve(); printf("%d\n", res); } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> #include <vector> // 0 : space 1 : twall 2 : start 3 : goal 4 : wall int board[32][32]; int W, H; void init() { for (int i = 0; i < (int)32; ++i) { for (int j = 0; j < (int)32; ++j) { board[i][j] = 4; } } } void Load() { scanf("%d %d", &W, &H); for (int i = 0; i < (int)H; ++i) { for (int j = 0; j < (int)W; ++j) { scanf("%d", &board[i + 1][j + 1]); } } } int dr[4] = {1, 0, -1, 0}; int dc[4] = {0, 1, 0, -1}; class Solve_ { std::pair<int, int> start, end; int dist[32][32][4]; int limit; bool ok; public: int operator()() { init(); ok = false; int res = (1 << 29); for (limit = 1; limit < 11; ++limit) { dfs(start, 0); if (ok) { res = limit; break; } } if (res >= 11) res = -1; return res; } void dfs(std::pair<int, int> p, int d) { if (d >= limit) return; int r = p.first, c = p.second; if (board[r][c] == 3) return; for (int k = 0; k < 4; ++k) { int nr = r + dr[k], nc = c + dc[k]; if (board[nr][nc] == 1 or board[nr][nc] == 4) continue; if (nr == end.first and nc == end.second) { ok = true; return; } for (;;) { int nnr = nr + dr[k], nnc = nc + dc[k]; if (board[nnr][nnc] == 1 or board[nnr][nnc] == 4) break; nr = nnr; nc = nnc; if (nr == end.first and nc == end.second) { ok = true; return; } } if (board[nr + dr[k]][nc + dc[k]] == 4) continue; // if( dist[nr][nc][k] <= d + 1 ) continue; dist[nr][nc][k] = d + 1; bool b = false; if (board[nr + dr[k]][nc + dc[k]] == 1) { b = true; } if (b) { board[nr + dr[k]][nc + dc[k]] = 0; } dfs(std::pair<int, int>(nr, nc), d + 1); if (b) { board[nr + dr[k]][nc + dc[k]] = 1; } if (ok) break; } } void init() { for (int i = 1; i <= H; ++i) { for (int j = 1; j <= W; ++j) { if (board[i][j] == 2) { start = std::pair<int, int>(i, j); } if (board[i][j] == 3) { end = std::pair<int, int>(i, j); } } } for (int i = 0; i < 32; ++i) { for (int j = 0; j < 32; ++j) { for (int k = 0; k < 4; ++k) { dist[i][j][k] = (1 << 29); } } } } }; Solve_ Solve; int main() { for (;;) { init(); Load(); if (W == 0 and H == 0) break; int res = Solve(); printf("%d\n", res); } return 0; }
replace
47
48
47
48
TLE
p00725
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define foreach(itr, a) \ for (__typeof((a).begin()) itr = (a).begin(); itr != (a).end(); itr++) #define ALL(x) (x).begin(), (x).end() #define pb push_back int w, h, ans = 0; int sx, sy; int f[21][21]; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; bool ng(int x, int y) { return (x < 0 or y < 0 or x >= w or y >= h); } void dfs(int x, int y, int depth) { if (depth + 1 >= ans) return; rep(i, 4) { int nx = x + dx[i], ny = y + dy[i]; if (!ng(nx, ny) and f[ny][nx] != 1) { while (!ng(nx, ny)) { // goal if (f[ny][nx] == 3) { ans = min(ans, depth + 1); return; } if (f[ny][nx] == 1) { f[ny][nx] = 0; dfs(nx - dx[i], ny - dy[i], depth + 1); f[ny][nx] = 1; break; } nx += dx[i]; ny += dy[i]; } } } return; } int main() { while (cin >> w >> h and w) { rep(i, h) rep(j, w) { cin >> f[i][j]; if (f[i][j] == 2) { sx = j; sy = i; f[i][j] = 0; } } ans = 114514; dfs(sx, sy, 0); if (ans <= 10) cout << ans << "\n"; else cout << -1 << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define foreach(itr, a) \ for (__typeof((a).begin()) itr = (a).begin(); itr != (a).end(); itr++) #define ALL(x) (x).begin(), (x).end() #define pb push_back int w, h, ans = 0; int sx, sy; int f[21][21]; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; bool ng(int x, int y) { return (x < 0 or y < 0 or x >= w or y >= h); } void dfs(int x, int y, int depth) { if (depth + 1 >= ans) return; rep(i, 4) { int nx = x + dx[i], ny = y + dy[i]; if (!ng(nx, ny) and f[ny][nx] != 1) { while (!ng(nx, ny)) { // goal if (f[ny][nx] == 3) { ans = min(ans, depth + 1); return; } if (f[ny][nx] == 1) { f[ny][nx] = 0; dfs(nx - dx[i], ny - dy[i], depth + 1); f[ny][nx] = 1; break; } nx += dx[i]; ny += dy[i]; } } } return; } int main() { while (cin >> w >> h and w) { rep(i, h) rep(j, w) { cin >> f[i][j]; if (f[i][j] == 2) { sx = j; sy = i; f[i][j] = 0; } } ans = 11; dfs(sx, sy, 0); if (ans <= 10) cout << ans << "\n"; else cout << -1 << "\n"; } return 0; }
replace
60
61
60
61
TLE
p00725
C++
Memory Limit Exceeded
#include <algorithm> #include <cstring> #include <iostream> #include <queue> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) typedef long long ll; int w, h; struct t_node { int f[20][20]; int x, y; int d; t_node() { d = 0; } t_node(const t_node &tn) { rep(dy, h) { rep(dx, w) { f[dx][dy] = tn.f[dx][dy]; } } x = tn.x; y = tn.y; d = tn.d; } t_node &operator=(const t_node &r) { rep(dy, h) { rep(dx, w) { f[dx][dy] = r.f[dx][dy]; } } d = r.d; x = r.x; y = r.y; return *this; } bool check(int dx, int dy, int &nx, int &ny) { nx = x; ny = y; while (0 <= nx + dx && nx + dx < w && 0 <= ny + dy && ny + dy < h) { if (f[nx + dx][ny + dy] == 1) { return true; } nx += dx; ny += dy; if (f[nx][ny] == 3) { return true; } } return false; } }; int main() { while (cin >> w >> h, w || h) { t_node tn; rep(y, h) { rep(x, w) { cin >> tn.f[x][y]; if (tn.f[x][y] == 2) { tn.f[x][y] = 0; tn.x = x; tn.y = y; } } } queue<t_node> que; que.push(tn); bool found = false; while (!que.empty()) { tn = que.front(); que.pop(); if (tn.f[tn.x][tn.y] == 3) { found = true; cout << tn.d << endl; break; } if (tn.d == 10) { continue; } int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1}; int nx, ny; rep(i, 4) { if (tn.f[tn.x + dx[i]][tn.y + dy[i]] != 1 && tn.check(dx[i], dy[i], nx, ny)) { bool flg = (tn.f[nx + dx[i]][ny + dy[i]] == 1); if (flg) { tn.f[nx + dx[i]][ny + dy[i]] = 0; } tn.d++; int tmpx = tn.x, tmpy = tn.y; tn.x = nx; tn.y = ny; que.push(tn); tn.x = tmpx; tn.y = tmpy; tn.d--; if (flg) { tn.f[nx + dx[i]][ny + dy[i]] = 1; } } } } if (!found) { cout << "-1" << endl; } } return 0; }
#include <algorithm> #include <cstring> #include <iostream> #include <queue> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) typedef long long ll; int w, h; struct t_node { short f[20][20]; int x, y; int d; t_node() { d = 0; } t_node(const t_node &tn) { rep(dy, h) { rep(dx, w) { f[dx][dy] = tn.f[dx][dy]; } } x = tn.x; y = tn.y; d = tn.d; } t_node &operator=(const t_node &r) { rep(dy, h) { rep(dx, w) { f[dx][dy] = r.f[dx][dy]; } } d = r.d; x = r.x; y = r.y; return *this; } bool check(int dx, int dy, int &nx, int &ny) { nx = x; ny = y; while (0 <= nx + dx && nx + dx < w && 0 <= ny + dy && ny + dy < h) { if (f[nx + dx][ny + dy] == 1) { return true; } nx += dx; ny += dy; if (f[nx][ny] == 3) { return true; } } return false; } }; int main() { while (cin >> w >> h, w || h) { t_node tn; rep(y, h) { rep(x, w) { cin >> tn.f[x][y]; if (tn.f[x][y] == 2) { tn.f[x][y] = 0; tn.x = x; tn.y = y; } } } queue<t_node> que; que.push(tn); bool found = false; while (!que.empty()) { tn = que.front(); que.pop(); if (tn.f[tn.x][tn.y] == 3) { found = true; cout << tn.d << endl; break; } if (tn.d == 10) { continue; } int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1}; int nx, ny; rep(i, 4) { if (tn.f[tn.x + dx[i]][tn.y + dy[i]] != 1 && tn.check(dx[i], dy[i], nx, ny)) { bool flg = (tn.f[nx + dx[i]][ny + dy[i]] == 1); if (flg) { tn.f[nx + dx[i]][ny + dy[i]] = 0; } tn.d++; int tmpx = tn.x, tmpy = tn.y; tn.x = nx; tn.y = ny; que.push(tn); tn.x = tmpx; tn.y = tmpy; tn.d--; if (flg) { tn.f[nx + dx[i]][ny + dy[i]] = 1; } } } } if (!found) { cout << "-1" << endl; } } return 0; }
replace
15
16
15
16
MLE
p00725
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> P; typedef pair<ll, ll> Pll; typedef vector<int> Vi; typedef tuple<int, int, int> T; #define FOR(i, s, x) for (int i = s; i < (int)(x); i++) #define REP(i, x) FOR(i, 0, x) #define ALL(c) c.begin(), c.end() #define DUMP(x) cerr << #x << " = " << (x) << endl const int dr[4] = {-1, 0, 1, 0}; const int dc[4] = {0, 1, 0, -1}; int R, C; inline bool in(int r, int c) { return 0 <= r and r < R and 0 <= c and c < C; } int bfs(vector<vector<int>> B) { int sr = 0, sc = 0, gr = 0, gc = 0; REP(i, R) REP(j, C) { if (B[i][j] == 2) sr = i, sc = j; if (B[i][j] == 3) gr = i, gc = j; } vector<vector<bool>> board(R, vector<bool>(C, false)); REP(i, R) REP(j, C) board[i][j] = B[i][j] == 0; board[sr][sc] = board[gr][gc] = true; queue<tuple<int, int, vector<vector<bool>>, int>> que; que.emplace(sr, sc, board, 0); while (not que.empty()) { int r, c, cnt; vector<vector<bool>> board; tie(r, c, board, cnt) = que.front(); que.pop(); if (cnt == 10) break; REP(i, 4) { int nr = r + dr[i], nc = c + dc[i]; if (in(nr, nc) and board[nr][nc]) { while (in(nr, nc) and board[nr][nc]) { if (nr == gr and nc == gc) return cnt + 1; nr += dr[i], nc += dc[i]; } if (in(nr, nc) and cnt < 10) { vector<vector<bool>> next_board = board; next_board[nr][nc] = true; que.emplace(nr - dr[i], nc - dc[i], next_board, cnt + 1); } } } } return -1; } int main() { // use scanf in CodeForces! cin.tie(0); ios_base::sync_with_stdio(false); while (true) { cin >> C >> R; if (R == 0 and C == 0) break; vector<vector<int>> B(R, vector<int>(C)); REP(i, R) REP(j, C) cin >> B[i][j]; cout << bfs(B) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> P; typedef pair<ll, ll> Pll; typedef vector<int> Vi; typedef tuple<int, int, int> T; #define FOR(i, s, x) for (int i = s; i < (int)(x); i++) #define REP(i, x) FOR(i, 0, x) #define ALL(c) c.begin(), c.end() #define DUMP(x) cerr << #x << " = " << (x) << endl const int dr[4] = {-1, 0, 1, 0}; const int dc[4] = {0, 1, 0, -1}; int R, C; inline bool in(int r, int c) { return 0 <= r and r < R and 0 <= c and c < C; } int bfs(vector<vector<int>> B) { int sr = 0, sc = 0, gr = 0, gc = 0; REP(i, R) REP(j, C) { if (B[i][j] == 2) sr = i, sc = j; if (B[i][j] == 3) gr = i, gc = j; } vector<vector<bool>> board(R, vector<bool>(C, false)); REP(i, R) REP(j, C) board[i][j] = B[i][j] == 0; board[sr][sc] = board[gr][gc] = true; queue<tuple<int, int, vector<vector<bool>>, int>> que; que.emplace(sr, sc, board, 0); while (not que.empty()) { int r, c, cnt; vector<vector<bool>> board; tie(r, c, board, cnt) = que.front(); que.pop(); if (cnt == 10) break; REP(i, 4) { int nr = r + dr[i], nc = c + dc[i]; if (in(nr, nc) and board[nr][nc]) { while (in(nr, nc) and board[nr][nc]) { if (nr == gr and nc == gc) return cnt + 1; nr += dr[i], nc += dc[i]; } if (in(nr, nc) and cnt < 9) { vector<vector<bool>> next_board = board; next_board[nr][nc] = true; que.emplace(nr - dr[i], nc - dc[i], next_board, cnt + 1); } } } } return -1; } int main() { // use scanf in CodeForces! cin.tie(0); ios_base::sync_with_stdio(false); while (true) { cin >> C >> R; if (R == 0 and C == 0) break; vector<vector<int>> B(R, vector<int>(C)); REP(i, R) REP(j, C) cin >> B[i][j]; cout << bfs(B) << endl; } return 0; }
replace
51
52
51
52
MLE
p00725
C++
Time Limit Exceeded
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str(); } typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define REP(i, n) FOR(i, 0, (n)-1) const double EPS = 1e-12; const double PI = acos(-1.0); const int INF = INT_MAX / 10; typedef vector<char> vc; typedef vector<vc> vvc; int dx[] = {0, 1, 0, -1}; int dy[] = {-1, 0, 1, 0}; struct state { vvc f; int x, y, d; state(vvc f, int x, int y, int d) : f(f), x(x), y(y), d(d){}; }; int main() { int w, h; while (cin >> w >> h, w) { vvc field(h, vc(w)); int sx, sy; REP(i, h) { REP(j, w) { cin >> field[i][j]; if (field[i][j] == '2') { sx = j, sy = i; } } } stack<state> Q; Q.push(state(field, sx, sy, 0)); int ans = INF; while (!Q.empty()) { state st = Q.top(); Q.pop(); if (st.d == 10) { continue; } REP(d, 4) { bool ok = false; int nx = st.x; int ny = st.y; first: nx += dx[d]; ny += dy[d]; if (nx < 0 || w <= nx || ny < 0 || h <= ny) { continue; } switch (st.f[ny][nx]) { case '0': case '2': ok = true; goto first; break; case '1': if (ok) { st.f[ny][nx] = '0'; Q.push(state(st.f, nx - dx[d], ny - dy[d], st.d + 1)); st.f[ny][nx] = '1'; } break; case '3': ans = min(ans, st.d + 1); break; } } } cout << (ans == INF ? -1 : ans) << endl; } return 0; }
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str(); } typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define REP(i, n) FOR(i, 0, (n)-1) const double EPS = 1e-12; const double PI = acos(-1.0); const int INF = INT_MAX / 10; typedef vector<char> vc; typedef vector<vc> vvc; int dx[] = {0, 1, 0, -1}; int dy[] = {-1, 0, 1, 0}; struct state { vvc f; int x, y, d; state(vvc f, int x, int y, int d) : f(f), x(x), y(y), d(d){}; }; int main() { int w, h; while (cin >> w >> h, w) { vvc field(h, vc(w)); int sx, sy; REP(i, h) { REP(j, w) { cin >> field[i][j]; if (field[i][j] == '2') { sx = j, sy = i; } } } stack<state> Q; Q.push(state(field, sx, sy, 0)); int ans = INF; while (!Q.empty()) { state st = Q.top(); Q.pop(); if (st.d == 10 || ans <= st.d) { continue; } REP(d, 4) { bool ok = false; int nx = st.x; int ny = st.y; first: nx += dx[d]; ny += dy[d]; if (nx < 0 || w <= nx || ny < 0 || h <= ny) { continue; } switch (st.f[ny][nx]) { case '0': case '2': ok = true; goto first; break; case '1': if (ok) { st.f[ny][nx] = '0'; Q.push(state(st.f, nx - dx[d], ny - dy[d], st.d + 1)); st.f[ny][nx] = '1'; } break; case '3': ans = min(ans, st.d + 1); break; } } } cout << (ans == INF ? -1 : ans) << endl; } return 0; }
replace
76
77
76
77
TLE
p00725
C++
Time Limit Exceeded
#include <bits/stdc++.h> typedef long long LL; #define SORT(c) sort((c).begin(), (c).end()) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) using namespace std; int dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0}; int solve(vector<vector<int>> field, int h, int w, int y, int x, int steps) { if (steps > 10) return 100; int answer = 100; REP(i, 4) { if (y + dy[i] < 0 || h <= y + dy[i] || x + dx[i] < 0 || w <= x + dx[i] || field[y + dy[i]][x + dx[i]] == 1) continue; else { int tmpx = x, tmpy = y; while (0 <= tmpy && tmpy < h && 0 <= tmpx && tmpx < w && field[tmpy][tmpx] == 0) { tmpy += dy[i]; tmpx += dx[i]; } if (tmpy < 0 || h <= tmpy || tmpx < 0 || w <= tmpx) continue; if (field[tmpy][tmpx] == 3) return 1; if (field[tmpy][tmpx] == 1) { vector<vector<int>> newfield; newfield.resize(h); REP(j, h) { newfield[j].resize(w); REP(k, w) newfield[j][k] = field[j][k]; } newfield[tmpy][tmpx] = 0; answer = min( answer, solve(newfield, h, w, tmpy - dy[i], tmpx - dx[i], steps + 1) + 1); } } } return answer; } int main(void) { for (;;) { int w, h; cin >> w >> h; if (!w && !h) return 0; vector<vector<int>> field; field.resize(h); REP(i, h) { field[i].resize(w); REP(j, w) { cin >> field[i][j]; } } int x, y; REP(i, h) { REP(j, w) { if (field[i][j] == 2) { y = i; x = j; field[i][j] = 0; } } } int answer = solve(field, h, w, y, x, 0); if (answer > 10) answer = -1; cout << answer << endl; } }
#include <bits/stdc++.h> typedef long long LL; #define SORT(c) sort((c).begin(), (c).end()) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) using namespace std; int dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0}; int solve(vector<vector<int>> field, int h, int w, int y, int x, int steps) { if (steps >= 10) return 100; int answer = 100; REP(i, 4) { if (y + dy[i] < 0 || h <= y + dy[i] || x + dx[i] < 0 || w <= x + dx[i] || field[y + dy[i]][x + dx[i]] == 1) continue; else { int tmpx = x, tmpy = y; while (0 <= tmpy && tmpy < h && 0 <= tmpx && tmpx < w && field[tmpy][tmpx] == 0) { tmpy += dy[i]; tmpx += dx[i]; } if (tmpy < 0 || h <= tmpy || tmpx < 0 || w <= tmpx) continue; if (field[tmpy][tmpx] == 3) return 1; if (field[tmpy][tmpx] == 1) { vector<vector<int>> newfield; newfield.resize(h); REP(j, h) { newfield[j].resize(w); REP(k, w) newfield[j][k] = field[j][k]; } newfield[tmpy][tmpx] = 0; answer = min( answer, solve(newfield, h, w, tmpy - dy[i], tmpx - dx[i], steps + 1) + 1); } } } return answer; } int main(void) { for (;;) { int w, h; cin >> w >> h; if (!w && !h) return 0; vector<vector<int>> field; field.resize(h); REP(i, h) { field[i].resize(w); REP(j, w) { cin >> field[i][j]; } } int x, y; REP(i, h) { REP(j, w) { if (field[i][j] == 2) { y = i; x = j; field[i][j] = 0; } } } int answer = solve(field, h, w, y, x, 0); if (answer > 10) answer = -1; cout << answer << endl; } }
replace
10
11
10
11
TLE
p00725
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctype.h> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> using namespace std; int w, h; int board[21][21]; int tesu[21][21]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; void solve(int sx, int sy) { // cout << sx << " " << sy << endl; for (int i = 0; i < 4; i++) { int nx = sx + dx[i]; int ny = sy + dy[i]; if (board[nx][ny] == 1) continue; while (nx >= 0 && ny >= 0 && nx < h && ny < w && board[nx][ny] != 1) { if (board[nx][ny] == 3) { tesu[nx][ny] = min(tesu[sx][sy] + 1, tesu[nx][ny]); break; } nx += dx[i]; ny += dy[i]; } if (board[nx][ny] == 3) continue; // cout << " " << nx << " " << ny << endl; if (nx < 0 || ny < 0 || nx >= h || ny >= w) continue; nx -= dx[i]; ny -= dy[i]; // cout << tesu[sx][sy] << endl; int tmp = tesu[nx][ny]; tesu[nx][ny] = tesu[sx][sy] + 1; board[nx + dx[i]][ny + dy[i]] = 0; solve(nx, ny); tesu[nx][ny] = tmp; board[nx + dx[i]][ny + dy[i]] = 1; } return; } int main() { while (1) { cin >> w >> h; if (w == 0 && h == 0) break; int sx, sy, gx, gy; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> board[i][j]; if (board[i][j] == 2) { sx = i; sy = j; board[i][j] = 0; } else if (board[i][j] == 3) { gx = i; gy = j; } tesu[i][j] = 11; } } tesu[sx][sy] = 0; solve(sx, sy); if (tesu[gx][gy] != 11) cout << tesu[gx][gy] << endl; else cout << -1 << endl; } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctype.h> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> using namespace std; int w, h; int board[21][21]; int tesu[21][21]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; void solve(int sx, int sy) { // cout << sx << " " << sy << endl; if (tesu[sx][sy] > 10) return; for (int i = 0; i < 4; i++) { int nx = sx + dx[i]; int ny = sy + dy[i]; if (board[nx][ny] == 1) continue; while (nx >= 0 && ny >= 0 && nx < h && ny < w && board[nx][ny] != 1) { if (board[nx][ny] == 3) { tesu[nx][ny] = min(tesu[sx][sy] + 1, tesu[nx][ny]); break; } nx += dx[i]; ny += dy[i]; } if (board[nx][ny] == 3) continue; // cout << " " << nx << " " << ny << endl; if (nx < 0 || ny < 0 || nx >= h || ny >= w) continue; nx -= dx[i]; ny -= dy[i]; // cout << tesu[sx][sy] << endl; int tmp = tesu[nx][ny]; tesu[nx][ny] = tesu[sx][sy] + 1; board[nx + dx[i]][ny + dy[i]] = 0; solve(nx, ny); tesu[nx][ny] = tmp; board[nx + dx[i]][ny + dy[i]] = 1; } return; } int main() { while (1) { cin >> w >> h; if (w == 0 && h == 0) break; int sx, sy, gx, gy; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> board[i][j]; if (board[i][j] == 2) { sx = i; sy = j; board[i][j] = 0; } else if (board[i][j] == 3) { gx = i; gy = j; } tesu[i][j] = 11; } } tesu[sx][sy] = 0; solve(sx, sy); if (tesu[gx][gy] != 11) cout << tesu[gx][gy] << endl; else cout << -1 << endl; } }
insert
24
24
24
26
TLE
p00725
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cmath> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef pair<int, int> pi; #define MK make_pair #define F first #define S second int a[50][50]; const int MX = 1e6; int search(int x, int y, int w) { int k = MX; if (w == 0) { return k; } if (a[x][y + 1] != 1) { for (int i = 1;; i++) { if (a[x][y + i] == -1) { break; } if (a[x][y + i] == 1) { a[x][y + i] = 0; k = min(k, search(x, y + i - 1, w - 1) + 1); a[x][y + i] = 1; break; } if (a[x][y + i] == 3) { return 0; } } } if (a[x + 1][y] != 1) { for (int i = 1;; i++) { if (a[x + i][y] == -1) { break; } if (a[x + i][y] == 1) { a[x + i][y] = 0; k = min(k, search(x + i - 1, y, w - 1) + 1); a[x + i][y] = 1; break; } if (a[x + i][y] == 3) { return 0; } } } if (a[x][y - 1] != 1) { for (int i = -1;; i--) { if (a[x][y + i] == -1) { break; } if (a[x][y + i] == 1) { a[x][y + i] = 0; k = min(k, search(x, y + i + 1, w - 1) + 1); a[x][y + i] = 1; break; } if (a[x][y + i] == 3) { return 0; } } } if (a[x - 1][y] != 1) { for (int i = -1;; i--) { if (a[x + i][y] == -1) { break; } if (a[x + i][y] == 1) { a[x + i][y] = 0; k = min(k, search(x + i + 1, y, w - 1) + 1); a[x + i][y] = 1; break; } if (a[x + i][y] == 3) { return 0; } } } return k; } int main() { while (1) { int w, h; cin >> w >> h; fill(&a[0][0], &a[49][50], -1); int x, y; for (int i = 1; i <= h; i++) { for (int t = 1; t <= w; t++) { cin >> a[i][t]; if (a[i][t] == 2) { x = i; y = t; } } } int ans = search(x, y, 10); cout << (ans == MX ? -1 : ans + 1) << endl; } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef pair<int, int> pi; #define MK make_pair #define F first #define S second int a[50][50]; const int MX = 1e6; int search(int x, int y, int w) { int k = MX; if (w == 0) { return k; } if (a[x][y + 1] != 1) { for (int i = 1;; i++) { if (a[x][y + i] == -1) { break; } if (a[x][y + i] == 1) { a[x][y + i] = 0; k = min(k, search(x, y + i - 1, w - 1) + 1); a[x][y + i] = 1; break; } if (a[x][y + i] == 3) { return 0; } } } if (a[x + 1][y] != 1) { for (int i = 1;; i++) { if (a[x + i][y] == -1) { break; } if (a[x + i][y] == 1) { a[x + i][y] = 0; k = min(k, search(x + i - 1, y, w - 1) + 1); a[x + i][y] = 1; break; } if (a[x + i][y] == 3) { return 0; } } } if (a[x][y - 1] != 1) { for (int i = -1;; i--) { if (a[x][y + i] == -1) { break; } if (a[x][y + i] == 1) { a[x][y + i] = 0; k = min(k, search(x, y + i + 1, w - 1) + 1); a[x][y + i] = 1; break; } if (a[x][y + i] == 3) { return 0; } } } if (a[x - 1][y] != 1) { for (int i = -1;; i--) { if (a[x + i][y] == -1) { break; } if (a[x + i][y] == 1) { a[x + i][y] = 0; k = min(k, search(x + i + 1, y, w - 1) + 1); a[x + i][y] = 1; break; } if (a[x + i][y] == 3) { return 0; } } } return k; } int main() { while (1) { int w, h; cin >> w >> h; if (w == 0 && h == 0) { break; } fill(&a[0][0], &a[49][50], -1); int x, y; for (int i = 1; i <= h; i++) { for (int t = 1; t <= w; t++) { cin >> a[i][t]; if (a[i][t] == 2) { x = i; y = t; } } } int ans = search(x, y, 10); cout << (ans == MX ? -1 : ans + 1) << endl; } return 0; }
insert
99
99
99
102
TLE
p00725
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; int w, h; int sx, sy; int res; int table[30][30]; int dx[] = {0, 1, 0, -1}; int dy[] = {-1, 0, 1, 0}; void dfs(int x, int y, int cnt) { if (cnt >= res) return; for (int dir = 0; dir < 4; dir++) { int nx = x + dx[dir], ny = y + dy[dir]; if (nx < 0 || w <= nx || ny < 0 || h <= ny) continue; //????????? else if (table[ny][nx] == 1) continue; //???????§???????????????? else if (table[ny][nx] == 3) { res = cnt; return; } //??´?????? while (true) { nx += dx[dir]; ny += dy[dir]; if (nx < 0 || w <= nx || ny < 0 || h <= ny) break; //????????? else if (table[ny][nx] == 1) { table[ny][nx] = 0; //?????????????£???? dfs(nx - dx[dir], ny - dy[dir], cnt + 1); //?¬????????????? table[ny][nx] = 1; //?????????????£??????????????????? break; } else if (table[ny][nx] == 3) { res = cnt; //??´?????? return; } } } } int main() { while (cin >> w >> h, w) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> table[i][j]; if (table[i][j] == 2) { sx = j; sy = i; } } } res = 100; dfs(sx, sy, 1); if (res > 10) res = -1; cout << res << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; int w, h; int sx, sy; int res; int table[30][30]; int dx[] = {0, 1, 0, -1}; int dy[] = {-1, 0, 1, 0}; void dfs(int x, int y, int cnt) { if (cnt >= res) return; for (int dir = 0; dir < 4; dir++) { int nx = x + dx[dir], ny = y + dy[dir]; if (nx < 0 || w <= nx || ny < 0 || h <= ny) continue; //????????? else if (table[ny][nx] == 1) continue; //???????§???????????????? else if (table[ny][nx] == 3) { res = cnt; return; } //??´?????? while (true) { nx += dx[dir]; ny += dy[dir]; if (nx < 0 || w <= nx || ny < 0 || h <= ny) break; //????????? else if (table[ny][nx] == 1) { table[ny][nx] = 0; //?????????????£???? dfs(nx - dx[dir], ny - dy[dir], cnt + 1); //?¬????????????? table[ny][nx] = 1; //?????????????£??????????????????? break; } else if (table[ny][nx] == 3) { res = cnt; //??´?????? return; } } } } int main() { while (cin >> w >> h, w) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> table[i][j]; if (table[i][j] == 2) { sx = j; sy = i; } } } res = 11; dfs(sx, sy, 1); if (res > 10) res = -1; cout << res << endl; } return 0; }
replace
71
72
71
72
TLE
p00725
C++
Time Limit Exceeded
#include <cstdio> #include <vector> using namespace std; #define MAX_H 20 #define MAX_W 20 #define INF (1 << 28) int w, h; int F[MAX_H][MAX_W]; int G[MAX_H][MAX_W]; int gy, gx; int y, x; int py, px; void debug(); const int dy[] = {1, 0, -1, 0}; const int dx[] = {0, 1, 0, -1}; void move(int d) { // printf(" %d\n", d); while (F[y][x] == 0) { x += dx[d]; y += dy[d]; } if (F[y][x] == 3) return; F[y][x] = 0; y -= dy[d]; x -= dx[d]; } bool can_move(int d) { if (F[y + dy[d]][x + dx[d]] == 1) return false; int _y = y; int _x = x; while (F[_y][_x] == 0) { _y += dy[d]; _x += dx[d]; if (_y < 0 || h <= _y || _x < 0 || w <= _x) return false; } return true; } void debug() { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { printf("%d ", F[i][j]); } printf("\n"); } } int solve(int cur) { if (y == gy && x == gx) return 0; if (cur >= 10) return INF; int min_cost = INF; for (int i = 0; i < 4; i++) { if (can_move(i)) { int backup[h][w]; int py = y, px = x; for (int j = 0; j < h; j++) for (int k = 0; k < w; k++) backup[j][k] = F[j][k]; move(i); min_cost = min(min_cost, solve(cur) + 1); y = py, x = px; for (int j = 0; j < h; j++) for (int k = 0; k < w; k++) F[j][k] = backup[j][k]; } } return min_cost; } int main() { while (scanf("%d %d", &w, &h), w || h) { int sy, sx; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { scanf("%d", &F[i][j]); if (F[i][j] == 2) { y = i, x = j; F[i][j] = 0; } if (F[i][j] == 3) gy = i, gx = j; } int ans = solve(0); printf("%d\n", (ans > 10 ? -1 : ans)); } return 0; }
#include <cstdio> #include <vector> using namespace std; #define MAX_H 20 #define MAX_W 20 #define INF (1 << 28) int w, h; int F[MAX_H][MAX_W]; int G[MAX_H][MAX_W]; int gy, gx; int y, x; int py, px; void debug(); const int dy[] = {1, 0, -1, 0}; const int dx[] = {0, 1, 0, -1}; void move(int d) { // printf(" %d\n", d); while (F[y][x] == 0) { x += dx[d]; y += dy[d]; } if (F[y][x] == 3) return; F[y][x] = 0; y -= dy[d]; x -= dx[d]; } bool can_move(int d) { if (F[y + dy[d]][x + dx[d]] == 1) return false; int _y = y; int _x = x; while (F[_y][_x] == 0) { _y += dy[d]; _x += dx[d]; if (_y < 0 || h <= _y || _x < 0 || w <= _x) return false; } return true; } void debug() { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { printf("%d ", F[i][j]); } printf("\n"); } } int solve(int cur) { if (y == gy && x == gx) return 0; if (cur >= 10) return INF; int min_cost = INF; for (int i = 0; i < 4; i++) { if (can_move(i)) { int backup[h][w]; int py = y, px = x; for (int j = 0; j < h; j++) for (int k = 0; k < w; k++) backup[j][k] = F[j][k]; move(i); min_cost = min(min_cost, solve(cur + 1) + 1); y = py, x = px; for (int j = 0; j < h; j++) for (int k = 0; k < w; k++) F[j][k] = backup[j][k]; } } return min_cost; } int main() { while (scanf("%d %d", &w, &h), w || h) { int sy, sx; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { scanf("%d", &F[i][j]); if (F[i][j] == 2) { y = i, x = j; F[i][j] = 0; } if (F[i][j] == 3) gy = i, gx = j; } int ans = solve(0); printf("%d\n", (ans > 10 ? -1 : ans)); } return 0; }
replace
71
72
71
72
TLE
p00725
C++
Runtime Error
#include <stdio.h> #include <string.h> int gx, gy, h, w; int cost[20][20]; const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; void solve(int x, int y, int field[20][20]) { static int count = 0; int i, j, flag; if (cost[y][x] > count || cost[y][x] == -1) { cost[y][x] = count; if (field[y][x] == 3) return; } if (count < 10) { for (i = 0; i < 4; i++) { flag = 0; for (j = 1; x + dx[i] * j >= 0 && x + dx[i] * j < w && y + dy[i] * j >= 0 && y + dy[i] * j < h; j++) { if (field[y + dy[i] * j][x + dx[i] * j]) { flag = 1; break; } } if (field[y + dy[i] * j][x + dx[i] * j] == 3) { count++; solve(x + dx[i] * j, y + dy[i] * j, field); count--; } else if (j != 1 && flag) { field[y + dy[i] * j][x + dx[i] * j] = 0; count++; solve(x + dx[i] * (j - 1), y + dy[i] * (j - 1), field); field[y + dy[i] * j][x + dx[i] * j] = 1; count--; } } } } int main(void) { int i, j, field[20][20], sx, sy; while (1) { scanf("%d%d", &w, &h); if (w == 0 && h == 0) break; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) scanf("%d", &field[i][j]); } for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { if (field[i][j] == 2) { sy = i, sx = j; field[i][j] = 0; } else if (field[i][j] == 3) gy = i, gx = j; } } memset(cost, -1, sizeof(cost)); solve(sx, sy, field); printf("%d\n", cost[gy][gx]); } return 0; }
#include <stdio.h> #include <string.h> int gx, gy, h, w; int cost[20][20]; const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; void solve(int x, int y, int field[20][20]) { static int count = 0; int i, j, flag; if (cost[y][x] > count || cost[y][x] == -1) { cost[y][x] = count; if (field[y][x] == 3) return; } if (count < 10) { for (i = 0; i < 4; i++) { flag = 0; for (j = 1; x + dx[i] * j >= 0 && x + dx[i] * j < w && y + dy[i] * j >= 0 && y + dy[i] * j < h; j++) { if (field[y + dy[i] * j][x + dx[i] * j]) { flag = 1; break; } } if (flag && field[y + dy[i] * j][x + dx[i] * j] == 3) { count++; solve(x + dx[i] * j, y + dy[i] * j, field); count--; } else if (j != 1 && flag) { field[y + dy[i] * j][x + dx[i] * j] = 0; count++; solve(x + dx[i] * (j - 1), y + dy[i] * (j - 1), field); field[y + dy[i] * j][x + dx[i] * j] = 1; count--; } } } } int main(void) { int i, j, field[20][20], sx, sy; while (1) { scanf("%d%d", &w, &h); if (w == 0 && h == 0) break; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) scanf("%d", &field[i][j]); } for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { if (field[i][j] == 2) { sy = i, sx = j; field[i][j] = 0; } else if (field[i][j] == 3) gy = i, gx = j; } } memset(cost, -1, sizeof(cost)); solve(sx, sy, field); printf("%d\n", cost[gy][gx]); } return 0; }
replace
26
27
26
27
0
p00725
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <vector> using namespace std; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int w, h; int sx, sy; int ans = 100; bool outofrange(int y, int x) { if (y < 0 || y >= h || x < 0 || x >= w) { return true; } else { return false; } } void dfs(vector<vector<int>> grid, int sy, int sx, int depth) { if (depth + 1 >= ans) { return; } for (int i = 0; i < 4; i++) { int ny = sy + dy[i]; int nx = sx + dx[i]; if (outofrange(ny, nx) == true || grid[ny][nx] == 1) { continue; } while (outofrange(ny, nx) == false) { if (grid[ny][nx] == 3) { ans = min(ans, depth + 1); return; } if (grid[ny][nx] == 1) { grid[ny][nx] = 0; dfs(grid, ny - dy[i], nx - dx[i], depth + 1); grid[ny][nx] = 1; break; } ny += dy[i]; nx += dx[i]; } } } int main() { while (true) { cin >> w >> h; if (w == 0 && h == 0) { break; } ans = 100; vector<vector<int>> grid(h, vector<int>(w)); pair<int, int> start, goal; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { cin >> grid[y][x]; if (grid[y][x] == 2) { sy = y; sx = x; } } } dfs(grid, sy, sx, 0); cout << (ans <= 10 ? ans : -1) << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int w, h; int sx, sy; int ans = 100; bool outofrange(int y, int x) { if (y < 0 || y >= h || x < 0 || x >= w) { return true; } else { return false; } } void dfs(vector<vector<int>> grid, int sy, int sx, int depth) { if (depth + 1 >= ans || depth + 1 > 10) { return; } for (int i = 0; i < 4; i++) { int ny = sy + dy[i]; int nx = sx + dx[i]; if (outofrange(ny, nx) == true || grid[ny][nx] == 1) { continue; } while (outofrange(ny, nx) == false) { if (grid[ny][nx] == 3) { ans = min(ans, depth + 1); return; } if (grid[ny][nx] == 1) { grid[ny][nx] = 0; dfs(grid, ny - dy[i], nx - dx[i], depth + 1); grid[ny][nx] = 1; break; } ny += dy[i]; nx += dx[i]; } } } int main() { while (true) { cin >> w >> h; if (w == 0 && h == 0) { break; } ans = 100; vector<vector<int>> grid(h, vector<int>(w)); pair<int, int> start, goal; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { cin >> grid[y][x]; if (grid[y][x] == 2) { sy = y; sx = x; } } } dfs(grid, sy, sx, 0); cout << (ans <= 10 ? ans : -1) << endl; } return 0; }
replace
20
21
20
21
TLE
p00725
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define MOD 1000000007 #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3f #define EPS (1e-10) #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; typedef pair<int, int> P; struct st { int f[20][20], cnt, x, y, id; }; int dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1}; int main() { st in; int w, h; while (scanf("%d%d", &w, &h), w) { rep(i, h) rep(j, w) { scanf("%d", &in.f[i][j]); if (in.f[i][j] == 2) in.x = i, in.y = j; } in.cnt = 0; in.id = -1; queue<st> que; que.push(in); int Min = INT_MAX; while (!que.empty()) { st s = que.front(); que.pop(); if (s.f[s.x][s.y] == 3) { Min = min(Min, s.cnt); continue; } if (s.id == -1) { rep(i, 4) { int nx = s.x + dx[i], ny = s.y + dy[i]; if (0 <= nx && nx < h && 0 <= ny && ny < w && s.f[nx][ny] != 1) { st p = s; p.cnt++; p.x = nx; p.y = ny; p.id = i; if (p.cnt <= 10) que.push(p); } } } else { int nx = s.x + dx[s.id], ny = s.y + dy[s.id]; if (0 <= nx && nx < h && 0 <= ny && ny < w) { if (s.f[nx][ny] == 1) { st p = s; p.f[nx][ny] = 0; p.id = -1; que.push(p); } else { st p = s; p.x = nx; p.y = ny; que.push(p); } } } } if (Min == INT_MAX) puts("-1"); else printf("%d\n", Min); } }
#include <bits/stdc++.h> #define MOD 1000000007 #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3f #define EPS (1e-10) #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; typedef pair<int, int> P; struct st { int f[20][20], cnt, x, y, id; }; int dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1}; int main() { st in; int w, h; while (scanf("%d%d", &w, &h), w) { rep(i, h) rep(j, w) { scanf("%d", &in.f[i][j]); if (in.f[i][j] == 2) in.x = i, in.y = j; } in.cnt = 0; in.id = -1; queue<st> que; que.push(in); int Min = INT_MAX; while (!que.empty()) { st s = que.front(); que.pop(); if (s.f[s.x][s.y] == 3) { Min = min(Min, s.cnt); continue; } if (s.id == -1) { rep(i, 4) { int nx = s.x + dx[i], ny = s.y + dy[i]; if (0 <= nx && nx < h && 0 <= ny && ny < w && s.f[nx][ny] != 1) { st p = s; p.cnt++; p.x = nx; p.y = ny; p.id = i; if (p.cnt <= 10 && p.cnt < Min) que.push(p); } } } else { int nx = s.x + dx[s.id], ny = s.y + dy[s.id]; if (0 <= nx && nx < h && 0 <= ny && ny < w) { if (s.f[nx][ny] == 1) { st p = s; p.f[nx][ny] = 0; p.id = -1; que.push(p); } else { st p = s; p.x = nx; p.y = ny; que.push(p); } } } } if (Min == INT_MAX) puts("-1"); else printf("%d\n", Min); } }
replace
44
45
44
45
TLE
p00725
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; typedef long long ll; #define rep(i, n) for (int i = 0; i < n; i++) const ll MOD = 1000000007; int ans = 100000; int h, w; int dx[] = {0, -1, 0, 1}; int sy, sx, gy, gx; int dy[] = {-1, 0, 1, 0}; int moji[35][35]; void dfs(int y, int x, int cnt) // 1,,,top 2...left 3...down 4...right { if (cnt + 1 >= ans) return; // cout<<"nct :"<<cnt<<endl; for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (ny >= 0 && nx >= 0 && h > ny && w > nx && (moji[ny][nx] != 1)) { while (ny >= 0 && nx >= 0 && h > ny && w > nx) { if (moji[ny][nx] == 3) { ans = min(ans, cnt + 1); return; } // if(ny==gy&&nx==gx)return; // if(nx<0||ny<0||nx>=w||ny>=h)return -1; if (moji[ny][nx] == 1) { moji[ny][nx] = 0; // if(i==0) { dfs(ny - dy[i], nx - dx[i], cnt + 1); } moji[ny][nx] = 1; break; } nx += dx[i]; ny += dy[i]; } } // A:; } return; } int main() { while (cin >> w >> h, w) { ans = 100000; rep(i, 35) rep(j, 35) moji[i][j] = 0; rep(i, h) rep(j, w) { cin >> moji[i][j]; if (moji[i][j] == 2) { sx = j; sy = i; moji[i][j] = 0; } /*else if(moji[i][j]==3) { gx=j; gy=i; moji[i][j]=0; } */ } dfs(sy, sx, 0); cout << (ans <= 10 ? ans : -1) << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; typedef long long ll; #define rep(i, n) for (int i = 0; i < n; i++) const ll MOD = 1000000007; int ans = 100000; int h, w; int dx[] = {0, -1, 0, 1}; int sy, sx, gy, gx; int dy[] = {-1, 0, 1, 0}; int moji[35][35]; void dfs(int y, int x, int cnt) // 1,,,top 2...left 3...down 4...right { if (cnt + 1 >= ans) return; // cout<<"nct :"<<cnt<<endl; for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (ny >= 0 && nx >= 0 && h > ny && w > nx && (moji[ny][nx] != 1)) { while (ny >= 0 && nx >= 0 && h > ny && w > nx) { if (moji[ny][nx] == 3) { ans = min(ans, cnt + 1); return; } // if(ny==gy&&nx==gx)return; // if(nx<0||ny<0||nx>=w||ny>=h)return -1; if (moji[ny][nx] == 1) { moji[ny][nx] = 0; // if(i==0) { dfs(ny - dy[i], nx - dx[i], cnt + 1); } moji[ny][nx] = 1; break; } nx += dx[i]; ny += dy[i]; } } // A:; } return; } int main() { while (cin >> w >> h, w) { ans = 11; rep(i, 35) rep(j, 35) moji[i][j] = 0; rep(i, h) rep(j, w) { cin >> moji[i][j]; if (moji[i][j] == 2) { sx = j; sy = i; moji[i][j] = 0; } /*else if(moji[i][j]==3) { gx=j; gy=i; moji[i][j]=0; } */ } dfs(sy, sx, 0); cout << (ans <= 10 ? ans : -1) << endl; } return 0; }
replace
55
56
55
56
TLE
p00725
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; #define rep(i, n) for (int i = 0; i < n; i++) #define range(i, a, n) for (int i = a; i < n; i++) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define INF 1e9 #define EPS 1e-9 #define MOD (1e9 + 7) void put(string d) {} template <class H, class... T> void put(string d, H &h, T &...t) { cout << h; if (sizeof...(t)) cout << d; put(d, t...); } template <class T> void puti(T &a, string d = " ") { bool f = 1; for (auto &_ : a) cout << (exchange(f, 0) ? "" : d) << _; cout << endl; } template <class T> void putii(T &a, string d = " ") { for (auto &_ : a) puti(_, d); } int vx[] = {-1, 0, 1, 0}; int vy[] = {0, 1, 0, -1}; int main() { int w, h; while (cin >> w >> h, w) { int sx, sy, gx, gy; vvi a; a = vvi(h, vi(w, 0)); rep(i, h) rep(j, w) { int t; cin >> t; if (t == 2) sy = i, sx = j; if (t == 3) gy = i, gx = j; if (t == 1) a[i][j] = 1; } using data = tuple<int, int, int, vvi>; queue<data> q; q.emplace(0, sy, sx, a); while (!q.empty()) { int t, y, x, nx, ny; vvi f; tie(t, y, x, f) = q.front(); q.pop(); // cout<<t<<","<<y<<","<<x<<endl; // putii(f); if (t == 10) { cout << -1 << endl; goto fin; } rep(i, 4) { nx = x + vx[i], ny = y + vy[i]; if (!(ny >= h or ny < 0 or nx >= w or nx < 0) and f[ny][nx] == 1) continue; while (1) { if (gx == nx and gy == ny) { cout << t + 1 << endl; goto fin; } if (ny >= h or ny < 0 or nx >= w or nx < 0) break; if (f[ny][nx] == 1) { auto nf = f; nf[ny][nx] = 0, ny -= vy[i], nx -= vx[i]; q.emplace(t + 1, ny, nx, nf); break; } nx += vx[i], ny += vy[i]; } } } cout << -1 << endl; fin:; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<bool>; using vvi = vector<vi>; using pii = pair<int, int>; #define rep(i, n) for (int i = 0; i < n; i++) #define range(i, a, n) for (int i = a; i < n; i++) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define INF 1e9 #define EPS 1e-9 #define MOD (1e9 + 7) void put(string d) {} template <class H, class... T> void put(string d, H &h, T &...t) { cout << h; if (sizeof...(t)) cout << d; put(d, t...); } template <class T> void puti(T &a, string d = " ") { bool f = 1; for (auto &_ : a) cout << (exchange(f, 0) ? "" : d) << _; cout << endl; } template <class T> void putii(T &a, string d = " ") { for (auto &_ : a) puti(_, d); } int vx[] = {-1, 0, 1, 0}; int vy[] = {0, 1, 0, -1}; int main() { int w, h; while (cin >> w >> h, w) { int sx, sy, gx, gy; vvi a; a = vvi(h, vi(w, 0)); rep(i, h) rep(j, w) { int t; cin >> t; if (t == 2) sy = i, sx = j; if (t == 3) gy = i, gx = j; if (t == 1) a[i][j] = 1; } using data = tuple<int, int, int, vvi>; queue<data> q; q.emplace(0, sy, sx, a); while (!q.empty()) { int t, y, x, nx, ny; vvi f; tie(t, y, x, f) = q.front(); q.pop(); // cout<<t<<","<<y<<","<<x<<endl; // putii(f); if (t == 10) { cout << -1 << endl; goto fin; } rep(i, 4) { nx = x + vx[i], ny = y + vy[i]; if (!(ny >= h or ny < 0 or nx >= w or nx < 0) and f[ny][nx] == 1) continue; while (1) { if (gx == nx and gy == ny) { cout << t + 1 << endl; goto fin; } if (ny >= h or ny < 0 or nx >= w or nx < 0) break; if (f[ny][nx] == 1) { auto nf = f; nf[ny][nx] = 0, ny -= vy[i], nx -= vx[i]; q.emplace(t + 1, ny, nx, nf); break; } nx += vx[i], ny += vy[i]; } } } cout << -1 << endl; fin:; } return 0; }
replace
3
4
3
4
MLE
p00726
C++
Runtime Error
#include <algorithm> #include <cctype> #include <iostream> #include <string> #define range(i, a, b) for (int(i) = a; (i) < (b); (i)++) #define rep(i, n) range(i, 0, n) using namespace std; const int imax = 1234567; int n; string S; size_t cur = 0; int digit(); int number(); string expression(); int digit() { return S[cur++] - '0'; } int number() { int res = digit(); while (isdigit(S[cur])) { res = res * 10 + digit(); } return res; } string expression(void) { string rec = ""; while (cur < n) { // cout << cur << endl; if (rec.size() >= imax) return rec; if (isupper(S[cur])) { rec += S[cur++]; } else if (isdigit(S[cur])) { int r = number(); string c = ""; if (S[cur] == '(') { cur++; c = expression(); } else c = S[cur++]; rep(i, r) { if (rec.size() >= imax) return rec; rec += c; } } else if (S[cur] == ')') { cur++; return rec; } } return rec; } int main(void) { int i; while (cin >> S >> i) { if (S == "0") break; n = S.size(); cur = 0; string res = expression(); if (res[i] == '\0') cout << 0 << endl; else cout << res[i] << endl; } return 0; }
#include <algorithm> #include <cctype> #include <iostream> #include <string> #define range(i, a, b) for (int(i) = a; (i) < (b); (i)++) #define rep(i, n) range(i, 0, n) using namespace std; const int imax = 1234567; int n; string S; size_t cur = 0; int digit(); int number(); string expression(); int digit() { return S[cur++] - '0'; } int number() { int res = digit(); while (isdigit(S[cur])) { res = res * 10 + digit(); } return res; } string expression(void) { string rec = ""; while (cur < n) { // cout << cur << endl; if (rec.size() >= imax) return rec; if (isupper(S[cur])) { rec += S[cur++]; } else if (isdigit(S[cur])) { int r = number(); string c = ""; if (S[cur] == '(') { cur++; c = expression(); } else c = S[cur++]; rep(i, r) { if (rec.size() >= imax) return rec; rec += c; } } else if (S[cur] == ')') { cur++; return rec; } } return rec; } int main(void) { int i; while (cin >> S >> i) { if (S == "0") break; n = S.size(); cur = 0; string res = expression(); if (i >= res.size()) cout << 0 << endl; else cout << res[i] << endl; } return 0; }
replace
73
74
73
74
0
p00726
C++
Runtime Error
#include <iostream> using namespace std; typedef long long ll; string str; int pos; int loc; ll now; int number() { int res = 0; while (pos < str.size() && isdigit(str[pos])) { res = res * 10 + (int)(str[pos] - '0'); ++pos; } return res; } string alp() { string res = ""; while (pos < str.size() && 'A' <= str[pos] && str[pos] <= 'Z') { res += str[pos]; ++pos; } return res; } string expr2() { string res = ""; while (pos < str.size() && str[pos] != ')') { int n = 1; if (isdigit(str[pos])) { n = number(); } string a; if (str[pos] == '(') { ++pos; a = expr2(); ++pos; } else { a = alp(); } for (int i = 0; i < n; ++i) { res += a; } } return res; } ll expr() { ll res = 0; while (pos < str.size() && str[pos] != ')') { int n = 1; if (isdigit(str[pos])) { n = number(); } int op = pos; ll l; if (str[pos] == '(') { ++pos; l = expr(); ++pos; now -= l; } else { l = alp().size(); } if (now <= loc && loc < now + l * n) { pos = op; throw expr2()[(loc - now) % l]; } now += l * n; res += l * n; if (now > loc + 1) now = loc + 1; if (res > loc + 1) res = loc + 1; } return res; } char parse() { pos = 0; try { now = 0; pos = 0; expr(); } catch (char c) { return c; } return '0'; } int main() { while (cin >> str >> loc, str != "0" || loc != 0) { cout << parse() << endl; } }
#include <iostream> using namespace std; typedef long long ll; string str; int pos; int loc; ll now; int number() { int res = 0; while (pos < str.size() && isdigit(str[pos])) { res = res * 10 + (int)(str[pos] - '0'); ++pos; } return res; } string alp() { string res = ""; while (pos < str.size() && 'A' <= str[pos] && str[pos] <= 'Z') { res += str[pos]; ++pos; } return res; } string expr2() { string res = ""; while (pos < str.size() && str[pos] != ')') { int n = 1; if (isdigit(str[pos])) { n = number(); } string a; if (str[pos] == '(') { ++pos; a = expr2(); ++pos; } else { a = alp(); } for (int i = 0; i < n; ++i) { res += a; } } return res; } ll expr() { ll res = 0; while (pos < str.size() && str[pos] != ')') { int n = 1; if (isdigit(str[pos])) { n = number(); } int op = pos; ll l; if (str[pos] == '(') { ++pos; l = expr(); ++pos; now -= l; } else { l = alp().size(); } if (now <= loc && loc < now + l * n) { pos = op; string s; if (str[pos] == '(') { ++pos; s = expr2(); ++pos; } else { s = alp(); } throw s[(loc - now) % l]; } now += l * n; res += l * n; if (now > loc + 1) now = loc + 1; if (res > loc + 1) res = loc + 1; } return res; } char parse() { pos = 0; try { now = 0; pos = 0; expr(); } catch (char c) { return c; } return '0'; } int main() { while (cin >> str >> loc, str != "0" || loc != 0) { cout << parse() << endl; } }
replace
72
73
72
81
0
p00726
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define reps(i, j, k) for (int i = j; i < k; i++) #define rep(i, j) reps(i, 0, j) #define InumF 1 << 30 #define fr first #define sc second const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; typedef pair<int, int> Pii; int main() { string str; int n; while (true) { cin >> str; scanf("%d", &n); if (str == "0" && n == 0) break; int len = str.size(); string numew = ""; numew += str[0]; reps(i, 1, len) { if (isdigit(str[i - 1]) && isalpha(str[i])) { numew += "("; numew += str[i]; numew += ")"; } else { numew += str[i]; } } str = numew; stack<int> S; vector<int> par(128, -1); vector<int> num(128, -1); rep(i, str.size()) { if (isdigit(str[i])) { string tmp = ""; int j; for (j = i; str[j] != '('; j++) { tmp += str[j]; } num[i] = atoi(tmp.c_str()); i = j; } } rep(i, str.size()) { if (str[i] == '(') { for (int j = i - 1; j >= 0; j--) { if (num[j] != -1) { S.push(j); break; } } } if (str[i] == ')') { par[i] = S.top(); S.pop(); } // cout << num[i] << " "; } int now = 0; int cnt = 0; vector<int> nex = num; while (true) { // cout<<now<<"("<<cnt<<")"<<" "; if (isalpha(str[now])) { // cout<<str[now]; if (cnt == n) { cout << str[now] << endl; break; } now++, cnt++; } else if (isdigit(str[now])) { nex[now]--; if (nex[now] == 0) nex[now] = num[now]; int j = now; while (true) { if (str[j] == '(') break; j++; } j++; now = j; } else if (str[now] == '(') now++; else if (str[now] == ')') { if (nex[par[now]] == num[par[now]]) now++; else now = par[now]; } if (now == str.size()) { cout << 0 << endl; break; } } } return 0; }
#include <bits/stdc++.h> using namespace std; #define reps(i, j, k) for (int i = j; i < k; i++) #define rep(i, j) reps(i, 0, j) #define InumF 1 << 30 #define fr first #define sc second const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; typedef pair<int, int> Pii; int main() { string str; int n; while (true) { cin >> str; scanf("%d", &n); if (str == "0" && n == 0) break; int len = str.size(); string numew = ""; numew += str[0]; reps(i, 1, len) { if (isdigit(str[i - 1]) && isalpha(str[i])) { numew += "("; numew += str[i]; numew += ")"; } else { numew += str[i]; } } str = numew; stack<int> S; vector<int> par(1280, -1); vector<int> num(1280, -1); rep(i, str.size()) { if (isdigit(str[i])) { string tmp = ""; int j; for (j = i; str[j] != '('; j++) { tmp += str[j]; } num[i] = atoi(tmp.c_str()); i = j; } } rep(i, str.size()) { if (str[i] == '(') { for (int j = i - 1; j >= 0; j--) { if (num[j] != -1) { S.push(j); break; } } } if (str[i] == ')') { par[i] = S.top(); S.pop(); } // cout << num[i] << " "; } int now = 0; int cnt = 0; vector<int> nex = num; while (true) { // cout<<now<<"("<<cnt<<")"<<" "; if (isalpha(str[now])) { // cout<<str[now]; if (cnt == n) { cout << str[now] << endl; break; } now++, cnt++; } else if (isdigit(str[now])) { nex[now]--; if (nex[now] == 0) nex[now] = num[now]; int j = now; while (true) { if (str[j] == '(') break; j++; } j++; now = j; } else if (str[now] == '(') now++; else if (str[now] == ')') { if (nex[par[now]] == num[par[now]]) now++; else now = par[now]; } if (now == str.size()) { cout << 0 << endl; break; } } } return 0; }
replace
33
35
33
35
0
p00726
C++
Memory Limit Exceeded
#include "bits/stdc++.h" using namespace std; typedef vector<int> vi; 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 // string solve(int times,string s){ // string ret=""; // string part=""; // // int len=s.size(); // // rep(i,len){ // if( isalpha(s[i]) ){ // part+=s[i]; // } // else if( isdigit(s[i]) ){ // string num; // for(int j=i;j<len;j++){ // if(j>1000010)return part; // if( isdigit(s[j]) )num+=s[j]; // else{ // break; // } // } // i+=num.size(); // // if( s[i]!='(' ){ // part+=solve( stoi(num) , string( 1,s[i] ) ); // }else{ // i++; // string repeat=""; // int brackets=1; // for(int j=i;j<len;j++){ // if( s[j]==')' ){ // repeat+=')'; // brackets--; // // }else if( s[j]=='(' ){ // brackets++; // repeat+='('; // // }else{ // if(j==len-1)return "invalid2"; // // repeat+=s[j]; // } // if(brackets<=0)break; // } // // repeat.erase(repeat.size()-1); // i+=repeat.size(); // // part+=solve( stoi(num) , repeat ); // } // // // }else return "invalid1"; // } // // // rep(i,times)ret+=part; // //// cout<<ret<<endl; // return ret; //} string getDigit(int n, string s) { string ret = ""; for (int i = n; i < s.size(); i++) { if (isdigit(s[i])) ret += s[i]; else break; } return ret; } string solve(int num, string s) { // cout<<s<<endl; string part = ""; int len = s.size(); rep(i, len) { if (isalpha(s[i])) part += s[i]; if (part.size() > 1000100) return part; else if (isdigit(s[i])) { string deg = getDigit(i, s); i += deg.size(); if (s[i] == '(') { int bra = 1; i++; string repeat = ""; for (int j = i; j < len; j++) { if (s[j] == '(') bra++; if (s[j] == ')') bra--; if (bra == 0) break; else { repeat += s[j]; if (repeat.size() > 1000100) break; } } part += solve(stoi(deg), repeat); if (part.size() > 1000100) return part; i += repeat.size(); } else { part += solve(stoi(deg), string(1, s[i])); } } } if (part.size() > 1000100) return part; string ret = ""; rep(i, num) ret += part; return ret; } int main() { string s; int n; while (cin >> s >> n) { if (s == "0" && n == 0) break; string res = solve(1, s); if (res.size() <= n) cout << "0" << endl; else cout << res[n] << endl; // cout<<res.size()<<endl; } return 0; }
#include "bits/stdc++.h" using namespace std; typedef vector<int> vi; 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 // string solve(int times,string s){ // string ret=""; // string part=""; // // int len=s.size(); // // rep(i,len){ // if( isalpha(s[i]) ){ // part+=s[i]; // } // else if( isdigit(s[i]) ){ // string num; // for(int j=i;j<len;j++){ // if(j>1000010)return part; // if( isdigit(s[j]) )num+=s[j]; // else{ // break; // } // } // i+=num.size(); // // if( s[i]!='(' ){ // part+=solve( stoi(num) , string( 1,s[i] ) ); // }else{ // i++; // string repeat=""; // int brackets=1; // for(int j=i;j<len;j++){ // if( s[j]==')' ){ // repeat+=')'; // brackets--; // // }else if( s[j]=='(' ){ // brackets++; // repeat+='('; // // }else{ // if(j==len-1)return "invalid2"; // // repeat+=s[j]; // } // if(brackets<=0)break; // } // // repeat.erase(repeat.size()-1); // i+=repeat.size(); // // part+=solve( stoi(num) , repeat ); // } // // // }else return "invalid1"; // } // // // rep(i,times)ret+=part; // //// cout<<ret<<endl; // return ret; //} string getDigit(int n, string s) { string ret = ""; for (int i = n; i < s.size(); i++) { if (isdigit(s[i])) ret += s[i]; else break; } return ret; } string solve(int num, string s) { // cout<<s<<endl; string part = ""; int len = s.size(); rep(i, len) { if (isalpha(s[i])) part += s[i]; if (part.size() > 1000100) return part; else if (isdigit(s[i])) { string deg = getDigit(i, s); i += deg.size(); if (s[i] == '(') { int bra = 1; i++; string repeat = ""; for (int j = i; j < len; j++) { if (s[j] == '(') bra++; if (s[j] == ')') bra--; if (bra == 0) break; else { repeat += s[j]; if (repeat.size() > 1000100) break; } } part += solve(stoi(deg), repeat); if (part.size() > 1000100) return part; i += repeat.size(); } else { part += solve(stoi(deg), string(1, s[i])); } } } if (part.size() > 1000100) return part; string ret = ""; int limit = 1000100 / part.size() + 2; num = min(num, limit); for (int i = 0; i < num; i++) { ret += part; } return ret; } int main() { string s; int n; while (cin >> s >> n) { if (s == "0" && n == 0) break; string res = solve(1, s); if (res.size() <= n) cout << "0" << endl; else cout << res[n] << endl; // cout<<res.size()<<endl; } return 0; }
replace
133
135
133
138
MLE
p00726
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using P = pair<int, char>; string S; int n, it; int num() { int res = 0; while (it < n && isdigit(S[it])) { res = res * 10 + S[it] - '0'; it++; } return res; } P parser(int t) { P res(0, '0'); while (it < n && S[it] != ')') { if (isdigit(S[it])) { int nm = num(); if (S[it] == '(') { it++; int tmp = it; auto p = parser(t); if (p.second != '0') { res.second = p.second; break; } else if (t < p.first * nm) { it = tmp; t %= p.first; res.second = parser(t).second; } t -= p.first * nm; res.first += p.first * nm; assert(S[it++] == ')'); } else { if (t < nm) { res.second = S[it]; } t -= nm; res.first += nm; it++; } } else { if (t == 0) { res.second = S[it]; } t--; res.first++; it++; } if (t < 0) { break; } } return res; } int main() { int i; while (cin >> S >> i, S != "0" || i != 0) { n = S.size(); it = 0; cout << parser(i).second << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using P = pair<int, char>; string S; int n, it; int num() { int res = 0; while (it < n && isdigit(S[it])) { res = res * 10 + S[it] - '0'; it++; } return res; } P parser(int t) { P res(0, '0'); while (it < n && S[it] != ')') { if (isdigit(S[it])) { int nm = num(); if (S[it] == '(') { it++; int tmp = it; auto p = parser(t); if (p.second != '0') { res.second = p.second; break; } else if (t < p.first * nm) { it = tmp; t %= p.first; res.second = parser(t).second; break; } t -= p.first * nm; res.first += p.first * nm; assert(S[it++] == ')'); } else { if (t < nm) { res.second = S[it]; } t -= nm; res.first += nm; it++; } } else { if (t == 0) { res.second = S[it]; } t--; res.first++; it++; } if (t < 0) { break; } } return res; } int main() { int i; while (cin >> S >> i, S != "0" || i != 0) { n = S.size(); it = 0; cout << parser(i).second << endl; } return 0; }
insert
32
32
32
33
0
p00726
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; string s; const int MAX = 1e6 + 1; typedef long long Int; Int cnt(int &p); Int get_num(int &p) { int res = 0; while (isdigit(s[p])) { res *= 10; res += (s[p] - '0'); p++; } return res; } Int cnt_large(int &p) { Int res = 0; while (s[p] != ')') { Int val = cnt(p); res += val; if (res > MAX) return MAX; } return res; } Int cnt(int &p) { if (isalpha(s[p])) { p++; return 1; } if (isdigit(s[p])) { Int num = get_num(p); if (isalpha(s[p])) { p++; return num; } assert(s[p] == '('); p++; Int val = cnt_large(p); assert(s[p] == ')'); p++; if (val > MAX) { return MAX; } if (num * val > MAX) return MAX; return num * val; } return -1000000; } char target(int &p, Int x) { while (p < s.size()) { int prep = p; Int val = cnt(p); if (val >= x) { p = prep; if (isalpha(s[p])) { return s[p]; } Int num = get_num(p); if (isalpha(s[p])) { return s[p]; } assert(s[p] == '('); p++; int prep2 = p; Int val2 = cnt_large(p); p = prep2; return target(p, (x % val2 == 0) ? val2 : x % val2); } x -= val; } return '0'; } int main() { int n; while (cin >> s >> n, s != "0") { int p = 0; cout << target(p, n + 1) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; string s; const int MAX = 1e6 + 1; typedef long long Int; Int cnt(int &p); Int get_num(int &p) { int res = 0; while (isdigit(s[p])) { res *= 10; res += (s[p] - '0'); p++; } return res; } Int cnt_large(int &p) { Int res = 0; while (s[p] != ')') { Int val = cnt(p); res += val; if (res > MAX) res = MAX; } return res; } Int cnt(int &p) { if (isalpha(s[p])) { p++; return 1; } if (isdigit(s[p])) { Int num = get_num(p); if (isalpha(s[p])) { p++; return num; } assert(s[p] == '('); p++; Int val = cnt_large(p); assert(s[p] == ')'); p++; if (val > MAX) { return MAX; } if (num * val > MAX) return MAX; return num * val; } return -1000000; } char target(int &p, Int x) { while (p < s.size()) { int prep = p; Int val = cnt(p); if (val >= x) { p = prep; if (isalpha(s[p])) { return s[p]; } Int num = get_num(p); if (isalpha(s[p])) { return s[p]; } assert(s[p] == '('); p++; int prep2 = p; Int val2 = cnt_large(p); p = prep2; return target(p, (x % val2 == 0) ? val2 : x % val2); } x -= val; } return '0'; } int main() { int n; while (cin >> s >> n, s != "0") { int p = 0; cout << target(p, n + 1) << endl; } return 0; }
replace
25
26
25
26
0
p00728
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> using namespace std; int n; int main() { int ave; while (cin >> n) { int a[20]; int sum = 0; for (int i = 0; i <= n - 1; i++) { cin >> a[i]; } sort(a, a + n); for (int j = 1; j <= n - 2; j++) { sum += a[j]; } ave = floor(sum / (n - 2)); cout << ave << endl; } }
#include <algorithm> #include <cmath> #include <iostream> using namespace std; int n; int main() { int ave; while (cin >> n && (n > 0)) { int a[100]; int sum = 0; for (int i = 0; i <= n - 1; i++) { cin >> a[i]; } sort(a, a + n); for (int j = 1; j <= n - 2; j++) { sum += a[j]; } ave = floor(sum / (n - 2)); cout << ave << endl; } }
replace
9
11
9
11
0
p00728
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> #define REP(i, n) for (int i = 0; i < (n); i++) using namespace std; int main() { vector<int> vc; int n; while (cin >> n) { int tmp; vc.clear(); REP(i, n) { cin >> tmp; vc.push_back(tmp); } sort(vc.begin(), vc.end()); vc.erase(vc.begin()); vc.erase(--vc.end()); int sum = 0; vector<int>::iterator it = vc.begin(); while (it != vc.end()) { sum += (*it); it++; } cout << sum / (n - 2) << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> #define REP(i, n) for (int i = 0; i < (n); i++) using namespace std; int main() { vector<int> vc; int n; while (cin >> n) { if (n == 0) break; int tmp; vc.clear(); REP(i, n) { cin >> tmp; vc.push_back(tmp); } sort(vc.begin(), vc.end()); vc.erase(vc.begin()); vc.erase(--vc.end()); int sum = 0; vector<int>::iterator it = vc.begin(); while (it != vc.end()) { sum += (*it); it++; } cout << sum / (n - 2) << endl; } return 0; }
insert
12
12
12
14
-11
p00728
C++
Runtime Error
#include <iostream> using namespace std; int main() { int n, s; while (cin >> n && n > 0) { int large = 0; int small = 1000; int sum = 0; for (int i = 0; i > n; ++i) { cin >> s; sum = sum + s; if (large < s) { large = s; } if (small > s) { small = s; } } int ans = 0; ans = (sum - large - small) / (n - 2); cout << ans << endl; } }
#include <iostream> using namespace std; int main() { int n, s; while (cin >> n && n > 0) { int large = 0; int small = 1000; int sum = 0; for (int i = 0; i < n; ++i) { cin >> s; sum = sum + s; if (large < s) { large = s; } if (small > s) { small = s; } } int ans = 0; ans = (sum - large - small) / (n - 2); cout << ans << endl; } }
replace
10
11
10
11
0
p00728
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> using namespace std; int main() { int a[20]; int b, sum = 0, point; while (1) { cin >> b; if (b == 0) break; for (int i = 0; i < b; i++) { cin >> a[i]; } sort(a, a + b); for (int j = 1; j < b - 1; j++) { sum += a[j]; } point = sum / (b - 2); cout << point << endl; sum = 0; } return 0; }
#include <algorithm> #include <iostream> #include <string> using namespace std; int main() { int a[100]; int b, sum = 0, point; while (1) { cin >> b; if (b == 0) break; for (int i = 0; i < b; i++) { cin >> a[i]; } sort(a, a + b); for (int j = 1; j < b - 1; j++) { sum += a[j]; } point = sum / (b - 2); cout << point << endl; sum = 0; } return 0; }
replace
7
8
7
8
0