buggy_code
stringlengths
11
625k
fixed_code
stringlengths
17
625k
bug_type
stringlengths
2
4.45k
language
int64
0
8
token_count
int64
5
200k
change_count
int64
0
100
#include <iostream> using namespace std; int main() { const int MAX = 1e5; for (int n, m; cin >> n >> m, n;) { int cost[m][m]; int time[m][m]; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { cost[i][j] = MAX; time[i][j] = MAX; } } int a, b, c, t; for (int i = 0; i < n; i++) { cin >> a >> b >> c >> t; cost[a - 1][b - 1] = c; cost[b - 1][a - 1] = c; time[a - 1][b - 1] = t; time[b - 1][a - 1] = t; } for (int k = 0; k < m; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (cost[i][j] > cost[i][k] + cost[k][j]) cost[i][j] = cost[i][k] + cost[k][j]; if (time[i][j] > time[i][k] + time[k][j]) time[i][j] = time[i][k] + time[k][j]; } } } int l, r; cin >> l; for (int i = 0; i < l; i++) { cin >> a >> b >> r; if (r) cout << time[a - 1][b - 1] << endl; else cout << cost[a - 1][b - 1] << endl; } } return 0; }
#include <iostream> using namespace std; int main() { const int MAX = 1e5; for (int n, m; cin >> n >> m, n;) { int cost[m][m]; int time[m][m]; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { cost[i][j] = MAX; time[i][j] = MAX; } } int a, b, c, t; for (int i = 0; i < n; i++) { cin >> a >> b >> c >> t; cost[a - 1][b - 1] = c; cost[b - 1][a - 1] = c; time[a - 1][b - 1] = t; time[b - 1][a - 1] = t; } for (int k = 0; k < m; k++) { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { if (cost[i][j] > cost[i][k] + cost[k][j]) cost[i][j] = cost[i][k] + cost[k][j]; if (time[i][j] > time[i][k] + time[k][j]) time[i][j] = time[i][k] + time[k][j]; } } } int l, r; cin >> l; for (int i = 0; i < l; i++) { cin >> a >> b >> r; if (r) cout << time[a - 1][b - 1] << endl; else cout << cost[a - 1][b - 1] << endl; } } return 0; }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22]]
1
418
4
#include <iostream> using namespace std; int t[303][303]; int c[303][303]; int main() { for (int N, M; cin >> M >> N, (N || M);) { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { t[i][j] = 987654321; c[i][j] = 987654321; } for (int i = 0; i < N; i++) t[i][i] = c[i][i] = 0; for (int n = 0; n < M; n++) { int a, b, time, cost; cin >> a >> b >> time >> cost; a--, b--; t[a][b] = t[b][a] = time; c[a][b] = c[b][a] = cost; } for (int k = 0; k < N; k++) for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { t[i][j] = min(t[i][j], t[i][k] + t[k][j]); c[i][j] = min(c[i][j], c[i][k] + c[k][j]); } cin >> M; for (int m = 0; m < M; m++) { int a, b, r; cin >> a >> b >> r; a--, b--; if (r == 0) cout << c[a][b] << endl; else cout << t[a][b] << endl; } } }
#include <iostream> using namespace std; int t[303][303]; int c[303][303]; int main() { for (int N, M; cin >> M >> N, (N || M);) { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { t[i][j] = 987654321; c[i][j] = 987654321; } for (int i = 0; i < N; i++) t[i][i] = c[i][i] = 0; for (int n = 0; n < M; n++) { int a, b, time, cost; cin >> a >> b >> time >> cost; a--, b--; t[a][b] = t[b][a] = time; c[a][b] = c[b][a] = cost; } for (int k = 0; k < N; k++) for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { t[i][j] = min(t[i][j], t[i][k] + t[k][j]); c[i][j] = min(c[i][j], c[i][k] + c[k][j]); } cin >> M; for (int m = 0; m < M; m++) { int a, b, r; cin >> a >> b >> r; a--, b--; if (r == 1) cout << c[a][b] << endl; else cout << t[a][b] << endl; } } }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
397
2
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; int main() { int n, m; while (scanf("%d %d", &n, &m), n | m) { int d[2][101][101]; for (int i = 0; i <= m; ++i) for (int j = 0; j <= m; ++j) d[0][i][j] = d[1][i][j] = 100000000; for (int i = 0; i <= m; ++i) d[0][i][i] = d[1][i][i] = 0; for (int i = 0; i < n; ++i) { int a, b, cost, time; scanf("%d %d %d %d", &a, &b, &cost, &time); d[0][a][b] = d[0][b][a] = cost; d[1][a][b] = d[1][a][b] = time; } for (int k = 0; k <= m; ++k) for (int i = 0; i <= m; ++i) for (int j = 0; j <= m; ++j) for (int l = 0; l < 2; ++l) d[l][i][j] = min(d[l][i][j], d[l][i][k] + d[l][k][j]); int queries; scanf("%d", &queries); for (int i = 0; i < queries; ++i) { int p, q, r; scanf("%d %d %d", &p, &q, &r); printf("%d\n", d[r][p][q]); } } return 0; }
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; int main() { int n, m; while (scanf("%d %d", &n, &m), n | m) { int d[2][101][101]; for (int i = 0; i <= m; ++i) for (int j = 0; j <= m; ++j) d[0][i][j] = d[1][i][j] = 100000000; for (int i = 0; i <= m; ++i) d[0][i][i] = d[1][i][i] = 0; for (int i = 0; i < n; ++i) { int a, b, cost, time; scanf("%d %d %d %d", &a, &b, &cost, &time); d[0][a][b] = d[0][b][a] = cost; d[1][a][b] = d[1][b][a] = time; } for (int k = 0; k <= m; ++k) for (int i = 0; i <= m; ++i) for (int j = 0; j <= m; ++j) for (int l = 0; l < 2; ++l) d[l][i][j] = min(d[l][i][j], d[l][i][k] + d[l][k][j]); int queries; scanf("%d", &queries); for (int i = 0; i < queries; ++i) { int p, q, r; scanf("%d %d %d", &p, &q, &r); printf("%d\n", d[r][p][q]); } } return 0; }
[["-", 12, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["+", 12, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["-", 0, 11, 12, 11, 31, 69, 341, 342, 0, 22], ["+", 0, 11, 12, 11, 31, 69, 341, 342, 0, 22]]
1
439
4
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int a, b, time, cost, querys; int i, j, k, p, q, r, n, m; int times[101][101], costs[101][101]; while (true) { cin >> n >> m; if (!n && !m) break; for (i = 0; i < m; i++) { for (j = 0; j < m; j++) { times[i][j] = (i == j) ? 0 : 1e6; times[j][i] = (i == j) ? 0 : 1e6; } } for (i = 0; i < n; i++) { cin >> a >> b >> cost >> time; times[a - 1][b - 1] = time; times[b - 1][a - 1] = time; costs[a - 1][b - 1] = cost; costs[b - 1][a - 1] = cost; } for (k = 0; k < m; k++) { for (i = 0; i < m; i++) { for (j = 0; j < m; j++) { times[i][j] = min(times[i][j], times[i][k] + times[k][j]); costs[i][j] = min(costs[i][j], costs[i][k] + costs[k][j]); } } } cin >> querys; for (i = 0; i < querys; i++) { cin >> p >> q >> r; cout << (r ? times[p - 1][q - 1] : costs[p - 1][q - 1]) << endl; } } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int a, b, time, cost, querys; int i, j, k, p, q, r, n, m; int times[100][100], costs[100][100]; while (true) { cin >> n >> m; if (!n && !m) break; for (i = 0; i < m; i++) { for (j = 0; j < m; j++) { times[i][j] = (i == j) ? 0 : 1e6; costs[j][i] = (i == j) ? 0 : 1e6; } } for (i = 0; i < n; i++) { cin >> a >> b >> cost >> time; times[a - 1][b - 1] = time; times[b - 1][a - 1] = time; costs[a - 1][b - 1] = cost; costs[b - 1][a - 1] = cost; } for (k = 0; k < m; k++) { for (i = 0; i < m; i++) { for (j = 0; j < m; j++) { times[i][j] = min(times[i][j], times[i][k] + times[k][j]); costs[i][j] = min(costs[i][j], costs[i][k] + costs[k][j]); } } } cin >> querys; for (i = 0; i < querys; i++) { cin >> p >> q >> r; cout << (r ? times[p - 1][q - 1] : costs[p - 1][q - 1]) << endl; } } return 0; }
[["-", 8, 9, 0, 43, 49, 80, 49, 80, 81, 13], ["+", 8, 9, 0, 43, 49, 80, 49, 80, 81, 13], ["-", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13], ["-", 0, 1, 0, 11, 31, 69, 28, 69, 28, 22], ["+", 0, 1, 0, 11, 31, 69, 28, 69, 28, 22]]
1
407
10
#include <iostream> #define N 101 #define INF 1e+9 using namespace std; int main(void) { int n, m; while (cin >> n >> m, n || m) { int cost[N][N], times[N][N]; for (int i = 1; i <= m; ++i) for (int j = 1; j <= m; ++j) { cost[i][j] = times[i][j] = INF; } for (int i = 0, a, b, c, t; i < n; ++i) { cin >> a >> b >> c >> t; cost[a][b] = cost[b][a] = c; times[a][b] = times[b][a] = t; } int k; cin >> k; for (int i = 0, p, q, r; i < k; ++i) { cin >> p >> q >> r; if (r) { // time int d[N]; for (int j = 1; j <= m; ++j) d[j] = INF; d[p] = 0; bool f = false; for (;;) { for (int j = 1; j <= m; ++j) { // from for (int k = 1; k <= m; ++k) { // to if (d[j] != INF && d[j] + times[j][k] < d[k]) { f == true; d[k] = d[j] + times[j][k]; } } } if (f == false) break; f = false; } cout << d[q] << endl; } else { // cost int d[N]; for (int j = 1; j <= m; ++j) d[j] = INF; d[p] = 0; bool f = false; for (;;) { for (int j = 1; j <= m; ++j) { // from for (int k = 1; k <= m; ++k) { // to if (d[j] != INF && d[j] + cost[j][k] < d[k]) { f == true; d[k] = d[j] + cost[j][k]; } } } if (f == false) break; f = false; } cout << d[q] << endl; } } } return 0; }
#include <iostream> #define N 101 #define INF 1e+9 using namespace std; int main(void) { int n, m; while (cin >> n >> m, n || m) { int cost[N][N], times[N][N]; for (int i = 1; i <= m; ++i) for (int j = 1; j <= m; ++j) { cost[i][j] = times[i][j] = INF; } for (int i = 0, a, b, c, t; i < n; ++i) { cin >> a >> b >> c >> t; cost[a][b] = cost[b][a] = c; times[a][b] = times[b][a] = t; } int k; cin >> k; for (int i = 0, p, q, r; i < k; ++i) { cin >> p >> q >> r; if (r) { // time int d[N]; for (int j = 1; j <= m; ++j) d[j] = INF; d[p] = 0; bool f = false; for (;;) { for (int j = 1; j <= m; ++j) { // from for (int k = 1; k <= m; ++k) { // to if (d[j] != INF && d[j] + times[j][k] < d[k]) { f = true; d[k] = d[j] + times[j][k]; } } } if (f == false) break; f = false; } cout << d[q] << endl; } else { // cost int d[N]; for (int j = 1; j <= m; ++j) d[j] = INF; d[p] = 0; bool f = false; for (;;) { for (int j = 1; j <= m; ++j) { // from for (int k = 1; k <= m; ++k) { // to if (d[j] != INF && d[j] + cost[j][k] < d[k]) { f = true; d[k] = d[j] + cost[j][k]; } } } if (f == false) break; f = false; } cout << d[q] << endl; } } } return 0; }
[["-", 0, 57, 64, 9, 0, 1, 0, 16, 17, 60], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 17, 32]]
1
528
4
#include <iostream> using namespace std; const int MAX_N = 110; const int INF = 10000; int d_cost[MAX_N][MAX_N]; int d_time[MAX_N][MAX_N]; int n, m; void warshall_floyd(int d[MAX_N][MAX_N]) { for (int k = 0; k < m; k++) for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } int main() { while (cin >> n >> m, n || m) { int a, b, cost, time; int k, p, q, r; // 初期化 for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (i == j) d_cost[i][j] = d_time[i][j] = 0; else d_cost[i][j] = d_time[i][j] = INF; } for (int i = 0; i < n; i++) { cin >> a >> b >> cost >> time; a--, b--; d_cost[a][b] = d_cost[b][a] = cost; d_time[a][b] = d_time[b][a] = time; } warshall_floyd(d_cost); warshall_floyd(d_time); cin >> k; for (int i = 0; i < k; i++) { cin >> p >> q >> r; p--, q--; if (r == 0) cout << d_cost[p][q] << endl; else cout << d_time[p][q] << endl; } } return 0; }
#include <iostream> using namespace std; const int MAX_N = 110; const int INF = 10000; int d_cost[MAX_N][MAX_N]; int d_time[MAX_N][MAX_N]; int n, m; void warshall_floyd(int d[MAX_N][MAX_N]) { for (int k = 0; k < m; k++) for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } int main() { while (cin >> n >> m, n || m) { int a, b, cost, time; int k, p, q, r; // 初期化 for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) { if (i == j) d_cost[i][j] = d_time[i][j] = 0; else d_cost[i][j] = d_time[i][j] = INF; } for (int i = 0; i < n; i++) { cin >> a >> b >> cost >> time; a--, b--; d_cost[a][b] = d_cost[b][a] = cost; d_time[a][b] = d_time[b][a] = time; } warshall_floyd(d_cost); warshall_floyd(d_time); cin >> k; for (int i = 0; i < k; i++) { cin >> p >> q >> r; p--, q--; if (r == 0) cout << d_cost[p][q] << endl; else cout << d_time[p][q] << endl; } } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["-", 8, 9, 0, 7, 8, 7, 15, 16, 12, 22], ["+", 8, 9, 0, 7, 8, 7, 15, 16, 12, 22]]
1
390
4
inf = 0x10101010 def solve(A, strt): cost = [inf]*m visited = [False]*m cost[strt] = 0 while True: min = inf next = -1 visited[strt] = True for i in range(m): if visited[i]: continue if A[strt][i]: d = cost[strt] + A[strt][i] if d < cost[i]: cost[i] = d if cost[i] < min: min = cost[i] next = i strt = next if next == -1: break return cost while True: # 経路の情報の数n,駅の数m n, m = list(map(int, input().split())) if n == 0: break T = [[0]*m for i in range(m)] C = [[0]*m for i in range(m)] for i in range(n): a, b, c, t = list(map(int, input().split())) # 駅 駅 time cost T[a-1][b-1] = T[b-1][a-1] = t C[a-1][b-1] = C[b-1][a-1] = c TS = [solve(T, i) for i in range(m)] CS = [solve(C, i) for i in range(m)] for i in range(eval(input())): a, b, q = list(map(int, input().split())) if q == 0: print(CS[a-1][b-1]) else: print(TS[a-1][b-1])
inf = 0x10101010 def solve(A, strt): cost = [inf]*m visited = [False]*m cost[strt] = 0 while True: min = inf next = -1 visited[strt] = True for i in range(m): if visited[i]: continue if A[strt][i]: d = cost[strt] + A[strt][i] if d < cost[i]: cost[i] = d if min > cost[i]: min = cost[i] next = i strt = next if next == -1: break return cost while True: n,m = list(map(int, input().split())) if n == 0:break T = [[0]*m for i in range(m)] C = [[0]*m for i in range(m)] for i in range(n): a,b,c,t = list(map(int, input().split())) T[a-1][b-1] = T[b-1][a-1] = t C[a-1][b-1] = C[b-1][a-1] = c TS = [solve(T,i) for i in range(m)] CS = [solve(C,i) for i in range(m)] for i in range(eval(input())): a, b, q = list(map(int,input().split())) if q == 0: print(CS[a-1][b-1]) else: print(TS[a-1][b-1])
[["+", 0, 7, 8, 196, 0, 57, 15, 666, 0, 22], ["+", 0, 7, 8, 196, 0, 57, 15, 666, 667, 47], ["-", 0, 57, 64, 196, 0, 57, 15, 666, 667, 18], ["-", 0, 57, 64, 196, 0, 57, 15, 666, 0, 22]]
5
374
4
inf = 0x10101010 def solve(A,strt,goal): cost = [inf]*m visited = [False]*m cost[strt] = 0 while True: min = inf next = -1 visited[strt] = True for i in range(m): if visited[i]: continue if A[strt][i]: d = cost[strt] + A[strt][i] if d < cost[i]: cost[i] = d if min > cost[i]: min = cost[i] next = i strt = next if next == -1: break return cost[goal] while True: n,m = list(map(int,input().split())) if n == 0: break T = [[0]*m for i in range(m)] C = [[0]*m for i in range(m)] for i in range(n): a,b,c,t = list(map(int,input().split())) T[a-1][b-1] = T[b-1][a-1] = t C[a-1][b-1] = T[b-1][a-1] = c for i in range(eval(input())): a,b,q = list(map(int,input().split())) if q == 0: print(solve(C,a-1,b-1)) else: print(solve(T,a-1,b-1))
inf = 0x10101010 def solve(A,strt,goal): cost = [inf]*m visited = [False]*m cost[strt] = 0 while True: min = inf next = -1 visited[strt] = True for i in range(m): if visited[i]: continue if A[strt][i]: d = cost[strt] + A[strt][i] if d < cost[i]: cost[i] = d if min > cost[i]: min = cost[i] next = i strt = next if next == -1: break return cost[goal] while True: n,m = list(map(int,input().split())) if n == 0: break T = [[0]*m for i in range(m)] C = [[0]*m for i in range(m)] for i in range(n): a,b,c,t = list(map(int,input().split())) T[a-1][b-1] = T[b-1][a-1] = t C[a-1][b-1] = C[b-1][a-1] = c for i in range(eval(input())): a,b,q = list(map(int,input().split())) if q == 0: print(solve(C,a-1,b-1)) else: print(solve(T,a-1,b-1))
[["-", 0, 662, 12, 662, 31, 206, 51, 206, 51, 22], ["+", 0, 662, 12, 662, 31, 206, 51, 206, 51, 22]]
5
345
2
#include <stdio.h> #include <string.h> typedef struct item { char n[105]; int p, f, k, m[105]; } ITEM; ITEM d[105]; int func(int n) { int sp = 0, i; if (d[n].f == 0) return d[n].p; else { for (i = 0; i < d[n].k; i++) { sp += func(d[n].m[i]); } if (sp < d[n].p) d[n].p = sp; return sp; } } int main() { FILE *fp; // fp=fopen("test.txt","r"); fp = stdin; int n, m, i, j, k, x, y, f, p; char s[105]; while (1) { fscanf(fp, "%d", &n); if (n == 0) break; for (i = 0; i < n; i++) { fscanf(fp, "%s%d", d[i].n, &d[i].p); d[i].f = d[i].k = 0; } fscanf(fp, "%d", &m); for (i = 0; i < m; i++) { fscanf(fp, "%s%d", s, &x); for (j = 0; j < n; j++) { if (strcmp(d[j].n, s) == 0) { y = j; d[y].f = 1; d[y].k = x; break; } } f = p = 0; for (j = 0; j < x; j++) { fscanf(fp, "%s", s); for (k = 0; k < n; k++) { if (strcmp(d[k].n, s) == 0) { d[y].m[j] = k; // p+=d[k].p; // if(d[k].f==1)f=1; break; } } } // if(f==0 && d[y].p>p)d[y].p=p,d[y].f=0; } /*while(1){ f=0; for(i=0;i<n;i++){ if(d[i].f==1){ x=p=0; for(j=0;j<d[i].k;j++){ p+=d[ d[i].m[j] ].p; if(d[ d[i].m[j] ].f==1)x=1; } if(x==0)f=1; } } if(f==0)break; }*/ fscanf(fp, "%s", s); for (i = 0; i < n; i++) { if (strcmp(d[i].n, s) == 0) { printf("%d\n", func(i)); break; } } /*for(i=0;i<n;i++){ printf("%2d:%s %d %d\n",i,d[i].n,d[i].p,d[i].f); printf(" ");for(j=0;j<d[i].k;j++)printf("%d ",d[i].m[j]); printf("\n"); }*/ } return 0; }
#include <stdio.h> #include <string.h> typedef struct item { char n[105]; int p, f, k, m[105]; } ITEM; ITEM d[105]; int func(int n) { int sp = 0, i; if (d[n].f == 0) return d[n].p; else { for (i = 0; i < d[n].k; i++) { sp += func(d[n].m[i]); } if (sp < d[n].p) d[n].p = sp; return d[n].p; } } int main() { FILE *fp; // fp=fopen("test.txt","r"); fp = stdin; int n, m, i, j, k, x, y, f, p; char s[105]; while (1) { fscanf(fp, "%d", &n); if (n == 0) break; for (i = 0; i < n; i++) { fscanf(fp, "%s%d", d[i].n, &d[i].p); d[i].f = d[i].k = 0; } fscanf(fp, "%d", &m); for (i = 0; i < m; i++) { fscanf(fp, "%s%d", s, &x); for (j = 0; j < n; j++) { if (strcmp(d[j].n, s) == 0) { y = j; d[y].f = 1; d[y].k = x; break; } } f = p = 0; for (j = 0; j < x; j++) { fscanf(fp, "%s", s); for (k = 0; k < n; k++) { if (strcmp(d[k].n, s) == 0) { d[y].m[j] = k; // p+=d[k].p; // if(d[k].f==1)f=1; break; } } } // if(f==0 && d[y].p>p)d[y].p=p,d[y].f=0; } /*while(1){ f=0; for(i=0;i<n;i++){ if(d[i].f==1){ x=p=0; for(j=0;j<d[i].k;j++){ p+=d[ d[i].m[j] ].p; if(d[ d[i].m[j] ].f==1)x=1; } if(x==0)f=1; } } if(f==0)break; }*/ fscanf(fp, "%s", s); for (i = 0; i < n; i++) { if (strcmp(d[i].n, s) == 0) { printf("%d\n", func(i)); break; } } /*for(i=0;i<n;i++){ printf("%2d:%s %d %d\n",i,d[i].n,d[i].p,d[i].f); printf(" ");for(j=0;j<d[i].k;j++)printf("%d ",d[i].m[j]); printf("\n"); }*/ } return 0; }
[["-", 0, 57, 75, 76, 0, 9, 0, 37, 0, 22], ["+", 0, 9, 0, 37, 0, 118, 28, 69, 28, 22], ["+", 0, 9, 0, 37, 0, 118, 28, 69, 0, 70], ["+", 0, 9, 0, 37, 0, 118, 28, 69, 71, 22], ["+", 0, 9, 0, 37, 0, 118, 28, 69, 0, 73], ["+", 75, 76, 0, 9, 0, 37, 0, 118, 17, 131], ["+", 75, 76, 0, 9, 0, 37, 0, 118, 119, 120]]
0
495
7
#include <algorithm> #include <iostream> #include <map> #include <string> int main() { std::map<std::string, int> ItemData; int Item_num; while (std::cin >> Item_num) { if (Item_num == 0) break; for (int i = 0; i < Item_num; ++i) { std::string ItemName; int ItemPrice; std::cin >> ItemName >> ItemPrice; ItemData[ItemName] = ItemPrice; } int mix_num; std::cin >> mix_num; for (int i = 0; i < mix_num; ++i) { std::string ItemName; int Material_num; std::cin >> ItemName >> Material_num; int temp = 0; std::string MaterialName; for (int j = 0; j < Material_num; ++j) { std::cin >> MaterialName; temp += ItemData[MaterialName]; ItemData[ItemName] = std::min(ItemData[ItemName], temp); } } std::string Material; std::cin >> Material; std::cout << ItemData[Material] << std::endl; } return 0; }
#include <algorithm> #include <iostream> #include <map> #include <string> int main() { std::map<std::string, int> ItemData; int Item_num; while (std::cin >> Item_num) { if (Item_num == 0) break; for (int i = 0; i < Item_num; ++i) { std::string ItemName; int ItemPrice; std::cin >> ItemName >> ItemPrice; ItemData[ItemName] = ItemPrice; } int mix_num; std::cin >> mix_num; for (int i = 0; i < mix_num; ++i) { std::string ItemName; int Material_num; std::cin >> ItemName >> Material_num; int temp = 0; std::string MaterialName; for (int j = 0; j < Material_num; ++j) { std::cin >> MaterialName; temp += ItemData[MaterialName]; } ItemData[ItemName] = std::min(ItemData[ItemName], temp); } std::string Material; std::cin >> Material; std::cout << ItemData[Material] << std::endl; } return 0; }
[["+", 0, 7, 8, 9, 0, 7, 8, 9, 0, 46], ["-", 0, 7, 8, 9, 0, 7, 8, 9, 0, 46]]
1
210
2
#include <iostream> using namespace std; struct item { int id; int price; }; struct recipe { int id; int size; int price; int ingre[100]; }; item I[100]; recipe R[100]; string T[100]; int solve(int id, int n, int m); int main(int argc, char **argv) { string name, o, q; int n, m, p, k; while (1) { cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) { cin >> T[i]; I[i].id = i; cin >> I[i].price; } cin >> m; for (int i = 0; i < m; i++) { cin >> name; for (int j = 0; j < n; j++) { if (T[j] == name) { R[i].id = j; } } cin >> R[i].size; R[i].price = 0; for (int j = 0; j < R[i].size; j++) { cin >> name; for (int k = 0; k < n; k++) { if (T[k] == name) { R[i].ingre[j] = k; R[i].price += I[k].price; } } } } cin >> name; for (int i = 0; i < n; i++) { if (name == T[i]) { cout << solve(i, n, m) << endl; } } } return 0; } int solve(int id, int n, int m) { int money = 0; int min = 10000000; bool is_proc = false; for (int j = 0; j < m; j++) { if (R[j].id == id) { is_proc = true; money = 0; for (int k = 0; k < R[j].size; k++) { money += solve(R[j].ingre[k], n, m); } if (money < min) min = money; break; } } if (is_proc) return min; else return I[id].price; }
#include <iostream> using namespace std; struct item { int id; int price; }; struct recipe { int id; int size; int price; int ingre[100]; }; item I[100]; recipe R[100]; string T[100]; int solve(int id, int n, int m); int main(int argc, char **argv) { string name, o, q; int n, m, p, k; while (1) { cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) { cin >> T[i]; I[i].id = i; cin >> I[i].price; } cin >> m; for (int i = 0; i < m; i++) { cin >> name; for (int j = 0; j < n; j++) { if (T[j] == name) { R[i].id = j; } } cin >> R[i].size; R[i].price = 0; for (int j = 0; j < R[i].size; j++) { cin >> name; for (int k = 0; k < n; k++) { if (T[k] == name) { R[i].ingre[j] = k; R[i].price += I[k].price; } } } } cin >> name; for (int i = 0; i < n; i++) { if (name == T[i]) { cout << solve(i, n, m) << endl; } } } return 0; } int solve(int id, int n, int m) { int money = 0; int min = I[id].price; bool is_proc = false; for (int j = 0; j < m; j++) { if (R[j].id == id) { is_proc = true; money = 0; for (int k = 0; k < R[j].size; k++) { money += solve(R[j].ingre[k], n, m); } if (money < min) min = money; break; } } if (is_proc) return min; else return I[id].price; }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 43, 49, 50, 51, 118, 28, 69, 28, 22], ["+", 49, 50, 51, 118, 28, 69, 341, 342, 0, 70], ["+", 49, 50, 51, 118, 28, 69, 341, 342, 0, 22], ["+", 49, 50, 51, 118, 28, 69, 341, 342, 0, 73], ["+", 8, 9, 0, 43, 49, 50, 51, 118, 17, 131], ["+", 8, 9, 0, 43, 49, 50, 51, 118, 119, 120]]
1
493
7
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; int n, m; map<string, int> t; int c[111]; vector<int> v[111]; int solve(int k) { int sum = 0; if (v[k].empty()) return c[k]; for (int i = 0; i < (int)v[k].size(); i++) { sum += solve(v[k][i]); } return min(sum, c[k]); } int main(void) { while (cin >> n && n) { t.clear(); for (int i = 0; i < 111; i++) v[i].clear(); for (int i = 0; i < n; i++) { string s; int a; cin >> s >> a; t[s] = n; c[n] = a; } cin >> m; for (int j = 0; j < m; j++) { string s; int a; cin >> s >> a; int id = t[s]; for (int i = 0; i < a; i++) { cin >> s; v[id].push_back(t[s]); } } string s; cin >> s; cout << solve(t[s]) << endl; } }
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; int n, m; map<string, int> t; int c[111]; vector<int> v[111]; int solve(int k) { int sum = 0; if (v[k].empty()) return c[k]; for (int i = 0; i < (int)v[k].size(); i++) { sum += solve(v[k][i]); } return min(sum, c[k]); } int main(void) { while (cin >> n && n) { t.clear(); for (int i = 0; i < 111; i++) v[i].clear(); for (int i = 0; i < n; i++) { string s; int a; cin >> s >> a; t[s] = i; c[i] = a; } cin >> m; for (int j = 0; j < m; j++) { string s; int a; cin >> s >> a; int id = t[s]; for (int i = 0; i < a; i++) { cin >> s; v[id].push_back(t[s]); } } string s; cin >> s; cout << solve(t[s]) << endl; } }
[["-", 0, 7, 8, 9, 0, 1, 0, 11, 12, 22], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 12, 22], ["-", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22], ["+", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22]]
1
304
4
import java.util.*; public class Main { Scanner sc = new Scanner(System.in); double DP(double gn[][], int n, int m) { double result = 1.00; double gli[][] = new double[n][m]; for (int i = 0; i < n; i++) { gli[i][0] = 1.00; } for (int i = 1; i < m; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if (gli[j][i] < gli[k][i - 1] * gn[k][j]) { gli[j][i] = gli[k][i - 1] * gn[k][j]; } } } } for (int j = 0; j < n; j++) { if (gli[j][m - 1] > result) result = gli[j][m - 1]; } return result; } void doIt() { while (true) { int n = sc.nextInt(); int m = sc.nextInt(); if (n + m == 0) break; double gn[][] = new double[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { gn[i][j] = sc.nextDouble(); } } System.out.printf("%1.2f", DP(gn, n, m)); } } public static void main(String[] args) { // TODO Auto-generated method stub new Main().doIt(); } }
import java.util.*; public class Main { Scanner sc = new Scanner(System.in); double DP(double gn[][], int n, int m) { double result = 0; double gli[][] = new double[n][m]; for (int i = 0; i < n; i++) { gli[i][0] = 1.00; } for (int i = 1; i < m; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if (gli[j][i] < gli[k][i - 1] * gn[k][j]) { gli[j][i] = gli[k][i - 1] * gn[k][j]; } } } } for (int j = 0; j < n; j++) { if (gli[j][m - 1] > result) result = gli[j][m - 1]; } return result; } void doIt() { while (true) { int n = sc.nextInt(); int m = sc.nextInt(); if (n + m == 0) break; double gn[][] = new double[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { gn[i][j] = sc.nextDouble(); } } System.out.printf("%1.2f\n", DP(gn, n, m)); } } public static void main(String[] args) { // TODO Auto-generated method stub new Main().doIt(); } }
[["-", 0, 195, 8, 196, 0, 503, 49, 200, 51, 515], ["+", 0, 195, 8, 196, 0, 503, 49, 200, 51, 499], ["+", 0, 1, 0, 492, 3, 4, 0, 5, 0, 44]]
3
381
3
import java.util.Scanner; public class Main { int n, m; double table[][]; double memo[][]; public static void main(String[] args) { new Main().run(); } void run() { Scanner sc = new Scanner(System.in); while (true) { n = sc.nextInt(); m = sc.nextInt(); if (n + m == 0) break; table = new double[n][n]; memo = new double[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { table[i][j] = sc.nextDouble(); } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { memo[i][j] = -1; } } for (int i = 0; i < n; i++) { memo[i][m - 1] = 1; } for (int i = 0; i < n; i++) { bt(i, 0); } // for(int i=0;i<n;i++){ // for(int j=0;j<m;j++){ // System.out.print(" //"+memo[i][j]); // } // System.out.println(); // } double max = 0; for (int i = 0; i < n; i++) { max = Math.max(max, memo[i][0]); } System.out.printf("%.2f\n", max); } } double bt(int x, int d) { if (m <= d) return 1; if (memo[x][d] != -1) return memo[x][d]; double max = 0; for (int i = 0; i < n; i++) { max = Math.max(max, bt(x, d + 1) * table[i][x]); } return memo[x][d] = max; } }
import java.util.Scanner; public class Main { int n, m; double table[][]; double memo[][]; public static void main(String[] args) { new Main().run(); } void run() { Scanner sc = new Scanner(System.in); while (true) { n = sc.nextInt(); m = sc.nextInt(); if (n + m == 0) break; table = new double[n][n]; memo = new double[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { table[i][j] = sc.nextDouble(); } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { memo[i][j] = -1; } } for (int i = 0; i < n; i++) { memo[i][m - 1] = 1; } for (int i = 0; i < n; i++) { bt(i, 0); // for(int j=0;j<n;j++){ // for(int k=0;k<m;k++){ // System.out.print(" "+j+","+k+" //"+memo[j][k]); // } // System.out.println(); // } // System.out.println(); } double max = 0; for (int i = 0; i < n; i++) { max = Math.max(max, memo[i][0]); } System.out.printf("%.2f\n", max); } } double bt(int x, int d) { if (m <= d) return 1; if (memo[x][d] != -1) return memo[x][d]; double max = 0; for (int i = 0; i < n; i++) { // System.out.println("x="+x+" d="+d+" i="+i+" x="+x); max = Math.max(max, bt(i, d + 1) * table[i][x]); } return memo[x][d] = max; } }
[["-", 3, 4, 0, 16, 31, 492, 3, 4, 0, 22], ["+", 3, 4, 0, 16, 31, 492, 3, 4, 0, 22]]
3
422
2
#include <stdio.h> int main() { int n, m, i, j, k; double dp[110][110], d[110][110], max; while (scanf("%d %d", &n, &m), n || m) { for (i = 0; i < n; i++) { for (j = 0; j < n; j++) scanf("%lf", &d[i][j]); dp[0][i] = 1; } for (k = 1; k < m; k++) { for (i = 0; i < n; i++) { for (j = max = 0; j < n; j++) { if (max < dp[k - 1][j] * d[j][i]) max = dp[k - 1][j] * d[j][i]; } dp[k][i] = max; } } max = 0; for (i = 0; i < n; i++) { if (max < dp[m - 1][i]) max = dp[m - 1][i]; } printf("%f\n", max); } return 0; }
#include <stdio.h> int main() { int n, m, i, j, k; double dp[110][110], d[110][110], max; while (scanf("%d %d", &n, &m), n || m) { for (i = 0; i < n; i++) { for (j = 0; j < n; j++) scanf("%lf", &d[i][j]); dp[0][i] = 1; } for (k = 1; k < m; k++) { for (i = 0; i < n; i++) { for (j = max = 0; j < n; j++) { if (max < dp[k - 1][j] * d[j][i]) max = dp[k - 1][j] * d[j][i]; } dp[k][i] = max; } } max = 0; for (i = 0; i < n; i++) { if (max < dp[m - 1][i]) max = dp[m - 1][i]; } printf("%.2f\n", max); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
0
270
2
#include <stdio.h> #include <string.h> int main(void) { int n, m; double g[100][100]; double baby[100], baby_next[100]; int i, j, k; double max; while (1) { scanf("%d%d", &n, &m); if (n == 0 && m == 0) { break; } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%lf", &g[i][j]); } } for (i = 0; i < n; i++) { baby_next[i] = 1.0; } for (k = 0; k < m - 1; k++) { memcpy(baby, baby_next, sizeof(baby)); for (i = 0; i < n; i++) { max = 0.0; for (j = 0; j < n; j++) { if (max < baby[i] * g[j][i]) { max = baby[i] * g[j][i]; } } baby_next[i] = max; } // for (i = 0; i < n; i++){ // printf("<%.2lf>", baby_next[i]); //} // printf("\n"); } memcpy(baby, baby_next, sizeof(baby)); max = 0.0; for (i = 0; i < n; i++) { if (max < baby[i]) { max = baby[i]; } } printf("%.2lf\n", max); } return (0); }
#include <stdio.h> #include <string.h> int main(void) { int n, m; double g[100][100]; double baby[100], baby_next[100]; int i, j, k; double max; while (1) { scanf("%d%d", &n, &m); if (n == 0 && m == 0) { break; } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%lf", &g[i][j]); } } for (i = 0; i < n; i++) { baby_next[i] = 1.0; } for (k = 0; k < m - 1; k++) { memcpy(baby, baby_next, sizeof(baby)); for (i = 0; i < n; i++) { max = 0.0; for (j = 0; j < n; j++) { if (max < baby[j] * g[j][i]) { max = baby[j] * g[j][i]; // printf("<%d->%d %.2lf>", j+1, i+1,max); } } // printf("|"); baby_next[i] = max; } // printf("\n"); } memcpy(baby, baby_next, sizeof(baby)); max = 0.0; for (i = 0; i < n; i++) { if (max < baby[i]) { max = baby[i]; } } printf("%.2lf\n", max); } return (0); }
[["-", 15, 23, 0, 16, 12, 16, 31, 69, 71, 22], ["+", 15, 23, 0, 16, 12, 16, 31, 69, 71, 22], ["-", 0, 1, 0, 11, 12, 16, 31, 69, 71, 22], ["+", 0, 1, 0, 11, 12, 16, 31, 69, 71, 22]]
0
319
4
#include <stdio.h> #define Double double #define Int int #define For for #define Scanf scanf #define If if #define Printf printf #define Roop(i, n) for (i = 0; i < n; i++) #define Roop2(i, n) for (i = 1; i <= n; i++) #define Roop3(i, n) for (i = 2; i <= n; i++) Double dp[101][101], x[101][101]; int main() { Int i, j, k, n, m; Double max = 0.0; while (scanf("%d%d", &n, &m), n + m) { Roop(i, n) Roop(j, n) Scanf("%lf", &x[i][j]); Roop2(i, n) Roop(j, n) dp[i][j] = (double)(i == 1); Roop3(i, m) Roop(j, n) Roop(k, n) If(dp[i][j] < dp[i - 1][k] * x[k][j]) dp[i][j] = dp[i - 1][k] * x[k][j]; max = 0.0; Roop(i, n) if (max < dp[m][i]) max = dp[m][i]; Printf("%.2lf\n", max); } return 0; }
#include <stdio.h> #define Double double #define Int int #define For for #define Scanf scanf #define If if #define Printf printf #define Roop(i, n) for (i = 0; i < n; i++) #define Roop2(i, n) for (i = 1; i <= n; i++) #define Roop3(i, n) for (i = 2; i <= n; i++) Double dp[101][101], x[101][101]; int main() { Int i, j, k, n, m; Double max = 0.0; while (scanf("%d%d", &n, &m), n + m) { Roop(i, n) Roop(j, n) Scanf("%lf", &x[i][j]); Roop2(i, m) Roop(j, n) dp[i][j] = (double)(i == 1); Roop3(i, m) Roop(j, n) Roop(k, n) If(dp[i][j] < dp[i - 1][k] * x[k][j]) dp[i][j] = dp[i - 1][k] * x[k][j]; max = 0.0; Roop(i, n) if (max < dp[m][i]) max = dp[m][i]; Printf("%.2lf\n", max); } return 0; }
[["-", 8, 9, 0, 42, 0, 2, 3, 4, 0, 22], ["+", 8, 9, 0, 42, 0, 2, 3, 4, 0, 22]]
0
279
2
#include <algorithm> #include <iomanip> #include <iostream> using namespace std; double dp[100][100], g[100][100]; int main() { int n, m; while (cin >> n >> m, n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> g[i][j]; } } fill_n(dp[0], n, 1.0); for (int i = 1; i < n; i++) for (int j = 0; j < n; j++) dp[i][j] = 0; for (int i = 1; i < m; i++) { //回数 for (int prev = 0; prev < n; prev++) { // prev for (int now = 0; now < n; now++) { // now dp[i][now] = max(dp[i][now], dp[i - 1][prev] * g[prev][now]); } } } cout << fixed << setprecision(2) << *max_element(dp[m - 1], dp[m - 1] + n) << endl; } }
#include <algorithm> #include <iomanip> #include <iostream> using namespace std; double dp[100][100], g[100][100]; int main() { int n, m; while (cin >> n >> m, n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> g[i][j]; } } fill_n(dp[0], n, 1.0); for (int i = 1; i < m; i++) for (int j = 0; j < n; j++) dp[i][j] = 0; for (int i = 1; i < m; i++) { //回数 for (int prev = 0; prev < n; prev++) { // prev for (int now = 0; now < n; now++) { // now dp[i][now] = max(dp[i][now], dp[i - 1][prev] * g[prev][now]); } } } cout << fixed << setprecision(2) << *max_element(dp[m - 1], dp[m - 1] + n) << endl; } }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22]]
1
261
2
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef tuple<int, int> duo; #define TT template <typename T> #define ET(T) typename T::value_type TT inline T sq(T x) { return x * x; } TT inline T In() { T x; cin >> x; return x; } TT inline int ubnd(T &v, ET(T) x) { return upper_bound(begin(v), end(v), x) - begin(v); } TT inline int lbnd(T &v, ET(T) x) { return lower_bound(begin(v), end(v), x) - begin(v); } TT inline void sort(T &v) { sort(begin(v), end(v)); } TT inline void revs(T &v) { reverse(begin(v), end(v)); } TT inline void uniq(T &v) { sort(v); v.erase(unique(begin(v), end(v)), end(v)); } TT inline void inpt(T &v, int n, function<ET(T)()> f = In<ET(T)>) { for (v.reserve(n); n--; v.emplace_back(f())) ; } TT inline void show(T &v, string d = " ", string e = "\n") { int i = 0; for (auto &x : v) i++ && (cout << d), cout << x; cout << e; } #undef TT #undef ET inline void fast_io() { ios::sync_with_stdio(0); cin.tie(0); } inline int in() { int x; scanf("%d", &x); return x; } inline string instr() { string x; cin >> x; return x; } inline ll pow_mod(ll a, ll k, ll m) { ll r = 1; for (; k > 0; a = a * a % m, k >>= 1) if (k & 1) r = r * a % m; return r; } inline ll mod_inv(ll a, ll p) { return pow_mod(a, p - 2, p); } const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1}; const int Mod = 1000000000; int main() { int n, m; while (cin >> n >> m, n) { ld table[128][128]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> table[i][j]; } } ld dp[128][128]; for (int i = 0; i <= m; i++) { for (int j = 0; j < n; j++) { dp[i][j] = 0; } } for (int i = 0; i < n; i++) { dp[1][i] = 1.0; } for (int i = 2; i <= m; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { dp[i][j] = max(dp[i][j], dp[i - 1][k] * table[k][j]); } } } printf("%Lf\n", *max_element(dp[m], dp[m] + n)); } return (0); } // priority_queue queue deque front top push pop map set stringstream // max_element min_element insert count find get tie make_tuple
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef tuple<int, int> duo; #define TT template <typename T> #define ET(T) typename T::value_type TT inline T sq(T x) { return x * x; } TT inline T In() { T x; cin >> x; return x; } TT inline int ubnd(T &v, ET(T) x) { return upper_bound(begin(v), end(v), x) - begin(v); } TT inline int lbnd(T &v, ET(T) x) { return lower_bound(begin(v), end(v), x) - begin(v); } TT inline void sort(T &v) { sort(begin(v), end(v)); } TT inline void revs(T &v) { reverse(begin(v), end(v)); } TT inline void uniq(T &v) { sort(v); v.erase(unique(begin(v), end(v)), end(v)); } TT inline void inpt(T &v, int n, function<ET(T)()> f = In<ET(T)>) { for (v.reserve(n); n--; v.emplace_back(f())) ; } TT inline void show(T &v, string d = " ", string e = "\n") { int i = 0; for (auto &x : v) i++ && (cout << d), cout << x; cout << e; } #undef TT #undef ET inline void fast_io() { ios::sync_with_stdio(0); cin.tie(0); } inline int in() { int x; scanf("%d", &x); return x; } inline string instr() { string x; cin >> x; return x; } inline ll pow_mod(ll a, ll k, ll m) { ll r = 1; for (; k > 0; a = a * a % m, k >>= 1) if (k & 1) r = r * a % m; return r; } inline ll mod_inv(ll a, ll p) { return pow_mod(a, p - 2, p); } const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1}; const int Mod = 1000000000; int main() { int n, m; while (cin >> n >> m, n) { ld table[128][128]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> table[i][j]; } } ld dp[128][128]; for (int i = 0; i <= m; i++) { for (int j = 0; j < n; j++) { dp[i][j] = 0; } } for (int i = 0; i < n; i++) { dp[1][i] = 1.0; } for (int i = 2; i <= m; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { dp[i][j] = max(dp[i][j], dp[i - 1][k] * table[k][j]); } } } printf("%.2Lf\n", *max_element(dp[m], dp[m] + n)); } return (0); } // priority_queue queue deque front top push pop map set stringstream // max_element min_element insert count find get tie make_tuple
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
806
2
#include <algorithm> #include <cstdio> using namespace std; double growth[200][200]; double dp[200][200]; int main() { int n, m; while (scanf("%d %d", &n, &m), n + m) { for (int i = 0; i < 200; i++) { dp[0][i] = 1.0; for (int j = 1; j < 200; j++) { dp[j][i] = 0.0; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%lf", &growth[i][j]); } } for (int i = 0; i < m - 1; i++) { // printf("%d\n", i); for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { // printf("%lf\n", dp[i][j] * //growth[k][j]); dp[i + 1][j] = max(dp[i + 1][j], dp[i][k] * growth[k][j]); } // printf("%d %lf\n", j, dp[i + 1][j]); } } double maxi = 0.0; for (int i = 0; i < n; i++) { maxi = max(maxi, dp[m - 1][i]); } printf("%lf\n", maxi); } return 0; }
#include <algorithm> #include <cstdio> using namespace std; double growth[200][200]; double dp[200][200]; int main() { int n, m; while (scanf("%d %d", &n, &m), n + m) { for (int i = 0; i < 200; i++) { dp[0][i] = 1.0; for (int j = 1; j < 200; j++) { dp[j][i] = 0.0; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%lf", &growth[i][j]); } } for (int i = 0; i < m - 1; i++) { // printf("%d\n", i); for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { // printf("%lf\n", dp[i][j] * //growth[k][j]); dp[i + 1][j] = max(dp[i + 1][j], dp[i][k] * growth[k][j]); } // printf("%d %lf\n", j, dp[i + 1][j]); } } double maxi = 0.0; for (int i = 0; i < n; i++) { maxi = max(maxi, dp[m - 1][i]); } printf("%.2lf\n", maxi); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
302
2
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int main() { int n, m; while (cin >> n >> m, n || m) { double gr[128][128] = {0}; double dptab[128][128] = {0}; for (int i = 0; i < 128; i++) { dptab[0][i] = 1.0; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> gr[i][j]; } } for (int i = 1; i < m; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { dptab[i][j] = max(dptab[i][j], dptab[i - 1][j] * gr[k][j]); } } } double ans = -1; for (int i = 0; i < n; i++) { ans = max(dptab[m - 1][i], ans); } printf("%.2f\n", ans); } }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int main() { int n, m; while (cin >> n >> m, n || m) { double gr[128][128] = {0}; double dptab[128][128] = {0}; for (int i = 0; i < 128; i++) { dptab[0][i] = 1.0; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> gr[i][j]; } } for (int i = 1; i < m; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { dptab[i][j] = max(dptab[i][j], dptab[i - 1][k] * gr[k][j]); } } } double ans = -1; for (int i = 0; i < n; i++) { ans = max(dptab[m - 1][i], ans); } printf("%.2f\n", ans); } }
[["-", 3, 4, 0, 16, 31, 69, 341, 342, 0, 22], ["+", 3, 4, 0, 16, 31, 69, 341, 342, 0, 22]]
1
262
2
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define mp make_pair #define all(in) in.begin(), in.end() const double PI = acos(-1); const double EPS = 1e-10; const int inf = 1e9; using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; int main() { int n, m; while (cin >> n >> m, n + m) { double dp[110][110] = {0}; double in[110][110] = {0}; rep(i, n) rep(j, n) cin >> in[i][j]; rep(i, 110) dp[0][i] = 1; loop(i, 1, m) { rep(j, n) rep(k, n) dp[i][k] = max(dp[i][k], dp[i - 1][j] * in[j][k]); } double ma = -1; rep(i, n) ma = max(ma, dp[n - 1][i]); printf("%.2f\n", ma); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define mp make_pair #define all(in) in.begin(), in.end() const double PI = acos(-1); const double EPS = 1e-10; const int inf = 1e9; using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; int main() { int n, m; while (cin >> n >> m, n + m) { double dp[110][110] = {0}; double in[110][110] = {0}; rep(i, n) rep(j, n) cin >> in[i][j]; rep(i, 110) dp[0][i] = 1; loop(i, 1, m) { rep(j, n) rep(k, n) dp[i][k] = max(dp[i][k], dp[i - 1][j] * in[j][k]); } double ma = -1; rep(i, n) ma = max(ma, dp[m - 1][i]); printf("%.2f\n", ma); } }
[["-", 0, 69, 28, 69, 341, 342, 0, 16, 31, 22], ["+", 0, 69, 28, 69, 341, 342, 0, 16, 31, 22]]
1
294
2
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define mp make_pair #define all(in) in.begin(), in.end() #define shosu(x) fixed << setprecision(x) using namespace std; // kaewasuretyuui typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef pair<int, pii> pip; typedef vector<pip> vip; const double PI = acos(-1); const double EPS = 1e-8; const int inf = 1 << 30; int main() { int n, m; while (cin >> n >> m, n + m) { double in[110][110] = {0}, dp[110][110] = {0}; rep(i, n) rep(j, n) cin >> in[i][j]; rep(i, n) dp[0][i] = 1; rep(i, m - 1) rep(j, n) rep(k, n) dp[i + 1][k] = max(dp[i + 1][j], dp[i][j] * in[j][k]); double out = 0; rep(i, n) out = max(out, dp[m - 1][i]); cout << shosu(2) << out << endl; } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define mp make_pair #define all(in) in.begin(), in.end() #define shosu(x) fixed << setprecision(x) using namespace std; // kaewasuretyuui typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef pair<int, pii> pip; typedef vector<pip> vip; const double PI = acos(-1); const double EPS = 1e-8; const int inf = 1 << 30; int main() { int n, m; while (cin >> n >> m, n + m) { double in[110][110] = {0}, dp[110][110] = {0}; rep(i, n) rep(j, n) cin >> in[i][j]; rep(i, n) dp[0][i] = 1; rep(i, m - 1) rep(j, n) rep(k, n) dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] * in[j][k]); double out = 0; rep(i, n) out = max(out, dp[m - 1][i]); cout << shosu(2) << out << endl; } }
[["-", 12, 2, 3, 4, 0, 69, 341, 342, 0, 22], ["+", 12, 2, 3, 4, 0, 69, 341, 342, 0, 22]]
1
338
2
#include <cmath> #include <cstdio> #include <iostream> using namespace std; int n, m; double g[100][100]; double dp[101][100]; double rec(int a, int b) { // a回目,前に与えた肥料b if (dp[a][b] > 0) return dp[a][b]; double ret = 1.0; if (a == m - 1) ret = 1.0; else { double t = 0; for (int i = 0; i < n; ++i) { double s = g[b][i] * rec(a + 1, i); // printf("s=%lf\n", s); if (s > t) t = s; } ret *= t; // printf("t=%lf\n", t); } return dp[a][b] = ret; } int main() { while (1) { scanf(" %d %d", &n, &m); if (n == 0) break; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) scanf(" %lf", &g[i][j]); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) dp[i][j] = -1.0; double ans = 0; for (int i = 0; i < n; ++i) { double tmp = rec(0, i); if (tmp > ans) ans = tmp; } /* for(int i=0; i<n; ++i){ for(int j=0; j<n; ++j){ printf("%.4lf ", dp[i][j]); } printf("\n"); } */ ans *= 100; ans = round(ans); ans /= 100; printf("%.2lf\n", ans); } }
#include <cmath> #include <cstdio> #include <iostream> using namespace std; int n, m; double g[100][100]; double dp[101][100]; double rec(int a, int b) { // a回目,前に与えた肥料b if (dp[a][b] > 0) return dp[a][b]; double ret = 1.0; if (a == m - 1) ret = 1.0; else { double t = -1; for (int i = 0; i < n; ++i) { double s = g[b][i] * rec(a + 1, i); // printf("s=%lf\n", s); if (s > t) t = s; } ret = t; // printf("t=%lf\n", t); } return dp[a][b] = ret; } int main() { while (1) { scanf(" %d %d", &n, &m); if (n == 0) break; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) scanf(" %lf", &g[i][j]); for (int i = 0; i < 101; ++i) for (int j = 0; j < 100; ++j) dp[i][j] = -1.0; double ans = 0; for (int i = 0; i < n; ++i) { double tmp = rec(0, i); if (tmp > ans) ans = tmp; } /* for(int i=0; i<m; ++i){ for(int j=0; j<n; ++j){ printf("%.4lf ", dp[i][j]); } printf("\n"); } */ ans *= 100; ans = round(ans); ans /= 100; printf("%.2lf\n", ans); } }
[["-", 75, 76, 0, 9, 0, 43, 49, 50, 51, 13], ["+", 75, 76, 0, 9, 0, 43, 49, 50, 51, 13], ["-", 75, 76, 0, 9, 0, 1, 0, 11, 17, 108], ["+", 75, 76, 0, 9, 0, 1, 0, 11, 17, 32], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 8, 9, 0, 7, 8, 7, 15, 16, 12, 22], ["+", 8, 9, 0, 7, 8, 7, 15, 16, 12, 13]]
1
336
8
#include <cstdio> #include <iostream> using namespace std; int main() { for (;;) { int n, m; cin >> n >> m; if (n == 0) { return 0; } double map[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> map[i][j]; } } double dp[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { dp[i][j] = -1.0; } } for (int i = 0; i < n; i++) { dp[0][i] = 1.0; } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] * map[j][i]); } } } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { // cout << dp[i][j] << " "; } // cout << endl; } double max1 = -1.0; for (int i = 0; i < n; i++) { max1 = max(max1, dp[m - 1][i]); } // printf("%f",max1); printf("%2f\n", max1); } }
#include <cstdio> #include <iostream> using namespace std; int main() { for (;;) { int n, m; cin >> n >> m; if (n == 0) { return 0; } double map[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> map[i][j]; } } double dp[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { dp[i][j] = -1.0; } } for (int i = 0; i < n; i++) { dp[0][i] = 1.0; } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] * map[j][k]); } } } // cout << endl; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { // cout << dp[i][j] << " "; } // cout << endl; } double max1 = -1.0; for (int i = 0; i < n; i++) { max1 = max(max1, dp[m - 1][i]); } // printf("%f",max1); printf("%.2f\n", max1); } }
[["-", 3, 4, 0, 16, 12, 69, 341, 342, 0, 22], ["+", 3, 4, 0, 16, 12, 69, 341, 342, 0, 22], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
341
4
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(a) (a).begin(), (a).end() #define vi vector<int> #define pb push_back #define INF 999999999 //#define INF (1LL<<59) int main() { int n, m; while (cin >> n >> m && (n || m)) { double table[101][101]; rep(i, n) rep(j, n) cin >> table[i][j]; double dp[101][101]; rep(i, 101) rep(j, 101) dp[i][j] = -1; rep(i, n) dp[0][i] = 1; for (int i = 1; i < m; i++) { rep(j, n) { rep(k, n) { dp[i][j] = max(dp[i - 1][k] * table[k][j], dp[i][j]); } } } double ans = -1; rep(i, n) { ans = max(ans, dp[m - 1][i]); } printf("%.10lf\n", ans); } }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(a) (a).begin(), (a).end() #define vi vector<int> #define pb push_back #define INF 999999999 //#define INF (1LL<<59) int main() { int n, m; while (cin >> n >> m && (n || m)) { double table[101][101]; rep(i, n) rep(j, n) cin >> table[i][j]; double dp[101][101]; rep(i, 101) rep(j, 101) dp[i][j] = -1; rep(i, n) dp[0][i] = 1; for (int i = 1; i < m; i++) { rep(j, n) { rep(k, n) { dp[i][j] = max(dp[i - 1][k] * table[k][j], dp[i][j]); } } } double ans = -1; rep(i, n) { ans = max(ans, dp[m - 1][i]); } printf("%.2lf\n", ans); } }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
263
2
#include <cfloat> #include <cmath> #include <iomanip> #include <iostream> using namespace std; const int NMAX = 100; int main() { int n, m; double dp[2][NMAX]; double g[NMAX][NMAX]; double lg[NMAX][NMAX]; while (1) { cin >> n >> m; if (!n && !m) { break; } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> g[i][j]; lg[i][j] = log(g[i][j]); } } for (int i = 0; i < n; ++i) { dp[0][i] = 0.0; } for (int i = 0; i < m - 1; ++i) { for (int j = 0; j < n; ++j) { dp[!(i & 1)][j] = dp[i & 1][j] + lg[j][0]; for (int k = 1; k < n; ++k) { dp[!(i & 1)][j] = max(dp[!(i & 1)][j], dp[i & 1][k] + lg[k][j]); } } } double lans = -DBL_MAX; for (int i = 0; i < n; ++i) { lans = max(lans, dp[!(m & 1)][i]); } cout << fixed << setprecision(2) << round(exp(lans) * 100) / 100. << endl; } return 0; }
#include <cfloat> #include <cmath> #include <iomanip> #include <iostream> using namespace std; const int NMAX = 100; int main() { int n, m; double dp[2][NMAX]; double g[NMAX][NMAX]; double lg[NMAX][NMAX]; while (1) { cin >> n >> m; if (!n && !m) { break; } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> g[i][j]; lg[i][j] = log(g[i][j]); } } for (int i = 0; i < n; ++i) { dp[0][i] = 0.0; } for (int i = 0; i < m - 1; ++i) { for (int j = 0; j < n; ++j) { dp[!(i & 1)][j] = dp[i & 1][0] + lg[0][j]; for (int k = 0; k < n; ++k) { dp[!(i & 1)][j] = max(dp[!(i & 1)][j], dp[i & 1][k] + lg[k][j]); } } } double lans = -DBL_MAX; for (int i = 0; i < n; ++i) { lans = max(lans, dp[!(m & 1)][i]); } cout << fixed << setprecision(2) << round(exp(lans) * 100) / 100. << endl; } return 0; }
[["-", 0, 11, 12, 16, 31, 69, 341, 342, 0, 22], ["+", 0, 11, 12, 16, 31, 69, 341, 342, 0, 13], ["-", 12, 16, 12, 69, 28, 69, 341, 342, 0, 22], ["+", 12, 16, 12, 69, 28, 69, 341, 342, 0, 13], ["-", 0, 11, 12, 16, 12, 69, 341, 342, 0, 13], ["+", 0, 11, 12, 16, 12, 69, 341, 342, 0, 22], ["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
363
8
#include <algorithm> #include <cmath> #include <cstdlib> #include <iomanip> #include <iostream> #include <string> using namespace std; int main() { while (1) { int n, m; cin >> n >> m; if (n == 0 && m == 0) break; double data[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> data[i][j]; } } double dp[n][m]; for (int i = 0; i < n; i++) dp[i][0] = 1; for (int i = 1; i < m; i++) { for (int j = 0; j < n; j++) { double max = 0; for (int k = 0; k < n; k++) { double value = dp[k][i - 1] * data[k][j]; if (value > max) max = value; } dp[j][i] = max; } } double ans = 0; for (int i = 0; i < n; i++) { if (ans < dp[i][m - 1]) ans = dp[i][m - 1]; } cout << fixed << setprecision(2) << ans; } return 0; }
#include <algorithm> #include <cmath> #include <cstdlib> #include <iomanip> #include <iostream> #include <string> using namespace std; int main() { while (1) { int n, m; cin >> n >> m; if (n == 0 && m == 0) break; double data[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> data[i][j]; } } double dp[n][m]; for (int i = 0; i < n; i++) dp[i][0] = 1.0; for (int i = 1; i < m; i++) { for (int j = 0; j < n; j++) { double max = 0; for (int k = 0; k < n; k++) { double value = dp[k][i - 1] * data[k][j]; if (value > max) max = value; } dp[j][i] = max; } } double ans = 0; for (int i = 0; i < n; i++) { if (ans < dp[i][m - 1]) ans = dp[i][m - 1]; } cout << fixed << setprecision(2) << ans << endl; } return 0; }
[["-", 8, 9, 0, 7, 8, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 7, 8, 1, 0, 11, 12, 13], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 17, 151], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 12, 22]]
1
290
4
#include <bits/stdc++.h> using namespace std; #define MAX 150 int main() { int N, M; while (cin >> N >> M, N) { double g[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> g[i][j]; } } double dp[MAX][MAX]; for (int i = 0; i < MAX; i++) { for (int j = 0; j < MAX; j++) { dp[i][j] = (j == 0 ? 1 : 0); } } for (int i = 0; i < M - 1; i++) { for (int j = 0; j < N; j++) { for (int k = 0; k < N; k++) { dp[k][i + 1] = max(dp[k][i + 1], dp[j][i] * g[j][k]); } } } double res = 0; for (int i = 0; i < N; i++) { res = max(res, dp[i][M - 1]); } printf("%.10f\n", res); } return 0; }
#include <bits/stdc++.h> using namespace std; #define MAX 150 int main() { int N, M; while (cin >> N >> M, N) { double g[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> g[i][j]; } } double dp[MAX][MAX]; for (int i = 0; i < MAX; i++) { for (int j = 0; j < MAX; j++) { dp[i][j] = (j == 0 ? 1 : 0); } } for (int i = 0; i < M - 1; i++) { for (int j = 0; j < N; j++) { for (int k = 0; k < N; k++) { dp[k][i + 1] = max(dp[k][i + 1], dp[j][i] * g[j][k]); } } } double res = 0; for (int i = 0; i < N; i++) { res = max(res, dp[i][M - 1]); } printf("%.2f\n", res); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
282
2
#include <algorithm> #include <iomanip> #include <iostream> using namespace std; int n, m; double e[111][111], dp[111][111]; int main() { while (cin >> n >> m) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cin >> e[i][j]; } for (int i = 0; i < n; i++) dp[0][i] = 1.0; for (int i = 1; i < m; i++) { for (int j = 0; j < n; j++) { dp[i][j] = 0.0; for (int k = 0; k < n; k++) { dp[i][j] = max(dp[i][j], dp[i - 1][k] * e[k][j]); } } } cout << fixed << setprecision(2) << *max_element(dp[m - 1], dp[m - 1] + n) << endl; } return 0; }
#include <algorithm> #include <iomanip> #include <iostream> using namespace std; int n, m; double e[111][111], dp[111][111]; int main() { while (cin >> n >> m, n | m) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cin >> e[i][j]; } for (int i = 0; i < n; i++) dp[0][i] = 1.0; for (int i = 1; i < m; i++) { for (int j = 0; j < n; j++) { dp[i][j] = 0.0; for (int k = 0; k < n; k++) { dp[i][j] = max(dp[i][j], dp[i - 1][k] * e[k][j]); } } } cout << fixed << setprecision(2) << *max_element(dp[m - 1], dp[m - 1] + n) << endl; } return 0; }
[["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 0, 52, 15, 339, 51, 34, 12, 16, 31, 22], ["+", 0, 52, 15, 339, 51, 34, 12, 16, 17, 139], ["+", 0, 52, 15, 339, 51, 34, 12, 16, 12, 22]]
1
241
4
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <stack> #include <string> #include <utility> #include <vector> #define ll long long int #define ld long long double #define INF 1000000000 #define EPS 0.0000000001 #define rep(i, n) for (i = 0; i < n; i++) using namespace std; typedef pair<int, int> P; string str; int main() { int n, m; int i, j, k; double dp[105][105]; double grow[105][105]; int maxi[105]; //??????????????§???????????¨????????´????????\??? while (1) { cin >> n >> m; if (n == 0 && m == 0) break; rep(i, n) rep(j, n) cin >> grow[i][j]; double temp; int tempi; rep(j, n) { temp = 0; rep(i, n) if (temp < grow[i][j]) { temp = grow[i][j]; tempi = i; } maxi[j] = tempi; } rep(j, n) dp[0][j] = 1.0; for (i = 1; i < m; i++) for (j = 0; j < n; j++) { temp = 0; rep(k, n) temp = max(temp, dp[i - 1][k] * grow[k][j]); dp[i][j] = temp; } double sum = 0; rep(j, n) sum = max(sum, dp[n - 1][j]); printf("%.2f\n", sum); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <stack> #include <string> #include <utility> #include <vector> #define ll long long int #define ld long long double #define INF 1000000000 #define EPS 0.0000000001 #define rep(i, n) for (i = 0; i < n; i++) using namespace std; typedef pair<int, int> P; string str; int main() { int n, m; int i, j, k; double dp[105][105]; double grow[105][105]; int maxi [105]; //テヲツ按静ゥツ閉キテ」ツ?古ヲツ慊?・ツ、ツァテ」ツ?ォテ」ツ?ェテ」ツつ凝」ツ?ィテ」ツ?催」ツ?ョテァツ崢エテ・ツ可催」ツ?ョティツつ・テヲツ鳴? while (1) { cin >> n >> m; if (n == 0 && m == 0) break; rep(i, n) rep(j, n) cin >> grow[i][j]; double temp; int tempi; rep(j, n) { temp = 0; rep(i, n) if (temp < grow[i][j]) { temp = grow[i][j]; tempi = i; } maxi[j] = tempi; } rep(j, n) dp[0][j] = 1.0; for (i = 1; i < m; i++) for (j = 0; j < n; j++) { temp = 0; rep(k, n) temp = max(temp, dp[i - 1][k] * grow[k][j]); dp[i][j] = temp; } double sum = 0; rep(j, n) sum = max(sum, dp[m - 1][j]); printf("%.2f\n", sum); } }
[["-", 0, 69, 28, 69, 341, 342, 0, 16, 31, 22], ["+", 0, 69, 28, 69, 341, 342, 0, 16, 31, 22]]
1
332
2
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <utility> #include <vector> #define ll (long long) #define REP(i, n) for (int i = 0; i < (int)n; i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (a); i > (b); i--) #include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <utility> #include <vector> #define ll (long long) #define REP(i, n) for (int i = 0; i < (int)n; i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (a); i > (b); i--) using namespace std; int main(void) { int drugnumber; int drugcounter; /* x????¨????,y?????´????????? */ double drugeffect[120][120] = {0}; int i, j, k, l; double max = 0; double answer[100] = {0}; while (cin >> drugnumber >> drugcounter, drugnumber + drugnumber) { double combination[120][120] = {0}; /*??£??\*/ FOR(j, 1, drugnumber + 1) FOR(i, 1, drugnumber + 1) cin >> drugeffect[j][i]; // cout << drugeffect[2][1]; for (k = 1; k <= drugnumber; k++) combination[1][k] = 1.0; // cout << combination[1][2] << endl; /* j?????¬?????°,i????????°*/ for (i = 1; i < drugcounter; i++) for (j = 1; j <= drugnumber; j++) { for (k = 1; k <= drugnumber; k++) { if ((combination[i][j] * drugeffect[j][k]) >= combination[i + 1][j]) combination[i + 1][j] = combination[i][j] * drugeffect[j][k]; // cout << combination[i+1][j] << " "; /* if((combination[j][i] * drugeffect[j][i]) >= combination[j][i]) combination[j][i+1] = combination[j][i] * drugeffect[j][i]; */ } } for (i = 1; i <= drugnumber; i++) if (max < combination[drugcounter][i]) max = combination[drugcounter][i]; answer[l] = max; max = 0; l++; } REP(i, l) printf("%.2f\n", answer[i]); return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <utility> #include <vector> #define ll (long long) #define REP(i, n) for (int i = 0; i < (int)n; i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (a); i > (b); i--) #include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <utility> #include <vector> #define ll (long long) #define REP(i, n) for (int i = 0; i < (int)n; i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (a); i > (b); i--) using namespace std; int main(void) { int drugnumber; int drugcounter; /* x????¨????,y?????´????????? */ double drugeffect[120][120] = {0}; int i, j, k, l; double max = 0; double answer[100] = {0}; while (cin >> drugnumber >> drugcounter, drugnumber + drugnumber) { double combination[120][120] = {0}; /*??£??\*/ FOR(j, 1, drugnumber + 1) FOR(i, 1, drugnumber + 1) cin >> drugeffect[j][i]; // cout << drugeffect[2][1]; for (k = 1; k <= drugnumber; k++) combination[1][k] = 1.0; // cout << combination[1][2] << endl; /* j?????¬?????°,i????????°*/ for (i = 1; i < drugcounter; i++) for (j = 1; j <= drugnumber; j++) { for (k = 1; k <= drugnumber; k++) { if ((combination[i][j] * drugeffect[j][k]) >= combination[i + 1][k]) combination[i + 1][k] = combination[i][j] * drugeffect[j][k]; // cout << combination[i+1][j] << " "; /* if((combination[j][i] * drugeffect[j][i]) >= combination[j][i]) combination[j][i+1] = combination[j][i] * drugeffect[j][i]; */ } } for (i = 1; i <= drugnumber; i++) if (max < combination[drugcounter][i]) max = combination[drugcounter][i]; answer[l] = max; max = 0; l++; } REP(i, l) printf("%.2f\n", answer[i]); return 0; }
[["-", 15, 339, 51, 16, 12, 69, 341, 342, 0, 22], ["+", 15, 339, 51, 16, 12, 69, 341, 342, 0, 22], ["-", 64, 1, 0, 11, 31, 69, 341, 342, 0, 22], ["+", 64, 1, 0, 11, 31, 69, 341, 342, 0, 22]]
1
400
4
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> int n, m; double grow[100][100]; double dp[110][110]; int main() { while (std::cin >> n >> m, n + m) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { std::cin >> grow[i][j]; } } for (int i = 0; i < n; ++i) { dp[0][i] = 1.; } for (int i = 1; i < n; ++i) { for (int j = 0; j < n; ++j) { dp[i][j] = 0.; } } for (int i = 1; i < m; ++i) { for (int j = 0; j < n; ++j) { for (int k = 0; k < n; ++k) { dp[i][j] = std::max(dp[i][j], dp[i - 1][k] * grow[k][j]); } } } printf("%.2f\n", ((int)(*std::max_element(dp[m - 1], dp[m - 1] + n) * 100 + 0.5)) / 100.); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> int n, m; double grow[100][100]; double dp[110][110]; int main() { while (std::cin >> n >> m, n + m) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { std::cin >> grow[i][j]; } } for (int i = 0; i < n; ++i) { dp[0][i] = 1.; } for (int i = 1; i < m; ++i) { for (int j = 0; j < n; ++j) { dp[i][j] = 0.; } } for (int i = 1; i < m; ++i) { for (int j = 0; j < n; ++j) { for (int k = 0; k < n; ++k) { dp[i][j] = std::max(dp[i][j], dp[i - 1][k] * grow[k][j]); } } } printf( "%.2lf\n", ((long long)(*std::max_element(dp[m - 1], dp[m - 1] + n) * 100 + 0.5)) / 100.); } }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["-", 0, 16, 31, 23, 0, 74, 39, 77, 39, 40], ["+", 31, 23, 0, 74, 39, 77, 39, 86, 0, 96]]
1
295
7
#include <bits/stdc++.h> using namespace std; #define pb(n) push_back(n) #define fi first #define se second #define all(r) begin(r), end(r) #define vmax(ary) *max_element(all(ary)) #define vmin(ary) *min_element(all(ary)) #define debug(x) cout << #x << ": " << x << endl #define fcout(n) cout << fixed << setprecision((n)) #define scout(n) cout << setw(n) #define vary(type, name, size, init) vector<type> name(size, init) #define vvl(v, w, h, init) vector<vector<ll>> v(w, vector<ll>(h, init)) #define mp(a, b) make_pair(a, b) #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define REP(i, a, b) for (int i = (a); i < (int)(b); ++i) #define repi(it, array) \ for (auto it = array.begin(), end = array.end(); it != end; ++it) #define repa(n, array) for (auto &n : (array)) using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; const int mod = 1000000007; constexpr ll inf = ((1 << 30) - 1) * 2 + 1; constexpr double PI = acos(-1.0); double eps = 1e-10; const int dy[] = {-1, 0, 1, 0, 1, -1, 1, -1}; const int dx[] = {0, -1, 0, 1, 1, -1, -1, 1}; inline bool value(int x, int y, int w, int h) { return (x >= 0 && x < w && y >= 0 && y < h); } ll n, m; int f(int x, int y) { return x + y * n; } int main() { cin.tie(0); ios::sync_with_stdio(false); while (cin >> n >> m && n + m) { vector<vector<double>> v(n, vector<double>(n)); rep(i, n) { rep(j, n) { cin >> v[i][j]; } } vector<vector<double>> dp(m + 1, vector<double>(n, 0)); rep(i, n) { dp[0][i] = 1.0; } rep(i, m) { rep(j, n) { rep(k, n) { dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] * v[j][k]); } } } double ans = 0; rep(i, n) { ans = max(ans, dp[m - 1][i]); } fcout(10) << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define pb(n) push_back(n) #define fi first #define se second #define all(r) begin(r), end(r) #define vmax(ary) *max_element(all(ary)) #define vmin(ary) *min_element(all(ary)) #define debug(x) cout << #x << ": " << x << endl #define fcout(n) cout << fixed << setprecision((n)) #define scout(n) cout << setw(n) #define vary(type, name, size, init) vector<type> name(size, init) #define vvl(v, w, h, init) vector<vector<ll>> v(w, vector<ll>(h, init)) #define mp(a, b) make_pair(a, b) #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define REP(i, a, b) for (int i = (a); i < (int)(b); ++i) #define repi(it, array) \ for (auto it = array.begin(), end = array.end(); it != end; ++it) #define repa(n, array) for (auto &n : (array)) using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; const int mod = 1000000007; constexpr ll inf = ((1 << 30) - 1) * 2 + 1; constexpr double PI = acos(-1.0); double eps = 1e-10; const int dy[] = {-1, 0, 1, 0, 1, -1, 1, -1}; const int dx[] = {0, -1, 0, 1, 1, -1, -1, 1}; inline bool value(int x, int y, int w, int h) { return (x >= 0 && x < w && y >= 0 && y < h); } ll n, m; int f(int x, int y) { return x + y * n; } int main() { cin.tie(0); ios::sync_with_stdio(false); while (cin >> n >> m && n + m) { vector<vector<double>> v(n, vector<double>(n)); rep(i, n) { rep(j, n) { cin >> v[i][j]; } } vector<vector<double>> dp(m + 1, vector<double>(n, 0)); rep(i, n) { dp[0][i] = 1.0; } rep(i, m) { rep(j, n) { rep(k, n) { dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] * v[j][k]); } } } double ans = 0; rep(i, n) { ans = max(ans, dp[m - 1][i]); } fcout(2) << ans << endl; } return 0; }
[["-", 0, 16, 31, 16, 31, 2, 3, 4, 0, 13], ["+", 0, 16, 31, 16, 31, 2, 3, 4, 0, 13]]
1
520
2
#include <stdio.h> double dp[100][100]; #define REP(i, n) for (int i = 0; i < (n); i++) #define MAX(x, y) ((x) > (y) ? (x) : (y)) int main(void) { double grow[100][100]; //[ツ前ツづ個氾ャツ猟ソ][ツ債。ツづ個氾ャツ猟ソ] int n, m; while (1) { REP(i, 100) dp[0][i] = 1.0; scanf("%d%d", &n, &m); if (n == 0 || m == 0) { break; } REP(i, n) REP(j, n) scanf("%lf", &grow[i][j]); for (int i = 1; i < m; i++) { for (int j = 0; j < n; j++) { dp[i][j] = dp[i - 1][0] * grow[0][j]; for (int k = 1; k < n; k++) { dp[i][j] = MAX(dp[i][j], dp[i - 1][k] * grow[k][j]); } } } double ans = 0; REP(i, n) { ans = MAX(ans, dp[m - 1][i]); } printf("%lf\n", ans); } return (0); }
#include <stdio.h> double dp[100][100]; #define REP(i, n) for (int i = 0; i < (n); i++) #define MAX(x, y) ((x) > (y) ? (x) : (y)) int main(void) { double grow[100][100]; //[ツ前ツづ個氾ャツ猟ソ][ツ債。ツづ個氾ャツ猟ソ] int n, m; while (1) { REP(i, 100) dp[0][i] = 1.0; scanf("%d%d", &n, &m); if (n == 0 || m == 0) { break; } REP(i, n) REP(j, n) scanf("%lf", &grow[i][j]); for (int i = 1; i < m; i++) { for (int j = 0; j < n; j++) { dp[i][j] = dp[i - 1][0] * grow[0][j]; for (int k = 1; k < n; k++) { dp[i][j] = MAX(dp[i][j], dp[i - 1][k] * grow[k][j]); } } } double ans = 0; REP(i, n) { ans = MAX(ans, dp[m - 1][i]); } printf("%.2lf\n", ans); } return (0); }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
284
2
#include <algorithm> #include <iostream> using namespace std; int main() { int n, m; long double r, a[101][101], dp[101][101]; while (cin >> n >> m && (m + n)) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } fill(&dp[0][0], &dp[100][100], 0); for (int i = 0; i < n; i++) { dp[i][0] = 1.0; } for (int k = 1; k < m; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dp[i][k] = max(dp[i][k], dp[i][k - 1] * a[j][i]); } } } r = 0.0; for (int j = 0; j < n; j++) { r = max(r, dp[j][m - 1]); } printf("%.2f\n", r); } return 0; }
#include <algorithm> #include <iostream> using namespace std; int main() { int n, m; long double r, a[101][101], dp[101][101]; while (cin >> n >> m && (m + n)) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } fill(&dp[0][0], &dp[n][m], 0); for (int i = 0; i < n; i++) { dp[i][0] = 1.0; } for (int k = 1; k < m; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dp[i][k] = max(dp[i][k], dp[j][k - 1] * a[j][i]); } } } r = 0.0; for (int j = 0; j < n; j++) { r = max(r, dp[j][m - 1]); } printf("%.2Lf\n", r); } return 0; }
[["-", 0, 66, 28, 69, 28, 69, 341, 342, 0, 13], ["+", 0, 66, 28, 69, 28, 69, 341, 342, 0, 22], ["-", 3, 4, 0, 66, 28, 69, 341, 342, 0, 13], ["+", 3, 4, 0, 66, 28, 69, 341, 342, 0, 22], ["-", 0, 16, 31, 69, 28, 69, 341, 342, 0, 22], ["+", 0, 16, 31, 69, 28, 69, 341, 342, 0, 22], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
281
8
#include <cstdio> #include <iostream> #define INF 0 using namespace std; int main() { int n, m; double t[200][200]; while (cin >> n >> m, n || m) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> t[i][j]; double dp[200][200]; for (int i = 0; i <= n; i++) for (int j = 0; j < n; j++) dp[j][i] = INF; for (int i = 0; i < n; i++) dp[i][0] = 1.0; for (int i = 1; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) dp[j][i] = max(dp[j][i], dp[k][i - 1] * t[k][j]); double ans = INF; for (int i = 0; i < n; i++) ans = max(ans, dp[i][n - 1]); printf("%.2lf\n", ans); } return 0; }
#include <cstdio> #include <iostream> #define INF 0 using namespace std; int main() { int n, m; double t[200][200]; while (cin >> n >> m, n || m) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> t[i][j]; double dp[200][200]; for (int i = 0; i <= m; i++) for (int j = 0; j < n; j++) dp[j][i] = INF; for (int i = 0; i < n; i++) dp[i][0] = 1.0; for (int i = 1; i < m; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) dp[j][i] = max(dp[j][i], dp[k][i - 1] * t[k][j]); double ans = INF; for (int i = 0; i < n; i++) ans = max(ans, dp[i][m - 1]); printf("%.2lf\n", ans); } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["-", 3, 4, 0, 69, 341, 342, 0, 16, 31, 22], ["+", 3, 4, 0, 69, 341, 342, 0, 16, 31, 22]]
1
282
6
#include <algorithm> #include <iomanip> #include <iostream> #include <vector> using namespace std; int main() { cout << setprecision(2); cout << setiosflags(ios::fixed); int n, m, i, j, k; long double M; while (cin >> n >> m, n | m) { vector<vector<long double>> manure(n, vector<long double>(n)), dp(m, vector<long double>(n, 0.0)); for (i = 0; i < n; i++) for (j = 0; j < n; j++) cin >> manure[i][j]; for (j = 0; j < n; j++) dp[0][j] = 1.0; for (i = 1; i < m; i++) { for (j = 0; j < n; j++) { for (k = 0; k < n; k++) { dp[i][j] = max(dp[i][j], dp[i - 1][k] * manure[k][j]); } } } M = 0.0; for (j = 0; j < n; j++) M = max(M, dp[n - 1][j]); cout << M << endl; } }
#include <algorithm> #include <iomanip> #include <iostream> #include <vector> using namespace std; int main() { cout << setprecision(2); cout << setiosflags(ios::fixed); int n, m, i, j, k; long double M; while (cin >> n >> m, n | m) { vector<vector<long double>> manure(n, vector<long double>(n)), dp(m, vector<long double>(n, 0.0)); for (i = 0; i < n; i++) for (j = 0; j < n; j++) cin >> manure[i][j]; for (j = 0; j < n; j++) dp[0][j] = 1.0; for (i = 1; i < m; i++) { for (j = 0; j < n; j++) { for (k = 0; k < n; k++) { dp[i][j] = max(dp[i][j], dp[i - 1][k] * manure[k][j]); } } } M = 0.0; for (j = 0; j < n; j++) M = max(M, dp[m - 1][j]); cout << M << endl; } }
[["-", 0, 69, 28, 69, 341, 342, 0, 16, 31, 22], ["+", 0, 69, 28, 69, 341, 342, 0, 16, 31, 22]]
1
282
2
// 43 #include <algorithm> #include <iomanip> #include <iostream> using namespace std; int main() { for (int n, m; cin >> n >> m, n | m;) { double g[100][100] = {{}}; for (int y = 0; y < n; y++) { for (int x = 0; x < n; x++) { cin >> g[y][x]; } } double r[101][100] = {{}}; fill(r[0], r[1], 1); for (int i = 0; i < m - 1; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { r[i + 1][j] = max(r[i + 1][j], r[i][k] * g[k][j]); } } } cout << fixed << setprecision(2) << *max_element(r[n - 1], r[n]) << endl; } return 0; }
// 43 #include <algorithm> #include <iomanip> #include <iostream> using namespace std; int main() { for (int n, m; cin >> n >> m, n | m;) { double g[100][100] = {{}}; for (int y = 0; y < n; y++) { for (int x = 0; x < n; x++) { cin >> g[y][x]; } } double r[101][100] = {{}}; fill(r[0], r[1], 1); for (int i = 0; i < m - 1; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { r[i + 1][j] = max(r[i + 1][j], r[i][k] * g[k][j]); } } } cout << fixed << setprecision(2) << *max_element(r[m - 1], r[m]) << endl; } return 0; }
[["-", 3, 4, 0, 69, 341, 342, 0, 16, 31, 22], ["+", 3, 4, 0, 69, 341, 342, 0, 16, 31, 22], ["-", 28, 2, 3, 4, 0, 69, 341, 342, 0, 22], ["+", 28, 2, 3, 4, 0, 69, 341, 342, 0, 22]]
1
241
4
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef unsigned int UI; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef istringstream ISS; typedef ostringstream OSS; #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) #define EACH(v, c) for (auto &v : c) #define ITER(c) __typeof((c).begin()) #define IREP(c, it) for (ITER(c) it = c.begin(); it != c.end(); ++it) #define ALL(c) (c).begin(), (c).end() #define PB(n) push_back(n) #define MP(a, b) make_pair((a), (b)) #define EXIST(c, e) ((c).find(e) != (c).end()) #define fst first #define snd second #define DUMP(x) cerr << #x << " = " << (x) << endl #define DEBUG(x) \ cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl typedef vector<double> VD; typedef vector<VD> VVD; int main() { cin.tie(0); ios::sync_with_stdio(false); cout.precision(10); cout << fixed; while (true) { int n, m; cin >> n >> m; if (!(n | m)) { break; } VVD table(n, VD(n, 0.)); REP(i, 0, n) { REP(j, 0, n) { cin >> table[i][j]; } } VVD dp(m, VD(n, 0.)); fill(ALL(dp[0]), 1.); REP(i, 0, m - 1) { REP(j, 0, n) { REP(k, 0, n) { dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] * table[j][k]); } } } cout << *max_element(ALL(dp[m - 1])) << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef unsigned int UI; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef istringstream ISS; typedef ostringstream OSS; #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) #define EACH(v, c) for (auto &v : c) #define ITER(c) __typeof((c).begin()) #define IREP(c, it) for (ITER(c) it = c.begin(); it != c.end(); ++it) #define ALL(c) (c).begin(), (c).end() #define PB(n) push_back(n) #define MP(a, b) make_pair((a), (b)) #define EXIST(c, e) ((c).find(e) != (c).end()) #define fst first #define snd second #define DUMP(x) cerr << #x << " = " << (x) << endl #define DEBUG(x) \ cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl typedef vector<double> VD; typedef vector<VD> VVD; int main() { cin.tie(0); ios::sync_with_stdio(false); cout.precision(2); cout << fixed; while (true) { int n, m; cin >> n >> m; if (!(n | m)) { break; } VVD table(n, VD(n, 0.)); REP(i, 0, n) { REP(j, 0, n) { cin >> table[i][j]; } } VVD dp(m, VD(n, 0.)); fill(ALL(dp[0]), 1.); REP(i, 0, m - 1) { REP(j, 0, n) { REP(k, 0, n) { dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] * table[j][k]); } } } cout << *max_element(ALL(dp[m - 1])) << endl; } return 0; }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
1
418
2
#include <algorithm> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; #define INF 1e+9 #define EPS 1e-9 #define rep(i, j) for (int i = 0; i < (j); i++) #define reps(i, j, k) for (int i = j; i < (k); i++) typedef long long ll; double fert[128][128]; double dp[128][128]; int main() { int n, m; while (scanf("%d%d", &n, &m), (n | m)) { memset(dp, 0, sizeof(dp)); rep(i, n) rep(j, n) scanf("%lf", &fert[i][j]); rep(i, m) { rep(j, n) { bool flg = false; if (i == 0) dp[i][j] = 1.00; else { rep(k, n) { dp[i][j] = (!flg) ? dp[i - 1][k] * fert[k][j] : max(dp[i][j], dp[i - 1][k] * fert[k][j]); flg = true; } } } } double res = -1.0; rep(i, n) { res = max(res, dp[m - 1][i]); } printf("%lf\n", res); } return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; #define INF 1e+9 #define EPS 1e-9 #define rep(i, j) for (int i = 0; i < (j); i++) #define reps(i, j, k) for (int i = j; i < (k); i++) typedef long long ll; double fert[128][128]; double dp[128][128]; int main() { int n, m; while (scanf("%d%d", &n, &m), (n | m)) { memset(dp, 0, sizeof(dp)); rep(i, n) rep(j, n) scanf("%lf", &fert[i][j]); rep(i, m) { rep(j, n) { bool flg = false; if (i == 0) dp[i][j] = 1.0; else { rep(k, n) { dp[i][j] = (!flg) ? dp[i - 1][k] * fert[k][j] : max(dp[i][j], dp[i - 1][k] * fert[k][j]); flg = true; } } } } double res = -1.0; rep(i, n) { res = max(res, dp[m - 1][i]); } printf("%.2lf\n", res); } return 0; }
[["-", 8, 9, 0, 57, 64, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 57, 64, 1, 0, 11, 12, 13], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
346
4
#include <algorithm> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; #define INF 1e+9 #define EPS 1e-9 #define rep(i, j) for (int i = 0; i < (j); i++) #define reps(i, j, k) for (int i = j; i < (k); i++) typedef long long ll; double fert[128][128]; double dp[128][128]; int main() { int n, m; while (scanf("%d%d", &n, &m), (n | m)) { memset(dp, 0, sizeof(dp)); rep(i, n) rep(j, n) scanf("%lf", &fert[i][j]); rep(i, m) { rep(j, n) { if (i == 0) dp[i][j] = 1.00; else { rep(k, n) { dp[i][j] = max(dp[i][j], dp[i - 1][k] * fert[k][j]); } } } } double res = -1.0; rep(i, n) { res = max(res, dp[m - 1][i]); } printf("%lf\n", res); } return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; #define INF 1e+9 #define EPS 1e-9 #define rep(i, j) for (int i = 0; i < (j); i++) #define reps(i, j, k) for (int i = j; i < (k); i++) typedef long long ll; double fert[128][128]; double dp[128][128]; int main() { int n, m; while (scanf("%d%d", &n, &m), (n | m)) { memset(dp, 0, sizeof(dp)); rep(i, n) rep(j, n) scanf("%lf", &fert[i][j]); rep(i, m) { rep(j, n) { if (i == 0) dp[i][j] = 1.0; else { rep(k, n) { dp[i][j] = max(dp[i][j], dp[i - 1][k] * fert[k][j]); } } } } double res = -1.0; rep(i, n) { res = max(res, dp[m - 1][i]); } printf("%.2lf\n", res); } return 0; }
[["-", 8, 9, 0, 57, 64, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 57, 64, 1, 0, 11, 12, 13], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
314
4
#include <algorithm> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define repa(i, s, e) for (int i = s; i <= e; i++) #define repd(i, s, e) for (int i = s; i >= e; i--) typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int MAX_N = 101; const int MAX_M = 101; int N, M; double table[MAX_N][MAX_N]; double dp[MAX_M][MAX_N]; void solve() { repa(i, 0, M) rep(j, N) dp[i][j] = 0.0; rep(i, N) dp[0][i] = 1.0; repa(i, 1, M - 1) { rep(j, N) { double maxval = 0.0; rep(k, N) { double s = dp[i - 1][k] * table[k][j]; maxval = max(s, maxval); } dp[i][j] = maxval; } } double maxval = 0.0; rep(i, N) { maxval = max(maxval, dp[M - 1][i]); } printf("%.6f\n", maxval); } int main() { while (scanf("%d%d", &N, &M), N | M) { rep(i, N) { rep(j, N) { scanf("%lf", &table[i][j]); } } solve(); } }
#include <algorithm> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define repa(i, s, e) for (int i = s; i <= e; i++) #define repd(i, s, e) for (int i = s; i >= e; i--) typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int MAX_N = 101; const int MAX_M = 101; int N, M; double table[MAX_N][MAX_N]; double dp[MAX_M][MAX_N]; void solve() { rep(i, M) rep(j, N) dp[i][j] = 0.0; rep(i, N) dp[0][i] = 1.0; repa(i, 1, M - 1) { rep(j, N) { double maxval = 0.0; rep(k, N) { double s = dp[i - 1][k] * table[k][j]; maxval = max(s, maxval); } dp[i][j] = maxval; } } double maxval = 0.0; rep(i, N) { maxval = max(maxval, dp[M - 1][i]); } printf("%.2f\n", maxval); } int main() { while (scanf("%d%d", &N, &M), N | M) { rep(i, N) { rep(j, N) { scanf("%lf", &table[i][j]); } } solve(); } }
[["-", 0, 14, 8, 9, 0, 1, 0, 2, 63, 22], ["+", 0, 14, 8, 9, 0, 42, 0, 2, 63, 22], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 21], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["-", 0, 1, 0, 2, 3, 4, 0, 25, 0, 35], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
344
8
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; #define rep(i, k, n) for (int i = k; i < n; i++) double G[101][101]; int main() { int n, m; while (cin >> n >> m, n + m) { rep(i, 1, n + 1) rep(j, 1, n + 1) cin >> G[i][j]; double dp[101][101]; fill(dp[0], dp[101], 0); rep(j, 0, n + 1) dp[1][j] = 1; double mx = 0; rep(i, 1, n + 1) { rep(j, 1, n + 1) { rep(k, 1, n + 1) { dp[i][j] = max(dp[i][j], dp[i - 1][k] * G[k][j]); } } } rep(j, 1, n + 1) mx = max(mx, dp[n][j]); printf("%.2f\n", mx); } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; #define rep(i, k, n) for (int i = k; i < n; i++) double G[101][101]; int main() { int n, m; while (cin >> n >> m, n + m) { rep(i, 1, n + 1) rep(j, 1, n + 1) cin >> G[i][j]; double dp[101][101]; fill(dp[0], dp[101], 0); rep(j, 0, n + 1) dp[1][j] = 1; double mx = 0; rep(i, 2, m + 1) { rep(j, 1, n + 1) { rep(k, 1, n + 1) { dp[i][j] = max(dp[i][j], dp[i - 1][k] * G[k][j]); } } } rep(j, 1, n + 1) mx = max(mx, dp[m][j]); printf("%.2f\n", mx); } return 0; }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 31, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 31, 22], ["-", 3, 4, 0, 69, 28, 69, 341, 342, 0, 22], ["+", 3, 4, 0, 69, 28, 69, 341, 342, 0, 22]]
1
249
6
#include <cmath> #include <iostream> #include <stdio.h> #include <vector> using namespace std; void solve() { int n, m; while (cin >> n >> m, n || m) { long double data[101][101]; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { scanf("%lf", &data[i][j]); } } long double dp[101][101]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { dp[i][j] = 0; } } for (int i = 0; i < n; ++i) { dp[0][i] = 1; } for (int i = 1; i < m; ++i) { for (int j = 0; j < n; ++j) { for (int k = 0; k < n; ++k) { dp[i][j] = max(dp[i][j], dp[i - 1][k] * data[k][j]); } } } long double maximum = 0; for (int i = 0; i < n; ++i) { maximum = max(maximum, dp[m - 1][i]); } printf("%.2lf\n", maximum); } } int main() { solve(); return (0); }
#include <cmath> #include <iostream> #include <stdio.h> #include <vector> using namespace std; void solve() { int n, m; while (cin >> n >> m, n || m) { long double data[101][101]; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { scanf("%Lf", &data[i][j]); } } long double dp[101][101]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { dp[i][j] = 0; } } for (int i = 0; i < n; ++i) { dp[0][i] = 1; } for (int i = 1; i < m; ++i) { for (int j = 0; j < n; ++j) { for (int k = 0; k < n; ++k) { dp[i][j] = max(dp[i][j], dp[i - 1][k] * data[k][j]); } } } long double maximum = 0; for (int i = 0; i < n; ++i) { maximum = max(maximum, dp[m - 1][i]); } printf("%.2Lf\n", maximum); } } int main() { solve(); return (0); }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
322
17
def solve trans,m,n prev = Array.new(n,1.0) (m-1).times do cur = Array.new(n,0) n.times do |i| n.times do |j| cur[i] = [cur[i], prev[j]*trans[j][i]].max end end cur,prev = prev,cur end prev.max end loop do n,m = gets.chomp.split.map{|i|i.to_i} break if n==0 trans = [] n.times do a = gets.chomp.split.map{|i|i.to_f} trans << a end puts "%.5f" % solve(trans,m,n) end
def solve trans,m,n prev = Array.new(n,1.0) (m-1).times do cur = Array.new(n,0) n.times do |i| n.times do |j| cur[i] = [cur[i], prev[j]*trans[j][i]].max end end cur,prev = prev,cur end prev.max end loop do n,m = gets.chomp.split.map{|i|i.to_i} break if n==0 trans = [] n.times do a = gets.chomp.split.map{|i|i.to_f} trans << a end puts "%.2f" % solve(trans,m,n) end
[["-", 0, 652, 3, 4, 0, 738, 31, 557, 0, 6], ["+", 0, 652, 3, 4, 0, 738, 31, 557, 0, 6]]
4
158
23
# AOJ 0191: Baby Tree # Python3 2018.6.20 bal4u while True: n, m = list(map(int, input().split())) if n == 0: break d = [[0.0 for j in range(n)] for i in range(n)] for i in range(n): d[i] = list(map(float, input().split())) dp = [[0.0 for j in range(n)] for i in range(n)] for i in range(n): dp[0][i] = 1 for k in range(1, m): for i in range(n): for j in range(n): dp[k][j] = max(dp[k][j], dp[k-1][i]*d[i][j]) ans = 0 for i in range(n): if dp[m-1][i] > ans: ans = dp[m-1][i] print(format(ans, ".2f"))
# AOJ 0191: Baby Tree # Python3 2018.6.20 bal4u while True: n, m = list(map(int, input().split())) if n == 0: break d = [[0.0 for j in range(n)] for i in range(n)] for i in range(n): d[i] = list(map(float, input().split())) dp = [[0.0 for j in range(n)] for k in range(m)] for i in range(n): dp[0][i] = 1 for k in range(1, m): for i in range(n): for j in range(n): dp[k][j] = max(dp[k][j], dp[k-1][i]*d[i][j]) ans = 0 for i in range(n): if dp[m-1][i] > ans: ans = dp[m-1][i] print(format(ans, ".2f"))
[["-", 0, 1, 0, 662, 12, 658, 0, 659, 31, 22], ["+", 0, 1, 0, 662, 12, 658, 0, 659, 31, 22], ["-", 12, 658, 0, 659, 12, 652, 3, 4, 0, 22], ["+", 12, 658, 0, 659, 12, 652, 3, 4, 0, 22]]
5
225
4
#include <algorithm> #include <iostream> #include <vector> using namespace std; const int INF = 1 << 30; class Parking { public: int id, time; Parking(int _id = -1, int _time = INF) : id(_id), time(_time) {} }; int main() { int n, m; while (cin >> m >> n) { if ((n | m) == 0) break; vector<int> cars(n); for (int i = 0; i < n; ++i) cin >> cars[i]; int id = 0; vector<int> ans; vector<Parking> p1(m), p2(m); for (int time = 0; ans.size() < n; ++time) { for (int i = 0; i < m; ++i) { if (p1[i].id != -1) --p1[i].time; if (p2[i].id != -1) --p2[i].time; } for (int i = 0; i < m; ++i) { bool out = false; if (p1[i].id != -1 && p1[i].time <= 0) { ans.push_back(p1[i].id); p1[i].id = -1; p1[i].time = INF; out = true; } if (p1[i].id == -1 && p2[i].id != -1 && p2[i].time <= 0) { ans.push_back(p2[i].id); p2[i].id = -1; p2[i].time = INF; } if (out && p2[i].id != -1) { p1[i].id = p2[i].id; p1[i].time = p2[i].time; p2[i].id = -1; p2[i].time = INF; } } while (id < n && id * 10 <= time) { bool in = false; for (int i = 0; i < m; ++i) { if (p1[i].id == -1) { p1[i].id = id; p1[i].time = cars[id++]; in = true; break; } } if (in) continue; int minIndex = -1; for (int i = 0; i < m; ++i) { if (p2[i].id != -1) continue; if (p1[i].time >= time + cars[id]) { if (minIndex == -1 || p1[i].time < p1[minIndex].time) minIndex = i; } } if (minIndex != -1) { p2[minIndex].id = p1[minIndex].id; p2[minIndex].time = p1[minIndex].time; p1[minIndex].id = id; p1[minIndex].time = cars[id++]; continue; } for (int i = 0; i < m; ++i) { if (p2[i].id != -1) continue; if (p1[i].time < time + cars[id]) { if (minIndex == -1 || p1[i].time > p1[minIndex].time) minIndex = i; } } if (minIndex != -1) { p2[minIndex].id = p1[minIndex].id; p2[minIndex].time = p1[minIndex].time; p1[minIndex].id = id; p1[minIndex].time = cars[id++]; continue; } break; } } for (int i = 0; i < n - 1; ++i) cout << ans[i] + 1 << " "; cout << ans[n - 1] + 1 << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; const int INF = 1 << 30; class Parking { public: int id, time; Parking(int _id = -1, int _time = INF) : id(_id), time(_time) {} }; int main() { int n, m; while (cin >> m >> n) { if ((n | m) == 0) break; vector<int> cars(n); for (int i = 0; i < n; ++i) cin >> cars[i]; int id = 0; vector<int> ans; vector<Parking> p1(m), p2(m); for (int time = 0; ans.size() < n; ++time) { for (int i = 0; i < m; ++i) { if (p1[i].id != -1) --p1[i].time; if (p2[i].id != -1) --p2[i].time; } for (int i = 0; i < m; ++i) { bool out = false; if (p1[i].id != -1 && p1[i].time <= 0) { ans.push_back(p1[i].id); p1[i].id = -1; p1[i].time = INF; out = true; } if (p1[i].id == -1 && p2[i].id != -1 && p2[i].time <= 0) { ans.push_back(p2[i].id); p2[i].id = -1; p2[i].time = INF; } if (out && p2[i].id != -1) { p1[i].id = p2[i].id; p1[i].time = p2[i].time; p2[i].id = -1; p2[i].time = INF; } } while (id < n && id * 10 <= time) { bool in = false; for (int i = 0; i < m; ++i) { if (p1[i].id == -1) { p1[i].id = id; p1[i].time = cars[id++]; in = true; break; } } if (in) continue; int minIndex = -1; for (int i = 0; i < m; ++i) { if (p2[i].id != -1) continue; if (p1[i].time >= cars[id]) { if (minIndex == -1 || p1[i].time < p1[minIndex].time) minIndex = i; } } if (minIndex != -1) { p2[minIndex].id = p1[minIndex].id; p2[minIndex].time = p1[minIndex].time; p1[minIndex].id = id; p1[minIndex].time = cars[id++]; continue; } for (int i = 0; i < m; ++i) { if (p2[i].id != -1) continue; if (p1[i].time < cars[id]) { if (minIndex == -1 || p1[i].time > p1[minIndex].time) minIndex = i; } } if (minIndex != -1) { p2[minIndex].id = p1[minIndex].id; p2[minIndex].time = p1[minIndex].time; p1[minIndex].id = id; p1[minIndex].time = cars[id++]; continue; } break; } } for (int i = 0; i < n - 1; ++i) cout << ans[i] + 1 << " "; cout << ans[n - 1] + 1 << endl; } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 12, 16, 31, 22], ["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 72]]
1
811
4
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <utility> using namespace std; #define dump(n) cerr << "# " << #n << "=" << (n) << endl #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define peri(i, a, b) for (int i = int(b); i-- > int(a);) #define rep(i, n) repi(i, 0, n) #define per(i, n) peri(i, 0, n) #define iter(c) __typeof__((c).begin()) #define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i) #define all(c) (c).begin(), (c).end() #define mp make_pair typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<string> vs; typedef pair<int, int> pii; struct State { int t, id; int io; // 0:駐車, 1:出庫 int len; // 駐車の時:駐車時間, 出庫の時:駐車スペース State() {} State(int t, int id, int io, int len) : t(t), id(id), io(io), len(len) {} bool operator<(const State &s) const { if (t != s.t) return t < s.t; if (io != s.io) return io > s.io; if (io == 0) return id > s.id; if (io == 1) return len != s.len ? len < s.len : id > s.id; return false; } bool operator>(const State &s) const { if (t != s.t) return t > s.t; if (io != s.io) return io < s.io; if (io == 0) return id < s.id; if (io == 1) return len != s.len ? len > s.len : id < s.id; return false; } }; int main() { for (int m, n; cin >> m >> n, m | n;) { priority_queue<State, vector<State>, greater<State>> pq; rep(i, n) { int t; cin >> t; pq.push(State(i * 10, i, 0, t)); } vvi pid(2, vi(m, -1)), out(2, vi(m, -1)); // [0][i]:下段, [1][i]:上段 vi res; while (pq.size()) { State cur = pq.top(); pq.pop(); if (cur.io == 0) { // 駐車 // printf("in %d\n",cur.id); rep(i, m) if (pid[0][i] == -1 && pid[1][i] == -1) { pid[0][i] = cur.id; out[0][i] = cur.t + cur.len; pq.emplace(cur.t + cur.len, cur.id, 1, i); goto end; } int j = -1; rep(i, m) if (pid[1][i] == -1 && out[0][i] >= cur.t + cur.len) { if (j == -1 || out[0][j] > out[0][i]) j = i; } if (j != -1) { pid[1][j] = pid[0][j]; out[1][j] = out[0][j]; pid[0][j] = cur.id; out[0][j] = cur.t + cur.len; pq.emplace(cur.t + cur.len, cur.id, 1, j); goto end; } rep(i, m) if (pid[1][i] == -1) { if (j == -1 || out[0][j] < out[0][i]) j = i; } if (j != -1) { pid[1][j] = pid[0][j]; out[1][j] = out[0][j]; pid[0][j] = cur.id; out[0][j] = cur.t + cur.len; pq.emplace(cur.t + cur.len, cur.id, 1, j); goto end; } pq.emplace(cur.t + 1, cur.id, 0, cur.len); } if (cur.io == 1) { // 出庫 // printf("out %d\n",cur.id); rep(i, 2) rep(j, m) if (pid[i][j] == cur.id) { if (i == 0) { pid[0][j] = pid[1][j]; out[0][j] = out[1][j]; pid[1][j] = out[1][j] = -1; res.push_back(cur.id); goto end; } if (i == 1) { pq.emplace(cur.t + 1, cur.id, 1, cur.len); goto end; } } } end:; } rep(i, res.size()) cout << res[i] + 1 << (i == res.size() - 1 ? '\n' : ' '); } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <utility> using namespace std; #define dump(n) cerr << "# " << #n << "=" << (n) << endl #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define peri(i, a, b) for (int i = int(b); i-- > int(a);) #define rep(i, n) repi(i, 0, n) #define per(i, n) peri(i, 0, n) #define iter(c) __typeof__((c).begin()) #define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i) #define all(c) (c).begin(), (c).end() #define mp make_pair typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<string> vs; typedef pair<int, int> pii; struct State { int t, id; int io; // 0:駐車, 1:出庫 int len; // 駐車の時:駐車時間, 出庫の時:駐車スペース State() {} State(int t, int id, int io, int len) : t(t), id(id), io(io), len(len) {} bool operator<(const State &s) const { if (t != s.t) return t < s.t; if (io != s.io) return io > s.io; if (io == 0) return id < s.id; if (io == 1) return len != s.len ? len < s.len : id > s.id; return false; } bool operator>(const State &s) const { if (t != s.t) return t > s.t; if (io != s.io) return io < s.io; if (io == 0) return id > s.id; if (io == 1) return len != s.len ? len > s.len : id < s.id; return false; } }; int main() { for (int m, n; cin >> m >> n, m | n;) { priority_queue<State, vector<State>, greater<State>> pq; rep(i, n) { int t; cin >> t; pq.push(State(i * 10, i, 0, t)); } vvi pid(2, vi(m, -1)), out(2, vi(m, -1)); // [0][i]:下段, [1][i]:上段 vi res; while (pq.size()) { State cur = pq.top(); pq.pop(); if (cur.io == 0) { // 駐車 rep(i, m) if (pid[0][i] == -1 && pid[1][i] == -1) { pid[0][i] = cur.id; out[0][i] = cur.t + cur.len; pq.emplace(cur.t + cur.len, cur.id, 1, i); goto end; } int j = -1; rep(i, m) if (pid[1][i] == -1 && out[0][i] >= cur.t + cur.len) { if (j == -1 || out[0][j] > out[0][i]) j = i; } if (j != -1) { pid[1][j] = pid[0][j]; out[1][j] = out[0][j]; pid[0][j] = cur.id; out[0][j] = cur.t + cur.len; pq.emplace(cur.t + cur.len, cur.id, 1, j); goto end; } rep(i, m) if (pid[1][i] == -1) { if (j == -1 || out[0][j] < out[0][i]) j = i; } if (j != -1) { pid[1][j] = pid[0][j]; out[1][j] = out[0][j]; pid[0][j] = cur.id; out[0][j] = cur.t + cur.len; pq.emplace(cur.t + cur.len, cur.id, 1, j); goto end; } pq.emplace(cur.t + 1, cur.id, 0, cur.len); } if (cur.io == 1) { // 出庫 rep(i, 2) rep(j, m) if (pid[i][j] == cur.id) { if (i == 0) { pid[0][j] = pid[1][j]; out[0][j] = out[1][j]; pid[1][j] = out[1][j] = -1; res.push_back(cur.id); goto end; } if (i == 1) { pq.emplace(cur.t + 1, cur.id, 1, cur.len); goto end; } } } end:; } rep(i, res.size()) cout << res[i] + 1 << (i == res.size() - 1 ? '\n' : ' '); } return 0; }
[["-", 8, 9, 0, 57, 64, 37, 0, 16, 17, 47], ["+", 8, 9, 0, 57, 64, 37, 0, 16, 17, 18], ["-", 8, 9, 0, 57, 64, 37, 0, 16, 17, 18], ["+", 8, 9, 0, 57, 64, 37, 0, 16, 17, 47]]
1
1,126
4
#include <algorithm> #include <cassert> #include <climits> #include <cstdio> #include <cstdlib> #include <deque> #include <iostream> #include <vector> #define REP(i, s, n) for (int i = s; i < n; i++) #define rep(i, n) REP(i, 0, n) #define IINF (INT_MAX) #define MAX 20 using namespace std; typedef pair<int, int> ii; int n, m; // n : the number of the space deque<int> car, TIME; deque<int> space[MAX]; // tyusya space deque<int> Index[MAX]; deque<ii> waiting; vector<int> ANS; bool Insert1(int cindex, int cTIME) { rep(i, n) { if (space[i].size() >= 2) continue; if (space[i].empty()) { space[i].push_back(cTIME); Index[i].push_back(cindex); return true; } } return false; } bool Insert2(int cindex, int ctime, int cTIME) { int pos = -1; bool found = false; rep(i, n) { assert(!space[i].empty()); if (space[i].size() >= 2) continue; int remain = space[i].front() - ctime; assert(remain >= 0); // if(remain < cTIME)continue; if (remain >= cTIME) found = true; if (pos == -1) pos = i; else { if (abs((space[i].front() - ctime) - cTIME) < abs((space[pos].front() - ctime) - cTIME)) { pos = i; } } } if (pos == -1 || !found) return false; space[pos].push_front(ctime + cTIME); Index[pos].push_front(cindex); return true; } bool Insert3(int cindex, int ctime, int cTIME) { int pos = -1; rep(i, n) { assert(!space[i].empty()); if (space[i].size() >= 2) continue; int remain = space[i].front() - ctime; assert(remain >= 0); if (remain >= cTIME) { return false; } if (pos == -1) pos = i; else { if (abs((space[i].front() - ctime) - cTIME) < abs((space[pos].front() - ctime) - cTIME)) { pos = i; } } } if (pos == -1) return false; space[pos].push_front(ctime + cTIME); Index[pos].push_front(cindex); return true; } void compute() { int ctime = 0; while (true) { // out rep(i, n) while (!space[i].empty() && space[i].front() <= ctime) { ANS.push_back(Index[i].front()); space[i].pop_front(), Index[i].pop_front(); } // IN if (!car.empty() && ctime % 10 == 0) { waiting.push_back(ii(car.front(), TIME.front())); car.pop_front(), TIME.pop_front(); } while (true) { deque<ii> next; deque<ii> tmp = waiting; while (!waiting.empty()) { int cindex = waiting.front().first; int cTIME = waiting.front().second; waiting.pop_front(); if (Insert1(cindex, cTIME + ctime)) { continue; } if (Insert2(cindex, ctime, cTIME)) { continue; } if (Insert3(cindex, ctime, cTIME)) { continue; } // it cannot be possible to insert the car next.push_back(ii(cindex, cTIME)); } waiting = next; if (tmp == next) break; if (waiting.empty()) break; } bool finish = true; rep(i, n) if (!space[i].empty()) { finish = false; break; } if (car.empty() && finish) { assert(waiting.empty()); break; } ctime += 1; } } int main() { while (cin >> n >> m, n | m) { waiting.clear(), ANS.clear(), TIME.clear(), car.clear(); rep(i, MAX) space[i].clear(), Index[i].clear(); rep(i, m) { int tm; cin >> tm; car.push_back(i); TIME.push_back(tm); } compute(); rep(i, ANS.size()) { if (i) cout << " "; cout << ANS[i] + 1; } cout << endl; } return 0; }
#include <algorithm> #include <cassert> #include <climits> #include <cstdio> #include <cstdlib> #include <deque> #include <iostream> #include <vector> #define REP(i, s, n) for (int i = s; i < n; i++) #define rep(i, n) REP(i, 0, n) #define IINF (INT_MAX) #define MAX 20 using namespace std; typedef pair<int, int> ii; int n, m; // n : the number of the space deque<int> car, TIME; deque<int> space[MAX]; // tyusya space deque<int> Index[MAX]; deque<ii> waiting; vector<int> ANS; bool Insert1(int cindex, int cTIME) { rep(i, n) { if (space[i].size() >= 2) continue; if (space[i].empty()) { space[i].push_back(cTIME); Index[i].push_back(cindex); return true; } } return false; } bool Insert2(int cindex, int ctime, int cTIME) { int pos = -1; bool found = false; rep(i, n) { assert(!space[i].empty()); if (space[i].size() >= 2) continue; int remain = space[i].front() - ctime; assert(remain >= 0); // if(remain < cTIME)continue; if (remain >= cTIME) found = true; else continue; if (pos == -1) pos = i; else { if (abs((space[i].front() - ctime) - cTIME) < abs((space[pos].front() - ctime) - cTIME)) { pos = i; } } } if (pos == -1 || !found) return false; space[pos].push_front(ctime + cTIME); Index[pos].push_front(cindex); return true; } bool Insert3(int cindex, int ctime, int cTIME) { int pos = -1; rep(i, n) { assert(!space[i].empty()); if (space[i].size() >= 2) continue; int remain = space[i].front() - ctime; assert(remain >= 0); if (remain >= cTIME) { return false; } if (pos == -1) pos = i; else { if (abs((space[i].front() - ctime) - cTIME) < abs((space[pos].front() - ctime) - cTIME)) { pos = i; } } } if (pos == -1) return false; space[pos].push_front(ctime + cTIME); Index[pos].push_front(cindex); return true; } void compute() { int ctime = 0; while (true) { // out rep(i, n) while (!space[i].empty() && space[i].front() <= ctime) { ANS.push_back(Index[i].front()); space[i].pop_front(), Index[i].pop_front(); } // IN if (!car.empty() && ctime % 10 == 0) { waiting.push_back(ii(car.front(), TIME.front())); car.pop_front(), TIME.pop_front(); } while (true) { deque<ii> next; deque<ii> tmp = waiting; while (!waiting.empty()) { int cindex = waiting.front().first; int cTIME = waiting.front().second; waiting.pop_front(); if (Insert1(cindex, cTIME + ctime)) { continue; } if (Insert2(cindex, ctime, cTIME)) { continue; } if (Insert3(cindex, ctime, cTIME)) { continue; } // it cannot be possible to insert the car next.push_back(ii(cindex, cTIME)); } waiting = next; if (tmp == next) break; if (waiting.empty()) break; } bool finish = true; rep(i, n) if (!space[i].empty()) { finish = false; break; } if (car.empty() && finish) { assert(waiting.empty()); break; } ctime += 1; } } int main() { while (cin >> n >> m, n | m) { waiting.clear(), ANS.clear(), TIME.clear(), car.clear(); rep(i, MAX) space[i].clear(), Index[i].clear(); rep(i, m) { int tm; cin >> tm; car.push_back(i); TIME.push_back(tm); } compute(); rep(i, ANS.size()) { if (i) cout << " "; cout << ANS[i] + 1; } cout << endl; } return 0; }
[["+", 0, 14, 8, 9, 0, 57, 75, 76, 0, 95], ["+", 8, 9, 0, 57, 75, 76, 0, 116, 0, 117], ["+", 8, 9, 0, 57, 75, 76, 0, 116, 0, 35]]
1
1,005
3
import java.util.ArrayList; import java.util.List; import java.util.Scanner; // Deven-Eleven public class Main { static int[][] move = {{0, -1}, {1, -1}, {-1, 0}, {1, 0}, {0, 1}, {1, 1}}; static int[][] move2 = {{-1, -1}, {0, -1}, {-1, 0}, {1, 0}, {-1, 1}, {0, 1}}; static int m, n, s; static int[][] bfs(int x, int y) { int[][] r = new int[m + 1][n + 1]; boolean[][] v = new boolean[m + 1][n + 1]; int step = 0; List<int[]> l = new ArrayList<int[]>(); v[x][y] = true; l.add(new int[] {x, y}); while (!l.isEmpty()) { List<int[]> next = new ArrayList<int[]>(); for (int[] a : l) { for (int k = 0; k < 6; k++) { int nx = a[0] + (a[1] % 2 == 0 ? move[k][0] : move2[k][0]); int ny = a[1] + (a[1] % 2 == 0 ? move[k][1] : move2[k][1]); if (1 <= nx && nx <= m && 1 <= ny && ny <= n && !v[nx][ny]) { v[nx][ny] = true; r[nx][ny] = step; next.add(new int[] {nx, ny}); } } } step++; l = next; } return r; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { m = sc.nextInt(); if (m == 0) break; n = sc.nextInt(); s = sc.nextInt(); int[][][] a = new int[s][m + 1][n + 1]; for (int i = 0; i < s; i++) a[i] = bfs(sc.nextInt(), sc.nextInt()); int max = 0; int t = sc.nextInt(); while (t-- != 0) { int[][] r = bfs(sc.nextInt(), sc.nextInt()); int c = 0; for (int x = 1; x <= m; x++) { for (int y = 1; y <= n; y++) { boolean f = true; for (int k = 0; k < s; k++) { if (r[x][y] >= a[k][x][y]) f = false; } if (f) c++; } } max = Math.max(max, c); } System.out.println(max); } } }
import java.util.ArrayList; import java.util.List; import java.util.Scanner; // Deven-Eleven public class Main { static int[][] move = {{0, -1}, {1, -1}, {-1, 0}, {1, 0}, {0, 1}, {1, 1}}; static int[][] move2 = {{-1, -1}, {0, -1}, {-1, 0}, {1, 0}, {-1, 1}, {0, 1}}; static int m, n, s; static int[][] bfs(int x, int y) { int[][] r = new int[m + 1][n + 1]; boolean[][] v = new boolean[m + 1][n + 1]; int step = 1; List<int[]> l = new ArrayList<int[]>(); v[x][y] = true; l.add(new int[] {x, y}); while (!l.isEmpty()) { List<int[]> next = new ArrayList<int[]>(); for (int[] a : l) { for (int k = 0; k < 6; k++) { int nx = a[0] + (a[1] % 2 == 0 ? move[k][0] : move2[k][0]); int ny = a[1] + (a[1] % 2 == 0 ? move[k][1] : move2[k][1]); if (1 <= nx && nx <= m && 1 <= ny && ny <= n && !v[nx][ny]) { v[nx][ny] = true; r[nx][ny] = step; next.add(new int[] {nx, ny}); } } } step++; l = next; } return r; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { m = sc.nextInt(); if (m == 0) break; n = sc.nextInt(); s = sc.nextInt(); int[][][] a = new int[s][m + 1][n + 1]; for (int i = 0; i < s; i++) a[i] = bfs(sc.nextInt(), sc.nextInt()); int max = 0; int t = sc.nextInt(); while (t-- != 0) { int[][] r = bfs(sc.nextInt(), sc.nextInt()); int c = 0; for (int x = 1; x <= m; x++) { for (int y = 1; y <= n; y++) { boolean f = true; for (int k = 0; k < s; k++) { if (r[x][y] >= a[k][x][y]) f = false; } if (f) c++; } } max = Math.max(max, c); } System.out.println(max); } } }
[["-", 0, 195, 8, 196, 0, 503, 49, 200, 51, 499], ["+", 0, 195, 8, 196, 0, 503, 49, 200, 51, 499]]
3
714
2
#define _USE_MATH_DEFINES #include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <stack> #include <vector> using namespace std; const int dy[6] = {-1, -1, 0, 1, 1, 0}; const int dx[2][6] = {{-1, 0, 1, 0, -1, -1}, {0, 1, 1, 1, 0, -1}}; struct Data { int y, x; bool is_deven; Data() {} Data(int _y, int _x, bool _is_deven) { y = _y; x = _x; is_deven = _is_deven; } }; int main() { int N, M, S, T, X[10], Y[10], P, Q; while (cin >> N >> M, N || M) { cin >> S; for (int i = 0; i < S; i++) { cin >> X[i] >> Y[i]; X[i]--; Y[i]--; } cin >> T; int ans = 0; for (int i = 0; i < T; i++) { cin >> P >> Q; P--; Q--; queue<Data> q; Data q_front; bool used[100][100] = {}; int deven_scope = 0; for (int j = 0; j < S; j++) { q.push(Data(Y[j], X[j], false)); } q.push(Data(Q, P, true)); while (!q.empty()) { q_front = q.front(); q.pop(); if (used[q_front.y][q_front.x]) continue; used[q_front.y][q_front.x] = true; if (q_front.is_deven) deven_scope++; for (int j = 0; j < 6; j++) { int ny = q_front.y + dy[j]; int nx = q_front.x + dx[q_front.y & 1][j]; if (ny < 0 || N <= ny || nx < 0 || M <= nx) continue; q.push(Data(ny, nx, q_front.is_deven)); } } ans = max(ans, deven_scope); } printf("%d\n", ans); } return 0; }
#define _USE_MATH_DEFINES #include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <stack> #include <vector> using namespace std; const int dy[6] = {-1, -1, 0, 1, 1, 0}; const int dx[2][6] = {{-1, 0, 1, 0, -1, -1}, {0, 1, 1, 1, 0, -1}}; struct Data { int y, x; bool is_deven; Data() {} Data(int _y, int _x, bool _is_deven) { y = _y; x = _x; is_deven = _is_deven; } }; int main() { int N, M, S, T, X[10], Y[10], P, Q; while (cin >> M >> N, M || N) { cin >> S; for (int i = 0; i < S; i++) { cin >> X[i] >> Y[i]; X[i]--; Y[i]--; } cin >> T; int ans = 0; for (int i = 0; i < T; i++) { cin >> P >> Q; P--; Q--; queue<Data> q; Data q_front; bool used[100][100] = {}; int deven_scope = 0; for (int j = 0; j < S; j++) { q.push(Data(Y[j], X[j], false)); } q.push(Data(Q, P, true)); while (!q.empty()) { q_front = q.front(); q.pop(); if (used[q_front.y][q_front.x]) continue; used[q_front.y][q_front.x] = true; if (q_front.is_deven) deven_scope++; for (int j = 0; j < 6; j++) { int ny = q_front.y + dy[j]; int nx = q_front.x + dx[q_front.y & 1][j]; if (ny < 0 || N <= ny || nx < 0 || M <= nx) continue; q.push(Data(ny, nx, q_front.is_deven)); } } ans = max(ans, deven_scope); } printf("%d\n", ans); } return 0; }
[["-", 15, 339, 51, 34, 31, 16, 31, 16, 12, 22], ["-", 0, 52, 15, 339, 51, 34, 31, 16, 17, 152], ["-", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 0, 52, 15, 339, 51, 34, 31, 16, 17, 152], ["-", 0, 52, 15, 339, 51, 34, 12, 16, 17, 106], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 0, 52, 15, 339, 51, 34, 12, 16, 17, 106], ["+", 0, 52, 15, 339, 51, 34, 12, 16, 12, 22]]
1
495
8
#include <cstdio> #include <iostream> #include <queue> #include <vector> using namespace std; struct NODE { int x, y, c, num; NODE(int x, int y, int c) : x(x), y(y), c(c) {} NODE(int x, int y, int c, int num) : x(x), y(y), c(c), num(num) {} }; int W, H; int doit(vector<NODE> v) { queue<NODE> Q; for (int i = 0; i < v.size(); i++) { Q.push(NODE(v[i].x, v[i].y, 0, i)); } int cnt[100][100] = {}; int ans[100][100]; for (int i = 0; i < 100; i++) for (int j = 0; j < 100; j++) ans[i][j] = 1e9; // kore Shindoinen!!!!!!!!!!!!! // kore Shindoinen!!!!!!!!!!!!! // kore Shindoinen!!!!!!!!!!!!! int dx[2][6] = {{-1, 0, 1, -1, -1, 0}, {-1, 1, 0, 1, 1, 1}}; int dy[2][6] = {{0, 1, 0, 1, -1, -1}, {0, 0, -1, 0, 1, -1}}; // kore Shindoinen!!!!!!!!!!!!! // kore Shindoinen!!!!!!!!!!!!! // kore Shindoinen!!!!!!!!!!!!! while (Q.size()) { NODE q = Q.front(); Q.pop(); if (q.x < 0 || q.y < 0 || q.x >= W || q.y >= H) continue; if (ans[q.y][q.x] < q.c) continue; if (ans[q.y][q.x] > 10000) { ans[q.y][q.x] = q.c; cnt[q.y][q.x] = (1 << q.num); } else if (ans[q.y][q.x] == q.c) { cnt[q.y][q.x] |= (1 << q.num); continue; } for (int i = 0; i < 6; i++) { Q.push(NODE(q.x + dx[q.y % 2][i], q.y + dy[q.y % 2][i], q.c + 1, q.num)); } } int cntt = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (cnt[i][j] == (1 << (v.size() - 1))) { cntt++; } } } return cntt; } int main() { while (cin >> W >> H && W) { int s, N; cin >> s; vector<NODE> alr; for (int i = 0; i < s; i++) { int x, y; cin >> x >> y; x--, y--; alr.push_back(NODE(x, y, 0)); } cin >> N; int ans = 0; for (int i = 0; i < N; i++) { int x, y; cin >> x >> y; x--, y--; alr.push_back(NODE(x, y, 0)); ans = max(ans, doit(alr)); alr.pop_back(); } cout << ans << endl; } }
#include <cstdio> #include <iostream> #include <queue> #include <vector> using namespace std; struct NODE { int x, y, c, num; NODE(int x, int y, int c) : x(x), y(y), c(c) {} NODE(int x, int y, int c, int num) : x(x), y(y), c(c), num(num) {} }; int W, H; int doit(vector<NODE> v) { queue<NODE> Q; for (int i = 0; i < v.size(); i++) { Q.push(NODE(v[i].x, v[i].y, 0, i)); } int cnt[100][100] = {}; int ans[100][100]; for (int i = 0; i < 100; i++) for (int j = 0; j < 100; j++) ans[i][j] = 1e9; // kore Shindoinen!!!!!!!!!!!!! // kore Shindoinen!!!!!!!!!!!!! // kore Shindoinen!!!!!!!!!!!!! int dx[2][6] = {{-1, 0, 1, -1, -1, 0}, {-1, 0, 0, 1, 1, 1}}; int dy[2][6] = {{0, 1, 0, 1, -1, -1}, {0, 1, -1, 0, 1, -1}}; // kore Shindoinen!!!!!!!!!!!!! // kore Shindoinen!!!!!!!!!!!!! // kore Shindoinen!!!!!!!!!!!!! while (Q.size()) { NODE q = Q.front(); Q.pop(); if (q.x < 0 || q.y < 0 || q.x >= W || q.y >= H) continue; if (ans[q.y][q.x] < q.c) continue; if (ans[q.y][q.x] > 10000) { ans[q.y][q.x] = q.c; cnt[q.y][q.x] = (1 << q.num); } else if (ans[q.y][q.x] == q.c) { cnt[q.y][q.x] |= (1 << q.num); continue; } for (int i = 0; i < 6; i++) { Q.push(NODE(q.x + dx[q.y % 2][i], q.y + dy[q.y % 2][i], q.c + 1, q.num)); } } int cntt = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { // printf("%3d",cnt[i][j]); if (cnt[i][j] == (1 << (v.size() - 1))) { cntt++; } } // puts(""); } return cntt; } int main() { while (cin >> W >> H && W) { int s, N; cin >> s; vector<NODE> alr; for (int i = 0; i < s; i++) { int x, y; cin >> x >> y; x--, y--; alr.push_back(NODE(x, y, 0)); } cin >> N; int ans = 0; for (int i = 0; i < N; i++) { int x, y; cin >> x >> y; x--, y--; alr.push_back(NODE(x, y, 0)); ans = max(ans, doit(alr)); alr.pop_back(); } cout << ans << endl; } }
[["-", 0, 43, 49, 50, 51, 83, 0, 83, 0, 13], ["+", 0, 43, 49, 50, 51, 83, 0, 83, 0, 13]]
1
768
4
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <map> #include <numeric> #include <queue> #include <set> #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) typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; const int INF = 1001001001; int dist(pii a, pii b) { int dy = a.first - b.first; int dx = a.second - b.second; if (0 <= dy * dx) { dy = abs(dy); dx = abs(dx); return dy + max(0, dx - (dy + 1 - min(a.first, b.first) % 2) / 2); } else { dy = abs(dy); dx = abs(dx); return dy + max(0, dx - (dy + min(a.first, b.first) % 2) / 2); } } int main() { int m, n; while (scanf("%d%d", &m, &n), m) { int s; scanf("%d", &s); pii conv[10]; rep(i, s) scanf("%d%d", &conv[i].second, &conv[i].first); int d[101][101]; rep(i, n) rep(j, m) { d[i + 1][j + 1] = INF; rep(k, s) d[i + 1][j + 1] = min(d[i + 1][j + 1], dist(pii(i + 1, j + 1), conv[k])); } int t, ans = 0; scanf("%d", &t); rep(i, t) { int x, y; scanf("%d%d", &x, &y); int cnt = 0; rep(j, n) rep(k, m) cnt += dist(pii(j, k), pii(y, x)) < d[j + 1][k + 1]; ans = max(ans, cnt); } printf("%d\n", ans); } return 0; }
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <map> #include <numeric> #include <queue> #include <set> #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) typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; const int INF = 1001001001; int dist(pii a, pii b) { int dy = a.first - b.first; int dx = a.second - b.second; if (0 <= dy * dx) { dy = abs(dy); dx = abs(dx); return dy + max(0, dx - (dy + 1 - min(a.first, b.first) % 2) / 2); } else { dy = abs(dy); dx = abs(dx); return dy + max(0, dx - (dy + min(a.first, b.first) % 2) / 2); } } int main() { int m, n; while (scanf("%d%d", &m, &n), m) { int s; scanf("%d", &s); pii conv[10]; rep(i, s) scanf("%d%d", &conv[i].second, &conv[i].first); int d[101][101]; rep(i, n) rep(j, m) { d[i + 1][j + 1] = INF; rep(k, s) d[i + 1][j + 1] = min(d[i + 1][j + 1], dist(pii(i + 1, j + 1), conv[k])); } int t, ans = 0; scanf("%d", &t); rep(i, t) { int x, y; scanf("%d%d", &x, &y); int cnt = 0; rep(j, n) rep(k, m) cnt += dist(pii(j + 1, k + 1), pii(y, x)) < d[j + 1][k + 1]; ans = max(ans, cnt); } printf("%d\n", ans); } return 0; }
[["+", 3, 4, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 3, 4, 0, 2, 3, 4, 0, 16, 12, 13]]
1
499
4
def g;gets.split.map &:to_i;end while(m,n=g)[0]>0 a=(1..g[0]).map{|i|g<<i} p (1..g[0]).map{d=a+[c=g<<11] (0..m*n).count{|k|i=k/n+1;j=k%n+1 d.min_by{|x,y,t|z=(y-j).abs [z+[(x-i).abs-(z+(i<x ?1-j%2:j%2))/2,0].max,t]}==c}}.max end
def g;gets.split.map &:to_i;end while(m,n=g)[0]>0 a=(1..g[0]).map{|i|g<<i} p (1..g[0]).map{d=a+[c=g<<11] (0..m*n-1).count{|k|i=k/n+1;j=k%n+1 d.min_by{|x,y,t|z=(y-j).abs [z+[(x-i).abs-(z+(i<x ?1-j%2:j%2))/2,0].max,t]}==c}}.max end
[["-", 0, 493, 0, 89, 8, 170, 0, 768, 0, 769], ["+", 0, 493, 0, 89, 8, 170, 0, 768, 0, 769]]
4
47
2
### constant MAX_INT = (1 << 31) - 1 NEIGHBORS = [ [[-1, -1], [ 0, -1], [-1, 0], [ 1, 0], [-1, 1], [ 0, 1]], [[ 0, -1], [ 1, -1], [-1, 0], [ 1, 0], [ 0, 1], [ 1, 1]] ] ### global variables ### subroutines def gen_dists(x, y, m, n) dists = n.times.map{m.times.map{MAX_INT}} dists[y][x] = 0 queue = [[x, y]] while ! queue.empty? x0, y0 = queue.shift d0 = dists[y0][x0] + 1 for nxy in NEIGHBORS[y0 & 1] dx, dy = nxy x1 = x0 + dx y1 = y0 + dy if x1 >= 0 && x1 < m && y1 >= 0 && y1 < m && dists[y1][x1] > d0 dists[y1][x1] = d0 queue << [x1, y1] end end end dists end ### main while true m, n = gets.strip.split(' ').map{|s| s.to_i} break if m == 0 && n == 0 s = gets.strip.to_i min_dists = n.times.map{m.times.map{MAX_INT}} for i in (0...s) x0, y0 = gets.strip.split(' ').map{|s| s.to_i - 1} dists = gen_dists(x0, y0, m, n) for y in (0...n) for x in (0...m) min_dists[y][x] = dists[y][x] if min_dists[y][x] > dists[y][x] end end end #p min_dists t = gets.strip.to_i max_count = 0 t.times.each do x0, y0 = gets.strip.split(' ').map{|s| s.to_i - 1} ndists = gen_dists(x0, y0, m, n) count = 0 for y in (0...n) for x in (0...m) count += 1 if ndists[y][x] < min_dists[y][x] end end max_count = count if max_count < count end puts max_count end
### constant MAX_INT = (1 << 31) - 1 NEIGHBORS = [ [[-1, -1], [ 0, -1], [-1, 0], [ 1, 0], [-1, 1], [ 0, 1]], [[ 0, -1], [ 1, -1], [-1, 0], [ 1, 0], [ 0, 1], [ 1, 1]] ] ### global variables ### subroutines def gen_dists(x, y, m, n) dists = n.times.map{m.times.map{MAX_INT}} dists[y][x] = 0 queue = [[x, y]] while ! queue.empty? x0, y0 = queue.shift d0 = dists[y0][x0] + 1 for nxy in NEIGHBORS[y0 & 1] dx, dy = nxy x1 = x0 + dx y1 = y0 + dy if x1 >= 0 && x1 < m && y1 >= 0 && y1 < n && dists[y1][x1] > d0 dists[y1][x1] = d0 queue << [x1, y1] end end end dists end ### main while true m, n = gets.strip.split(' ').map{|s| s.to_i} break if m == 0 && n == 0 s = gets.strip.to_i min_dists = n.times.map{m.times.map{MAX_INT}} for i in (0...s) x0, y0 = gets.strip.split(' ').map{|s| s.to_i - 1} dists = gen_dists(x0, y0, m, n) for y in (0...n) for x in (0...m) min_dists[y][x] = dists[y][x] if min_dists[y][x] > dists[y][x] end end end #p min_dists t = gets.strip.to_i max_count = 0 t.times.each do x0, y0 = gets.strip.split(' ').map{|s| s.to_i - 1} ndists = gen_dists(x0, y0, m, n) count = 0 for y in (0...n) for x in (0...m) count += 1 if ndists[y][x] < min_dists[y][x] end end max_count = count if max_count < count end puts max_count end
[["-", 0, 121, 15, 738, 31, 738, 12, 738, 12, 22], ["+", 0, 121, 15, 738, 31, 738, 12, 738, 12, 22]]
4
501
2
import java.util.*; class Main { public static void main(String[] z) { int a, b = 0, t, i; for (Scanner s = new Scanner(System.in);; System.out.printf("%c %d\n", a + 65, b), b = 0) for (i = a = 5; i-- > 0;) if ((t = s.nextInt() + s.nextInt()) < 1) return; else if (b < t) { b = t; a = i; } } }
import java.util.*; class Main { public static void main(String[] z) { int a, b = 0, t, i; for (Scanner s = new Scanner(System.in);; System.out.printf("%c %d\n", a + 64, b), b = 0) for (i = a = 0; i++ < 5;) if ((t = s.nextInt() + s.nextInt()) < 1) return; else if (b < t) { b = t; a = i; } } }
[["-", 0, 7, 26, 492, 3, 4, 0, 16, 12, 499], ["+", 0, 7, 26, 492, 3, 4, 0, 16, 12, 499], ["-", 0, 7, 8, 7, 502, 11, 12, 11, 12, 499], ["+", 0, 7, 8, 7, 502, 11, 12, 11, 12, 499], ["-", 0, 7, 8, 7, 15, 16, 31, 27, 0, 68], ["-", 8, 196, 0, 7, 8, 7, 15, 16, 17, 47], ["-", 8, 196, 0, 7, 8, 7, 15, 16, 12, 499], ["+", 0, 7, 8, 7, 15, 16, 31, 27, 0, 29], ["+", 8, 196, 0, 7, 8, 7, 15, 16, 17, 18], ["+", 8, 196, 0, 7, 8, 7, 15, 16, 12, 499]]
3
123
10
#include <algorithm> #include <iostream> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { int l; while (cin >> l && l) { bool flag = false; int t, t2; vector<int> v; rep(i, 12) { cin >> t >> t2; v.push_back(t - t2); } rep(i, 12) { l -= v[i]; if (l <= 0) { flag = true; cout << i + 1 << endl; break; } } if (flag) { cout << "NA" << endl; } } return 0; }
#include <algorithm> #include <iostream> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { int l; while (cin >> l && l) { bool flag = false; int t, t2; vector<int> v; rep(i, 12) { cin >> t >> t2; v.push_back(t - t2); } rep(i, 12) { l -= v[i]; if (l <= 0) { flag = true; cout << i + 1 << endl; break; } } if (!flag) { cout << "NA" << endl; } } return 0; }
[["+", 8, 9, 0, 57, 15, 339, 51, 91, 17, 111]]
1
144
1
#include <iostream> using namespace std; int main() { int l, m, n; int k[12]; while (cin >> l) { if (l = 0) break; for (int i = 0; i < 12; i++) { cin >> m >> n; k[i] = m - n; } for (int i = 0; i < 12; i++) { l -= k[i]; if (l <= 0) { cout << i + 1 << endl; break; } } if (l > 0) cout << "NA" << endl; } }
#include <iostream> using namespace std; int main() { int l, m, n; int k[12]; while (cin >> l) { if (l == 0) break; for (int i = 0; i < 12; i++) { cin >> m >> n; k[i] = m - n; } for (int i = 0; i < 12; i++) { l -= k[i]; if (l <= 0) { cout << i + 1 << endl; break; } } if (l > 0) cout << "NA" << endl; } }
[["-", 8, 9, 0, 57, 15, 339, 51, 11, 17, 32], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60]]
1
127
2
#include <iostream> using namespace std; int main() { int l, m, n; while (true) { cin >> l; if (!l) break; int f = 1; for (int i = 0; i < 12; i++) { cin >> m >> n; l -= m - n; if (l <= 0 && f) { cout << i + 1 << endl; f = 0; } } if (l <= 0) cout << "NA" << endl; } return 0; }
#include <iostream> using namespace std; int main() { int l, m, n; while (true) { cin >> l; if (!l) break; int f = 1; for (int i = 0; i < 12; i++) { cin >> m >> n; l -= m - n; if (l <= 0 && f) { cout << i + 1 << endl; f = 0; } } if (l > 0) cout << "NA" << endl; } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 47]]
1
108
2
#include <iostream> using namespace std; int sum, n, a, b, ok; int main() { while (true) { sum = 0; ok = 0; cin >> n; if (n == 0) { break; } for (int i = 0; i < 12; i++) { cin >> a >> b; sum += a - b; if (sum >= n && ok == 0) { cout << i << endl; ok = 1; } } if (ok == 0) { cout << "NA" << endl; } } }
#include <iostream> using namespace std; int sum, n, a, b, ok; int main() { while (true) { sum = 0; ok = 0; cin >> n; if (n == 0) { break; } for (int i = 0; i < 12; i++) { cin >> a >> b; sum += a - b; if (sum >= n && ok == 0) { cout << i + 1 << endl; ok = 1; } } if (ok == 0) { cout << "NA" << endl; } } }
[["+", 0, 1, 0, 16, 31, 16, 12, 16, 17, 72], ["+", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13]]
1
117
2
#include <iostream> using namespace std; int main() { int l, m, n; while (cin >> l, l) { int ct = 0; for (int i = 0; i < 12; i++) { cin >> m >> n; if (l > 0) { l -= (m - n); ct++; } } if (ct > 12) { cout << "NA"; } else { cout << ct; } cout << endl; } return (0); }
#include <iostream> using namespace std; int main() { int l, m, n; while (cin >> l, l) { int ct = 0; for (int i = 0; i < 12; i++) { cin >> m >> n; if (l > 0) { l -= (m - n); ct++; } } if (l > 0) { cout << "NA"; } else { cout << ct; } cout << endl; } return (0); }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
105
4
#include <iostream> using namespace std; int main() { int shotoku[12]; int l, m, n; int i, total; while (cin >> l, l) { for (i = 0; i < 12; i++) { cin >> m >> n; shotoku[i] = m - n; } total = 0; for (i = 0; i < 12; i++) { total += shotoku[i]; if (l <= total) { cout << i + 1; break; } } if (i == 13) cout << "NA"; cout << endl; } return 0; }
#include <iostream> using namespace std; int main() { int shotoku[12]; int l, m, n; int i, total; while (cin >> l, l) { for (i = 0; i < 12; i++) { cin >> m >> n; shotoku[i] = m - n; } total = 0; for (i = 0; i < 12; i++) { total += shotoku[i]; if (l <= total) { cout << i + 1; break; } } if (i == 12) cout << "NA"; cout << endl; } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
131
2
#include <iostream> using namespace std; int main(int argc, char *argv[]) { while (1) { bool isfirst = true; int M, N, L, S = 0; cin >> L; if (L == 0) break; for (int i = 0; i < 12; i++) { cin >> M >> N; S += M - N; if (L <= S && isfirst) { cout << i + 1 << endl; isfirst = false; } } cout << "NA" << endl; NEXT:; } return 0; }
#include <iostream> using namespace std; int main(int argc, char *argv[]) { while (1) { bool isfirst = true; int M, N, L, S = 0; cin >> L; if (L == 0) break; for (int i = 0; i < 12; i++) { cin >> M >> N; S += M - N; if (L <= S && isfirst) { cout << i + 1 << endl; isfirst = false; } } if (isfirst) { cout << "NA" << endl; } NEXT:; } return 0; }
[["+", 8, 9, 0, 52, 8, 9, 0, 57, 0, 121], ["+", 0, 52, 8, 9, 0, 57, 15, 339, 0, 24], ["+", 0, 52, 8, 9, 0, 57, 15, 339, 51, 22], ["+", 0, 52, 8, 9, 0, 57, 15, 339, 0, 25], ["+", 0, 52, 8, 9, 0, 57, 64, 9, 0, 45], ["+", 0, 52, 8, 9, 0, 57, 64, 9, 0, 46]]
1
118
6
#include <iostream> #define N 12 using namespace std; int main() { int L; int m, n; int mon; bool skip; while (true) { cin >> L; if (L == 0) break; // sum = L; skip = false; for (int i = 0; i < N; i++) { cin >> m >> n; L -= m - n; if (L <= 0 && !skip) { skip = true; mon = i + 1; } } if (skip) cout << mon; else cout << "NA\n"; } }
#include <iostream> #define N 12 using namespace std; int main() { int L; int m, n; int mon; bool skip; while (true) { cin >> L; if (L == 0) break; // sum = L; skip = false; for (int i = 0; i < N; i++) { cin >> m >> n; L -= m - n; if (L <= 0 && !skip) { skip = true; mon = i + 1; } } if (skip) cout << mon << endl; else cout << "NA\n"; } }
[["+", 8, 9, 0, 57, 64, 1, 0, 16, 17, 151], ["+", 8, 9, 0, 57, 64, 1, 0, 16, 12, 22]]
1
117
2
#include <stdio.h> int main() { int l, m, n, i, j; while (0 <= scanf("%d", &l)) { if (l == 0) break; j = -1; for (i = 0; i < 12; i++) { scanf("%d%d", &m, &n); l -= (m - n); if (j == -1 && l <= 0) j = i; } if (j != 0) { printf("%d\n", j + 1); continue; } printf("NA\n"); } return 0; }
#include <stdio.h> int main() { int l, m, n, i, j; while (0 <= scanf("%d", &l)) { if (l == 0) break; j = -1; for (i = 0; i < 12; i++) { scanf("%d%d", &m, &n); l -= (m - n); if (j == -1 && l <= 0) j = i; } if (j != -1) { printf("%d\n", j + 1); continue; } printf("NA\n"); } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
130
2
#include <stdio.h> int main() { int L, M, N, t = 0, c = 0, i; for (;; t = 0, c = 0) { scanf("%d", &L); if (L == 0) break; for (i = 0; i < 12; i++) { scanf("%d%d", &M, &N); t += M - N; if (t >= L && c == 0) c = i; } if (c != 0) printf("%d\n", c + 1); else puts("NA"); } return 0; }
#include <stdio.h> int main() { int L, M, N, t = 0, c = 0, i; for (;; t = 0, c = 0) { scanf("%d", &L); if (L == 0) break; for (i = 1; i < 13; i++) { scanf("%d%d", &M, &N); t += M - N; if (t >= L && c == 0) { c = i; } } if (c != 0) printf("%d\n", c); else puts("NA"); } return 0; }
[["-", 0, 7, 8, 9, 0, 7, 10, 11, 12, 13], ["+", 0, 7, 8, 9, 0, 7, 10, 11, 12, 13], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 45], ["+", 0, 7, 8, 9, 0, 7, 8, 9, 0, 46], ["-", 64, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["-", 64, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
134
8
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() const int INF = 1 << 30; int main() { int l; while (cin >> l) { bool f = 0; int a[13], b[13]; REP(i, 13) { cin >> a[i] >> b[i]; } REP(i, 12) { l -= a[i] - b[i]; if (l <= 0) { cout << i + 1 << endl; f = 1; break; } } if (!f) { cout << "NA" << endl; } } }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() const int INF = 1 << 30; int main() { int l; while (cin >> l, l) { bool f = 0; int a[12], b[12]; REP(i, 12) { cin >> a[i] >> b[i]; } REP(i, 12) { l -= a[i] - b[i]; if (l <= 0) { cout << i + 1 << endl; f = 1; break; } } if (!f) { cout << "NA" << endl; } } }
[["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 12, 22], ["-", 0, 52, 8, 9, 0, 43, 49, 80, 81, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 80, 81, 13], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
1
167
8
#include <cstdio> int main() { int l; while (scanf("%d", &l), l) { int cm = -1; for (int i = 1; i <= 12; i++) { int m, n; scanf("%d%d", &m, &n); l -= m - n; if (l <= 0 && cm < 0) cm = i; } if (cm < 0) puts("NA"); else printf("%d", cm); } }
#include <cstdio> int main() { int l; while (scanf("%d", &l), l) { int cm = -1; for (int i = 1; i <= 12; i++) { int m, n; scanf("%d%d", &m, &n); l -= m - n; if (l <= 0 && cm < 0) cm = i; } if (cm < 0) puts("NA"); else printf("%d\n", cm); } }
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
1
109
1
#include <iostream> using namespace std; int main() { long long int l, m, n; long long int savingMoney; bool output = true; while (cin >> l, l) { savingMoney = 0; for (int i = 1; i <= 12; i++) { cin >> m >> n; savingMoney += m - n; if (l <= savingMoney && output) { cout << i << endl; output = false; } if (i == 12 && savingMoney < l) { cout << "NA" << endl; } } } }
#include <iostream> using namespace std; int main() { long long int l, m, n; long long int savingMoney; bool output = true; while (cin >> l, l) { savingMoney = 0; output = true; for (int i = 1; i <= 12; i++) { cin >> m >> n; savingMoney += m - n; if (l <= savingMoney && output) { cout << i << endl; output = false; } if (i == 12 && savingMoney < l) { cout << "NA" << endl; } } } }
[["+", 0, 52, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 146], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
113
4
#include <iostream> using namespace std; int main(void) { int M[12] = {0}; int N[12] = {0}; int L = 0; while (true) { int ans = 0; cin >> L; if (L == 0) { return 0; } int i = 0; for (i = 0; i < 12; i++) { cin >> M[i] >> N[i]; } for (int i = 0; i < 12; i++) { if (L <= 0) { break; } ans++; L = L + N[i] - M[i]; } if (i == 12) { cout << "NA" << endl; continue; } cout << ans << endl; } return 0; }
#include <iostream> using namespace std; int main(void) { int M[12] = {0}; int N[12] = {0}; int L = 0; while (true) { int ans = 0; cin >> L; if (L == 0) { return 0; } int i = 0; for (i = 0; i < 12; i++) { cin >> M[i] >> N[i]; } for (i = 0; i < 12; i++) { if (L <= 0) { break; } ans++; L = L + N[i] - M[i]; } if (i == 12 && L > 0) { cout << "NA" << endl; continue; } cout << ans << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 10, 43, 39, 40], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 98], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 47], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 12, 13]]
1
166
5
while (l = gets.to_i) != 0 sum = 0 flag = true 1.upto(12) do |i| m,n = gets.split(" ").map(&:to_i) sum += m - n if sum >= l && flag puts i flag = false end end puts "NA" unless flag end
while (l = gets.to_i) != 0 sum = 0 flag = true 1.upto(12) do |i| m,n = gets.split(" ").map(&:to_i) sum += m - n if sum >= l && flag puts i flag = false end end puts "NA" if flag end
[["-", 0, 493, 0, 89, 8, 170, 0, 745, 0, 747], ["+", 0, 493, 0, 89, 8, 170, 0, 751, 0, 121]]
4
69
2
class NextTrip def solve while true target_price = gets.to_i break if target_price <= 0 calc(target_price) end end private def calc(target_price) answer = 'NA' 1.upto(12) do |month| m, n = gets.split.map(&:to_i) target_price -= (m - n) if target_price <= 0 && answer == 'NA' answer = month end end p answer end end next_trip = NextTrip.new next_trip.solve
class NextTrip def solve while true target_price = gets.to_i break if target_price <= 0 calc(target_price) end end private def calc(target_price) answer = 'NA' 1.upto(12) do |month| m, n = gets.split.map(&:to_i) target_price -= (m - n) if target_price <= 0 && answer == 'NA' answer = month end end puts answer end end next_trip = NextTrip.new next_trip.solve
[["-", 8, 736, 0, 735, 8, 736, 0, 652, 735, 22], ["+", 8, 736, 0, 735, 8, 736, 0, 652, 735, 22]]
4
90
2
while True: l = eval(input()) if l==0: break c = []; m = 0; for i in range(12): c += list(map(int, input().split())) for i in range(0,12,2): m += c[i]-c[i+1] if m>=l: print(i/2+1); break; if m<l: print("NA")
while True: l = eval(input()) if l==0: break c = []; m = 0; for i in range(12): c += list(map(int, input().split())) for i in range(0,24,2): m += c[i]-c[i+1] if m>=l: print(i/2+1); break; if m<l: print("NA")
[["-", 8, 196, 0, 7, 12, 652, 3, 4, 0, 612], ["+", 8, 196, 0, 7, 12, 652, 3, 4, 0, 612]]
5
103
2
#include <iostream> #include <queue> #include <string.h> using namespace std; #define TATE 1 #define YOKO 0 struct ST { int x; int y; }; int main(void) { int map[101][101]; int board[101][101]; int T[] = {0, -1, 1, 0}; int Y[] = {1, 0, 0, -1}; int w, h, xs, xg, ys, yg, n, color, muki, x, y; ST RE; ST GO; queue<ST> Q; while (1) { memset(map, 0, sizeof(map)); memset(board, 0, sizeof(board)); cin >> w >> h; if (w == 0 && h == 0) break; cin >> xs >> ys >> xg >> yg >> n; for (int i = 0; i < n; i++) { cin >> color >> muki >> x >> y; if (muki == YOKO) { for (int j = 0; j < 4; j++) { board[y][x + j] = board[y + 1][x + j] = color; } } else { for (int j = 0; j < 4; j++) { board[y + j][x] = board[y + j][x + 1] = color; } } } /* for(int i=1;i<=h;i++){ for(int j=1;j<=w;j++){ cout<<board[i][j]; } cout<<endl; }*/ RE.x = xs; RE.y = ys; map[ys][xs] = 1; Q.push(RE); int flag = 0; while (!Q.empty()) { RE = Q.front(); Q.pop(); if (RE.x == xg && RE.y == yg && board[RE.y][RE.x] == board[yg][xg]) { flag = 1; // cout<<RE.x<<" "<<RE.y<<endl; break; } else { for (int i = 0; i < 4; i++) { GO = RE; if (GO.x + Y[i] > 0 && GO.y + T[i] > 0 && GO.x + Y[i] < 101 && GO.y + T[i] < 101 && board[GO.y + T[i]][GO.x + Y[i]] == board[yg][xg] && map[GO.y + T[i]][GO.x + Y[i]] != 1) { GO.x += Y[i]; GO.y += T[i]; map[GO.y][GO.x] = 1; Q.push(GO); } } } } /* for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ cout<<board[i][j]; } cout<<endl; }*/ if (flag == 1) { cout << "OK" << endl; } else { cout << "NG" << endl; } } }
#include <iostream> #include <queue> #include <string.h> using namespace std; #define TATE 1 #define YOKO 0 struct ST { int x; int y; }; int main(void) { int map[101][101]; int board[101][101]; int T[] = {0, -1, 1, 0}; int Y[] = {1, 0, 0, -1}; int w, h, xs, xg, ys, yg, n, color, muki, x, y; ST RE; ST GO; queue<ST> Q; while (1) { memset(map, 0, sizeof(map)); memset(board, 0, sizeof(board)); cin >> w >> h; if (w == 0 && h == 0) break; cin >> xs >> ys >> xg >> yg >> n; for (int i = 0; i < n; i++) { cin >> color >> muki >> x >> y; if (muki == YOKO) { for (int j = 0; j < 4; j++) { board[y][x + j] = board[y + 1][x + j] = color; } } else { for (int j = 0; j < 4; j++) { board[y + j][x] = board[y + j][x + 1] = color; } } } /* for(int i=1;i<=h;i++){ for(int j=1;j<=w;j++){ cout<<board[i][j]; } cout<<endl; }*/ RE.x = xs; RE.y = ys; map[ys][xs] = 1; Q.push(RE); int flag = 0; while (!Q.empty()) { RE = Q.front(); Q.pop(); if (RE.x == xg && RE.y == yg && board[RE.y][RE.x] == board[yg][xg]) { flag = 1; // cout<<RE.x<<" "<<RE.y<<endl; break; } else { for (int i = 0; i < 4; i++) { GO = RE; if (GO.x + Y[i] > 0 && GO.y + T[i] > 0 && GO.x + Y[i] < 101 && GO.y + T[i] < 101 && board[GO.y + T[i]][GO.x + Y[i]] == board[GO.y][GO.x] && map[GO.y + T[i]][GO.x + Y[i]] != 1) { GO.x += Y[i]; GO.y += T[i]; map[GO.y][GO.x] = 1; Q.push(GO); } } } } /* for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ cout<<board[i][j]; } cout<<endl; }*/ if (flag == 1) { cout << "OK" << endl; } else { cout << "NG" << endl; } } }
[["-", 12, 16, 12, 69, 28, 69, 341, 342, 0, 22], ["+", 12, 69, 28, 69, 341, 342, 0, 118, 28, 22], ["+", 12, 69, 28, 69, 341, 342, 0, 118, 17, 131], ["+", 12, 69, 28, 69, 341, 342, 0, 118, 119, 120], ["-", 31, 16, 12, 16, 12, 69, 341, 342, 0, 22], ["+", 12, 16, 12, 69, 341, 342, 0, 118, 28, 22], ["+", 12, 16, 12, 69, 341, 342, 0, 118, 17, 131], ["+", 12, 16, 12, 69, 341, 342, 0, 118, 119, 120]]
1
589
8
#include <iostream> #include <stack> #include <string.h> using namespace std; struct ST { int a; int b; int c; }; long long brock[1000][1000]; long long ds[1000][1000]; int q[4] = {0, -1, 0, 1}; int p[4] = {1, 0, -1, 0}; int main() { stack<ST> S; ST ma; ST ba; int w, h; //???????????§?????? int xs, ys; //??????????????§?¨? int xg, yg; //??´????????§?¨? int n; //????????????????????° int c, d, x, y; //????????????????????± int i, j, m, check = 0; while (1) { memset(ds, 0, sizeof(ds)); memset(brock, 0, sizeof(brock)); cin >> w >> h; //???????????§????????\??? if (w == 0 && h == 0) { break; } cin >> xs >> ys >> xg >> yg; //??????????????¨??´???????????§?¨???\??? cin >> n; //????????????????????° for (i = 0; i < n; i++) { cin >> c >> d >> x >> y; if (d == 0) { for (m = y; m < y + 2; m++) { for (j = x; j < x + 4; j++) { brock[m][j] = c; } } } else { for (m = y; m < y + 4; m++) { for (j = x; j < x + 2; j++) { brock[m][j] = c; } } } } /*for(i=1;i<h;i++){ for(j=1;j<w;j++){ cout<<brock[i][j]; } cout<<endl; }*/ if (brock[ys][xs] != brock[yg][xg]) { cout << "NG" << endl; } else { ma.a = ys; ma.b = xs; ma.c = brock[ys][xs]; ds[ys][xs] = 1; S.push(ma); check = 0; while (!S.empty()) { ba = S.top(); S.pop(); ds[ba.a][ba.b] = 1; if (ba.a == yg && ba.b == xg) { check = 1; break; } else { for (i = 0; i < 4; i++) { if (ba.a + p[i] > 0 && ba.a + p[i] <= h && ba.b + q[i] > 0 && ba.b + q[i] <= w && ds[ba.a + p[i]][ba.b + q[i]] == 0 && brock[ba.a + p[i]][ba.b + q[i]] == brock[yg][xg]) { ma.a = ba.a + p[i]; ma.b = ba.b + q[i]; ma.c = brock[ba.a + p[i]][ba.b + q[i]]; S.push(ma); } } } } if (check == 1) { cout << "OK" << endl; } else { cout << "NG" << endl; } } } return 0; }
#include <iostream> #include <stack> #include <string.h> using namespace std; struct ST { int a; int b; int c; }; long long brock[1000][1000]; long long ds[1000][1000]; int q[4] = {0, -1, 0, 1}; int p[4] = {1, 0, -1, 0}; int main() { stack<ST> S; ST ma; ST ba; int w, h; //???????????§?????? int xs, ys; //??????????????§?¨? int xg, yg; //??´????????§?¨? int n; //????????????????????° int c, d, x, y; //????????????????????± int i, j, m, check = 0; while (1) { memset(ds, 0, sizeof(ds)); memset(brock, 0, sizeof(brock)); cin >> w >> h; //???????????§????????\??? if (w == 0 && h == 0) { break; } cin >> xs >> ys >> xg >> yg; //??????????????¨??´???????????§?¨???\??? cin >> n; //????????????????????° for (i = 0; i < n; i++) { cin >> c >> d >> x >> y; if (d == 0) { for (m = y; m < y + 2; m++) { for (j = x; j < x + 4; j++) { brock[m][j] = c; } } } else { for (m = y; m < y + 4; m++) { for (j = x; j < x + 2; j++) { brock[m][j] = c; } } } } /*for(i=1;i<h;i++){ for(j=1;j<w;j++){ cout<<brock[i][j]; } cout<<endl; }*/ if (brock[ys][xs] != brock[yg][xg]) { cout << "NG" << endl; } else { ma.a = ys; ma.b = xs; ma.c = brock[ys][xs]; ds[ys][xs] = 1; S.push(ma); check = 0; while (!S.empty()) { ba = S.top(); S.pop(); ds[ba.a][ba.b] = 1; if (ba.a == yg && ba.b == xg) { check = 1; continue; } for (i = 0; i < 4; i++) { if (ba.a + p[i] > 0 && ba.a + p[i] <= h && ba.b + q[i] > 0 && ba.b + q[i] <= w && ds[ba.a + p[i]][ba.b + q[i]] == 0 && brock[ba.a + p[i]][ba.b + q[i]] == brock[yg][xg]) { ma.a = ba.a + p[i]; ma.b = ba.b + q[i]; ma.c = brock[ba.a + p[i]][ba.b + q[i]]; S.push(ma); } } } if (check == 1) { cout << "OK" << endl; } else { cout << "NG" << endl; } } } return 0; }
[["-", 8, 9, 0, 57, 64, 9, 0, 93, 0, 94], ["+", 8, 9, 0, 57, 64, 9, 0, 116, 0, 117], ["-", 0, 52, 8, 9, 0, 57, 75, 76, 0, 95], ["-", 8, 9, 0, 57, 75, 76, 0, 9, 0, 45], ["-", 0, 7, 8, 9, 0, 57, 64, 9, 0, 46]]
1
675
5
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; #define pu push #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 2000000000 #define sz(x) ((int)(x).size()) #define fi first #define sec second #define SORT(x) sort((x).begin(), (x).end()) #define all(x) (x).begin(), (x).end() #define EQ(a, b) (abs((a) - (b)) < eps) int w, h; int sx, sy, gx, gy; int n; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; bool used[120][120]; int f[120][120]; void set(int c, int d, int x, int y) { if (d == 0) { for (int i = 0; i < 2; i++) { for (int j = 0; j < 4; j++) { f[x + i][y + j] = c; } } } else { for (int i = 0; i < 4; i++) { for (int j = 0; j < 2; j++) { f[x + i][y + j] = c; } } } return; } bool judge() { if (f[sx][sy] != f[gx][gy]) return false; memset(used, false, sizeof(used)); queue<P> q; q.push(mp(sx, sy)); used[sx][sy] = true; while (!q.empty()) { P a = q.front(); q.pop(); for (int i = 0; i < 4; i++) { int nx = a.fi + dx[i], ny = a.sec + dy[i]; if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue; if (used[nx][ny] || f[nx][ny] != f[a.fi][a.sec]) continue; if (nx == gx && ny == gy) return true; used[nx][ny] = true; q.push(mp(nx, ny)); } } return false; } int main() { while (1) { memset(f, 0, sizeof(f)); scanf("%d %d", &h, &w); if (w == 0 && h == 0) break; scanf("%d %d", &sy, &sx); scanf("%d %d", &gy, &gx); scanf("%d", &n); for (int i = 0; i < n; i++) { int c, d, x, y; scanf("%d %d %d %d", &c, &d, &y, &x); set(c, d, x, y); } if (judge()) puts("OK"); else puts("NG"); } return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; #define pu push #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 2000000000 #define sz(x) ((int)(x).size()) #define fi first #define sec second #define SORT(x) sort((x).begin(), (x).end()) #define all(x) (x).begin(), (x).end() #define EQ(a, b) (abs((a) - (b)) < eps) int w, h; int sx, sy, gx, gy; int n; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; bool used[120][120]; int f[120][120]; void set(int c, int d, int x, int y) { if (d == 0) { for (int i = 0; i < 2; i++) { for (int j = 0; j < 4; j++) { f[x + i][y + j] = c; } } } else { for (int i = 0; i < 4; i++) { for (int j = 0; j < 2; j++) { f[x + i][y + j] = c; } } } return; } bool judge() { if (f[sx][sy] != f[gx][gy]) return false; memset(used, false, sizeof(used)); queue<P> q; q.push(mp(sx, sy)); used[sx][sy] = true; while (!q.empty()) { P a = q.front(); q.pop(); for (int i = 0; i < 4; i++) { int nx = a.fi + dx[i], ny = a.sec + dy[i]; if (nx <= 0 || nx > w || ny <= 0 || ny > h) continue; if (used[nx][ny] || f[nx][ny] != f[a.fi][a.sec]) continue; if (nx == gx && ny == gy) return true; used[nx][ny] = true; q.push(mp(nx, ny)); } } return false; } int main() { while (1) { memset(f, 0, sizeof(f)); scanf("%d %d", &h, &w); if (w == 0 && h == 0) break; scanf("%d %d", &sy, &sx); scanf("%d %d", &gy, &gx); scanf("%d", &n); for (int i = 0; i < n; i++) { int c, d, x, y; scanf("%d %d %d %d", &c, &d, &y, &x); set(c, d, x, y); } if (judge()) puts("OK"); else puts("NG"); } return 0; }
[["-", 51, 16, 31, 16, 31, 16, 31, 16, 17, 18], ["+", 51, 16, 31, 16, 31, 16, 31, 16, 17, 19], ["-", 51, 16, 31, 16, 31, 16, 12, 16, 17, 20], ["+", 51, 16, 31, 16, 31, 16, 12, 16, 17, 47], ["-", 15, 339, 51, 16, 31, 16, 12, 16, 17, 18], ["+", 15, 339, 51, 16, 31, 16, 12, 16, 17, 19], ["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 20], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 47]]
1
682
8
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define REP(i, s, e) for (int i = (s); i < (int)(e); i++) #define rep(i, n) REP(i, 0, n) #define pb push_back #define mp make_pair #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<vi> vii; typedef vector<ll> vl; typedef vector<vl> vll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1000000; const double EPS = 1e-8; const int mod = 1e9 + 7; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; int w, h, sx, sy, gx, gy, n; int c, d, x, y, nx, ny; int dat[110][110]; bool used[110][110]; bool outOfRange(int x, int y) { if (x > w || x < 1 || y > h || y < 1) return true; return false; } int main() { while (cin >> w >> h && w && h) { cin >> sx >> sy >> gx >> gy >> n; rep(i, 110) rep(j, 110) dat[i][j] = 0, used[i][j] = false; rep(i, n) { cin >> c >> d >> x >> y; if (d == 0) { for (int j = 0; j < 4; j++) { for (int k = 0; k < 2; k++) { dat[x + j][y + k] = c; } } } else { for (int j = 0; j < 4; j++) { for (int k = 0; k < 2; k++) { dat[x + k][y + j] = c; } } } } queue<pair<int, pii>> q; pair<int, pii> p; q.push(mp(0, mp(sx, sy))); used[sx][sy] = true; bool ans = false; while (!q.empty()) { p = q.front(); q.pop(); c = p.fi; x = p.se.fi; y = p.se.se; if (x == gx && y == gy) { ans = true; break; } for (int i = 0; i < n; i++) { nx = x + dx[i]; ny = y + dy[i]; if (outOfRange(nx, ny) || used[nx][ny] == true) continue; if (dat[x][y] == dat[nx][ny]) { q.push(mp(c + 1, mp(nx, ny))); used[nx][ny] = true; } } } cout << (ans ? "OK" : "NO") << endl; } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define REP(i, s, e) for (int i = (s); i < (int)(e); i++) #define rep(i, n) REP(i, 0, n) #define pb push_back #define mp make_pair #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<vi> vii; typedef vector<ll> vl; typedef vector<vl> vll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1000000; const double EPS = 1e-8; const int mod = 1e9 + 7; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; int w, h, sx, sy, gx, gy, n; int c, d, x, y, nx, ny; int dat[110][110]; bool used[110][110]; bool outOfRange(int x, int y) { if (x > w || x < 1 || y > h || y < 1) return true; return false; } int main() { while (cin >> w >> h && w && h) { cin >> sx >> sy >> gx >> gy >> n; rep(i, 110) rep(j, 110) dat[i][j] = 0, used[i][j] = false; rep(i, n) { cin >> c >> d >> x >> y; if (d == 0) { for (int j = 0; j < 4; j++) { for (int k = 0; k < 2; k++) { dat[x + j][y + k] = c; } } } else { for (int j = 0; j < 4; j++) { for (int k = 0; k < 2; k++) { dat[x + k][y + j] = c; } } } } queue<pair<int, pii>> q; pair<int, pii> p; q.push(mp(0, mp(sx, sy))); used[sx][sy] = true; bool ans = false; while (!q.empty()) { p = q.front(); q.pop(); c = p.fi; x = p.se.fi; y = p.se.se; if (x == gx && y == gy) { ans = true; break; } for (int i = 0; i < 4; i++) { nx = x + dx[i]; ny = y + dy[i]; if (outOfRange(nx, ny) || used[nx][ny] == true) continue; if (dat[x][y] == dat[nx][ny]) { q.push(mp(c + 1, mp(nx, ny))); used[nx][ny] = true; } } } cout << (ans ? "OK" : "NG") << endl; } }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 31, 16, 12, 23, 0, 41, 75, 5, 0, 6], ["+", 31, 16, 12, 23, 0, 41, 75, 5, 0, 6]]
1
682
4
#include <iostream> #include <vector> using namespace std; #define BLANK 0 const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, -1, 0, 1}; bool dfs(int color, int x, int y, int gx, int gy, vector<vector<int>> &blocks) { if (x == gx && y == gy) { return true; } blocks[x][y] = BLANK; for (int i = 0; i < 4; i++) if (blocks[x + dx[i]][y + dy[i]] == color) if (dfs(color, x + dx[i], y + dy[i], gx, gy, blocks)) return true; return false; } int main(void) { while (true) { int w, h; cin >> w >> h; if (w == 0 && h == 0) { break; } int sx, sy, gx, gy; cin >> sx >> sy; cin >> gx >> gy; int n; cin >> n; vector<vector<int>> blocks(w + 2, vector<int>(h + 2, BLANK)); for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; int now_w = (d == 0) ? 4 : 2; int now_h = (d == 0) ? 2 : 4; for (int j = 0; j < now_w; j++) for (int k = 0; k < now_h; k++) blocks[x + j][y + k] = c; } // ???????????? if (blocks[sx][sy] == BLANK) { cout << "NG" << endl; return 0; } if (dfs(blocks[sx][sy], sx, sy, gx, gy, blocks)) { cout << "OK" << endl; } else { cout << "NG" << endl; } } return 0; }
#include <iostream> #include <vector> using namespace std; #define BLANK 0 const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, -1, 0, 1}; bool dfs(int color, int x, int y, int gx, int gy, vector<vector<int>> &blocks) { if (x == gx && y == gy) { return true; } blocks[x][y] = BLANK; for (int i = 0; i < 4; i++) if (blocks[x + dx[i]][y + dy[i]] == color) if (dfs(color, x + dx[i], y + dy[i], gx, gy, blocks)) return true; return false; } int main(void) { while (true) { int w, h; cin >> w >> h; if (w == 0 && h == 0) { break; } int sx, sy, gx, gy; cin >> sx >> sy; cin >> gx >> gy; int n; cin >> n; vector<vector<int>> blocks(w + 2, vector<int>(h + 2, BLANK)); for (int i = 0; i < n; i++) { int c, d, x, y; cin >> c >> d >> x >> y; int now_w = (d == 0) ? 4 : 2; int now_h = (d == 0) ? 2 : 4; for (int j = 0; j < now_w; j++) for (int k = 0; k < now_h; k++) blocks[x + j][y + k] = c; } // ???????????? if (blocks[sx][sy] == BLANK) { cout << "NG" << endl; continue; } // ?????? if (dfs(blocks[sx][sy], sx, sy, gx, gy, blocks)) { cout << "OK" << endl; } else { cout << "NG" << endl; } } return 0; }
[["-", 8, 9, 0, 57, 64, 9, 0, 37, 0, 38], ["-", 8, 9, 0, 57, 64, 9, 0, 37, 0, 13], ["+", 8, 9, 0, 57, 64, 9, 0, 116, 0, 117]]
1
435
3
#include <algorithm> #include <cassert> #include <cctype> #include <complex> #include <cstdio> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; int show = 1; int w, h, sx, sy, gx, gy, d[101][101], dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}, C; int field[101][101]; const int INF = 9999999; typedef pair<int, int> P; void bfs() { queue<P> que; for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) d[i][j] = INF; que.push(P(sy, sx)); d[sy][sx] = 0; while (!que.empty()) { P p = que.front(); que.pop(); if (p.first == gy && p.second == gx) break; for (int i = 0; i < 4; i++) { int ny = p.first + dy[i], nx = p.second + dx[i]; if (1 <= ny && ny <= h && 1 <= nx && nx <= w && field[ny][nx] == C && d[ny][nx] == INF) { que.push(P(ny, nx)); d[ny][nx] = d[p.first][p.second] + 1; } } } if (d[gy][gx] == INF) cout << "NG" << endl; else cout << "OK" << endl; } int main() { while (cin >> w >> h) { if (!h) return 0; int n, c, di, x, y; cin >> sx >> sy >> gx >> gy >> n; for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) field[i][j] = 0; for (int i = 0; i < n; i++) { cin >> c >> di >> x >> y; for (int j = y; j < y + 2 + di * 2; j++) for (int k = x; k < x + 2 + !di * 2; k++) field[j][k] = c; } C = field[1][1]; if (!C) cout << "NG" << endl; else bfs(); if (show) { for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (field[i][j]) cout << field[i][j]; else cout << " "; } cout << endl; } for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (d[i][j] < INF) cout << d[i][j] << " "; else cout << " "; } cout << endl; } } } }
#include <algorithm> #include <cassert> #include <cctype> #include <complex> #include <cstdio> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; int show = 0; int w, h, sx, sy, gx, gy, d[101][101], dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}, C; int field[101][101]; const int INF = 9999999; typedef pair<int, int> P; void bfs() { queue<P> que; for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) d[i][j] = INF; que.push(P(sy, sx)); d[sy][sx] = 0; while (!que.empty()) { P p = que.front(); que.pop(); if (p.first == gy && p.second == gx) break; for (int i = 0; i < 4; i++) { int ny = p.first + dy[i], nx = p.second + dx[i]; if (1 <= ny && ny <= h && 1 <= nx && nx <= w && field[ny][nx] == C && d[ny][nx] == INF) { que.push(P(ny, nx)); d[ny][nx] = d[p.first][p.second] + 1; } } } if (d[gy][gx] == INF) cout << "NG" << endl; else cout << "OK" << endl; } int main() { while (cin >> w >> h) { if (!h) return 0; int n, c, di, x, y; cin >> sx >> sy >> gx >> gy >> n; for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) field[i][j] = 0; for (int i = 0; i < n; i++) { cin >> c >> di >> x >> y; for (int j = y; j < y + 2 + di * 2; j++) for (int k = x; k < x + 2 + !di * 2; k++) field[j][k] = c; } C = field[sy][sx]; if (!C) cout << "NG" << endl; else bfs(); if (show) { for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (field[i][j]) cout << field[i][j]; else cout << " "; } cout << endl; } for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (d[i][j] < INF) cout << d[i][j] << " "; else cout << " "; } cout << endl; } } } }
[["-", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["-", 0, 11, 12, 69, 28, 69, 341, 342, 0, 13], ["+", 0, 11, 12, 69, 28, 69, 341, 342, 0, 22], ["-", 0, 1, 0, 11, 12, 69, 341, 342, 0, 13], ["+", 0, 1, 0, 11, 12, 69, 341, 342, 0, 22]]
1
696
6
#include <cstring> #include <iostream> #include <queue> #include <utility> using namespace std; typedef pair<int, int> P; #define REP(i, b, n) for (int i = b; i < n; i++) #define rep(i, n) REP(i, 0, n) const int dy[4] = {-1, 1, 0, 0}; const int dx[4] = {0, 0, -1, 1}; const int MAX_W = 101, MAX_H = 101; int maze[MAX_H][MAX_W]; int w, h, xs, ys, xg, yg, color; void solve() { memset(maze, 0, sizeof maze); int n; cin >> xs >> ys >> xg >> yg >> n; rep(i, n) { int c, d, x, y; cin >> c >> d >> x >> y; // cout<<c<<d<<x<<y<<endl; if (d == 0) { REP(j, x, x + 4) REP(k, y, y + 2) maze[k][j] = c; } else { REP(j, x, x + 2) REP(k, y, y + 4) maze[k][j] = c; } } if (maze[ys][xs] == 0) { cout << "NG" << endl; return; } else { color = maze[ys][xs]; } bool flag = false; queue<P> que; que.push(P(xs, ys)); while (!que.empty() || !flag) { P p = que.front(); que.pop(); int tx = p.first, ty = p.second; if (tx == xg && ty == yg) { flag = true; continue; } if (maze[ty][tx] == 0) continue; maze[ty][tx] = 0; rep(z, 4) { int ttx = tx + dx[z], tty = ty + dy[z]; if (ttx < 1 || tty < 1 || ttx >= w || tty >= h) continue; if (maze[tty][ttx] != color) continue; que.push(P(ttx, tty)); } } if (flag) { cout << "OK" << endl; } else { cout << "NG" << endl; } } int main(int argc, char const *argv[]) { while (cin >> w >> h, w || h) { solve(); } return 0; }
#include <cstring> #include <iostream> #include <queue> #include <utility> using namespace std; typedef pair<int, int> P; #define REP(i, b, n) for (int i = b; i < n; i++) #define rep(i, n) REP(i, 0, n) const int dy[4] = {-1, 1, 0, 0}; const int dx[4] = {0, 0, -1, 1}; const int MAX_W = 102, MAX_H = 102; int maze[MAX_H][MAX_W]; int w, h, xs, ys, xg, yg, color; void solve() { memset(maze, 0, sizeof maze); int n; cin >> xs >> ys >> xg >> yg >> n; rep(i, n) { int c, d, x, y; cin >> c >> d >> x >> y; // cout<<c<<d<<x<<y<<endl; if (d == 0) { REP(j, x, x + 4) REP(k, y, y + 2) maze[k][j] = c; } else { REP(j, x, x + 2) REP(k, y, y + 4) maze[k][j] = c; } } /* rep(i,h+1) { rep(j,w+1) cout<<maze[i][j]; cout<<endl; }*/ if (maze[ys][xs] == 0) { cout << "NG" << endl; return; } else { color = maze[ys][xs]; } bool flag = false; queue<P> que; que.push(P(xs, ys)); while (!que.empty() && !flag) { P p = que.front(); que.pop(); int tx = p.first, ty = p.second; if (tx == xg && ty == yg) { flag = true; continue; } if (maze[ty][tx] == 0) continue; maze[ty][tx] = 0; rep(z, 4) { int ttx = tx + dx[z], tty = ty + dy[z]; if (ttx < 1 || tty < 1 || ttx > w || tty > h) continue; if (maze[tty][ttx] != color) continue; que.push(P(ttx, tty)); } } if (flag) { cout << "OK" << endl; } else { cout << "NG" << endl; } } int main(int argc, char const *argv[]) { while (cin >> w >> h, w || h) { solve(); } return 0; }
[["-", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["-", 8, 9, 0, 52, 15, 339, 51, 16, 17, 106], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 17, 98], ["-", 15, 339, 51, 16, 31, 16, 12, 16, 17, 20], ["+", 15, 339, 51, 16, 31, 16, 12, 16, 17, 47], ["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 20], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 47]]
1
518
10
#include <algorithm> // require sort next_permutation count etc. #include <cctype> // require tolower, toupper #include <cfloat> #include <climits> #include <cmath> #include <cstdio> // require printf #include <cstdlib> // require abs #include <cstring> // require memset #include <deque> #include <fstream> // require freopen #include <functional> #include <iomanip> // require setw #include <iostream> #include <limits> #include <map> #include <numeric> // require accumulate #include <queue> #include <set> #include <sstream> // require stringstream #include <stack> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; int maze[20][20]; bool used[20][20]; int main() { const int dir[][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // cut here before submit // freopen ("testcase.block", "r", stdin ); int w, h; while (cin >> w >> h && w && h) { memset(maze, 0, sizeof(maze)); memset(used, false, sizeof(used)); P start, goal; cin >> start.first >> start.second; start.first--; start.second--; cin >> goal.first >> goal.second; goal.first--; goal.second--; int n; cin >> n; for (int i = 0; i < n; ++i) { int color, dir; P block; cin >> color >> dir >> block.first >> block.second; block.first--; block.second--; if (dir == 0) { for (int col = 0; col < 4; ++col) { maze[block.second][block.first + col] = color; maze[block.second + 1][block.first + col] = color; } // end for } else { for (int row = 0; row < 4; ++row) { maze[block.second + row][block.first] = color; maze[block.second + row][block.first + 1] = color; } // end for } // end if } // end for queue<P> que; que.push(P(start.first, start.second)); int search_color = maze[start.second][start.first]; bool found = false; while (!que.empty()) { P curr = que.front(); que.pop(); int x = curr.first; int y = curr.second; if (x == goal.first && y == goal.second) { found = true; break; } // end if used[y][x] = true; for (int k = 0; k < 4; ++k) { int nx = x + dir[k][0]; int ny = y + dir[k][1]; if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue; if ((maze[ny][nx] == search_color) && !used[ny][nx]) { P next; next.first = nx; next.second = ny; que.push(next); } // end if } // end for } // end while if (found) cout << "OK" << endl; else cout << "NG" << endl; } // end loop return 0; }
#include <algorithm> // require sort next_permutation count etc. #include <cctype> // require tolower, toupper #include <cfloat> #include <climits> #include <cmath> #include <cstdio> // require printf #include <cstdlib> // require abs #include <cstring> // require memset #include <deque> #include <fstream> // require freopen #include <functional> #include <iomanip> // require setw #include <iostream> #include <limits> #include <map> #include <numeric> // require accumulate #include <queue> #include <set> #include <sstream> // require stringstream #include <stack> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; int maze[100][100]; bool used[100][100]; int main() { const int dir[][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // cut here before submit // freopen ("testcase.block", "r", stdin ); int w, h; while (cin >> w >> h && w && h) { memset(maze, 0, sizeof(maze)); memset(used, false, sizeof(used)); P start, goal; cin >> start.first >> start.second; start.first--; start.second--; cin >> goal.first >> goal.second; goal.first--; goal.second--; int n; cin >> n; for (int i = 0; i < n; ++i) { int color, dir; P block; cin >> color >> dir >> block.first >> block.second; block.first--; block.second--; if (dir == 0) { for (int col = 0; col < 4; ++col) { maze[block.second][block.first + col] = color; maze[block.second + 1][block.first + col] = color; } // end for } else { for (int row = 0; row < 4; ++row) { maze[block.second + row][block.first] = color; maze[block.second + row][block.first + 1] = color; } // end for } // end if } // end for queue<P> que; que.push(P(start.first, start.second)); int search_color = maze[start.second][start.first]; bool found = false; while (!que.empty()) { P curr = que.front(); que.pop(); int x = curr.first; int y = curr.second; if (x == goal.first && y == goal.second) { found = true; break; } // end if used[y][x] = true; for (int k = 0; k < 4; ++k) { int nx = x + dir[k][0]; int ny = y + dir[k][1]; if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue; if ((maze[ny][nx] == search_color) && !used[ny][nx]) { P next; next.first = nx; next.second = ny; que.push(next); } // end if } // end for } // end while if (found) cout << "OK" << endl; else cout << "NG" << endl; } // end loop return 0; }
[["-", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["+", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13]]
1
637
8
#include <algorithm> #include <cstdio> #include <cstring> #include <map> #include <vector> #define rep(i, j) FOR(i, 0, j) #define FOR(i, j, k) for (int i = j; i < k; ++i) using namespace std; int w, h, xs, ys, xg, yg, n, c, d, x, y, b[101][101]; int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0}; bool dfs(int x, int y) { int nx, ny; b[x][y] = 0; if (x == xg && y == yg) return true; bool r = 0; rep(i, 4) { nx = x + dx[i]; ny = y + dy[i]; if (nx > 0 && nx <= h && ny > 0 && ny <= w && b[nx][ny] == c) r = r || dfs(nx, ny); } return r; } int main() { while (scanf("%d %d", &w, &h) && w && h) { memset(b, 0, sizeof(b)); scanf("%d%d%d%d", &ys, &xs, &yg, &xg); scanf("%d", &n); rep(i, n) { scanf("%d%d%d%d", &c, &d, &y, &x); rep(j, (d ? 4 : 2)) rep(k, (d ? 2 : 4)) b[x + j][y + k] = c; } c = b[xs][ys]; if (dfs(xs, ys)) puts("OK"); else puts("NG"); } return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <map> #include <vector> #define rep(i, j) FOR(i, 0, j) #define FOR(i, j, k) for (int i = j; i < k; ++i) using namespace std; int w, h, xs, ys, xg, yg, n, c, d, x, y, b[101][101]; int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0}; bool dfs(int x, int y) { int nx, ny; b[x][y] = 0; if (x == xg && y == yg) return true; bool r = 0; rep(i, 4) { nx = x + dx[i]; ny = y + dy[i]; if (nx > 0 && nx <= h && ny > 0 && ny <= w && b[nx][ny] == c) r = r || dfs(nx, ny); } return r; } int main() { while (scanf("%d %d", &w, &h) && w && h) { memset(b, 0, sizeof(b)); scanf("%d%d%d%d", &ys, &xs, &yg, &xg); scanf("%d", &n); rep(i, n) { scanf("%d%d%d%d", &c, &d, &y, &x); rep(j, (d ? 4 : 2)) rep(k, (d ? 2 : 4)) b[x + j][y + k] = c; } /* rep(i, h){ rep(j, w) printf("%d ", b[i][j]); puts(""); }*/ c = b[xs][ys]; // printf("%d\n", c); // fprintf(stderr, "error\n"); if (c > 0 && dfs(xs, ys)) puts("OK"); else puts("NG"); } return 0; }
[["+", 0, 57, 15, 339, 51, 16, 31, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 47], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 98]]
1
376
4
#include <stdio.h> int main() { int i, j, k; int a, b, c; int n[200] = {0}; while (1) { scanf("%d", &a); if (a == 0) { break; } b = 0; while (a) { n[b] = a % 8; a /= 8; b++; } n[b] = 0; c = 0; i = 0; while (i <= b) { if (n[i] > 5) { n[i]++; } if (n[i] > 3) { n[i]++; } i++; } for (i = b - 1; i >= 0; i--) { printf("%d", n[i]); } printf("\n"); } }
#include <stdio.h> int main() { int i, j, k; int a, b, c; int n[200] = {0}; while (1) { scanf("%d", &a); if (a == 0) { break; } b = 0; while (a) { n[b] = a % 8; a /= 8; b++; } n[b] = 0; c = 0; i = 0; while (i <= b) { if (n[i] > 3) { n[i]++; } if (n[i] > 5) { n[i]++; } i++; } for (i = b - 1; i >= 0; i--) { printf("%d", n[i]); } printf("\n"); } }
[["-", 8, 9, 0, 57, 15, 23, 0, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 23, 0, 16, 12, 13]]
0
180
4
#include <stdio.h> int main(void) { int n; char s[16]; char new_num[8] = {0, 1, 2, 3, 5, 7, 8, 9}; int i; while (1) { scanf("%d", &n); if (n == 0) { break; } sprintf(s, "%o", n); for (i = 0; s[i] != '\0'; i++) { if (s[i] >= '4') { s[i]++; } if (s[i] >= '5') { s[i]++; } } printf("%s\n", s); } return (0); }
#include <stdio.h> int main(void) { int n; char s[16]; char new_num[8] = {0, 1, 2, 3, 5, 7, 8, 9}; int i; while (1) { scanf("%d", &n); if (n == 0) { break; } sprintf(s, "%o", n); for (i = 0; s[i] != '\0'; i++) { if (s[i] >= '5') { s[i]++; } if (s[i] >= '4') { s[i]++; } } printf("%s\n", s); } return (0); }
[["-", 0, 57, 15, 23, 0, 16, 12, 103, 0, 125], ["+", 0, 57, 15, 23, 0, 16, 12, 103, 0, 125]]
0
155
4
#include <iostream> using namespace std; int table[] = {0, 1, 2, 3, 5, 7, 8, 9}; void solve(int n) { if (n >= 8) { solve(n / 8); } cout << table[n % 8]; } int main() { int n; while (cin >> n) { solve(n); cout << endl; } }
#include <iostream> using namespace std; int table[] = {0, 1, 2, 3, 5, 7, 8, 9}; void solve(int n) { if (n >= 8) { solve(n / 8); } cout << table[n % 8]; } int main() { int n; while (cin >> n, n) { solve(n); cout << endl; } }
[["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 12, 22]]
1
87
2
/*#include <iostream> unsigned long long int func(unsigned long long int a); unsigned long long answ[1000000000]; int main(){ unsigned long long int n; while(std::cin >> n, n){ unsigned long long int ans = 0; unsigned long long int n_=n; while(n_--){ ans++; unsigned long long int b = func(ans); ans+=b; n-=b; } std::cout << ans << std::endl; } return 0; } unsigned long long int func(unsigned long long int a){ int res=1; while(a > 0){ if(a % 10 == 4 || a % 10 == 6)return res; a /= 10; res*=10; } return 0; }*/ #include <iostream> #include <stack> #define _m_ 1 const int Ebit[8] = {0, 1, 2, 3, 5, 7, 8, 9}; int main() { int n; std::stack<int> st; while (_m_) { std::cin >> n; if (n == 0) break; while (_m_) { if (n < 8) { st.push(Ebit[n]); break; } int amari = n % 8; n /= 8; st.push(Ebit[amari]); } } while (st.size()) { std::cout << st.top(); st.pop(); } std::cout << std::endl; return 0; }
/*#include <iostream> unsigned long long int func(unsigned long long int a); unsigned long long answ[1000000000]; int main(){ unsigned long long int n; while(std::cin >> n, n){ unsigned long long int ans = 0; unsigned long long int n_=n; while(n_--){ ans++; unsigned long long int b = func(ans); ans+=b; n-=b; } std::cout << ans << std::endl; } return 0; } unsigned long long int func(unsigned long long int a){ int res=1; while(a > 0){ if(a % 10 == 4 || a % 10 == 6)return res; a /= 10; res*=10; } return 0; }*/ #include <iostream> #include <stack> #define _m_ 1 const int Ebit[8] = {0, 1, 2, 3, 5, 7, 8, 9}; int main() { int n; std::stack<int> st; while (_m_) { std::cin >> n; if (n == 0) break; while (_m_) { if (n < 8) { st.push(Ebit[n]); break; } int amari = n % 8; n /= 8; st.push(Ebit[amari]); } while (st.size()) { std::cout << st.top(); st.pop(); } std::cout << std::endl; } return 0; }
[["-", 0, 14, 8, 9, 0, 52, 8, 9, 0, 46], ["+", 0, 14, 8, 9, 0, 52, 8, 9, 0, 46]]
1
154
2
#include <stdio.h> int main(void) { int i; char ans[10]; while (scanf("%d", &i), i) { sprintf(ans, "%o", i); for (i = 0; i < 10; i++) { if (ans[i] == 0) break; if (ans[i] >= 4) ans[i]++; if (ans[i] >= 6) ans[i]++; } printf("%s\n", ans); } return 0; }
#include <stdio.h> int main(void) { int i; char ans[10]; while (scanf("%d", &i), i) { sprintf(ans, "%o", i); for (i = 0; i < 10; i++) { if (ans[i] == '\0') break; if (ans[i] >= '4') ans[i]++; if (ans[i] >= '6') ans[i]++; } printf("%s\n", ans); } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 0, 57, 15, 339, 51, 16, 12, 103, 0, 104], ["+", 0, 57, 15, 339, 51, 16, 12, 103, 0, 44]]
1
114
8
#include <bits/stdc++.h> #define INF (1 << 29) #define all(c) (c).begin(), (c).end() #define pb(c) push_back(c) using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; int getv(int x) { if (x = 4) return 5; return (x < 4) ? x : x + 2; } void convert(int n) { if (n = 0) return; convert(n / 8); cout << getv(n % 8); } int main() { ios::sync_with_stdio(false); int n; while (cin >> n && n) { convert(n); cout << endl; } }
#include <bits/stdc++.h> #define INF (1 << 29) #define all(c) (c).begin(), (c).end() #define pb(c) push_back(c) using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; int getv(int x) { if (x == 4) return 5; return (x < 4) ? x : x + 2; } void convert(int n) { if (n == 0) return; convert(n / 8); cout << getv(n % 8); } int main() { ios::sync_with_stdio(false); int n; while (cin >> n && n) { convert(n); cout << endl; } }
[["-", 8, 9, 0, 57, 15, 339, 51, 11, 17, 32], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60]]
1
141
4
#include <cstdio> int a[] = {0, 1, 2, 3, 5, 7, 8, 9}; int b; long long retans(int bef) { if (bef < 8) { return a[bef]; } else { long long ans = a[bef % 8]; ans += retans(bef / 8) * 10; return ans; } } int main() { for (;;) { scanf("%d", &b); if (b == 0) { break; } else { long long pri = retans(b); printf("%lld", pri); } } }
#include <cstdio> int a[] = {0, 1, 2, 3, 5, 7, 8, 9}; int b; long long retans(int bef) { if (bef < 8) { return a[bef]; } else { long long ans = a[bef % 8]; ans += retans(bef / 8) * 10; return ans; } } int main() { for (;;) { scanf("%d", &b); if (b == 0) { break; } else { long long pri = retans(b); printf("%lld\n", pri); } } }
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
1
133
1
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; typedef pair<char, int> P; char buf[11]; int main() { int n; while (scanf("%d", &n), n) { sprintf(buf, "%o", n); rep(i, strlen(buf)) { if (buf[i] >= '5') buf[i]++; if (buf[i] == '4') buf[i]++; } printf("%s\n", buf); } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; typedef pair<char, int> P; char buf[11]; int main() { int n; while (scanf("%d", &n), n) { sprintf(buf, "%o", n); rep(i, strlen(buf)) { if (buf[i] >= '5') buf[i] += 2; if (buf[i] == '4') buf[i]++; } printf("%s\n", buf); } return 0; }
[["-", 8, 9, 0, 57, 64, 1, 0, 27, 17, 29], ["+", 8, 9, 0, 57, 64, 1, 0, 11, 17, 107], ["+", 8, 9, 0, 57, 64, 1, 0, 11, 12, 13]]
1
128
3
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { unsigned n; while (cin >> n, n) { int b = 1; long long m = 0; while (n) { m += b * (n % 8); n /= 8; b *= 10; } stringstream ss; string s; ss << m; ss >> s; ss.str(""); ss.clear(stringstream::goodbit); for (int i = 0; i < s.size(); i++) { if (s[i] == '4') s[i] = '5'; else if (s[i] == '5') s[i] = '7'; else if (s[i] == '6') s[i] = '8'; else if (s[i] == '7') s[i] = '9'; } ss << s; ss >> m; cout << m << endl; } return 0; }
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { long long n; while (cin >> n, n) { int b = 1; long long m = 0; while (n) { m += b * (n % 8); n /= 8; b *= 10; } stringstream ss; string s; ss << m; ss >> s; ss.str(""); ss.clear(stringstream::goodbit); for (int i = 0; i < s.size(); i++) { if (s[i] == '4') s[i] = '5'; else if (s[i] == '5') s[i] = '7'; else if (s[i] == '6') s[i] = '8'; else if (s[i] == '7') s[i] = '9'; } ss << s; ss >> m; cout << m << endl; } return 0; }
[["-", 0, 14, 8, 9, 0, 43, 39, 86, 0, 87], ["+", 0, 14, 8, 9, 0, 43, 39, 86, 0, 96]]
1
215
3
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { vector<string> v(8); string s; int n, i; for (i = 0; i < 8; i++) v[i] = '0' + i + (i >= 4) + (i >= 6); while (cin >> n, n) { s = ""; while (n > 0) { s += v[n % 8]; n /= 8; } reverse(s.begin(), s.end()); cout << s << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { vector<string> v(8); string s; int n, i; for (i = 0; i < 8; i++) v[i] = '0' + i + (i >= 4) + (i >= 5); while (cin >> n, n) { s = ""; while (n > 0) { s += v[n % 8]; n /= 8; } reverse(s.begin(), s.end()); cout << s << endl; } return 0; }
[["-", 0, 11, 12, 16, 12, 23, 0, 16, 12, 13], ["+", 0, 11, 12, 16, 12, 23, 0, 16, 12, 13]]
1
129
2
#include <iostream> using namespace std; int main() { int a[5], cnt[3]; while (1) { cin >> a[0]; if (a[0] == 0) break; for (int i = 1; i < 5; i++) cin >> a[i]; for (int i = 0; i < 3; i++) cnt[i] = 0; for (int i = 0; i < 5; i++) cnt[a[i] - 1]++; if ((cnt[0] > 0 && cnt[1] > 0 && cnt[2] > 0) || a[0] == 5 || a[1] == 5 || a[2] == 5) { for (int i = 0; i < 5; i++) cout << "3" << endl; } else if (cnt[0] == 0) { for (int i = 0; i < 5; i++) { if (a[i] == 2) { cout << "1" << endl; } else { cout << "2" << endl; } } } else if (cnt[1] == 0) { for (int i = 0; i < 5; i++) { if (a[i] == 3) { cout << "1" << endl; } else { cout << "2" << endl; } } } else { for (int i = 0; i < 5; i++) { if (a[i] == 1) { cout << "1" << endl; } else { cout << "2" << endl; } } } } return 0; }
#include <iostream> using namespace std; int main() { int a[5], cnt[3]; while (1) { cin >> a[0]; if (a[0] == 0) break; for (int i = 1; i < 5; i++) cin >> a[i]; for (int i = 0; i < 3; i++) cnt[i] = 0; for (int i = 0; i < 5; i++) cnt[a[i] - 1]++; if ((cnt[0] > 0 && cnt[1] > 0 && cnt[2] > 0) || cnt[0] == 5 || cnt[1] == 5 || cnt[2] == 5) { for (int i = 0; i < 5; i++) cout << "3" << endl; } else if (cnt[0] == 0) { for (int i = 0; i < 5; i++) { if (a[i] == 2) { cout << "1" << endl; } else { cout << "2" << endl; } } } else if (cnt[1] == 0) { for (int i = 0; i < 5; i++) { if (a[i] == 3) { cout << "1" << endl; } else { cout << "2" << endl; } } } else { for (int i = 0; i < 5; i++) { if (a[i] == 1) { cout << "1" << endl; } else { cout << "2" << endl; } } } } return 0; }
[["-", 31, 16, 31, 16, 12, 16, 31, 69, 28, 22], ["+", 31, 16, 31, 16, 12, 16, 31, 69, 28, 22], ["-", 51, 16, 31, 16, 12, 16, 31, 69, 28, 22], ["+", 51, 16, 31, 16, 12, 16, 31, 69, 28, 22], ["-", 15, 339, 51, 16, 12, 16, 31, 69, 28, 22], ["+", 15, 339, 51, 16, 12, 16, 31, 69, 28, 22]]
1
352
6
#include <algorithm> #include <iostream> #include <map> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { int t; while (cin >> t && t) { vector<int> v; v.push_back(t); rep(i, 4) { cin >> t; v.push_back(t); } map<int, int> m; rep(i, 5) { m[v[i]]++; } if (m.size() >= 3 || m.size() == 1) { rep(i, 5) cout << 3 << endl; } else { if (m[1] > 0 && m[2] > 0) { rep(i, 5) { if (v[i] == 1) cout << 1 << endl; else cout << 2 << endl; } } else if (m[2] > 0 && m[3] > 0) { rep(i, 5) { if (v[i] == 1) cout << 1 << endl; else cout << 2 << endl; } } else if (m[1] > 0 && m[3] > 0) { rep(i, 5) { if (v[i] == 3) cout << 1 << endl; else cout << 2 << endl; } } } } return 0; }
#include <algorithm> #include <iostream> #include <map> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { int t; while (cin >> t && t) { vector<int> v; v.push_back(t); rep(i, 4) { cin >> t; v.push_back(t); } map<int, int> m; rep(i, 5) { m[v[i]]++; } if (m.size() >= 3 || m.size() == 1) { rep(i, 5) cout << 3 << endl; } else { if (m[1] > 0 && m[2] > 0) { rep(i, 5) { if (v[i] == 1) cout << 1 << endl; else cout << 2 << endl; } } else if (m[2] > 0 && m[3] > 0) { rep(i, 5) { if (v[i] == 2) cout << 1 << endl; else cout << 2 << endl; } } else if (m[1] > 0 && m[3] > 0) { rep(i, 5) { if (v[i] == 3) cout << 1 << endl; else cout << 2 << endl; } } } } return 0; }
[["-", 0, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 0, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
294
2
#include <iostream> using namespace std; int main() { while (true) { int a[5]; int f[4] = {0}; for (int i = 0; i < 5; i++) { cin >> a[i]; if (i == 0 && a[i] == 0) { return 0; } f[a[i]]++; } if (f[1] > 0 && f[2] > 0 && f[3] > 0 || f[1] > 5 || f[2] > 5 || f[3] > 5) { for (int i = 0; i < 5; i++) { cout << "3" << endl; } } else if (f[1] > 0 && f[3] > 0) { for (int i = 0; i < 5; i++) { if (a[i] == 1) { cout << "2" << endl; } else { cout << "1" << endl; } } } else if (f[2] > 0 && f[3] > 0) { for (int i = 0; i < 5; i++) { if (a[i] == 3) { cout << "2" << endl; } else { cout << "1" << endl; } } } else { for (int i = 0; i < 5; i++) { cout << a[i] << endl; } } } }
#include <iostream> using namespace std; int main() { while (true) { int a[5]; int f[4] = {0}; for (int i = 0; i < 5; i++) { cin >> a[i]; if (i == 0 && a[i] == 0) { return 0; } f[a[i]]++; } if (f[1] > 0 && f[2] > 0 && f[3] > 0 || f[1] > 4 || f[2] > 4 || f[3] > 4) { for (int i = 0; i < 5; i++) { cout << "3" << endl; } } else if (f[1] > 0 && f[3] > 0) { for (int i = 0; i < 5; i++) { if (a[i] == 1) { cout << "2" << endl; } else { cout << "1" << endl; } } } else if (f[2] > 0 && f[3] > 0) { for (int i = 0; i < 5; i++) { if (a[i] == 3) { cout << "2" << endl; } else { cout << "1" << endl; } } } else { for (int i = 0; i < 5; i++) { cout << a[i] << endl; } } } }
[["-", 51, 16, 31, 16, 31, 16, 12, 16, 12, 13], ["+", 51, 16, 31, 16, 31, 16, 12, 16, 12, 13], ["-", 15, 339, 51, 16, 31, 16, 12, 16, 12, 13], ["+", 15, 339, 51, 16, 31, 16, 12, 16, 12, 13], ["-", 0, 57, 15, 339, 51, 16, 12, 16, 12, 13], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 12, 13]]
1
312
6
#include <iostream> using namespace std; int main() { int hand[5]; bool handbe[3]; bool isdraw; while (1) { isdraw = false; handbe[0] = false; handbe[1] = false; handbe[2] = false; cin >> hand[0]; if (hand[0] == 0) break; handbe[hand[0] - 1] = true; for (int i = 1; i < 5; ++i) { cin >> hand[i]; handbe[hand[i] - 1] = true; } if (handbe[0]) { //あいこ判定 if (handbe[1] == handbe[2]) isdraw = true; } else { if (handbe[1] != handbe[2]) isdraw = true; } if (isdraw) { for (int i = 0; i < 5; ++i) { cout << "3" << endl; } continue; } for (int j = 0; j < 3; ++j) { if (handbe[j] && handbe[(j + 1) % 3]) { for (int i = 0; i < 5; ++i) { cout << (hand[i] == ((j + 1) % 3) ? 1 : 2) << endl; } } } } return 0; }
// rock// #include <iostream> using namespace std; int main() { int hand[5]; bool handbe[3]; bool isdraw; while (1) { isdraw = false; handbe[0] = false; handbe[1] = false; handbe[2] = false; cin >> hand[0]; if (hand[0] == 0) break; handbe[hand[0] - 1] = true; for (int i = 1; i < 5; ++i) { cin >> hand[i]; handbe[hand[i] - 1] = true; } if (handbe[0]) { //あいこ判定 if (handbe[1] == handbe[2]) isdraw = true; } else { if (handbe[1] != handbe[2]) isdraw = true; } if (isdraw) { for (int i = 0; i < 5; ++i) { cout << "3" << endl; } continue; } for (int j = 0; j < 3; ++j) { if (handbe[j] && handbe[(j + 1) % 3]) { for (int i = 0; i < 5; ++i) { cout << (hand[i] == j + 1 ? 1 : 2) << endl; } } } } return 0; }
[["-", 12, 23, 0, 41, 15, 16, 12, 23, 0, 24], ["-", 15, 16, 12, 23, 0, 16, 31, 23, 0, 24], ["-", 15, 16, 12, 23, 0, 16, 31, 23, 0, 25], ["-", 0, 41, 15, 16, 12, 23, 0, 16, 17, 109], ["-", 0, 41, 15, 16, 12, 23, 0, 16, 12, 13], ["-", 12, 23, 0, 41, 15, 16, 12, 23, 0, 25]]
1
280
6
#include <iostream> #include <vector> using namespace std; void solve(vector<int> &a) { int f[3] = {}; int s[] = {0, 1, 2}; int t[] = {1, 2, 1}; for (int i = 0; i < a.size(); i++) { f[a[i] - 1] = 1; } if ((f[0] + f[1] + f[2]) % 2 == 1) for (int i = 0; i < 5; i++) cout << "3\n"; else for (int j = 0; j < 3; j++) { if (f[s[j]] == 1 && f[t[j]] == 1) { for (int i = 0; i < 5; i++) { if (a[i] == s[j] + 1) cout << "1\n"; if (a[i] == t[j] + 1) cout << "2\n"; } } } } int main() { bool f = true; while (1) { vector<int> x; for (int i = 0; i < 5; i++) { int tmp; cin >> tmp; if (i == 0 && tmp == 0) { f = 0; break; } x.push_back(tmp); } if (!f) break; solve(x); } }
#include <iostream> #include <vector> using namespace std; void solve(vector<int> &a) { int f[3] = {}; int s[] = {0, 1, 2}; int t[] = {1, 2, 0}; for (int i = 0; i < a.size(); i++) { f[a[i] - 1] = 1; } if ((f[0] + f[1] + f[2]) % 2 == 1) for (int i = 0; i < 5; i++) cout << "3\n"; else for (int j = 0; j < 3; j++) { if (f[s[j]] == 1 && f[t[j]] == 1) { for (int i = 0; i < 5; i++) { if (a[i] == s[j] + 1) cout << "1\n"; if (a[i] == t[j] + 1) cout << "2\n"; } } } } int main() { bool f = true; while (1) { vector<int> x; for (int i = 0; i < 5; i++) { int tmp; cin >> tmp; if (i == 0 && tmp == 0) { f = 0; break; } x.push_back(tmp); } if (!f) break; solve(x); } }
[["-", 8, 9, 0, 43, 49, 50, 51, 83, 0, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 83, 0, 13]]
1
313
2
#include <iostream> using namespace std; int a, b, c, d, e, x[4], f; int main() { while (true) { x[0] = 0; x[1] = 0; x[2] - 0; x[3] - 0; cin >> a; if (a == 0) { break; } x[a]++; cin >> b >> c >> d >> e; x[b]++; x[c]++; x[d]++; x[e]++; if (x[1] >= 1 && x[2] >= 1 && x[3] >= 1) { cout << '3' << endl << '3' << endl << '3' << endl << '3' << endl << '3' << endl; } else if (x[1] >= 5 || x[2] >= 5 || x[3] >= 5) { cout << '3' << endl << '3' << endl << '3' << endl << '3' << endl << '3' << endl; } else { if (x[2] == 0) { if (a == 1) { a = 4; } if (b == 1) { b = 4; } if (c == 1) { c = 4; } if (d == 1) { d = 4; } if (e == 1) { e = 4; } } if (x[1] == 0) { f = 2; } if (x[2] == 0) { f = 3; } if (x[3] == 0) { f = 1; } if (a == f) { cout << '1' << endl; } else { cout << '2' << endl; } if (b == f) { cout << '1' << endl; } else { cout << '2' << endl; } if (c == f) { cout << '1' << endl; } else { cout << '2' << endl; } if (d == f) { cout << '1' << endl; } else { cout << '2' << endl; } if (e == f) { cout << '1' << endl; } else { cout << '2' << endl; } } } return 0; }
#include <iostream> using namespace std; int a, b, c, d, e, x[4], f; int main() { while (true) { x[0] = 0; x[1] = 0; x[2] = 0; x[3] = 0; cin >> a; if (a == 0) { break; } x[a]++; cin >> b >> c >> d >> e; x[b]++; x[c]++; x[d]++; x[e]++; if (x[1] >= 1 && x[2] >= 1 && x[3] >= 1) { cout << '3' << endl << '3' << endl << '3' << endl << '3' << endl << '3' << endl; } else if (x[1] >= 5 || x[2] >= 5 || x[3] >= 5) { cout << '3' << endl << '3' << endl << '3' << endl << '3' << endl << '3' << endl; } else { if (x[2] == 0) { if (a == 1) { a = 4; } if (b == 1) { b = 4; } if (c == 1) { c = 4; } if (d == 1) { d = 4; } if (e == 1) { e = 4; } } if (x[1] == 0) { f = 2; } if (x[2] == 0) { f = 3; } if (x[3] == 0) { f = 1; } if (a == f) { cout << '1' << endl; } else { cout << '2' << endl; } if (b == f) { cout << '1' << endl; } else { cout << '2' << endl; } if (c == f) { cout << '1' << endl; } else { cout << '2' << endl; } if (d == f) { cout << '1' << endl; } else { cout << '2' << endl; } if (e == f) { cout << '1' << endl; } else { cout << '2' << endl; } } } return 0; }
[["-", 0, 52, 8, 9, 0, 1, 0, 16, 17, 33], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32]]
1
490
4
#include <bits/stdc++.h> using namespace std; #define pb(n) push_back(n) #define fi first #define se second #define X real() #define Y imag() #define all(r) (r).begin(), (r).end() #define gsort(st, en) sort((st), (en), greater<int>()) #define vmax(ary) *max_element(all(ary)) #define vmin(ary) *min_element(all(ary)) #define debug(x) cout << #x << ": " << x << endl #define fcout(n) cout << fixed << setprecision((n)) #define scout(n) cout << setw(n) #define vary(type, name, size, init) vector<type> name(size, init) #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define REP(i, a, b) for (int i = (a); i < (int)(b); ++i) #define repi(it, array) \ for (auto it = array.begin(), end = array.end(); it != end; ++it) #define repa(n, array) for (auto &n : (array)) using ll = long long; using vi = vector<int>; using vl = vector<ll>; using dict = map<string, int>; using comd = complex<double>; using pii = pair<int, int>; constexpr int imax = ((1 << 30) - 1) * 2 + 1; constexpr int inf = 100000000; constexpr double PI = acos(-1.0); double eps = 1e-10; const int dy[] = {-1, 0, 1, 0}; const int dx[] = {0, -1, 0, 1}; inline bool value(int x, int y, int w, int h) { return (x >= 0 && x < w && y >= 0 && y < h); } double CalcDist(comd p1, comd p2) { return sqrt(pow(p1.X - p2.X, 2.0) + pow(p1.Y - p2.Y, 2.0)); } template <typename T> void out(deque<T> d) { for (size_t i = 0; i < d.size(); i++) { debug(d[i]); } } template <typename T> T ston(string &str, T n) { istringstream sin(str); T num; sin >> num; return num; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; while (cin >> n) { if (n == 0) break; set<int> s; vary(int, v, 5, 0); v[0] = n; s.insert(n); REP(i, 1, 5) { cin >> v[i]; s.insert(v[i]); } rep(i, 5) { if (s.size() != 2) { cout << 3 << endl; } else { if ((v[i] == 2 && *s.begin() == 1) || (v[i] == 3 && *s.begin() == 2) || (v[i] == 1 && (*(s.begin()) + 1) == 3)) { cout << 2 << endl; } else cout << 1 << endl; } } } return 0; }
#include <bits/stdc++.h> using namespace std; #define pb(n) push_back(n) #define fi first #define se second #define X real() #define Y imag() #define all(r) (r).begin(), (r).end() #define gsort(st, en) sort((st), (en), greater<int>()) #define vmax(ary) *max_element(all(ary)) #define vmin(ary) *min_element(all(ary)) #define debug(x) cout << #x << ": " << x << endl #define fcout(n) cout << fixed << setprecision((n)) #define scout(n) cout << setw(n) #define vary(type, name, size, init) vector<type> name(size, init) #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define REP(i, a, b) for (int i = (a); i < (int)(b); ++i) #define repi(it, array) \ for (auto it = array.begin(), end = array.end(); it != end; ++it) #define repa(n, array) for (auto &n : (array)) using ll = long long; using vi = vector<int>; using vl = vector<ll>; using dict = map<string, int>; using comd = complex<double>; using pii = pair<int, int>; constexpr int imax = ((1 << 30) - 1) * 2 + 1; constexpr int inf = 100000000; constexpr double PI = acos(-1.0); double eps = 1e-10; const int dy[] = {-1, 0, 1, 0}; const int dx[] = {0, -1, 0, 1}; inline bool value(int x, int y, int w, int h) { return (x >= 0 && x < w && y >= 0 && y < h); } double CalcDist(comd p1, comd p2) { return sqrt(pow(p1.X - p2.X, 2.0) + pow(p1.Y - p2.Y, 2.0)); } template <typename T> void out(deque<T> d) { for (size_t i = 0; i < d.size(); i++) { debug(d[i]); } } template <typename T> T ston(string &str, T n) { istringstream sin(str); T num; sin >> num; return num; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; while (cin >> n) { if (n == 0) break; set<int> s; vary(int, v, 5, 0); v[0] = n; s.insert(n); REP(i, 1, 5) { cin >> v[i]; s.insert(v[i]); } rep(i, 5) { if (s.size() != 2) { cout << 3 << endl; } else { if ((v[i] == 2 && *s.begin() == 1) || (v[i] == 3 && *s.begin() == 2) || (v[i] == 1 && (*(++s.begin()) == 3))) { cout << 2 << endl; } else cout << 1 << endl; } } } return 0; }
[["+", 0, 16, 31, 66, 28, 23, 0, 27, 17, 29], ["-", 0, 16, 12, 16, 31, 23, 0, 16, 17, 72], ["-", 0, 16, 12, 16, 31, 23, 0, 16, 12, 13], ["-", 12, 23, 0, 16, 12, 16, 31, 23, 0, 25], ["+", 51, 16, 12, 23, 0, 16, 12, 23, 0, 25]]
1
597
5