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
// 35 #include <algorithm> #include <iostream> #include <string> #include <utility> using namespace std; int main() { for (int n; cin >> n, n;) { pair<string, int> t[20000]; for (int i = 0; i < n; i++) { string s, g; cin >> s >> g; t[2 * i] = make_pair(s, 1); t[2 * i + 1] = make_pair(g, -1); } sort(t, t + 2 * n); int m = 0, c = 0; for (int i = 0; i < n; i++) { c += t[i].second; m = max(m, c); } cout << m << endl; } return 0; }
// 35 #include <algorithm> #include <iostream> #include <string> #include <utility> using namespace std; int main() { for (int n; cin >> n, n;) { pair<string, int> t[20000]; for (int i = 0; i < n; i++) { string s, g; cin >> s >> g; t[2 * i] = make_pair(s, 1); t[2 * i + 1] = make_pair(g, -1); } sort(t, t + 2 * n); int m = 0, c = 0; for (int i = 0; i < n * 2; i++) { c += t[i].second; m = max(m, c); } cout << m << endl; } return 0; }
[["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 48], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
164
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; const int MAX = 100000; typedef pair<int, int> P; int ans[MAX]; int ToS(int h, int m, int s) { return h * 60 * 60 + m * 60 + s; } void init() { fill(ans, ans + MAX, 0); } int main() { int n; while (cin >> n && n) { init(); for (int i = 0; i < n; i++) { int hh, ss, mm; int hh2, ss2, mm2; scanf("%d:%d:%d", &hh, &ss, &mm); scanf("%d:%d:%d", &hh2, &ss2, &mm2); int from = ToS(hh, mm, ss); int to = ToS(hh2, mm2, ss2); for (int i = from; i < to; i++) ans[i]++; } int out = 0; for (int i = 0; i < MAX; i++) out = max(out, ans[i]); cout << out << endl; } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; const int MAX = 100000; typedef pair<int, int> P; int ans[MAX]; int ToS(int h, int m, int s) { return h * 60 * 60 + m * 60 + s; } void init() { fill(ans, ans + MAX, 0); } int main() { int n; while (cin >> n && n) { init(); for (int i = 0; i < n; i++) { int hh, ss, mm; int hh2, ss2, mm2; scanf("%d:%d:%d", &hh, &mm, &ss); scanf("%d:%d:%d", &hh2, &mm2, &ss2); int from = ToS(hh, mm, ss); int to = ToS(hh2, mm2, ss2); for (int i = from; i < to; i++) ans[i]++; } int out = 0; for (int i = 0; i < MAX; i++) out = max(out, ans[i]); cout << out << endl; } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 66, 28, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 66, 28, 22]]
1
244
loop { n, m = gets.split.map &:to_i break if n == 0 road = (1..n).map{ gets.split.map &:to_i } road.sort_by!(&:last).reverse! while !road.empty? if road[0][0] > m road[0][0] -= m break else m -= road[0][0] road.shift end end p road.map{|d, q| d * q}.inject &:+ }
loop { n, m = gets.split.map &:to_i break if n == 0 road = (1..n).map{ gets.split.map &:to_i } road.sort_by!(&:last).reverse! while !road.empty? if road[0][0] > m road[0][0] -= m break else m -= road[0][0] road.shift end end p road.map{|d, q| d * q}.inject(0, &:+) }
[["+", 0, 652, 3, 4, 0, 652, 3, 4, 0, 24], ["+", 0, 652, 3, 4, 0, 652, 3, 4, 0, 612], ["+", 0, 652, 3, 4, 0, 652, 3, 4, 0, 21], ["+", 0, 652, 3, 4, 0, 652, 3, 4, 0, 25]]
4
104
# AOJ2019 shukuba = [] n = 0 m = 0 def solve(): shukuba.sort(key = lambda x: x[0]) s = sum(map(lambda x: x[0], shukuba)) - m if s <= 0: return 0 e = 0 i = 0 while s > 0: d = shukuba[i][0] if s >= shukuba[i][0] else s e += shukuba[i][1] * d s -= d i += 1 return e while True: shukuba = [] line = input() n, m = map(int, line.split()) if n == 0 and m == 0: break for _ in range(0, n): c = list(map(int, list(input().split()))) shukuba.append(c) print(solve())
# AOJ2019 shukuba = [] n = 0 m = 0 def solve(): shukuba.sort(key = lambda x: x[1]) s = sum(map(lambda x: x[0], shukuba)) - m if s <= 0: return 0 e = 0 i = 0 while s > 0: d = shukuba[i][0] if s >= shukuba[i][0] else s e += shukuba[i][1] * d s -= d i += 1 return e while True: shukuba = [] line = input() n, m = map(int, line.split()) if n == 0 and m == 0: break for _ in range(0, n): c = list(map(int, list(input().split()))) shukuba.append(c) print(solve())
[["-", 3, 4, 0, 653, 51, 670, 8, 206, 206, 612], ["+", 3, 4, 0, 653, 51, 670, 8, 206, 206, 612]]
5
185
while True: N,M = map(int,input().split()) if N == 0: break expect = 0 a = [] exsum = 0 for i in range(N): #二次元配列を作る a.append(list(map(int,input().split()))) exsum += a[i][0]*a[i][1] #誰も雇わなかった場合の期待値 b = sorted(a, key=lambda x: x[1]) #危険度の高い区間ほど後ろにくるように並べる i = N-1 while M > 0: #危険度が高い区間(配列のうしろ)から順に人を雇っていく if b[i][0] <= M: exsum -= b[i][0]*b[i][1] M -= b[i][0] i -= 1 else: exsum -= b[i][1]*M M = 0 i -= 1 print(exsum)
while True: N,M = map(int,input().split()) if N == 0: break expect = 0 a = [] exsum = 0 for i in range(N): #二次元配列を作る a.append(list(map(int,input().split()))) exsum += a[i][0]*a[i][1] #誰も雇わなかった場合の期待値 b = sorted(a, key=lambda x: x[1]) #危険度の高い区間ほど後ろにくるように並べる i = N-1 while M > 0 and i >= 0: #危険度が高い区間(配列のうしろ)から順に人を雇っていく if b[i][0] <= M: exsum -= b[i][0]*b[i][1] M -= b[i][0] i -= 1 else: exsum -= b[i][1]*M M = 0 i -= 1 print(exsum)
[["+", 0, 52, 8, 196, 0, 52, 15, 679, 17, 355], ["+", 8, 196, 0, 52, 15, 679, 12, 666, 0, 22], ["+", 8, 196, 0, 52, 15, 679, 12, 666, 667, 20], ["+", 8, 196, 0, 52, 15, 679, 12, 666, 0, 612]]
5
173
while True: N,M = list(map(int,input().split())) if N == M == 0: break DP = [list(map(int,input().split())) for _ in range(N)] DP = sorted(DP, key = lambda x:x[1], reverse = True) ans = 0 for i in range(N): if M-DP[i][0] >= 0: M -= DP[i][0] else: ans += DP[i][1]*(DP[i][0]-M) ans += sum(DP[j][1]*DP[j][0] for j in range(N-i-1)) break print(ans)
while True: N,M = list(map(int,input().split())) if N == M == 0: break DP = [list(map(int,input().split())) for _ in range(N)] DP = sorted(DP, key = lambda x:x[1], reverse = True) ans = 0 for i in range(N): if M-DP[i][0] >= 0: M -= DP[i][0] else: ans += DP[i][1]*(DP[i][0]-M) ans += sum(DP[j][1]*DP[j][0] for j in range(i+1,N)) break print(ans)
[["-", 12, 652, 3, 4, 0, 657, 31, 657, 31, 22], ["-", 12, 652, 3, 4, 0, 657, 31, 657, 17, 33], ["-", 0, 659, 12, 652, 3, 4, 0, 657, 17, 33], ["+", 0, 659, 12, 652, 3, 4, 0, 657, 17, 72], ["+", 3, 668, 0, 659, 12, 652, 3, 4, 0, 21], ["+", 3, 668, 0, 659, 12, 652, 3, 4, 0, 22]]
5
168
while True: n, m = map(int, input().split()) if n == 0 and m == 0: break sections = [] for i in range(n): d, p = map(int, input().split()) sections.append([p, d]) sections.sort() sections.reverse() e = 0 for p, d in sections: if d < m: m -= d d = 0 elif m < d: d -= m m = 0 e += p * d print(e)
while True: n, m = map(int, input().split()) if n == 0 and m == 0: break sections = [] for i in range(n): d, p = map(int, input().split()) sections.append([p, d]) sections.sort() sections.reverse() e = 0 for p, d in sections: if d <= m: m -= d d = 0 elif m > 0: d -= m m = 0 e += p * d print(e)
[["-", 0, 7, 8, 196, 0, 57, 15, 666, 667, 18], ["+", 0, 7, 8, 196, 0, 57, 15, 666, 667, 19], ["-", 8, 196, 0, 57, 75, 665, 15, 666, 667, 18], ["-", 8, 196, 0, 57, 75, 665, 15, 666, 0, 22], ["+", 8, 196, 0, 57, 75, 665, 15, 666, 667, 47], ["+", 8, 196, 0, 57, 75, 665, 15, 666, 0, 612]]
5
118
while True: n, m = map(int, input().split()) dpList = [] if (n,m) == (0,0): break for i in range(n): row = list(map(int,input().split())) dpList.append(row) dpList = sorted(dpList, key = lambda x:x[1], reverse = True) num = 0 for i in range(n): if m >= dpList[i][0]: m -= dpList[i][0] else: num += (dpList[i][0] - m) * dpList[i][1] print(num)
while True: n, m = map(int, input().split()) dpList = [] if (n,m) == (0,0): break for i in range(n): row = list(map(int,input().split())) dpList.append(row) dpList = sorted(dpList, key = lambda x:x[1], reverse = True) num = 0 for i in range(n): if m >= dpList[i][0]: m -= dpList[i][0] else: num += (dpList[i][0] - m) * dpList[i][1] m = 0 print(num)
[["+", 75, 76, 8, 196, 0, 1, 0, 662, 31, 22], ["+", 75, 76, 8, 196, 0, 1, 0, 662, 0, 32], ["+", 75, 76, 8, 196, 0, 1, 0, 662, 12, 612]]
5
146
while True: n, m = map(int, input().split()) if n == 0: break risks = [(p, d) for d, p in (map(int, input().split()) for _ in range(n))] risks.sort() while m: p, d = risks.pop() if d >= m: remain = p * (d - m) break m -= d print(remain + sum(p * d for p, d in risks))
while True: n, m = map(int, input().split()) if n == 0: break risks = [(p, d) for d, p in (map(int, input().split()) for _ in range(n))] risks.sort() remain = 0 while risks: p, d = risks.pop() if d >= m: remain = p * (d - m) break m -= d print(remain + sum(p * d for p, d in risks))
[["+", 0, 52, 8, 196, 0, 1, 0, 662, 31, 22], ["+", 0, 52, 8, 196, 0, 1, 0, 662, 0, 32], ["+", 0, 52, 8, 196, 0, 1, 0, 662, 12, 612], ["-", 0, 656, 0, 52, 8, 196, 0, 52, 15, 22], ["+", 0, 656, 0, 52, 8, 196, 0, 52, 15, 22]]
5
112
while True: n, m = map(int, input().split()) if n == 0: break else: s = 0 l = [] for _ in range(n): d, p = map(int, input().split()) s += d * p l.append((p,d)) l.sort() while m > 0: a, b = l.pop() if m >= b: s -= a * b m -= b else: s -= a * m m = 0 print(s)
while True: n, m = map(int, input().split()) if n == 0: break else: s = 0 l = [] for _ in range(n): d, p = map(int, input().split()) s += d * p l.append((p,d)) l.sort() while m > 0 and l: a, b = l.pop() if m >= b: s -= a * b m -= b else: s -= a * m m = 0 print(s)
[["+", 75, 76, 8, 196, 0, 52, 15, 679, 17, 355], ["+", 75, 76, 8, 196, 0, 52, 15, 679, 12, 22]]
5
119
da = list(map(int,input().split())) k = [[0 for i in range(2)]for j in range(10001)] i = 0 su = 0 j = 0 while da[0] + da[1] != 0: i = 0 su = 0 j = 0 for i in range(da[0]): k[i][0],k[i][1] = map(int,input().split()) sorted(k,key = lambda x: x[1],reverse = True) while j != i + 1: if da[1] > k[j][0]: da[1] -= k[j][0] j += 1 elif da[1] <= k[j][0] and da[1] > 0: su += (k[j][0] - da[1]) * k[j][1] da[1] = 0 j += 1 else: su += k[j][0] * k[j][1] j += 1 print(su) da = list(map(int,input().split())) k = [[0 for i in range(2)]for j in range(10001)]
da = list(map(int,input().split())) k = [[0 for i in range(2)]for j in range(10001)] i = 0 su = 0 j = 0 while da[0] + da[1] != 0: i = 0 su = 0 j = 0 for i in range(da[0]): k[i][0],k[i][1] = map(int,input().split()) k = sorted(k,key = lambda x: x[1],reverse = True) while j != i + 1: if da[1] > k[j][0]: da[1] -= k[j][0] j += 1 elif da[1] <= k[j][0] and da[1] > 0: su += (k[j][0] - da[1]) * k[j][1] da[1] = 0 j += 1 else: su += k[j][0] * k[j][1] j += 1 print(su) da = list(map(int,input().split())) k = [[0 for i in range(2)]for j in range(10001)]
[["+", 0, 52, 8, 196, 0, 1, 0, 662, 31, 22], ["+", 0, 52, 8, 196, 0, 1, 0, 662, 0, 32]]
5
280
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<pint> vpint; #define rep(i, n) for (int i = 0; i < (n); i++) #define REP(i, n) for (int i = n - 1; i >= (0); i--) #define reps(i, f, n) for (int i = (f); i < (n); i++) #define each(it, v) \ for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) #define all(v) (v).begin(), (v).end() #define eall(v) unique(all(v), v.end()) #define pb push_back #define mp make_pair #define fi first #define se second #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) const int MOD = 1e9 + 7; const int INF = 1e9; const ll INFF = 1e18; set<int> se; using tup = tuple<int, int, int>; vector<pair<int, int>> G[110]; // G[i] := i??????G[i].fi?????§????????¢G[i].se ll dist[110][110]; // dist[i][j] := // ??????i?????§???????????????j??§???????????????????????? void dijkstra(int start, int M) { rep(i, 110) rep(j, 110) dist[i][j] = INFF; dist[start][M] = 0; priority_queue<tup, vector<tup>, greater<tup>> que; // ?????¢ ?????? ???????????? que.push(make_tuple(0, start, M)); while (!que.empty()) { int cost, u, nokori; tie(cost, u, nokori) = que.top(); que.pop(); if (dist[u][nokori] < cost) continue; for (auto tmp : G[u]) { // u -> v int v = tmp.first, time = tmp.second; //??£??\???????????? ?????¢ int nnokori = nokori - time; if (nnokori < 0) continue; if (se.count(v)) { reps(i, nnokori, M + 1) { // ???????????????i????????? int wait = i - nnokori; if (dist[v][i] <= dist[u][nokori] + time + wait) continue; dist[v][i] = dist[u][nokori] + time + wait; que.push(make_tuple(dist[v][i], v, i)); } } else { // ??????????????? if (dist[v][nnokori] <= dist[u][nokori] + time) continue; dist[v][nnokori] = dist[u][nokori] + time; que.push(make_tuple(dist[v][nnokori], v, nnokori)); } } } } int main(void) { while (1) { rep(i, 110) G[i].clear(); int N, M, L, K, A, H; scanf("%d %d %d %d %d %d", &N, &M, &L, &K, &A, &H); if (N == 0 && M == 0 & L == 0 && K == 0 && A == 0 && H == 0) break; rep(i, L) { int l; scanf("%d", &l); se.insert(l); } rep(i, K) { int X, Y, T; scanf("%d %d %d", &X, &Y, &T); G[X].pb(mp(Y, T)), G[Y].pb(mp(X, T)); } dijkstra(A, M); ll ans = INFF; rep(i, M + 1) { chmin(ans, dist[H][i]); } if (ans == INFF) printf("Help!\n"); else printf("%lld\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<pint> vpint; #define rep(i, n) for (int i = 0; i < (n); i++) #define REP(i, n) for (int i = n - 1; i >= (0); i--) #define reps(i, f, n) for (int i = (f); i < (n); i++) #define each(it, v) \ for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) #define all(v) (v).begin(), (v).end() #define eall(v) unique(all(v), v.end()) #define pb push_back #define mp make_pair #define fi first #define se second #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) const int MOD = 1e9 + 7; const int INF = 1e9; const ll INFF = 1e18; set<int> se; using tup = tuple<int, int, int>; vector<pair<int, int>> G[110]; // G[i] := i??????G[i].fi?????§????????¢G[i].se ll dist[110][110]; // dist[i][j] := // ??????i?????§???????????????j??§???????????????????????? void dijkstra(int start, int M) { rep(i, 110) rep(j, 110) dist[i][j] = INFF; dist[start][M] = 0; priority_queue<tup, vector<tup>, greater<tup>> que; // ?????¢ ?????? ???????????? que.push(make_tuple(0, start, M)); while (!que.empty()) { int cost, u, nokori; tie(cost, u, nokori) = que.top(); que.pop(); if (dist[u][nokori] < cost) continue; for (auto tmp : G[u]) { // u -> v int v = tmp.first, time = tmp.second; //??£??\???????????? ?????¢ int nnokori = nokori - time; if (nnokori < 0) continue; if (se.count(v)) { reps(i, nnokori, M + 1) { // ???????????????i????????? int wait = i - nnokori; if (dist[v][i] <= dist[u][nokori] + time + wait) continue; dist[v][i] = dist[u][nokori] + time + wait; que.push(make_tuple(dist[v][i], v, i)); } } else { // ??????????????? if (dist[v][nnokori] <= dist[u][nokori] + time) continue; dist[v][nnokori] = dist[u][nokori] + time; que.push(make_tuple(dist[v][nnokori], v, nnokori)); } } } } int main(void) { while (1) { rep(i, 110) G[i].clear(); se.clear(); int N, M, L, K, A, H; scanf("%d %d %d %d %d %d", &N, &M, &L, &K, &A, &H); if (N == 0 && M == 0 & L == 0 && K == 0 && A == 0 && H == 0) break; rep(i, L) { int l; scanf("%d", &l); se.insert(l); } rep(i, K) { int X, Y, T; scanf("%d %d %d", &X, &Y, &T); G[X].pb(mp(Y, T)), G[Y].pb(mp(X, T)); } dijkstra(A, M); ll ans = INFF; rep(i, M + 1) { chmin(ans, dist[H][i]); } if (ans == INFF) printf("Help!\n"); else printf("%lld\n", ans); } return 0; }
[["+", 8, 9, 0, 1, 0, 2, 63, 118, 28, 22], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 17, 131], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 119, 120], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
751
#include <bits/stdc++.h> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) for (int i = 0; i < (b); i++) #define all(a) (a).begin(), (a).end() #define debug(x) cout << "debug " << x << endl; const int INF = 100000000; using namespace std; const int N = 100; void warshallFloyd(int m[N][N], int n) { rep(k, n) { rep(i, n) { rep(j, n) { assert(m[i][k] + m[k][j] <= INF * 2); m[i][j] = min(m[i][j], m[i][k] + m[k][j]); } } } } int main() { int n, mm, l, k, a, h; while (cin >> n >> mm >> l >> k >> a >> h, n || mm || l || k || a || h) { int m[N][N], M[N][N]; bool L[N] = {0}; rep(i, N) rep(j, N) m[i][j] = INF; rep(i, n) m[n][n] = 0; memcpy(M, m, sizeof(m)); L[a] = 1; L[h] = 1; rep(i, l) { int inp; cin >> inp; L[inp] = 1; } rep(i, k) { int a, b, t; cin >> a >> b >> t; assert(t < INF); m[a][b] = m[b][a] = t; } warshallFloyd(m, n); rep(i, n) { rep(j, n) { if (m[i][j] > mm) continue; if (L[i] && L[j]) { M[i][j] = m[i][j]; } } } warshallFloyd(M, n); if (M[a][h] == INF) cout << "help!" << endl; else cout << M[a][h] + max(M[a][h] - mm, 0) << endl; } }
#include <bits/stdc++.h> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) for (int i = 0; i < (b); i++) #define all(a) (a).begin(), (a).end() #define debug(x) cout << "debug " << x << endl; const int INF = 100000000; using namespace std; const int N = 100; void warshallFloyd(int m[N][N], int n) { rep(k, n) { rep(i, n) { rep(j, n) { assert(m[i][k] + m[k][j] <= INF * 2); m[i][j] = min(m[i][j], m[i][k] + m[k][j]); } } } } int main() { int n, mm, l, k, a, h; while (cin >> n >> mm >> l >> k >> a >> h, n || mm || l || k || a || h) { int m[N][N], M[N][N]; bool L[N] = {0}; rep(i, N) rep(j, N) m[i][j] = INF; rep(i, n) m[n][n] = 0; memcpy(M, m, sizeof(m)); L[a] = 1; L[h] = 1; rep(i, l) { int inp; cin >> inp; L[inp] = 1; } rep(i, k) { int a, b, t; cin >> a >> b >> t; assert(t < INF); m[a][b] = m[b][a] = t; } warshallFloyd(m, n); rep(i, n) { rep(j, n) { if (m[i][j] > mm) continue; if (L[i] && L[j]) { M[i][j] = m[i][j]; } } } warshallFloyd(M, n); if (M[a][h] == INF) cout << "Help!" << endl; else cout << M[a][h] + max(M[a][h] - mm, 0) << endl; } }
[["-", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
480
#include <algorithm> #include <iostream> #include <queue> using namespace std; constexpr int INF = 1 << 29; struct state { int pos, life, cost; state() {} state(int a, int b, int c) : pos(a), life(b), cost(c) {} }; int N, M, L, K, A, H; int fridge[111]; int graph[111][111]; bool solve() { cin >> N >> M >> L >> K >> A >> H; if (N + M + L + K + A + H == 0) { return false; } fill(fridge, fridge + L, 0); for (int j = 0; j < L; ++j) { int l; cin >> l; fridge[l] = 1; } fridge[A] = fridge[H] = 1; for (int j = 0; j < N; ++j) { fill(graph[j], graph[j] + N, INF); graph[j][j] = 0; } for (int j = 0; j < K; ++j) { int a, b, c; cin >> a >> b >> c; graph[a][b] = min(graph[a][b], c); graph[b][a] = min(graph[b][a], c); } int min_cost[111][111]; for (int j = 0; j < N; ++j) { fill(min_cost[j], min_cost[j] + M + 1, INF); } min_cost[A][M] = 0; queue<state> que; que.emplace(A, M, 0); while (!que.empty()) { state st = que.front(); que.pop(); int pos = st.pos, life = st.life, cost = st.cost; if (cost > min_cost[pos][life]) { continue; } if (fridge[pos]) { for (int j = 1; life + j <= M; ++j) { if (cost + j < min_cost[pos][life + j]) { min_cost[pos][life + j] = cost + j; que.emplace(pos, life + j, cost + j); } } } for (int j = 0; j < N; ++j) { if (j == pos || life < graph[pos][j]) { continue; } int next_cost = cost + graph[pos][j]; if (next_cost < min_cost[j][life - graph[pos][j]]) { min_cost[j][life - graph[pos][j]] = next_cost; que.emplace(j, life - graph[pos][j], next_cost); } } } int ret = INF; for (int j = 0; j <= M; ++j) { ret = min(ret, min_cost[H][j]); } if (ret == INF) { cout << "Help!" << endl; } else { cout << ret << endl; } return true; } int main() { while (solve()) ; return 0; }
#include <algorithm> #include <iostream> #include <queue> using namespace std; constexpr int INF = 1 << 29; struct state { int pos, life, cost; state() {} state(int a, int b, int c) : pos(a), life(b), cost(c) {} }; int N, M, L, K, A, H; int fridge[111]; int graph[111][111]; bool solve() { cin >> N >> M >> L >> K >> A >> H; if (N + M + L + K + A + H == 0) { return false; } fill(fridge, fridge + N, 0); for (int j = 0; j < L; ++j) { int l; cin >> l; fridge[l] = 1; } fridge[A] = fridge[H] = 1; for (int j = 0; j < N; ++j) { fill(graph[j], graph[j] + N, INF); graph[j][j] = 0; } for (int j = 0; j < K; ++j) { int a, b, c; cin >> a >> b >> c; graph[a][b] = min(graph[a][b], c); graph[b][a] = min(graph[b][a], c); } int min_cost[111][111]; for (int j = 0; j < N; ++j) { fill(min_cost[j], min_cost[j] + M + 1, INF); } min_cost[A][M] = 0; queue<state> que; que.emplace(A, M, 0); while (!que.empty()) { state st = que.front(); que.pop(); int pos = st.pos, life = st.life, cost = st.cost; if (cost > min_cost[pos][life]) { continue; } if (fridge[pos]) { for (int j = 1; life + j <= M; ++j) { if (cost + j < min_cost[pos][life + j]) { min_cost[pos][life + j] = cost + j; que.emplace(pos, life + j, cost + j); } } } for (int j = 0; j < N; ++j) { if (j == pos || life < graph[pos][j]) { continue; } int next_cost = cost + graph[pos][j]; if (next_cost < min_cost[j][life - graph[pos][j]]) { min_cost[j][life - graph[pos][j]] = next_cost; que.emplace(j, life - graph[pos][j], next_cost); } } } int ret = INF; for (int j = 0; j <= M; ++j) { ret = min(ret, min_cost[H][j]); } if (ret == INF) { cout << "Help!" << endl; } else { cout << ret << endl; } return true; } int main() { while (solve()) ; return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 12, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 22]]
1
695
#include <iostream> #include <queue> #include <vector> #define N_NODE 100 #define INF (1 << 29) using namespace std; struct Edge { int to, cost; }; class Path { public: Path(int _num, int _cost, int _M) { num = _num; cost = _cost; limit_time = _M; } int num; int cost; int limit_time; bool operator<(const Path &other) const //??????const???????????¨priority_queue??§??¨??????????????? { return this->cost < other.cost; } bool operator>(const Path &other) const //??????const???????????¨priority_queue??§??¨??????????????? { return !(*this < other); } }; //?????????????????????????¨?????????????????????? bool find_freeze(vector<int> &vec, int &number) { bool ret = false; for (auto &fc : vec) ret |= (fc == number); return ret; } int main() { int N, M, L, K, A, H; while (cin >> N >> M >> L >> K >> A >> H, N) { int start = A; //??????????????????????°?????????? int min_cost[N_NODE] [N_NODE]; // min_cost[????????°][????????????????????????] for (int i = 0; i < N_NODE; ++i) { for (int j = 0; j < N_NODE; ++j) { min_cost[i][j] = INF; } } min_cost[start][M] = 0; priority_queue<Path, vector<Path>, greater<Path>> que; que.push(Path(start, 0, M)); // bin sort // bin[from] = { Edge{to, cost}, Edge{to, cost}, ... } /* vector<Edge> bin[N_NODE]; bin[0].push_back( Edge{ 1, 3 } ); bin[1].push_back( Edge{ 0, 3 } ); bin[0].push_back( Edge{ 2, 1 } ); bin[2].push_back( Edge{ 0, 1 } ); bin[1].push_back( Edge{ 2, 1 } ); bin[2].push_back( Edge{ 1, 1 } ); bin[1].push_back( Edge{ 3, 3 } ); bin[3].push_back( Edge{ 1, 3 } ); bin[2].push_back( Edge{ 3, 2 } ); bin[3].push_back( Edge{ 2, 2 } ); bin[2].push_back( Edge{ 4, 5 } ); bin[4].push_back( Edge{ 2, 5 } ); bin[3].push_back( Edge{ 4, 1 } ); bin[4].push_back( Edge{ 3, 1 } ); */ /* ????????? bin ?????°?????? { 0, 3, 1, INF, INF }, { 3, 0, 1, 3, INF }, { 1, 1, 0, 2, 5 }, {INF, 3, 2, 0, 1 }, {INF, INF, 5, 1, 0 }, */ vector<int> freezable_city; freezable_city.reserve(L); //??????????¨????????????????vector????????? for (int i = 0; i < L; ++i) { int tmp = 0; cin >> tmp; freezable_city.push_back(tmp); } // bin??????????????°?????? vector<Edge> bin[N_NODE]; for (int i = 0; i < K; ++i) { int from, to, cost; cin >> from >> to >> cost; bin[from].push_back(Edge{to, cost}); bin[to].push_back(Edge{from, cost}); } bool needHelp = true; while (!que.empty()) { Path now = que.top(); que.pop(); //???????????????????°????????????????????????°????????? if (min_cost[now.num][now.limit_time] < now.cost) continue; for (int i = 0; i < bin[now.num].size(); i++) { //?????????????????????????¨???????????????? if (find_freeze(freezable_city, now.num) && now.limit_time < M) { for (int recovery_amount = 1; recovery_amount < M - now.limit_time; ++recovery_amount) { if (min_cost[now.num][now.limit_time + recovery_amount] > now.cost + recovery_amount) { que.push(Path(now.num, now.cost + recovery_amount, now.limit_time + recovery_amount)); min_cost[now.num][now.limit_time + recovery_amount] = now.cost + recovery_amount; } } } //??????????????????????????????i????????????????????????????????????min_cost??????????°????????????°??´??° int next_cost = now.cost + bin[now.num][i].cost; int next_minute = now.limit_time - bin[now.num][i].cost; //?§??????????????????????????????£???????????????????????????????°??????????????????? if (next_minute >= 0 && (min_cost[bin[now.num][i].to][next_minute] > next_cost)) { needHelp = false; min_cost[bin[now.num][i].to][next_minute] = next_cost; que.push(Path(bin[now.num][i].to, next_cost, next_minute)); } } } // cout << min_cost[H][0] << endl; // cout << min_cost[H][1] << endl; // cout << min_cost[H][2] << endl; int ans = INF; for (int i = 0; i < N_NODE; ++i) { ans = min(ans, min_cost[H][i]); } if (needHelp || ans == INF) cout << "Help!" << endl; else { // cout << "ans: " << min_cost[H][0] << endl; cout << ans << endl; // cout << min_cost[H][1] << endl; // cout << min_cost[H][2] << endl; } } return 0; }
#include <iostream> #include <queue> #include <vector> #define N_NODE 101 #define INF (1 << 29) using namespace std; struct Edge { int to, cost; }; class Path { public: Path(int _num, int _cost, int _M) { num = _num; cost = _cost; limit_time = _M; } int num; int cost; int limit_time; bool operator<(const Path &other) const //??????const???????????¨priority_queue??§??¨??????????????? { return this->cost < other.cost; } bool operator>(const Path &other) const //??????const???????????¨priority_queue??§??¨??????????????? { return !(*this < other); } }; //?????????????????????????¨?????????????????????? bool find_freeze(vector<int> &vec, int &number) { bool ret = false; for (auto &fc : vec) ret |= (fc == number); return ret; } int main() { int N, M, L, K, A, H; while (cin >> N >> M >> L >> K >> A >> H, N) { int start = A; //??????????????????????°?????????? int min_cost[N_NODE] [N_NODE]; // min_cost[????????°][????????????????????????] for (int i = 0; i < N_NODE; ++i) { for (int j = 0; j < N_NODE; ++j) { min_cost[i][j] = INF; } } min_cost[start][M] = 0; priority_queue<Path, vector<Path>, greater<Path>> que; que.push(Path(start, 0, M)); // bin sort // bin[from] = { Edge{to, cost}, Edge{to, cost}, ... } /* vector<Edge> bin[N_NODE]; bin[0].push_back( Edge{ 1, 3 } ); bin[1].push_back( Edge{ 0, 3 } ); bin[0].push_back( Edge{ 2, 1 } ); bin[2].push_back( Edge{ 0, 1 } ); bin[1].push_back( Edge{ 2, 1 } ); bin[2].push_back( Edge{ 1, 1 } ); bin[1].push_back( Edge{ 3, 3 } ); bin[3].push_back( Edge{ 1, 3 } ); bin[2].push_back( Edge{ 3, 2 } ); bin[3].push_back( Edge{ 2, 2 } ); bin[2].push_back( Edge{ 4, 5 } ); bin[4].push_back( Edge{ 2, 5 } ); bin[3].push_back( Edge{ 4, 1 } ); bin[4].push_back( Edge{ 3, 1 } ); */ /* ????????? bin ?????°?????? { 0, 3, 1, INF, INF }, { 3, 0, 1, 3, INF }, { 1, 1, 0, 2, 5 }, {INF, 3, 2, 0, 1 }, {INF, INF, 5, 1, 0 }, */ vector<int> freezable_city; freezable_city.reserve(L); //??????????¨????????????????vector????????? for (int i = 0; i < L; ++i) { int tmp = 0; cin >> tmp; freezable_city.push_back(tmp); } // bin??????????????°?????? vector<Edge> bin[N_NODE]; for (int i = 0; i < K; ++i) { int from, to, cost; cin >> from >> to >> cost; bin[from].push_back(Edge{to, cost}); bin[to].push_back(Edge{from, cost}); } bool needHelp = true; while (!que.empty()) { Path now = que.top(); que.pop(); //???????????????????°????????????????????????°????????? if (min_cost[now.num][now.limit_time] < now.cost) continue; for (int i = 0; i < bin[now.num].size(); i++) { //?????????????????????????¨???????????????? if (find_freeze(freezable_city, now.num) && now.limit_time < M) { for (int recovery_amount = 1; recovery_amount <= M - now.limit_time; ++recovery_amount) { if (min_cost[now.num][now.limit_time + recovery_amount] > now.cost + recovery_amount) { que.push(Path(now.num, now.cost + recovery_amount, now.limit_time + recovery_amount)); min_cost[now.num][now.limit_time + recovery_amount] = now.cost + recovery_amount; } } } //??????????????????????????????i????????????????????????????????????min_cost??????????°????????????°??´??° int next_cost = now.cost + bin[now.num][i].cost; int next_minute = now.limit_time - bin[now.num][i].cost; //?§??????????????????????????????£???????????????????????????????°??????????????????? if (next_minute >= 0 && (min_cost[bin[now.num][i].to][next_minute] > next_cost)) { needHelp = false; min_cost[bin[now.num][i].to][next_minute] = next_cost; que.push(Path(bin[now.num][i].to, next_cost, next_minute)); } } } // cout << min_cost[H][0] << endl; // cout << min_cost[H][1] << endl; // cout << min_cost[H][2] << endl; int ans = INF; for (int i = 0; i < N_NODE; ++i) { ans = min(ans, min_cost[H][i]); } if (needHelp || ans == INF) cout << "Help!" << endl; else { // cout << "ans: " << min_cost[H][0] << endl; cout << ans << endl; // cout << min_cost[H][1] << endl; // cout << min_cost[H][2] << endl; } } return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["-", 0, 57, 64, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 57, 64, 9, 0, 7, 15, 16, 17, 19]]
1
783
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; int n, m, l, k, a, h; bool frez[125]; int dist[125][125]; vector<pii> g[125]; int main() { while (true) { scanf("%d%d%d%d%d%d", &n, &m, &l, &k, &a, &h); if (n + m + l + k + a + h == 0) return 0; for (int i = 0; i < 125; i++) { frez[i] = false; g[i].clear(); for (int j = 0; j < 125; j++) { dist[i][j] = 830252521; } } frez[a] = frez[h] = true; while (l--) { int x; scanf("%d", &x); frez[x] = true; } while (k--) { int s, t, u; scanf("%d%d%d", &s, &t, &u); g[s].push_back(pii(t, u)); g[t].push_back(pii(s, u)); } priority_queue<pair<int, pii>> Q; dist[a][m] = 0; Q.push(make_pair(-0, pii(a, m))); while (Q.size()) { pair<int, pii> PP = Q.top(); Q.pop(); int p = PP.second.first; int d = PP.second.second; if (dist[p][d] != -PP.first) continue; // printf("%d,%d -> %d\n",p,d,dist[p][d]); // edge for (int c = 0; c < g[p].size(); c++) { int to = g[p][c].first; int cost = g[p][c].second; int nd = d - cost; if (nd < 0) continue; if (dist[p][d] + cost < dist[to][nd]) { dist[to][nd] = dist[p][d] + cost; Q.push(make_pair(-dist[to][nd], pii(to, nd))); } } // refr if (frez[p] && d < m) { if (dist[p][d] + 1 < dist[p][d + 1]) { dist[p][d + 1] = dist[p][d] + 1; Q.push(make_pair(-dist[p][d + 1], pii(p, d + 1))); } } } int ans = 830252521; for (int i = 0; i <= m; i++) ans = min(ans, dist[h][i]); if (ans == 830252521) { puts("help!"); } else { printf("%d\n", ans); } } }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; int n, m, l, k, a, h; bool frez[125]; int dist[125][125]; vector<pii> g[125]; int main() { while (true) { scanf("%d%d%d%d%d%d", &n, &m, &l, &k, &a, &h); if (n + m + l + k + a + h == 0) return 0; for (int i = 0; i < 125; i++) { frez[i] = false; g[i].clear(); for (int j = 0; j < 125; j++) { dist[i][j] = 830252521; } } frez[a] = frez[h] = true; while (l--) { int x; scanf("%d", &x); frez[x] = true; } while (k--) { int s, t, u; scanf("%d%d%d", &s, &t, &u); g[s].push_back(pii(t, u)); g[t].push_back(pii(s, u)); } priority_queue<pair<int, pii>> Q; dist[a][m] = 0; Q.push(make_pair(-0, pii(a, m))); while (Q.size()) { pair<int, pii> PP = Q.top(); Q.pop(); int p = PP.second.first; int d = PP.second.second; if (dist[p][d] != -PP.first) continue; // printf("%d,%d -> %d\n",p,d,dist[p][d]); // edge for (int c = 0; c < g[p].size(); c++) { int to = g[p][c].first; int cost = g[p][c].second; int nd = d - cost; if (nd < 0) continue; if (dist[p][d] + cost < dist[to][nd]) { dist[to][nd] = dist[p][d] + cost; Q.push(make_pair(-dist[to][nd], pii(to, nd))); } } // refr if (frez[p] && d < m) { if (dist[p][d] + 1 < dist[p][d + 1]) { dist[p][d + 1] = dist[p][d] + 1; Q.push(make_pair(-dist[p][d + 1], pii(p, d + 1))); } } } int ans = 830252521; for (int i = 0; i <= m; i++) ans = min(ans, dist[h][i]); if (ans == 830252521) { puts("Help!"); } else { printf("%d\n", ans); } } }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
646
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <fstream> #include <iomanip> #include <iostream> #include <list> #include <map> #include <memory.h> #include <memory> #include <numeric> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> #define _USE_MATH_DEFINES using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; typedef vector<int> vec; typedef vector<vec> mat; const int INF = 1 << 30; const double EPS = 1e-9; map<int, bool> hos; int dist[100][101]; vector<P> G[100]; int main() { int N, M, L, K, A, H; int h; int x, y, dis; while (cin >> N >> M >> L >> K >> A >> H && (N || M || L || K || A || H)) { for (int i = 0; i < L; i++) { cin >> h; hos[h] = true; } for (int i = 0; i < K; i++) { cin >> x >> y >> dis; G[x].push_back(P(y, dis)); G[y].push_back(P(x, dis)); } bool f = true; fill(&dist[0][0], &dist[99][100] + 1, INF); dist[A][M] = 0; priority_queue<PP, vector<PP>, greater<PP>> que; que.push(PP(0, P(A, M))); while (!que.empty()) { PP pp = que.top(); que.pop(); int time = pp.first; int place = pp.second.first; int rest = pp.second.second; if (time < dist[place][rest]) continue; if (place == H) { cout << dist[place][rest] << endl; f = false; break; } if (hos[place]) { for (int i = rest + 1; i <= M; i++) { if (dist[place][i] > dist[place][rest] + i - rest) { dist[place][i] = dist[place][rest] + i - rest; que.push(PP(dist[place][i], P(place, i))); } } } for (int i = 0; i < G[place].size(); i++) { P p = G[place][i]; if (rest - p.second < 0) continue; if (dist[p.first][rest - p.second] > dist[place][rest] + p.second) { dist[p.first][rest - p.second] = dist[place][rest] + p.second; que.push( PP(dist[p.first][rest - p.second], P(p.first, rest - p.second))); } } } if (f) cout << "Help!" << endl; for (int i = 0; i < 100; i++) G[i].clear(); } return 0; }
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <fstream> #include <iomanip> #include <iostream> #include <list> #include <map> #include <memory.h> #include <memory> #include <numeric> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> #define _USE_MATH_DEFINES using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; typedef vector<int> vec; typedef vector<vec> mat; const int INF = 1 << 30; const double EPS = 1e-9; map<int, bool> hos; int dist[100][101]; vector<P> G[100]; int main() { int N, M, L, K, A, H; int h; int x, y, dis; while (cin >> N >> M >> L >> K >> A >> H && (N || M || L || K || A || H)) { for (int i = 0; i < L; i++) { cin >> h; hos[h] = true; } for (int i = 0; i < K; i++) { cin >> x >> y >> dis; G[x].push_back(P(y, dis)); G[y].push_back(P(x, dis)); } bool f = true; fill(&dist[0][0], &dist[99][100] + 1, INF); dist[A][M] = 0; priority_queue<PP, vector<PP>, greater<PP>> que; que.push(PP(0, P(A, M))); while (!que.empty()) { PP pp = que.top(); que.pop(); int time = pp.first; int place = pp.second.first; int rest = pp.second.second; if (time < dist[place][rest]) continue; if (place == H) { cout << dist[place][rest] << endl; f = false; break; } if (hos[place]) { for (int i = rest + 1; i <= M; i++) { if (dist[place][i] > dist[place][rest] + i - rest) { dist[place][i] = dist[place][rest] + i - rest; que.push(PP(dist[place][i], P(place, i))); } } } for (int i = 0; i < G[place].size(); i++) { P p = G[place][i]; if (rest - p.second < 0) continue; if (dist[p.first][rest - p.second] > dist[place][rest] + p.second) { dist[p.first][rest - p.second] = dist[place][rest] + p.second; que.push( PP(dist[p.first][rest - p.second], P(p.first, rest - p.second))); } } } if (f) cout << "Help!" << endl; for (int i = 0; i < 100; i++) G[i].clear(); hos.clear(); } return 0; }
[["+", 8, 9, 0, 1, 0, 2, 63, 118, 28, 22], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 17, 131], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 119, 120], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
696
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; const int MAX = 101; const int INF = (1 << 25); int T[MAX][MAX]; int N, M, L, K, A, H; bool heal[MAX]; struct Edge { int to, cost; Edge(int a, int b) : to(a), cost(b){}; Edge() {} }; struct State { int pos, blood, t; State(int a, int b, int c) : pos(a), blood(b), t(c) {} bool operator<(const State &s) const { return t > s.t; } }; vector<Edge> E[MAX]; void init() { for (int i = 0; i < MAX; i++) { fill(T[i], T[i] + MAX, INF); heal[i] = false; E[i].clear(); } } void input() { for (int i = 0; i < L; i++) { int in; cin >> in; heal[in] = true; } for (int i = 0; i < K; i++) { int a, b, c; cin >> a >> b >> c; E[a].push_back(Edge(b, c)); E[b].push_back(Edge(a, c)); } } void solve() { priority_queue<State> Q; Q.push(State(A, M, 0)); T[A][M] = 0; while (!Q.empty()) { State s = Q.top(); Q.pop(); if (T[s.pos][s.blood] < s.t) continue; // heal if (heal[s.pos]) { for (int i = 1; s.blood + i <= M; i++) { if (T[s.pos][s.blood + i] > s.t + i) { T[s.pos][s.blood + i] = s.t + i; Q.push(State(s.pos, s.blood + i, s.t + i)); } } } // go next for (int i = 0; i < E[s.pos].size(); i++) { int nex = E[s.pos][i].to; int cos = E[s.pos][i].cost; int rem = s.blood; if (rem >= cos && T[nex][rem - cos] > s.t + cos) { T[nex][rem - cos] = s.t + cos; Q.push(State(nex, rem - cos, s.t + cos)); } } } int ans = INF; for (int i = 0; i < MAX; i++) ans = min(ans, T[H][i]); if (ans == INF) cout << "help!" << endl; else cout << ans << endl; } int main() { while (cin >> N >> M >> L >> K >> A >> H && N + M + L + K + A + H) { init(); input(); solve(); } return 0; }
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; const int MAX = 101; const int INF = (1 << 25); int T[MAX][MAX]; int N, M, L, K, A, H; bool heal[MAX]; struct Edge { int to, cost; Edge(int a, int b) : to(a), cost(b){}; Edge() {} }; struct State { int pos, blood, t; State(int a, int b, int c) : pos(a), blood(b), t(c) {} bool operator<(const State &s) const { return t > s.t; } }; vector<Edge> E[MAX]; void init() { for (int i = 0; i < MAX; i++) { fill(T[i], T[i] + MAX, INF); heal[i] = false; E[i].clear(); } } void input() { for (int i = 0; i < L; i++) { int in; cin >> in; heal[in] = true; } for (int i = 0; i < K; i++) { int a, b, c; cin >> a >> b >> c; E[a].push_back(Edge(b, c)); E[b].push_back(Edge(a, c)); } } void solve() { priority_queue<State> Q; Q.push(State(A, M, 0)); T[A][M] = 0; while (!Q.empty()) { State s = Q.top(); Q.pop(); if (T[s.pos][s.blood] < s.t) continue; // heal if (heal[s.pos]) { for (int i = 1; s.blood + i <= M; i++) { if (T[s.pos][s.blood + i] > s.t + i) { T[s.pos][s.blood + i] = s.t + i; Q.push(State(s.pos, s.blood + i, s.t + i)); } } } // go next for (int i = 0; i < E[s.pos].size(); i++) { int nex = E[s.pos][i].to; int cos = E[s.pos][i].cost; int rem = s.blood; if (rem >= cos && T[nex][rem - cos] > s.t + cos) { T[nex][rem - cos] = s.t + cos; Q.push(State(nex, rem - cos, s.t + cos)); } } } int ans = INF; for (int i = 0; i < MAX; i++) ans = min(ans, T[H][i]); if (ans == INF) cout << "Help!" << endl; else cout << ans << endl; } int main() { while (cin >> N >> M >> L >> K >> A >> H && N + M + L + K + A + H) { init(); input(); solve(); } return 0; }
[["-", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
723
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <vector> using namespace std; #define INF (1 << 30) typedef pair<int, int> P; typedef pair<int, P> T; vector<P> edge[105]; int n, m, l, k, a, h; bool recover[105]; int dist[105][105]; void dijkstra(int s) { for (int i = 0; i < 105; i++) for (int j = 0; j < 105; j++) dist[i][j] = INF; dist[s][m] = 0; priority_queue<T, vector<T>, greater<T>> pq; pq.push(T(0, P(s, m))); while (!pq.empty()) { T tmp = pq.top(); pq.pop(); int d = tmp.first, from = tmp.second.first, blood = tmp.second.second; if (dist[from][blood] < d) continue; for (int i = 0; i < edge[from].size(); i++) { int to = edge[from][i].first, r = edge[from][i].second; if (r > blood) continue; if (dist[to][blood - r] <= d + r) continue; dist[to][blood - r] = d + r; pq.push(T(dist[to][blood - r], P(to, blood - r))); } if (blood < m && recover[from]) { if (dist[from][blood + 1] <= d + 1) continue; dist[from][blood + 1] = d + 1; pq.push(T(dist[from][blood + 1], P(from, blood + 1))); } } } int main() { int t, x, y; while (cin >> n >> m >> l >> k >> a >> h, n) { fill(recover, recover + 105, false); for (int i = 0; i < 105; i++) edge[i].clear(); for (int i = 0; i < l; i++) { cin >> t; recover[t] = true; } for (int i = 0; i < k; i++) { cin >> x >> y >> t; edge[x].push_back(P(y, t)); edge[y].push_back(P(x, t)); } dijkstra(a); int res = INF; for (int i = 0; i < 105; i++) { res = min(res, dist[h][i]); } if (res == INF) cout << "Help!" << endl; cout << res << endl; } return 0; }
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <vector> using namespace std; #define INF (1 << 30) typedef pair<int, int> P; typedef pair<int, P> T; vector<P> edge[105]; int n, m, l, k, a, h; bool recover[105]; int dist[105][105]; void dijkstra(int s) { for (int i = 0; i < 105; i++) for (int j = 0; j < 105; j++) dist[i][j] = INF; dist[s][m] = 0; priority_queue<T, vector<T>, greater<T>> pq; pq.push(T(0, P(s, m))); while (!pq.empty()) { T tmp = pq.top(); pq.pop(); int d = tmp.first, from = tmp.second.first, blood = tmp.second.second; if (dist[from][blood] < d) continue; for (int i = 0; i < edge[from].size(); i++) { int to = edge[from][i].first, r = edge[from][i].second; if (r > blood) continue; if (dist[to][blood - r] <= d + r) continue; dist[to][blood - r] = d + r; pq.push(T(dist[to][blood - r], P(to, blood - r))); } if (blood < m && recover[from]) { if (dist[from][blood + 1] <= d + 1) continue; dist[from][blood + 1] = d + 1; pq.push(T(dist[from][blood + 1], P(from, blood + 1))); } } } int main() { int t, x, y; while (cin >> n >> m >> l >> k >> a >> h, n) { fill(recover, recover + 105, false); for (int i = 0; i < 105; i++) edge[i].clear(); for (int i = 0; i < l; i++) { cin >> t; recover[t] = true; } for (int i = 0; i < k; i++) { cin >> x >> y >> t; edge[x].push_back(P(y, t)); edge[y].push_back(P(x, t)); } dijkstra(a); int res = INF; for (int i = 0; i < 105; i++) { res = min(res, dist[h][i]); } if (res == INF) cout << "Help!" << endl; else cout << res << endl; } return 0; }
[["+", 0, 52, 8, 9, 0, 57, 75, 76, 0, 95]]
1
623
#define _CRT_SECURE_NO_WARNINGS //#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; typedef long long ll; //#define int ll //#define endl "\n" typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; #define all(c) (c).begin(), (c).end() #define loop(i, a, b) for (ll i = a; i < ll(b); i++) #define rep(i, b) loop(i, 0, b) #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple template <class T> ostream &operator<<(ostream &os, vector<T> const &); template <int n, class... T> typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &, tuple<T...> const &) {} template <int n, class... T> typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os, tuple<T...> const &t) { os << (n == 0 ? "" : " ") << get<n>(t); _ot<n + 1>(os, t); } template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) { _ot<0>(os, t); return os; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> const &p) { return os << "(" << p.first << ", " << p.second << ") "; } template <class T> ostream &operator<<(ostream &os, vector<T> const &v) { rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " "); return os; } template <class T> inline bool chmax(T &x, T const &y) { return x < y ? x = y, true : false; } template <class T> inline bool chmin(T &x, T const &y) { return x > y ? x = y, true : false; } #ifdef DEBUG #define dump(...) \ (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \ << endl) #else #define dump(...) #endif // ll const mod = 1000000007; ll const inf = 1 << 28; int N; vvi g; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); while (cin >> N && N) { vector<string> s(N); rep(i, N) cin >> s[i]; sort(all(s)); s.erase(unique(all(s)), s.end()); N = s.size(); rep(i, N) rep(j, N) if (i != j) { if (s[i].find(s[j]) != string::npos) s[j] = "~"; } s.erase(remove(all(s), "~"), s.end()); N = s.size(); g.assign(N, vi(N, inf)); rep(i, N) rep(j, N) { if (i == j) continue; for (int k = min(s[i].size(), s[j].size()); k >= 0; k--) { auto a = max(s[i].begin(), s[i].end() - k); auto b = s[i].end(); auto c = s[j].begin(); auto d = min(s[j].begin() + k, s[j].end()); if (b - a == d - c && equal(a, b, c)) { g[i][j] = k; break; } } } vector<pair<int, vector<char>>> cand; vector<char> k(N); rep(i, N) k[i] = i; int m = inf; do { int l = s[k[0]].size(); rep(i, N - 1) l += s[k[i + 1]].size() - g[k[i]][k[i + 1]]; m = min(l, m); if (l <= m) cand.eb(l, k); } while (next_permutation(all(k))); reverse(all(cand)); string ans(1, '~'); dump(cand.size()); rep(i, cand.size()) { if (i != 0 && cand[i - 1].first < cand[i - 1].first) break; vector<char> &l = cand[i].second; string c = s[l[0]]; rep(j, N - 1) c += s[l[j + 1]].substr(g[l[j]][l[j + 1]]); ans = min(ans, c, [](string const &a, string const &b) { return a.size() != b.size() ? a.size() < b.size() : a < b; }); } cout << ans << endl; } }
#define _CRT_SECURE_NO_WARNINGS //#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; typedef long long ll; //#define int ll //#define endl "\n" typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; #define all(c) (c).begin(), (c).end() #define loop(i, a, b) for (ll i = a; i < ll(b); i++) #define rep(i, b) loop(i, 0, b) #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple template <class T> ostream &operator<<(ostream &os, vector<T> const &); template <int n, class... T> typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &, tuple<T...> const &) {} template <int n, class... T> typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os, tuple<T...> const &t) { os << (n == 0 ? "" : " ") << get<n>(t); _ot<n + 1>(os, t); } template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) { _ot<0>(os, t); return os; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> const &p) { return os << "(" << p.first << ", " << p.second << ") "; } template <class T> ostream &operator<<(ostream &os, vector<T> const &v) { rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " "); return os; } template <class T> inline bool chmax(T &x, T const &y) { return x < y ? x = y, true : false; } template <class T> inline bool chmin(T &x, T const &y) { return x > y ? x = y, true : false; } #ifdef DEBUG #define dump(...) \ (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \ << endl) #else #define dump(...) #endif // ll const mod = 1000000007; ll const inf = 1 << 28; int N; vvi g; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); while (cin >> N && N) { vector<string> s(N); rep(i, N) cin >> s[i]; sort(all(s)); s.erase(unique(all(s)), s.end()); N = s.size(); rep(i, N) rep(j, N) if (i != j) { if (s[i].find(s[j]) != string::npos) s[j] = "~"; } s.erase(remove(all(s), "~"), s.end()); N = s.size(); g.assign(N, vi(N, inf)); rep(i, N) rep(j, N) { if (i == j) continue; for (int k = min(s[i].size(), s[j].size()); k >= 0; k--) { auto a = max(s[i].begin(), s[i].end() - k); auto b = s[i].end(); auto c = s[j].begin(); auto d = min(s[j].begin() + k, s[j].end()); if (b - a == d - c && equal(a, b, c)) { g[i][j] = k; break; } } } vector<pair<int, vector<char>>> cand; vector<char> k(N); rep(i, N) k[i] = i; int m = inf; do { int l = s[k[0]].size(); rep(i, N - 1) l += s[k[i + 1]].size() - g[k[i]][k[i + 1]]; m = min(l, m); if (l <= m) cand.eb(l, k); } while (next_permutation(all(k))); reverse(all(cand)); string ans(101, '~'); dump(cand.size()); rep(i, cand.size()) { if (i != 0 && cand[i - 1].first < cand[i - 1].first) break; vector<char> &l = cand[i].second; string c = s[l[0]]; rep(j, N - 1) c += s[l[j + 1]].substr(g[l[j]][l[j + 1]]); ans = min(ans, c, [](string const &a, string const &b) { return a.size() != b.size() ? a.size() < b.size() : a < b; }); } cout << ans << endl; } }
[["-", 8, 9, 0, 43, 49, 50, 51, 4, 0, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 4, 0, 13]]
1
1,140
#include <bits/stdc++.h> using namespace std; int n; vector<string> str; string mem[15][15]; typedef pair<string, int> P; string con(string a, string b) { int K = 0; for (int i = 0; i < a.size(); i++) { int j = i, k = 0; while (a[j] && b[k] && a[j] == b[k]) j++, k++; if (!a[j] || !b[k]) K = max(K, k); } return b.substr(K, b.size()); } string solve() { string dp[1 << 14][14]; for (int i = 0; i < n; i++) dp[1 << i][i] = str[i]; for (int i = 0; i < (1 << n); i++) for (int k = 0; k < n; k++) { if (dp[i][k] == "") continue; for (int j = 0; j < n; j++) { if ((i & (1 << j))) continue; string nx = dp[i][k] + mem[k][j]; int ni = i | 1 << j; string &ndp = dp[ni][j]; if (ndp.size() || nx.size() < ndp.size() || (nx.size() == ndp.size() && ndp > nx)) ndp = nx; } } string res = dp[(1 << n) - 1][0]; for (int i = 1; i < n; i++) { string a = dp[(1 << n) - 1][i]; if (res.size() > a.size() || (a.size() == res.size() && res > a)) res = a; } return res; } int main() { while (cin >> n, n) { str.resize(n); for (int i = 0; i < n; i++) cin >> str[i]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j && con(str[i], str[j]) == "") { str.erase(str.begin() + j); n--; i = -1; break; } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) mem[i][j] = con(str[i], str[j]); cout << solve() << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n; vector<string> str; string mem[15][15]; typedef pair<string, int> P; string con(string a, string b) { int K = 0; for (int i = 0; i < a.size(); i++) { int j = i, k = 0; while (a[j] && b[k] && a[j] == b[k]) j++, k++; if (!a[j] || !b[k]) K = max(K, k); } return b.substr(K, b.size()); } string solve() { string dp[1 << 14][14]; for (int i = 0; i < n; i++) dp[1 << i][i] = str[i]; for (int i = 0; i < (1 << n); i++) for (int k = 0; k < n; k++) { if (dp[i][k] == "") continue; for (int j = 0; j < n; j++) { if ((i & (1 << j))) continue; string nx = dp[i][k] + mem[k][j]; int ni = i | 1 << j; string &ndp = dp[ni][j]; if (!ndp.size() || nx.size() < ndp.size() || (nx.size() == ndp.size() && ndp > nx)) ndp = nx; } } string res = dp[(1 << n) - 1][0]; for (int i = 1; i < n; i++) { string a = dp[(1 << n) - 1][i]; if (res.size() > a.size() || (a.size() == res.size() && res > a)) res = a; } return res; } int main() { while (cin >> n, n) { str.resize(n); for (int i = 0; i < n; i++) cin >> str[i]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j && con(str[i], str[j]) == "") { str.erase(str.begin() + j); n--; i = -1; break; } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) mem[i][j] = con(str[i], str[j]); cout << solve() << endl; } return 0; }
[["+", 15, 339, 51, 16, 31, 16, 31, 91, 17, 111]]
1
617
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; struct before_main { before_main() { cin.tie(0); ios::sync_with_stdio(false); } } before_main; #define REP(i, a, b) for (int i = a; i < (int)b; i++) #define rep(i, n) REP(i, 0, n) #define all(c) (c).begin(), (c).end() #define zero(a) memset(a, 0, sizeof a) #define minus(a) memset(a, -1, sizeof a) template <class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); } template <class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); } typedef long long ll; int const inf = 1 << 29; int to_int(char c) { if (isdigit(c)) { return c - '0'; } if (c == 'A') { return 11; } return 10; } bool test_ace17(int value, int ace11) { while (ace11) { if (value <= 17) { return true; } ace11--; value -= 10; } return false; } int main() { int T; cin >> T; rep(_, T) { vector<int> in; rep(i, 10) { char c; cin >> c; in.push_back(to_int(c)); } if (in[0] + in[1] == 21) { cout << "blackjack" << endl; continue; } if (in[0] + in[1] > 21) { in[0] -= 10; } REP(k, 2, 9) { int value = 0; int ace11 = 0; rep(i, k + 1) { value += in[i]; ace11 += in[i] == 11; } while (value > 21 && ace11) { ace11--; value -= 10; } if (value > 21) { cout << "bust" << endl; break; } if (test_ace17(value, ace11)) { continue; } if (value <= 16) { continue; } cout << value << endl; break; } } return 0; }
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; struct before_main { before_main() { cin.tie(0); ios::sync_with_stdio(false); } } before_main; #define REP(i, a, b) for (int i = a; i < (int)b; i++) #define rep(i, n) REP(i, 0, n) #define all(c) (c).begin(), (c).end() #define zero(a) memset(a, 0, sizeof a) #define minus(a) memset(a, -1, sizeof a) template <class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); } template <class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); } typedef long long ll; int const inf = 1 << 29; int to_int(char c) { if (isdigit(c)) { return c - '0'; } if (c == 'A') { return 11; } return 10; } bool test_ace17(int value, int ace11) { while (ace11) { if (value <= 17) { return true; } ace11--; value -= 10; } return false; } int main() { int T; cin >> T; rep(_, T) { vector<int> in; rep(i, 10) { char c; cin >> c; in.push_back(to_int(c)); } if (in[0] + in[1] == 21) { cout << "blackjack" << endl; continue; } if (in[0] + in[1] > 21) { in[0] -= 10; } REP(k, 1, 10) { int value = 0; int ace11 = 0; rep(i, k + 1) { value += in[i]; ace11 += in[i] == 11; } while (value > 21 && ace11) { ace11--; value -= 10; } if (value > 21) { cout << "bust" << endl; break; } if (test_ace17(value, ace11)) { continue; } if (value <= 16) { continue; } cout << value << endl; break; } } return 0; }
[["-", 0, 30, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 0, 30, 0, 1, 0, 2, 3, 4, 0, 13]]
1
486
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define all(v) (v).begin(), (v).end() #define rev(s) (s).rbegin(), (s).rend() #define MP make_pair #define X first #define Y second using namespace std; typedef long long ll; typedef pair<int, int> P; typedef vector<int> vi; void checkace(int &hand, int &ace) { if (ace && hand > 21) { hand -= 10; ace--; } } int main() { int dataset; cin >> dataset; map<char, int> card; rep(i, 9) { card['1' + i] = 1 + i; } rep(i, 4) { card["TJQK"[i]] = 10; } card['A'] = 11; while (dataset--) { int hand = 0, ace = 0; rep(i, 2) { char c; cin >> c; int n = card[c]; hand += n; if (n == 11) ace++; } checkace(hand, ace); queue<int> pile; rep(i, 8) { char c; cin >> c; pile.push(card[c]); } bool bj = hand == 21; while (hand < 16 || (hand < 17 && ace > 0)) { int n = pile.front(); pile.pop(); hand += n; if (n == 11) ace++; checkace(hand, ace); } if (hand > 21) cout << "bust" << endl; else { if (bj) { cout << "blackjack" << endl; } else { cout << hand << endl; } } } return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define all(v) (v).begin(), (v).end() #define rev(s) (s).rbegin(), (s).rend() #define MP make_pair #define X first #define Y second using namespace std; typedef long long ll; typedef pair<int, int> P; typedef vector<int> vi; void checkace(int &hand, int &ace) { if (ace && hand > 21) { hand -= 10; ace--; } } int main() { int dataset; cin >> dataset; map<char, int> card; rep(i, 9) { card['1' + i] = 1 + i; } rep(i, 4) { card["TJQK"[i]] = 10; } card['A'] = 11; while (dataset--) { int hand = 0, ace = 0; rep(i, 2) { char c; cin >> c; int n = card[c]; hand += n; if (n == 11) ace++; } checkace(hand, ace); queue<int> pile; rep(i, 8) { char c; cin >> c; pile.push(card[c]); } bool bj = hand == 21; while (hand <= 16 || (hand == 17 && ace > 0)) { int n = pile.front(); pile.pop(); hand += n; if (n == 11) ace++; checkace(hand, ace); } if (hand > 21) cout << "bust" << endl; else { if (bj) { cout << "blackjack" << endl; } else { cout << hand << endl; } } } return 0; }
[["-", 0, 52, 15, 339, 51, 16, 31, 16, 17, 18], ["+", 0, 52, 15, 339, 51, 16, 31, 16, 17, 19], ["-", 51, 16, 12, 23, 0, 16, 31, 16, 17, 18], ["+", 51, 16, 12, 23, 0, 16, 31, 16, 17, 60]]
1
394
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; typedef long long LL; int main() { int n; while (cin >> n && n > 0) { if (n <= 16) { cout << 0 << endl; continue; } LL f = n; REP(i, 7) f *= i + 1; if (n % 2 == 0) { n /= 2; vector<LL> dp1(n); vector<LL> dp2(n); dp1[0] = 1; REP(_, 7) { vector<LL> next1(n); vector<LL> next2(n); REP(i, n) { if (dp1[i]) { for (int j = i + 2; j < n; j++) { next1[j] += dp1[i]; } for (int j = i + 1; j < n; j++) { next2[j] += dp1[i]; } } if (dp2[i]) { for (int j = i + 2; j < n; j++) { next2[j] += dp1[i]; } for (int j = i + 1; j < n; j++) { next1[j] += dp1[i]; } } } dp1.swap(next1); dp2.swap(next2); } LL ans = 0; REP(i, n - 1) ans += dp1[i]; REP(i, n) ans += dp2[i]; cout << f * ans << endl; } else { n--; vector<LL> dp(n); dp[0] = 1; REP(_, 7) { vector<LL> next(n); REP(i, n) if (dp[i]) { for (int j = i + 2; j < n; j++) { next[j] += dp[i]; } } dp.swap(next); } LL ans = 0; REP(i, n) ans += dp[i]; cout << f * ans << endl; } } return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; typedef long long LL; int main() { int n; while (cin >> n && n > 0) { if (n <= 16) { cout << 0 << endl; continue; } LL f = n; REP(i, 7) f *= i + 1; if (n % 2 == 0) { n /= 2; vector<LL> dp1(n); vector<LL> dp2(n); dp1[0] = 1; REP(_, 7) { vector<LL> next1(n); vector<LL> next2(n); REP(i, n) { if (dp1[i]) { for (int j = i + 2; j < n; j++) { next1[j] += dp1[i]; } for (int j = i + 1; j < n; j++) { next2[j] += dp1[i]; } } if (dp2[i]) { for (int j = i + 2; j < n; j++) { next2[j] += dp2[i]; } for (int j = i + 1; j < n; j++) { next1[j] += dp2[i]; } } } dp1.swap(next1); dp2.swap(next2); } LL ans = 0; REP(i, n - 1) ans += dp1[i]; REP(i, n) ans += dp2[i]; cout << f * ans << endl; } else { n--; vector<LL> dp(n); dp[0] = 1; REP(_, 7) { vector<LL> next(n); REP(i, n) if (dp[i]) { for (int j = i + 2; j < n; j++) { next[j] += dp[i]; } } dp.swap(next); } LL ans = 0; REP(i, n) ans += dp[i]; cout << f * ans << endl; } } return 0; }
[["-", 8, 9, 0, 1, 0, 11, 12, 69, 28, 22], ["+", 8, 9, 0, 1, 0, 11, 12, 69, 28, 22]]
1
452
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; int n; vector<vector<long long>> memo2; long long solve2(int pos, int rest) { if (rest == 0) return 1; if (pos >= n) return 0; if (memo2[pos][rest] != -1) return memo2[pos][rest]; long long ret = 0; for (int i = pos; i <= n - 2; ++i) ret += solve2(i + 2, rest - 1) * rest; return memo2[pos][rest] = ret; } vector<vector<vector<long long>>> memo; long long solve(int pos, int rest, int isFirstHalf) { if (rest == 0) return 1; if (pos >= n / 2) return 0; if (memo[pos][rest][isFirstHalf] != -1) return memo[pos][rest][isFirstHalf]; long long ret = 0; for (int i = pos; i < n / 2; ++i) { if (i == n / 2 - 1) { if (i == pos) { if (!isFirstHalf) ret += solve(i + 1, rest - 1, 1) * rest; } else { ret += solve(i + 1, rest - 1, 1) * rest; } } else if (i == pos) { ret += solve(i + 1, rest - 1, isFirstHalf ^ 1); } else { ret += solve(i + 1, rest - 1, 0) * rest; ret += solve(i + 1, rest - 1, 1) * rest; } } return memo[pos][rest][isFirstHalf] = ret; } int main() { for (;;) { cin >> n; if (n == 0) return 0; if (n % 2 == 1) { memo2.assign(n, vector<long long>(8, -1)); long long ret = solve2(2, 7) * n; cout << ret << endl; } else { memo.assign(n / 2, vector<vector<long long>>(8, vector<long long>(2, -1))); long long ret = solve(1, 7, 1) * n; cout << ret << endl; } } }
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; int n; vector<vector<long long>> memo2; long long solve2(int pos, int rest) { if (rest == 0) return 1; if (pos >= n) return 0; if (memo2[pos][rest] != -1) return memo2[pos][rest]; long long ret = 0; for (int i = pos; i <= n - 2; ++i) ret += solve2(i + 2, rest - 1) * rest; return memo2[pos][rest] = ret; } vector<vector<vector<long long>>> memo; long long solve(int pos, int rest, int isFirstHalf) { if (rest == 0) return 1; if (pos >= n / 2) return 0; if (memo[pos][rest][isFirstHalf] != -1) return memo[pos][rest][isFirstHalf]; long long ret = 0; for (int i = pos; i < n / 2; ++i) { if (i == n / 2 - 1) { if (i == pos) { if (!isFirstHalf) ret += solve(i + 1, rest - 1, 1) * rest; } else { ret += solve(i + 1, rest - 1, 1) * rest; } } else if (i == pos) { ret += solve(i + 1, rest - 1, isFirstHalf ^ 1) * rest; } else { ret += solve(i + 1, rest - 1, 0) * rest; ret += solve(i + 1, rest - 1, 1) * rest; } } return memo[pos][rest][isFirstHalf] = ret; } int main() { for (;;) { cin >> n; if (n == 0) return 0; if (n % 2 == 1) { memo2.assign(n, vector<long long>(8, -1)); long long ret = solve2(2, 7) * n; cout << ret << endl; } else { memo.assign(n / 2, vector<vector<long long>>(8, vector<long long>(2, -1))); long long ret = solve(1, 7, 1) * n; cout << ret << endl; } } }
[["+", 64, 9, 0, 1, 0, 11, 12, 16, 17, 48], ["+", 64, 9, 0, 1, 0, 11, 12, 16, 12, 22]]
1
517
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <valarray> #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() #define chmax(a, b) (a < b ? (a = b, 1) : 0) #define chmin(a, b) (a > b ? (a = b, 1) : 0) #define valid(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w) typedef long long ll; typedef pair<int, int> pii; const int INF = 1 << 29; const double PI = acos(-1); const double EPS = 1e-8; ll dp[10][101][2][2]; void odd_init() { dp[0][1][0][0] = dp[1][1][1][1] = 1; REP(i, 9) { for (int j = 1; j < 100; ++j) { REP(k, 2) { dp[i][j + 1][k][0] += dp[i][j][k][0] + dp[i][j][k][1]; dp[i + 1][j + 1][k][1] += dp[i][j][k][0]; } } } } ll dp2[10][101][3][3]; void even_init() { dp2[0][1][0][0] = dp2[1][1][1][1] = dp2[1][1][2][2] = 1; REP(i, 9) { REP(j, 100) { REP(k, 3) { dp2[i][j + 1][k][0] += dp2[i][j][k][0] + dp2[i][j][k][1] + dp2[i][j][k][2]; dp2[i + 1][j + 1][k][1] += dp2[i][j][k][0] + dp2[i][j][k][2]; dp2[i + 1][j + 1][k][2] += dp2[i][j][k][0] + dp2[i][j][k][1]; } } } } ll solve_odd(int n) { return (dp[8][n][0][0] + dp[8][n][0][1] + dp[8][n][1][0]) * 40320; } ll solve_even(int n) { ll ans = 0; REP(i, 3) REP(j, 3) { if (i == 1 && j == 2 || i == 2 && j == 1) continue; ans += dp2[8][n / 2][0][i]; } return ans * 40320; } int main() { odd_init(); even_init(); int n; while (cin >> n, n) { if (n & 1) { cout << solve_odd(n) << endl; } else { cout << solve_even(n) << endl; } } }
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <valarray> #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() #define chmax(a, b) (a < b ? (a = b, 1) : 0) #define chmin(a, b) (a > b ? (a = b, 1) : 0) #define valid(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w) typedef long long ll; typedef pair<int, int> pii; const int INF = 1 << 29; const double PI = acos(-1); const double EPS = 1e-8; ll dp[10][101][2][2]; void odd_init() { dp[0][1][0][0] = dp[1][1][1][1] = 1; REP(i, 9) { for (int j = 1; j < 100; ++j) { REP(k, 2) { dp[i][j + 1][k][0] += dp[i][j][k][0] + dp[i][j][k][1]; dp[i + 1][j + 1][k][1] += dp[i][j][k][0]; } } } } ll dp2[10][101][3][3]; void even_init() { dp2[0][1][0][0] = dp2[1][1][1][1] = dp2[1][1][2][2] = 1; REP(i, 9) { REP(j, 100) { REP(k, 3) { dp2[i][j + 1][k][0] += dp2[i][j][k][0] + dp2[i][j][k][1] + dp2[i][j][k][2]; dp2[i + 1][j + 1][k][1] += dp2[i][j][k][0] + dp2[i][j][k][2]; dp2[i + 1][j + 1][k][2] += dp2[i][j][k][0] + dp2[i][j][k][1]; } } } } ll solve_odd(int n) { return (dp[8][n][0][0] + dp[8][n][0][1] + dp[8][n][1][0]) * 40320; } ll solve_even(int n) { ll ans = 0; REP(i, 3) REP(j, 3) { if (i == 1 && j == 2 || i == 2 && j == 1) continue; ans += dp2[8][n / 2][i][j]; } return ans * 40320; } int main() { odd_init(); even_init(); int n; while (cin >> n, n) { if (n & 1) { cout << solve_odd(n) << endl; } else { cout << solve_even(n) << endl; } } }
[["-", 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, 22], ["+", 0, 1, 0, 11, 12, 69, 341, 342, 0, 22]]
1
714
/* AOJ 2030 Title:Ruins @kankichi573 */ #include <limits.h> #include <math.h> #include <stdio.h> extern double sqrt(double); int compare_int(const void *a, const void *b) { return *(int *)b - *(int *)a; } int solve(int m, int n) { int min_, sum, i, j, k, l, a[4], b[4]; min_ = INT_MAX; for (i = 1; i < sqrt(m); i++) { if (m % i) continue; a[0] = i; a[1] = m / i; for (j = 1; j < sqrt(n); j++) { if (n % j) continue; a[2] = j; a[3] = n / j; for (k = 0; k < 4; k++) b[k] = a[k]; qsort(b, 4, sizeof(int), compare_int); sum = 0; for (k = 0; k < 3; k++) sum += (b[k] - b[k + 1]) * (b[k] - b[k + 1]); if (sum < min_) { /* for(l=0;l<4;l++) printf("%d:",b[l]); printf("\n"); */ min_ = sum; } } } return (min_); } main() { int N, M, ret; while (scanf("%d %d", &N, &M) && (N || M)) { ret = solve(N, M); printf("%d\n", ret); } return (0); }
/* AOJ 2030 Title:Ruins @kankichi573 */ #include <limits.h> #include <math.h> #include <stdio.h> extern double sqrt(double); int compare_int(const void *a, const void *b) { return *(int *)b - *(int *)a; } int solve(int m, int n) { int min_, sum, i, j, k, l, a[4], b[4]; min_ = INT_MAX; for (i = 1; i <= sqrt(m); i++) { if (m % i) continue; a[0] = i; a[1] = m / i; for (j = 1; j <= sqrt(n); j++) { if (n % j) continue; a[2] = j; a[3] = n / j; for (k = 0; k < 4; k++) b[k] = a[k]; qsort(b, 4, sizeof(int), compare_int); sum = 0; for (k = 0; k < 3; k++) sum += (b[k] - b[k + 1]) * (b[k] - b[k + 1]); if (sum < min_) { /* for(l=0;l<4;l++) printf("%d:",b[l]); printf("\n"); */ min_ = sum; } } } return (min_); } main() { int N, M, ret; while (scanf("%d %d", &N, &M) && (N || M)) { ret = solve(N, M); printf("%d\n", ret); } return (0); }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19]]
0
328
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define LOG(...) printf(__VA_ARGS__) //#define LOG(...) #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) #define RSORT(c) sort((c).rbegin(), (c).rend()) #define CLR(a) memset((a), 0, sizeof(a)) typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; int main() { int x, y; while (cin >> x >> y, x) { vector<int> xv; vector<int> yv; for (int i = 1; i < sqrt(x); i++) { if (x % i == 0) xv.push_back(i); } for (int i = 1; i < sqrt(y); i++) { if (y % i == 0) yv.push_back(i); } vector<int> calc(4); int ans = 100000007; REP(i, xv.size()) { REP(j, yv.size()) { calc[0] = xv[i]; calc[1] = x / xv[i]; calc[2] = yv[j]; calc[3] = y / yv[j]; SORT(calc); ans = min(ans, (calc[0] - calc[1]) * (calc[0] - calc[1]) + (calc[2] - calc[1]) * (calc[2] - calc[1]) + (calc[3] - calc[2]) * (calc[3] - calc[2])); } } cout << ans << endl; } }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define LOG(...) printf(__VA_ARGS__) //#define LOG(...) #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) #define RSORT(c) sort((c).rbegin(), (c).rend()) #define CLR(a) memset((a), 0, sizeof(a)) typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; int main() { int x, y; while (cin >> x >> y, x) { vector<int> xv; vector<int> yv; for (int i = 1; i <= sqrt(x); i++) { if (x % i == 0) xv.push_back(i); } for (int i = 1; i <= sqrt(y); i++) { if (y % i == 0) yv.push_back(i); } vector<int> calc(4); int ans = 1000000007; REP(i, xv.size()) { REP(j, yv.size()) { calc[0] = xv[i]; calc[1] = x / xv[i]; calc[2] = yv[j]; calc[3] = y / yv[j]; SORT(calc); ans = min(ans, (calc[0] - calc[1]) * (calc[0] - calc[1]) + (calc[2] - calc[1]) * (calc[2] - calc[1]) + (calc[3] - calc[2]) * (calc[3] - calc[2])); } } cout << ans << endl; } }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
1
483
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> // #include <bits/stdc++.h> using namespace std; #define LOG(...) fprintf(stderr, __VA_ARGS__) //#define LOG(...) #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort(ALL(c)) #define RSORT(c) sort(RALL(c)) #define SQ(n) (n) * (n) typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; int main() { int a, b; int minv; vi l(4); while (cin >> a >> b, a | b) { minv = (int)1e9; int ra = sqrt(a); int rb = sqrt(b); FOR(i, 1, ra) { if (a % i != 0) continue; FOR(j, 1, rb) { if (b % j != 0) continue; l[0] = i; l[1] = a / i; l[2] = j; l[3] = b / j; SORT(l); int v = SQ(l[0] - l[1]) + SQ(l[1] - l[2]) + SQ(l[2] - l[3]); minv = min(v, minv); } } cout << minv << endl; } }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> // #include <bits/stdc++.h> using namespace std; #define LOG(...) fprintf(stderr, __VA_ARGS__) //#define LOG(...) #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort(ALL(c)) #define RSORT(c) sort(RALL(c)) #define SQ(n) (n) * (n) typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; int main() { int a, b; int minv; vi l(4); while (cin >> a >> b, a | b) { minv = (int)1e9; int ra = sqrt(a) + 1; int rb = sqrt(b) + 1; FOR(i, 1, ra) { if (a % i != 0) continue; FOR(j, 1, rb) { if (b % j != 0) continue; l[0] = i; l[1] = a / i; l[2] = j; l[3] = b / j; SORT(l); int v = SQ(l[0] - l[1]) + SQ(l[1] - l[2]) + SQ(l[2] - l[3]); minv = min(v, minv); } } cout << minv << endl; } }
[["+", 8, 9, 0, 43, 49, 50, 51, 16, 17, 72], ["+", 8, 9, 0, 43, 49, 50, 51, 16, 12, 13]]
1
366
#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) { vi a, b; for (int i = 2; i * i <= n; i++) if (n % i == 0) a.pb(i); for (int i = 2; i * i <= m; i++) if (m % i == 0) b.pb(i); int mi = inf; rep(i, a.size()) rep(j, b.size()) { vi t; t.pb(a[i]); t.pb(n / a[i]); t.pb(b[j]); t.pb(m / b[j]); sort(all(t)); int c = t[1] - t[0]; int d = t[2] - t[1]; int e = t[3] - t[2]; mi = min(mi, c * c + d * d + e * e); } cout << mi << endl; } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define mp make_pair #define all(in) in.begin(), in.end() const double PI = acos(-1); const double EPS = 1e-10; const int inf = 1e9; using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; int main() { int n, m; while (cin >> n >> m, n + m) { vi a, b; for (int i = 1; i * i <= n; i++) if (n % i == 0) a.pb(i); for (int i = 1; i * i <= m; i++) if (m % i == 0) b.pb(i); int mi = inf; rep(i, a.size()) rep(j, b.size()) { vi t; t.pb(a[i]); t.pb(n / a[i]); t.pb(b[j]); t.pb(m / b[j]); sort(all(t)); int c = t[1] - t[0]; int d = t[2] - t[1]; int e = t[3] - t[2]; mi = min(mi, c * c + d * d + e * e); } cout << mi << endl; } }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
345
#include <algorithm> #include <stdio.h> int a, b; int main() { while (scanf("%d%d", &a, &b), a + b) { int ret = 999999999; for (int i = 1; i * i <= a; i++) { if (a % i) continue; for (int j = 1; j * j <= b; j++) { if (b % j) continue; int p[4] = {i, a / i, j, b / j}; std::sort(p, p + 4); int res = 0; for (int k = 0; k < 2; k++) res += (p[k + 1] - p[k]) * (p[k + 1] - p[k]); ret = std::min(ret, res); } } printf("%d\n", ret); } }
#include <algorithm> #include <stdio.h> int a, b; int main() { while (scanf("%d%d", &a, &b), a + b) { int ret = 999999999; for (int i = 1; i * i <= a; i++) { if (a % i) continue; for (int j = 1; j * j <= b; j++) { if (b % j) continue; int p[4] = {i, a / i, j, b / j}; std::sort(p, p + 4); int res = 0; for (int k = 0; k < 3; k++) res += (p[k + 1] - p[k]) * (p[k + 1] - p[k]); ret = std::min(ret, res); } } printf("%d\n", ret); } }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13]]
1
194
#include <algorithm> #include <iostream> #define sq(a) ((a) * (a)) #define INFTY (1 << 30) using namespace std; int main() { while (true) { int ans = INFTY; int s[4]; int a, b; scanf("%d%d", &a, &b); if (a == 0 && b == 0) break; for (int i = 1; i < a; ++i) { if (a % i == 0) { for (int j = 1; j < b; ++j) { if (b % j == 0) { s[0] = i; s[1] = a / i; s[2] = j; s[3] = b / j; sort(s, s + 4); int sum = sq(s[1] - s[0]) + sq(s[2] - s[1]) + sq(s[3] - s[2]); ans = min(ans, sum); } } } } printf("%d\n", ans); } }
#include <algorithm> #include <iostream> #define sq(a) ((a) * (a)) #define INFTY (1 << 30) using namespace std; int main() { while (true) { int ans = INFTY; int s[4]; int a, b; scanf("%d%d", &a, &b); if (a == 0 && b == 0) break; for (int i = 1; i <= a; ++i) { if (a % i == 0) { for (int j = 1; j <= b; ++j) { if (b % j == 0) { s[0] = i; s[1] = a / i; s[2] = j; s[3] = b / j; sort(s, s + 4); int sum = sq(s[3] - s[2]) + sq(s[2] - s[1]) + sq(s[1] - s[0]); /*for(int k = 0; k < 4; ++k){ for(int l = 0; l < 4; ++l){ if( k == l )continue; for(int m = 0; m < 4; ++m){ if( l == m || k == m ) continue; for(int n = 0; n < 4; ++n){ if( l == n || k == n || m == n )continue; int sum = sq(s[k]-s[l]) + sq(s[l]-s[m]) + sq(s[m]-s[n]); ans = min(ans, sum); } } } }*/ ans = min(ans, sum); } } } } printf("%d\n", ans); } }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 0, 57, 64, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 57, 64, 9, 0, 7, 15, 16, 17, 19], ["-", 3, 4, 0, 16, 31, 69, 341, 342, 0, 13], ["+", 3, 4, 0, 16, 31, 69, 341, 342, 0, 13], ["-", 3, 4, 0, 16, 12, 69, 341, 342, 0, 13], ["+", 3, 4, 0, 16, 12, 69, 341, 342, 0, 13]]
1
224
#include <algorithm> #include <iostream> #include <vector> using namespace std; const int inf = 999999; int func(int a[4]) { int t = 0; for (int i = 0; i < 3; i++) { t += ((a[i + 1] - a[i]) * (a[i + 1] - a[i])); } return t; } int main() { int n, m; while (cin >> n >> m && n && m) { vector<int> vn, vm; for (int i = 1; i < n; i++) { if (n % i == 0) { vn.push_back(i); } } for (int i = 1; i < m; i++) { if (m % i == 0) { vm.push_back(i); } } int s = inf; for (int i = 0; i < vn.size(); i++) { for (int j = 0; j < vm.size(); j++) { int a[4]; a[0] = vn[i]; a[1] = n / vn[i]; a[2] = vm[j]; a[3] = m / vm[j]; sort(a, a + 4); s = min(s, func(a)); } } cout << s << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; const int inf = 99999999; int func(int a[4]) { int t = 0; for (int i = 0; i < 3; i++) { t += ((a[i + 1] - a[i]) * (a[i + 1] - a[i])); } return t; } int main() { int n, m; while (cin >> n >> m && n && m) { vector<int> vn, vm; for (int i = 1; i <= n; i++) { if (n % i == 0) { vn.push_back(i); } } for (int i = 1; i <= m; i++) { if (m % i == 0) { vm.push_back(i); } } int s = inf; for (int i = 0; i < vn.size(); i++) { for (int j = 0; j < vm.size(); j++) { int a[4]; a[0] = vn[i]; a[1] = n / vn[i]; a[2] = vm[j]; a[3] = m / vm[j]; sort(a, a + 4); s = min(s, func(a)); } } cout << s << endl; } return 0; }
[["-", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19]]
1
307
#include <algorithm> #include <iostream> using namespace std; const int INF = 1000000000; main() { int a, b; while (cin >> a >> b && (a | b)) { int ans = INF; for (int i = 1; i + i <= a; i++) { if (a % i != 0) continue; for (int j = 1; j + j <= b; j++) { if (b % j != 0) continue; int tmp[4] = {i, a / i, j, b / j}; sort(tmp, tmp + 4); int sum = 0; for (int i = 1; i < 4; i++) { sum += (tmp[i] - tmp[i - 1]) * (tmp[i] - tmp[i - 1]); } ans = min(ans, sum); } } cout << ans << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; const int INF = 1000000000; main() { int a, b; while (cin >> a >> b && (a | b)) { int ans = INF; for (int i = 1; i <= a; i++) { if (a % i != 0) continue; for (int j = 1; j <= b; j++) { if (b % j != 0) continue; int tmp[4] = {i, a / i, j, b / j}; sort(tmp, tmp + 4); int sum = 0; for (int i = 1; i < 4; i++) { sum += (tmp[i] - tmp[i - 1]) * (tmp[i] - tmp[i - 1]); } ans = min(ans, sum); } } cout << ans << endl; } return 0; }
[["-", 8, 9, 0, 7, 15, 16, 31, 16, 17, 72], ["-", 8, 9, 0, 7, 15, 16, 31, 16, 12, 22], ["-", 8, 9, 0, 7, 15, 16, 31, 16, 31, 22]]
1
199
#!/usr/bin/ruby while(a,b=gets.split.map(&:to_i))!=[0,0]do r=99999999 2.step(Math.sqrt(a)){|i| i=i.to_i if a%i>0 then next end 2.step(Math.sqrt(b)){|j| j=j.to_i if b%j>0 then next end x=[i,a/i,j,b/j].sort p=(x[3]-x[2])**2+(x[2]-x[1])**2+(x[1]-x[0])**2 if r>p then r=p end } } p r end
#!/usr/bin/ruby while(a,b=gets.split.map(&:to_i))!=[0,0]do r=99999999 1.step(Math.sqrt(a)){|i| i=i.to_i if a%i>0 then next end 1.step(Math.sqrt(b)){|j| j=j.to_i if b%j>0 then next end x=[i,a/i,j,b/j].sort p=(x[3]-x[2])**2+(x[2]-x[1])**2+(x[1]-x[0])**2 if r>p then r=p end } } p r end
[["-", 0, 493, 0, 89, 8, 170, 0, 652, 486, 612], ["+", 0, 493, 0, 89, 8, 170, 0, 652, 486, 612], ["-", 15, 652, 3, 4, 0, 772, 0, 773, 0, 6], ["+", 15, 652, 3, 4, 0, 772, 0, 773, 0, 6]]
4
137
#include <iostream> using namespace std; int main() { int n; cin >> n; for (int z = 0; z < n; z++) { int n2; cin >> n2; int a[200]; for (int i = 0; i < n2; i++) { cin >> a[i]; } int u = -1000, d = -1000; for (int i = 0; i < n2 - 1; i++) { if (u < a[i + 1] - a[i]) u = a[i + 1] - a[i]; } for (int i = 1; i < n2; i++) { if (d < a[i - 1] - a[i]) d = a[i - 1] - a[i]; } cout << u << " " << d << endl; } }
#include <iostream> using namespace std; int main() { int n; cin >> n; for (int z = 0; z < n; z++) { int n2; cin >> n2; int a[200]; for (int i = 0; i < n2; i++) { cin >> a[i]; } int u = 0, d = 0; for (int i = 0; i < n2 - 1; i++) { if (u < a[i + 1] - a[i]) u = a[i + 1] - a[i]; } for (int i = 1; i < n2; i++) { if (d < a[i - 1] - a[i]) d = a[i - 1] - a[i]; } cout << u << " " << d << endl; } }
[["-", 0, 7, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 7, 8, 9, 0, 43, 49, 50, 51, 13]]
1
186
#include <iostream> using namespace std; int main() { int n, are, h1, h2, d, u; d = u = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> are >> h1; for (int j = 1; j < are; j++) { cin >> h2; if (h1 < h2) { if (u < h2 - h1) u = h2 - h1; } else if (h1 > h2) { if (d < h1 - h2) d = h1 - h2; } h1 = h2; } cout << u << " " << d << endl; } return 0; }
#include <iostream> using namespace std; int main() { int n, are, h1, h2, d, u; d = u = 0; cin >> n; for (int i = 0; i < n; i++) { d = u = 0; cin >> are >> h1; for (int j = 1; j < are; j++) { cin >> h2; if (h1 < h2) { if (u < h2 - h1) u = h2 - h1; } else if (h1 > h2) { if (d < h1 - h2) d = h1 - h2; } h1 = h2; } cout << u << " " << d << endl; } return 0; }
[["+", 0, 7, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 8, 9, 0, 1, 0, 11, 12, 11, 31, 22], ["+", 8, 9, 0, 1, 0, 11, 12, 11, 17, 32], ["+", 8, 9, 0, 1, 0, 11, 12, 11, 12, 13], ["+", 8, 9, 0, 7, 8, 9, 0, 1, 0, 35]]
1
141
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int a[1000], q; int mx = 0; int mi = 0; cin >> q; for (int f = 0; f < q; f++) { cin >> a[q]; if (f >= 1) { mx = max((a[q] - a[q - 1]), mx); mi = min((a[q] - a[q - 1]), mi); } } cout << mx << " " << -1 * mi << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int a[1000], q; int mx = 0; int mi = 0; cin >> q; for (int f = 0; f < q; f++) { cin >> a[f]; if (f >= 1) { mx = max((a[f] - a[f - 1]), mx); mi = min((a[f] - a[f - 1]), mi); } } cout << mx << " " << -1 * mi << endl; } return 0; }
[["-", 0, 1, 0, 16, 12, 69, 341, 342, 0, 22], ["+", 0, 1, 0, 16, 12, 69, 341, 342, 0, 22], ["-", 0, 23, 0, 16, 31, 69, 341, 342, 0, 22], ["+", 0, 23, 0, 16, 31, 69, 341, 342, 0, 22], ["-", 0, 16, 12, 69, 341, 342, 0, 16, 31, 22], ["+", 0, 16, 12, 69, 341, 342, 0, 16, 31, 22]]
1
147
#include <bits/stdc++.h> using namespace std; int main() { int n, t, tt; cin >> tt; for (int j = 0; j < tt; j++) { int cu = 0, cd = 0, m = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> t; if (t - m > cu) cu = t - m; if (m - t > cd) cd = m - t; m = t; } cout << cu << " " << cd << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, t, tt; cin >> tt; for (int j = 0; j < tt; j++) { int cu = 0, cd = 0, m = 0; cin >> n; cin >> m; for (int i = 1; i < n; i++) { cin >> t; if (t - m > cu) cu = t - m; if (m - t > cd) cd = m - t; m = t; } cout << cu << " " << cd << endl; } return 0; }
[["+", 0, 7, 8, 9, 0, 1, 0, 16, 31, 22], ["+", 0, 7, 8, 9, 0, 1, 0, 16, 17, 152], ["+", 0, 7, 8, 9, 0, 1, 0, 16, 12, 22], ["+", 8, 9, 0, 7, 8, 9, 0, 1, 0, 35], ["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
123
#include <cstdio> #include <iostream> #include <string> using namespace std; int main() { int i, j, n, t, in[2], max, min; cin >> t; for (i = 0; i < t; i++) { cin >> n; cin >> in[1]; max = min = 0; for (j = 1; j < n; j++) { in[0] = in[1]; cin >> in[1]; if (in[1] > in[0]) { if (max < in[1] - in[0]) max = in[1] - in[0]; } else if (min == 0 || min > in[0] - in[1]) min = in[0] - in[1]; } cout << max << " " << min << endl; } return 0; }
#include <cstdio> #include <iostream> #include <string> using namespace std; int main() { int i, j, n, t, in[2], max, min; cin >> t; for (i = 0; i < t; i++) { cin >> n; cin >> in[1]; max = min = 0; for (j = 1; j < n; j++) { in[0] = in[1]; cin >> in[1]; if (in[1] > in[0]) { if (max < in[1] - in[0]) max = in[1] - in[0]; } else if (min == 0 || min < in[0] - in[1]) min = in[0] - in[1]; } cout << max << " " << min << endl; } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 47], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 18]]
1
188
/* * 2100.cpp * * Created on: 2012/07/08 * Author: shimomire */ #include <algorithm> #include <complex> #include <float.h> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <string> #include <vector> using namespace std; #define REP(i, b, n) for (int i = b; i < n; i++) #define REP0(i, n) REP(i, 0, n) #define REPr(i, b, n) for (int i = n - 1; i >= b; i--) #define REPr0(i, n) REPr(i, 0, n) #define foreach(it, o) \ for (__typeof((o).begin()) it = (o).begin(); it != (o).end(); ++it) #define ALL(o) (o).begin(), (o).end() typedef complex<double> P; bool operator<(const P &a, const P &b) { return abs(a) < abs(b); } int datanum, n; int main() { cin >> datanum; REP0(datac, datanum) { cin >> n; int data[n]; int sub[n]; REP0(i, n) { cin >> data[i]; } REP0(i, n - 1) { sub[i] = data[i + 1] - data[i]; } sort(sub, sub + n); cout << max(sub[n - 2], 0) << " " << max(-sub[0], 0) << endl; } }
/* * 2100.cpp * * Created on: 2012/07/08 * Author: shimomire */ #include <algorithm> #include <complex> #include <float.h> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <string> #include <vector> using namespace std; #define REP(i, b, n) for (int i = b; i < n; i++) #define REP0(i, n) REP(i, 0, n) #define REPr(i, b, n) for (int i = n - 1; i >= b; i--) #define REPr0(i, n) REPr(i, 0, n) #define foreach(it, o) \ for (__typeof((o).begin()) it = (o).begin(); it != (o).end(); ++it) #define ALL(o) (o).begin(), (o).end() typedef complex<double> P; bool operator<(const P &a, const P &b) { return abs(a) < abs(b); } int datanum, n; int main() { cin >> datanum; REP0(datac, datanum) { cin >> n; int data[n]; int sub[n]; REP0(i, n) { cin >> data[i]; } REP0(i, n - 1) { sub[i] = data[i + 1] - data[i]; } sort(sub, sub + n - 1); cout << max(sub[n - 2], 0) << " " << max(-sub[0], 0) << endl; } }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 33], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
232
#include <iostream> using namespace std; int main() { int to; cin >> to; for (int oo = 0; oo < to; oo++) { int n; cin >> n; int h[n], ud, upmax = 0, downmax = 0; for (int i = 0; i < n; i++) { cin >> h[i]; } for (int j = 0; j < n - 1; j++) { if (h[j] > h[j + 1]) { ud = h[j] - h[j + 1]; if (upmax < ud) { downmax = ud; } } else if (h[j] < h[j + 1]) { ud = h[j + 1] - h[j]; if (upmax < ud) { upmax = ud; } } } cout << upmax << " " << downmax << endl; } return 0; }
#include <iostream> using namespace std; int main() { int to; cin >> to; for (int oo = 0; oo < to; oo++) { int n; cin >> n; int h[n], ud, upmax = 0, downmax = 0; for (int i = 0; i < n; i++) { cin >> h[i]; } for (int j = 0; j < n - 1; j++) { if (h[j] > h[j + 1]) { ud = h[j] - h[j + 1]; if (downmax < ud) { downmax = ud; } } else if (h[j] < h[j + 1]) { ud = h[j + 1] - h[j]; if (upmax < ud) { upmax = ud; } } } cout << upmax << " " << downmax << endl; } return 0; }
[["-", 64, 9, 0, 57, 15, 339, 51, 16, 31, 22], ["+", 64, 9, 0, 57, 15, 339, 51, 16, 31, 22]]
1
199
#include <stdio.h> int max(int x, int y) { return x > y ? x : y; } int min(int x, int y) { return x < y ? x : y; } int main(void) { int t, n, h[101], i, up = 0, down = 0; scanf("%d", &t); while (t--) { scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &h[i]); for (i = 0; i < n - 1; i++) { up = max(up, h[i + 1] - h[i]); down = min(down, h[i + 1] - h[i]); } printf("%d %d\n", up, -down); } return 0; }
#include <stdio.h> int max(int x, int y) { return x > y ? x : y; } int min(int x, int y) { return x < y ? x : y; } int main(void) { int t, n, h[101], i, up = 0, down = 0; scanf("%d", &t); while (t--) { scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &h[i]); for (i = up = down = 0; i < n - 1; i++) { up = max(up, h[i + 1] - h[i]); down = min(down, h[i + 1] - h[i]); } printf("%d %d\n", up, -down); } return 0; }
[["+", 8, 9, 0, 7, 10, 11, 12, 11, 31, 22], ["+", 8, 9, 0, 7, 10, 11, 12, 11, 17, 32], ["+", 0, 7, 10, 11, 12, 11, 12, 11, 31, 22], ["+", 0, 7, 10, 11, 12, 11, 12, 11, 17, 32]]
1
193
#include <iostream> using namespace std; int main() { int num = 0, count = 0, h = 0, hh = 0, m = 0, n = 0, k = 0, s = 0; cin >> num; for (int i = 0; i < num; i++) { cin >> count; for (int j = 0; j < count; j++) { cin >> h; if (j == 0) { s = h; } else s = hh; hh = h; m = hh - s; if (m <= 0 && n >= m) { n = -m; } else if (m >= 0 && k <= m) { k = m; // cout << m << endl; } } cout << k << " " << n << endl; m = 0; n = 0; k = 0; s = 0; hh = 0; h = 0; } return 0; }
#include <iostream> using namespace std; int main() { int num = 0, count = 0, h = 0, hh = 0, m = 0, n = 0, k = 0, s = 0; cin >> num; for (int i = 0; i < num; i++) { cin >> count; for (int j = 0; j < count; j++) { cin >> h; if (j == 0) s = h; else s = hh; hh = h; m = hh - s; if (m <= 0 && -n >= m) { n = -m; // cout << n << endl; } else if (m >= 0 && k <= m) { k = m; } } cout << k << " " << n << endl; m = 0; n = 0; k = 0; s = 0; hh = 0; h = 0; } return 0; }
[["-", 0, 7, 8, 9, 0, 57, 64, 9, 0, 45], ["-", 0, 7, 8, 9, 0, 57, 64, 9, 0, 46], ["+", 15, 339, 51, 16, 12, 16, 31, 91, 17, 33]]
1
190
import java.util.*; class Main { public static void main(String[] z) { Scanner s = new Scanner(System.in); for (int n, c, i, j; (n = s.nextInt()) > 0; System.out.println(((n -= c) < 0 ? "deficient" : n > 0 ? "abundant" : "perfect") + " number")) for (c = n > 1 ? 1 : 0, i = 2, j = n; i < j; i++) c += n % i < 1 ? i + (i != (j = n / i) ? j : 0) : 0; } }
import java.util.*; class Main { public static void main(String[] z) { Scanner s = new Scanner(System.in); for (int n, c, i, j; (n = s.nextInt()) > 0; System.out.println(((n -= c) > 0 ? "deficient" : n < 0 ? "abundant" : "perfect") + " number")) for (c = n > 1 ? 1 : 0, i = 2, j = n; i < j; i++) c += n % i < 1 ? i + (i != (j = n / i) ? j : 0) : 0; } }
[["-", 0, 16, 31, 23, 0, 510, 15, 16, 17, 18], ["+", 0, 16, 31, 23, 0, 510, 15, 16, 17, 47], ["-", 31, 23, 0, 510, 75, 510, 15, 16, 17, 47], ["+", 31, 23, 0, 510, 75, 510, 15, 16, 17, 18]]
3
149
#include <stdio.h> int main(void) { int n; int i, j, num; while (scanf("%d", &n) && n != 0) { num = 0; for (i = 1; i * i < n; i++) { if (n % i == 0) num = num + i + n / i; } num = -n; if (num == n) { printf("perfect number\n"); } else if (num > n) { printf("abundant number\n"); } else { printf("deficient number\n"); } } return 0; }
#include <stdio.h> int main(void) { int n; int i, j, num; while (scanf("%d", &n) && n != 0) { num = 0; for (i = 1; i * i < n; i++) { if (n % i == 0) num = num + i + n / i; } num -= n; if (num == n) { printf("perfect number\n"); } else if (num > n) { printf("abundant number\n"); } else { printf("deficient number\n"); } } return 0; }
[["-", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["-", 8, 9, 0, 1, 0, 11, 12, 91, 17, 33], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 110]]
0
128
#include <stdio.h> int main(void) { int n[100]; int i, j, s, t, u, v, w, x; int sum = 0; i = 0; do { scanf("%d", &n[i]); i++; } while (n[i - 1] != 0); for (i = 0; n[i] != 0; i++) { s = n[i]; t = n[i] / 6 + 1; sum += 1; for (j = 2; j < t; j++) { if (s % j == 0) sum += j; } if (n[i] % 5 == 0) { x = n[i] / 5; if (s % x == 0) sum += x; } if (n[i] % 4 == 0) { w = n[i] / 4; if (s % w == 0) sum += w; } if (n[i] % 3 == 0) { u = n[i] / 3; if (s % u == 0) sum += u; } if (n[i] % 2 == 0) { v = n[i] / 2; if (s % v == 0) sum += v; } if (sum == n[i]) printf("perfect number\n"); else if (sum > n[i]) printf("abundant number\n"); else if (sum < n[i]) printf("deficient number\n"); sum = 0; } return 0; }
#include <stdio.h> int main(void) { int n[100]; int i, j, s, t, u, v, w, x; int sum = 0; i = 0; do { scanf("%d", &n[i]); i++; } while (n[i - 1] != 0); for (i = 0; n[i] != 0; i++) { s = n[i]; t = n[i] / 6 + 1; for (j = 1; j < t; j++) { if (s % j == 0) sum += j; } if (n[i] % 5 == 0) { x = n[i] / 5; if (s % x == 0) sum += x; } if (n[i] % 4 == 0) { w = n[i] / 4; if (s % w == 0) sum += w; } if (n[i] % 3 == 0) { u = n[i] / 3; if (s % u == 0) sum += u; } if (n[i] % 2 == 0) { v = n[i] / 2; if (s % v == 0) sum += v; } if (sum == n[i]) printf("perfect number\n"); else if (sum > n[i]) printf("abundant number\n"); else if (sum < n[i]) printf("deficient number\n"); sum = 0; } return 0; }
[["-", 0, 7, 8, 9, 0, 1, 0, 11, 31, 22], ["-", 0, 7, 8, 9, 0, 1, 0, 11, 17, 107], ["-", 0, 7, 8, 9, 0, 1, 0, 11, 12, 13], ["-", 8, 9, 0, 7, 8, 9, 0, 1, 0, 35], ["-", 0, 7, 8, 9, 0, 7, 10, 11, 12, 13], ["+", 0, 7, 8, 9, 0, 7, 10, 11, 12, 13]]
0
335
#include <stdio.h> int main() { int n, sum, i; sum = 0; while (1) { scanf("%d", &n); if (n == 0) break; for (i = 1; i * i <= n; i++) { if (n % i == 0) { if (i == 1) sum = 1; else if (n == i * i) sum += i; else sum += (i + (n / i)); } } if (n == 1) sum = 0; if (n == sum) printf("perfect number\n"); if (n > sum) printf("deficient number\n"); if (n < sum) printf("abundant numbe\n"); } return 0; }
#include <stdio.h> int main() { int n, sum, i; sum = 0; while (1) { scanf("%d", &n); if (n == 0) break; for (i = 1; i * i <= n; i++) { if (n % i == 0) { if (i == 1) sum = 1; else if (n == i * i) sum += i; else sum += (i + (n / i)); } } if (n == 1) sum = 0; if (n == sum) printf("perfect number\n"); if (n > sum) printf("deficient number\n"); if (n < sum) printf("abundant number\n"); } return 0; }
[["-", 64, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 64, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
0
161
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n, n) { int ans = 1; for (int i = 0; i * i < n; i++) { if (n % i == 0) { ans += i; if (n / i != i) ans += n / i; } } if (n != 1 && ans < n) { cout << "deficient number" << endl; } else if (ans > n) { cout << "abundant number" << endl; } else { cout << "perfect number" << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n, n) { int ans = 1; for (int i = 2; i * i < n; i++) { if (n % i == 0) { ans += i; if (n / i != i) ans += n / i; } } if (n != 1 && ans == n) { cout << "perfect number" << endl; } else if (ans > n) { cout << "abundant number" << endl; } else { cout << "deficient number" << endl; } } return 0; }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 18], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 60], ["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
127
#include <iostream> using namespace std; int sun_factors(int n) { if (n <= 1) return 0; int i, s = 1; for (i = 2; i * i < n; i++) if (n % i == 0) s += i + n / i; if (i * i == n && n % i == 0) s += i; return s; } int main() { int n; while (cin >> n && n) { int s = sun_factors(n); if (s < n) cout << "deficient number" << endl; else if (s == n) cout << "perfect number" << endl; else cout << "abundant numberr" << endl; } return 0; }
#include <iostream> using namespace std; int sun_factors(int n) { if (n <= 1) return 0; int i, s = 1; for (i = 2; i * i < n; i++) if (n % i == 0) s += i + n / i; if (i * i == n && n % i == 0) s += i; return s; } int main() { int n; while (cin >> n && n) { int s = sun_factors(n); if (s < n) cout << "deficient number" << endl; else if (s == n) cout << "perfect number" << endl; else cout << "abundant number" << endl; } return 0; }
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
150
#include <algorithm> #include <iostream> using namespace std; int main() { int n, i; while (cin >> n, n) { int s = -n; for (i = 1; i * i <= n; i++) { if (n % i == 0) s += i + n / 1; } i--; if (i * i == n) s -= i; cout << (s == n ? "perfect" : s > n ? "abundant" : "deficient") << " number" << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; int main() { int n, i; while (cin >> n, n) { int s = -n; for (i = 1; i * i <= n; i++) { if (n % i == 0) s += i + n / i; } i--; if (i * i == n) s -= i; cout << (s == n ? "perfect" : s > n ? "abundant" : "deficient") << " number" << endl; } return 0; }
[["-", 64, 1, 0, 11, 12, 16, 12, 16, 12, 13], ["+", 64, 1, 0, 11, 12, 16, 12, 16, 12, 22]]
1
116
#include <stdio.h> #pragma warning(disable : 4996) int n; int main() { while (true) { scanf("%d", &n); if (n == 0) break; int ret = 1; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { if (i * i != n) { ret += i + n / i; } else { ret += i; } } } if (n > ret) { printf("deficient number\n"); } else if (n == ret) { printf("perfect number\n"); } else { printf("abundant number\n"); } } return 0; }
#include <stdio.h> #pragma warning(disable : 4996) int n; int main() { while (true) { scanf("%d", &n); if (n == 0) break; int ret = n > 1 ? 1 : 0; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { if (i * i != n) { ret += i + n / i; } else { ret += i; } } } if (n > ret) { printf("deficient number\n"); } else if (n == ret) { printf("perfect number\n"); } else { printf("abundant number\n"); } } return 0; }
[["+", 0, 43, 49, 50, 51, 41, 15, 16, 31, 22], ["+", 0, 43, 49, 50, 51, 41, 15, 16, 17, 47], ["+", 0, 43, 49, 50, 51, 41, 15, 16, 12, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 41, 0, 101], ["+", 8, 9, 0, 43, 49, 50, 51, 41, 0, 102], ["+", 8, 9, 0, 43, 49, 50, 51, 41, 75, 13]]
1
142
#include <bits/stdc++.h> #define rep(i, a, n) for (int i = a; i < n; i++) #define repb(i, a, b) for (int i = a; i >= b; i--) #define all(a) a.begin(), a.end() #define o(a) cout << a << endl #define int long long #define fi first #define se second using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; int calc(int n) { int res = 0; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { res += i; if (i * i != n) res += n / i; } } return res - n; } signed main() { int n; while (cin >> n, n) { int sum = calc(n); // o(sum); if (sum == n) cout << "perfect number" << endl; if (sum < n) cout << "dificient number" << endl; if (sum > n) cout << "abundant number" << endl; } }
#include <bits/stdc++.h> #define rep(i, a, n) for (int i = a; i < n; i++) #define repb(i, a, b) for (int i = a; i >= b; i--) #define all(a) a.begin(), a.end() #define o(a) cout << a << endl #define int long long #define fi first #define se second using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; int calc(int n) { int res = 0; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { res += i; if (i * i != n) res += n / i; } } return res - n; } signed main() { int n; while (cin >> n, n) { int sum = calc(n); // o(sum); if (sum == n) cout << "perfect number" << endl; if (sum < n) cout << "deficient number" << endl; if (sum > n) cout << "abundant number" << endl; } }
[["-", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
204
#include <bits/stdc++.h> #define r(i, n) for (int i = 1; i < n; i++) using namespace std; int main() { long long n, p, a, b; while (cin >> n, n) { long long s = 1; if (n == 1) s--; for (int i = 2; i < n / 2; i++) if (n % i == 0) s += i + n / i; if (n < s) cout << "abundant number" << endl; else if (n > s) cout << "deficient number" << endl; else cout << "perfect number" << endl; } }
#include <bits/stdc++.h> #define r(i, n) for (int i = 1; i < n; i++) using namespace std; int main() { long long n, p, a, b; while (cin >> n, n) { long long s = 1; if (n == 1) s--; for (int i = 2; i * i < n; i++) if (n % i == 0) s += i + n / i; if (n < s) cout << "abundant number" << endl; else if (n > s) cout << "deficient number" << endl; else cout << "perfect number" << endl; } }
[["+", 8, 9, 0, 7, 15, 16, 31, 16, 17, 48], ["+", 8, 9, 0, 7, 15, 16, 31, 16, 12, 22], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 17, 85], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
125
#include <algorithm> #include <cmath> #include <cstdio> #include <vector> using namespace std; static const int M = 31623; int main() { vector<int> primes; primes.push_back(2); for (int i = 3; i <= M; i += 2) { bool f = true; int r = (int)sqrt(i); for (int j = 0; primes[j] <= r; ++j) { if (i % primes[j] == 0) { f = false; break; } } if (f) { primes.push_back(i); } } int N; while (scanf("%d", &N), N) { int rest = N; long long sum = 1; for (int i = 0; i < (int)primes.size(); ++i) { int p = primes[i]; if (rest % p == 0) { long long term = 1; long long powp = p; while (rest % p == 0) { rest /= p; term += powp; powp *= p; if (term > 2 * N) goto abundant; } sum *= term; if (sum > 2 * N) goto abundant; } } if (rest > 1) { sum += 1 + rest; } sum -= N; if (N == sum) { puts("perfect number"); } else if (N < sum) { abundant: puts("abundant number"); } else { puts("deficient number"); } } }
#include <algorithm> #include <cmath> #include <cstdio> #include <vector> using namespace std; static const int M = 31623; int main() { vector<int> primes; primes.push_back(2); for (int i = 3; i <= M; i += 2) { bool f = true; int r = (int)sqrt(i); for (int j = 0; primes[j] <= r; ++j) { if (i % primes[j] == 0) { f = false; break; } } if (f) { primes.push_back(i); } } int N; while (scanf("%d", &N), N) { int rest = N; long long sum = 1; for (int i = 0; i < (int)primes.size(); ++i) { int p = primes[i]; if (rest % p == 0) { long long term = 1; long long powp = p; while (rest % p == 0) { rest /= p; term += powp; powp *= p; if (term > 2 * N) goto abundant; } sum *= term; if (sum > 2 * N) goto abundant; } } if (rest > 1) { sum *= 1 + rest; } sum -= N; if (N == sum) { puts("perfect number"); } else if (N < sum) { abundant: puts("abundant number"); } else { puts("deficient number"); } } }
[["-", 0, 57, 64, 9, 0, 1, 0, 11, 17, 107], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 17, 108]]
1
314
#include <iostream> using namespace std; int main() { long long int n; while (cin >> n, n) { long long int total = -n; for (long long int i = 1; i * i <= n; i++) { if (n % i == 0) { total += i + i / n; if (i == n / i) { total -= i; } } } if (total < n) { cout << "deficient number" << endl; ; } else if (total > n) { cout << "abundant number" << endl; } else { cout << "perfect number" << endl; } } return 0; }
#include <iostream> using namespace std; int main() { long long int n; while (cin >> n, n) { long long int total = -n; for (long long int i = 1; i * i <= n; i++) { if (n % i == 0) { total += i + n / i; if (i == n / i) { total -= i; } } } if (total < n) { cout << "deficient number" << endl; ; } else if (total > n) { cout << "abundant number" << endl; } else { cout << "perfect number" << endl; } } return 0; }
[["-", 0, 1, 0, 11, 12, 16, 12, 16, 31, 22], ["-", 0, 1, 0, 11, 12, 16, 12, 16, 17, 85], ["+", 0, 1, 0, 11, 12, 16, 12, 16, 17, 85], ["+", 0, 1, 0, 11, 12, 16, 12, 16, 12, 22]]
1
135
#include <cmath> #include <cstdio> int yaksu(int a) { if (a == 1) return 1; int res = a; for (int i = sqrt(a); i > 0; i--) { if (a % i == 0) { res -= i; if (i != a / i && i != 1) res = res - a / i; } if (res < 0) break; } return res; } int main() { int a, tk; while (1) { scanf("%d", &a); if (a == 0) return 0; tk = yaksu(a); if (tk > 0) printf("deficient number"); else if (tk == 0) printf("perfect number"); else printf("abundant number"); } }
#include <cmath> #include <cstdio> int yaksu(int a) { if (a == 1) return 1; int res = a; for (int i = sqrt(a); i > 0; i--) { if (a % i == 0) { res -= i; if (i != a / i && i != 1) res = res - a / i; } if (res < 0) break; } return res; } int main() { int a, tk; while (1) { scanf("%d", &a); if (a == 0) return 0; tk = yaksu(a); if (tk > 0) printf("deficient number\n"); else if (tk == 0) printf("perfect number\n"); else printf("abundant number\n"); } }
[["+", 64, 1, 0, 2, 3, 4, 0, 5, 0, 44], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
1
168
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EACH(t, i, c) for (t::iretator i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) const double EPS = 1e-10; int main() { int n; while (cin >> n, n) { int sum = 1; for (int i = 2; i * i < n; i++) { if (n % i == 0) { sum += i; sum += n / i; } } if (n == 1) { sum = 0; } if (sum == n) { cout << "perfect number" << endl; } else if (sum < n) { cout << "dificient number" << endl; } else { cout << "abunduant number" << endl; } } }
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EACH(t, i, c) for (t::iretator i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) const double EPS = 1e-10; int main() { int n; while (cin >> n, n) { int sum = 1; for (int i = 2; i * i < n; i++) { if (n % i == 0) { sum += i; sum += n / i; } } if (n == 1) { sum = 0; } if (sum == n) { cout << "perfect number" << endl; } else if (sum < n) { cout << "deficient number" << endl; } else { cout << "abundant number" << endl; } } }
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
302
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int main() { int n; while (scanf("%d", &n), n) { int cnt = 0; int x = 0; for (int i = 2; i * i < n; i++) { if (n % i == 0) { cnt += i; x = n / i; cnt += x; } } cnt++; if (n == 1) printf("dificient number\n"); else if (n == cnt) printf("perfect number\n"); else if (n > cnt) printf("deficient number\n"); else printf("abundant number\n"); } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int main() { int n; while (scanf("%d", &n), n) { int cnt = 0; int x = 0; for (int i = 2; i * i < n; i++) { if (n % i == 0) { cnt += i; x = n / i; cnt += x; } } cnt++; if (n == 1) printf("deficient number\n"); else if (n == cnt) printf("perfect number\n"); else if (n > cnt) printf("deficient number\n"); else printf("abundant number\n"); } return 0; }
[["-", 64, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 64, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
146
#include <climits> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <stack> #include <string> #include <vector> using namespace std; #define MAX 100000010 int n; void init() { n = 0; } bool input() { cin >> n; return n; } string solve() { int ans = 1; for (int i = 2; i * i < n + 1; i++) { if (n % i == 0) { ans += i + n / i; if (i == n / i) ans -= i; } } if (n == 0) ans = 0; if (ans == n) { return "perfect number"; } else if (ans < n) { return "deficient number"; } else if (ans > n) { return "abundant number"; } } int main() { while (init(), input()) { cout << solve() << endl; } }
#include <climits> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <stack> #include <string> #include <vector> using namespace std; #define MAX 100000010 int n; void init() { n = 0; } bool input() { cin >> n; return n; } string solve() { int ans = 1; for (int i = 2; i * i < n + 1; i++) { if (n % i == 0) { ans += i + n / i; if (i * i == n) ans -= i; } } if (n == 1) ans = 0; if (ans == n) { return "perfect number"; } else if (ans < n) { return "deficient number"; } else if (ans > n) { return "abundant number"; } } int main() { while (init(), input()) { cout << solve() << endl; } }
[["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 48], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 12, 22], ["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 85], ["-", 0, 57, 15, 339, 51, 16, 12, 16, 12, 22], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
187
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <string> using namespace std; int main() { int n; while (1) { cin >> n; if (n == 0) break; int sum, hoge = n; n < 2 ? sum = 0 : sum = 1; for (int i = 2; i < hoge && sum < n; i++) { if (n % i == 0) { //約数 hoge = n / i; // n = i * hoge if (i != hoge) { sum += i + hoge; } else { sum += i; } } } if (sum == n) cout << "perfect number" << endl; else if (sum < n) cout << "deficient number" << endl; else cout << "abundant numver" << endl; } }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <string> using namespace std; int main() { int n; while (1) { cin >> n; if (n == 0) break; int sum, hoge = n; n < 2 ? sum = 0 : sum = 1; for (int i = 2; i < hoge && sum < n; i++) { if (n % i == 0) { //約数 hoge = n / i; // n = i * hoge if (i != hoge) { sum += i + hoge; } else { sum += i; } } } if (sum == n) cout << "perfect number" << endl; else if (sum < n) cout << "deficient number" << endl; else cout << "abundant number" << endl; } }
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
159
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) #define each(itr, c) \ for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr) #define all(x) (x).begin(), (x).end() #define mp make_pair #define pb push_back #define fi first #define se second const double eps = 1e-10; struct point { double x, y; }; struct line { point a, b; }; double dist(point p, point q) { return sqrt((p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y)); } int main() { int n; while (cin >> n, n) { vector<line> v(n); rep(i, n) cin >> v[i].a.x >> v[i].a.y >> v[i].b.x >> v[i].b.y; int ans = 1; rep(i, n) { vector<point> q; rep(j, i) { //??????????????¨????????? bool exist = true; //?????? point t; //??´???i??¨??´???j?????????????±??????? //??´???i???y?????????????????´??? if (v[i].a.x == v[i].b.x) { //??´???j???y???????????? if (v[j].a.x == v[j].b.x) exist = false; else { //??´???j???????????¨?????? double mj = (v[j].a.y - v[j].b.y) / (v[j].a.x - v[j].b.x); double nj = v[j].a.y - mj * v[j].a.x; //???????¨???? t.x = v[i].a.x; t.y = mj * t.x + nj; } } else { //??´???i???????????¨?????? double mi = (v[i].a.y - v[i].b.y) / (v[i].a.x - v[i].b.x); double ni = v[i].a.y - mi * v[i].a.x; //??´???j???y???????????? if (v[j].a.x == v[j].b.x) { //???????¨???? t.x = v[j].a.x; t.y = mi * t.x + ni; } else { //??´???j???????????¨?????? double mj = (v[j].a.y - v[j].b.y) / (v[j].a.x - v[j].b.x); double nj = v[j].a.y - mj * v[j].a.x; //??????????????§???????????? if (fabs(mi - mj) < eps) exist = false; else { //???????¨???? t.x = (nj - ni) / (mi - mj); t.y = mi * t.x + ni; } } } //??????????????¨????????? if (!exist) continue; //???????????¨?????¢???????????§??????????????? if (fabs(t.x) > 100 + eps && fabs(t.y) > 100 + eps) continue; bool dif = true; //?????§?????????????????¨??°????????????????????????????????? rep(k, q.size()) { if (dist(t, q[k]) < eps) dif = false; } if (dif) q.pb(t); } ans += 1 + q.size(); } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) #define each(itr, c) \ for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr) #define all(x) (x).begin(), (x).end() #define mp make_pair #define pb push_back #define fi first #define se second const double eps = 1e-10; struct point { double x, y; }; struct line { point a, b; }; double dist(point p, point q) { return sqrt((p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y)); } int main() { int n; while (cin >> n, n) { vector<line> v(n); rep(i, n) cin >> v[i].a.x >> v[i].a.y >> v[i].b.x >> v[i].b.y; int ans = 1; rep(i, n) { vector<point> q; rep(j, i) { //??????????????¨????????? bool exist = true; //?????? point t; //??´???i??¨??´???j?????????????±??????? //??´???i???y?????????????????´??? if (v[i].a.x == v[i].b.x) { //??´???j???y???????????? if (v[j].a.x == v[j].b.x) exist = false; else { //??´???j???????????¨?????? double mj = (v[j].a.y - v[j].b.y) / (v[j].a.x - v[j].b.x); double nj = v[j].a.y - mj * v[j].a.x; //???????¨???? t.x = v[i].a.x; t.y = mj * t.x + nj; } } else { //??´???i???????????¨?????? double mi = (v[i].a.y - v[i].b.y) / (v[i].a.x - v[i].b.x); double ni = v[i].a.y - mi * v[i].a.x; //??´???j???y???????????? if (v[j].a.x == v[j].b.x) { //???????¨???? t.x = v[j].a.x; t.y = mi * t.x + ni; } else { //??´???j???????????¨?????? double mj = (v[j].a.y - v[j].b.y) / (v[j].a.x - v[j].b.x); double nj = v[j].a.y - mj * v[j].a.x; //??????????????§???????????? if (fabs(mi - mj) < eps) exist = false; else { //???????¨???? t.x = (nj - ni) / (mi - mj); t.y = mi * t.x + ni; } } } //??????????????¨????????? if (!exist) continue; //???????????¨?????¢???????????§??????????????? if (fabs(t.x) > 100 - eps || fabs(t.y) > 100 - eps) continue; // printf("%d,%d -> %lf,%lf\n",i,j,t.x,t.y); bool dif = true; //?????§?????????????????¨??°????????????????????????????????? rep(k, q.size()) { if (dist(t, q[k]) < eps) dif = false; } if (dif) q.pb(t); } ans += 1 + q.size(); } cout << ans << endl; } return 0; }
[["-", 15, 339, 51, 16, 31, 16, 12, 16, 17, 72], ["+", 15, 339, 51, 16, 31, 16, 12, 16, 17, 33], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 98], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 106], ["-", 15, 339, 51, 16, 12, 16, 12, 16, 17, 72], ["+", 15, 339, 51, 16, 12, 16, 12, 16, 17, 33]]
1
721
import java.util.*; public class Main { public static void main(String[] agrs) { Scanner sc = new Scanner(System.in); IX: while (true) { int n = sc.nextInt(); if (n == 0) break; boolean[][] a = new boolean[n][n]; for (int i = 0; i < n; i++) { a[i][i] = true; } int[][] b = new int[n][]; for (int i = 0; i < n; i++) { int l = sc.nextInt(); b[i] = new int[l]; for (int j = 0; j < l; j++) { b[i][j] = sc.nextInt(); } } for (int i = 1; i <= 30; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < b[j].length; k++) { if (b[j][k] == i) { for (int l = 0; l < n; l++) { for (int m = 0; m < b[l].length; m++) { if (b[l][m] == i) { for (int N = 0; N < n; N++) { if (a[k][N]) { a[l][N] = true; } if (a[l][N]) { a[k][N] = true; } } } } } } } } IN: for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if (!a[j][k]) continue IN; } System.out.println(i); continue IX; } } System.out.println(-1); } } }
import java.util.*; public class Main { public static void main(String[] agrs) { Scanner sc = new Scanner(System.in); IX: while (true) { int n = sc.nextInt(); if (n == 0) break; boolean[][] a = new boolean[n][n]; for (int i = 0; i < n; i++) { a[i][i] = true; } int[][] b = new int[n][]; for (int i = 0; i < n; i++) { int l = sc.nextInt(); b[i] = new int[l]; for (int j = 0; j < l; j++) { b[i][j] = sc.nextInt(); } } for (int i = 1; i <= 30; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < b[j].length; k++) { if (b[j][k] == i) { for (int l = 0; l < n; l++) { for (int m = 0; m < b[l].length; m++) { if (b[l][m] == i) { for (int N = 0; N < n; N++) { if (a[j][N]) { a[l][N] = true; } if (a[l][N]) { a[j][N] = true; } } } } } } } } IN: for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if (!a[j][k]) continue IN; } System.out.println(i); continue IX; } } System.out.println(-1); } } }
[["-", 0, 57, 15, 15, 0, 504, 516, 504, 71, 22], ["+", 0, 57, 15, 15, 0, 504, 516, 504, 71, 22], ["-", 0, 1, 0, 11, 31, 504, 516, 504, 71, 22], ["+", 0, 1, 0, 11, 31, 504, 516, 504, 71, 22]]
3
431
#include <cstring> #include <iostream> #include <vector> using namespace std; int n, used[51][51], day[51][32]; int solve() { vector<int> l; for (int i = 1; i <= 30; i++) { l.clear(); for (int j = 0; j < n; j++) if (day[j][i] == 1) l.push_back(j); int a[51] = {}; for (int j = 0; j < l.size(); j++) for (int k = 0; k < n; k++) a[k] = (a[k] | used[l[j]][k]); for (int j = 0; j < l.size(); j++) { for (int k = 0; k < n; k++) used[l[j]][k] = a[k]; for (int k = j + 1; k < l.size(); k++) used[l[j]][l[k]] = used[l[k]][l[j]] = 1; } for (int j = 0; j < l.size(); j++) { int cnt = 0; while (cnt < n && used[j][cnt] == 1) cnt++; if (cnt == n) return i; } } return -1; } int main() { while (1) { cin >> n; if (n == 0) break; memset(used, 0, sizeof(used)); memset(day, 0, sizeof(day)); for (int i = 0; i < n; i++) { used[i][i] = 1; int a, b; cin >> a; while (a--) cin >> b, day[i][b] = 1; } cout << solve() << endl; } return 0; }
#include <cstring> #include <iostream> #include <vector> using namespace std; int n, used[51][51], day[51][32]; int solve() { vector<int> l; for (int i = 1; i <= 30; i++) { l.clear(); for (int j = 0; j < n; j++) if (day[j][i] == 1) l.push_back(j); int a[51] = {}; for (int j = 0; j < l.size(); j++) for (int k = 0; k < n; k++) a[k] = (a[k] | used[l[j]][k]); for (int j = 0; j < l.size(); j++) { for (int k = 0; k < n; k++) used[l[j]][k] = a[k]; for (int k = j + 1; k < l.size(); k++) used[l[j]][l[k]] = used[l[k]][l[j]] = 1; } for (int j = 0; j < l.size(); j++) { int cnt = 0; while (cnt < n && used[l[j]][cnt] == 1) cnt++; if (cnt == n) return i; } } return -1; } int main() { while (1) { cin >> n; if (n == 0) break; memset(used, 0, sizeof(used)); memset(day, 0, sizeof(day)); for (int i = 0; i < n; i++) { used[i][i] = 1; int a, b; cin >> a; while (a--) cin >> b, day[i][b] = 1; } cout << solve() << endl; } return 0; }
[["+", 31, 69, 28, 69, 341, 342, 0, 69, 28, 22], ["+", 28, 69, 341, 342, 0, 69, 341, 342, 0, 70], ["+", 28, 69, 341, 342, 0, 69, 341, 342, 0, 73]]
1
429
#include <algorithm> #include <iostream> #include <vector> using namespace std; #define MAX_D 70 #define MAX_N 60 #define INF 1 << 30 int X[MAX_D][MAX_D]; int DP[MAX_D][MAX_D]; int Y[MAX_D][MAX_D]; int n, p, a; int main() { while (true) { for (int i = 0; i < MAX_D; i++) { for (int j = 0; j < MAX_D; j++) { X[i][j] = 0; Y[i][j] = INF; } } cin >> n; if (n == 0) { break; } for (int i = 0; i < n; i++) { cin >> p; for (int j = 0; j < p; j++) { cin >> a; X[a][i] = 1; } } int res = INF; int res1 = 0; for (int j = 0; j < n; j++) { for (int k = 0; k < MAX_D; k++) { for (int l = 0; l < MAX_D; l++) { DP[k][l] = 0; } } DP[0][j] = 1; for (int k = 1; k < MAX_D; k++) { for (int l = 0; l < n; l++) { if (DP[k - 1][l] == 1) { DP[k][l] = 1; } } vector<int> V; int ok = 0; for (int l = 0; l < n; l++) { if (X[k][l] == 1) { if (DP[k - 1][l] == 1) { ok = 1; } V.push_back(l); } } if (ok == 1) { for (int l = 0; l < V.size(); l++) { DP[k][V[l]] = 1; } } } for (int k = 0; k < n; k++) { for (int l = 0; l < MAX_N; l++) { if (DP[l][k] == 1) { Y[j][k] = min(Y[j][k], l); } } } } for (int i = 0; i < n; i++) { int res2 = 0; for (int j = 0; j < n; j++) { res2 = max(res2, Y[i][j]); } res = min(res, res2); } if (res >= INF) { cout << "-1" << endl; } else { cout << res << endl; } } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; #define MAX_D 70 #define MAX_N 60 #define INF 1 << 30 int X[MAX_D][MAX_D]; int DP[MAX_D][MAX_D]; int Y[MAX_D][MAX_D]; int n, p, a; int main() { while (true) { for (int i = 0; i < MAX_D; i++) { for (int j = 0; j < MAX_D; j++) { X[i][j] = 0; Y[i][j] = INF; } } cin >> n; if (n == 0) { break; } for (int i = 0; i < n; i++) { cin >> p; for (int j = 0; j < p; j++) { cin >> a; X[a][i] = 1; } } int res = INF; int res1 = 0; for (int j = 0; j < n; j++) { for (int k = 0; k < MAX_D; k++) { for (int l = 0; l < MAX_D; l++) { DP[k][l] = 0; } } DP[0][j] = 1; for (int k = 1; k < MAX_D; k++) { for (int l = 0; l < n; l++) { if (DP[k - 1][l] == 1) { DP[k][l] = 1; } } vector<int> V; int ok = 0; for (int l = 0; l < n; l++) { if (X[k][l] == 1) { if (DP[k - 1][l] == 1) { ok = 1; } V.push_back(l); } } if (ok == 1) { for (int l = 0; l < V.size(); l++) { DP[k][V[l]] = 1; } } } for (int k = 0; k < n; k++) { for (int l = 0; l < MAX_N; l++) { if (DP[l][k] == 1) { Y[j][k] = min(Y[j][k], l); } } } } for (int i = 0; i < n; i++) { int res2 = 0; for (int j = 0; j < n; j++) { res2 = max(res2, Y[j][i]); } res = min(res, res2); } if (res >= INF) { cout << "-1" << endl; } else { cout << res << endl; } } return 0; }
[["-", 3, 4, 0, 69, 28, 69, 341, 342, 0, 22], ["+", 3, 4, 0, 69, 28, 69, 341, 342, 0, 22], ["-", 12, 2, 3, 4, 0, 69, 341, 342, 0, 22], ["+", 12, 2, 3, 4, 0, 69, 341, 342, 0, 22]]
1
582
#include <iostream> #include <vector> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < n; i++) ll dp[55][35]; bool ok[55][35]; int main() { int n, m, t; while (cin >> n, n) { int s = 0; rep(i, 31) { rep(j, n) { dp[j][i] = 0; ok[j][i] = false; } } for (int i = 0; i < n; i++) { cin >> m; for (int j = 0; j < m; j++) { cin >> t; ok[i][t] = true; } } rep(i, n) { dp[i][0] |= 1ll << i; } for (int j = 1; j < 31; j++) { rep(i, n) { if (ok[i][j] == true) rep(k, n) { if (ok[k][j] == true) { dp[i][j] |= dp[k][j - 1]; } } else dp[i][j] |= dp[i][j - 1]; } } rep(j, 31) { rep(i, n) { if (dp[i][j] == (1 << n) - 1) { s = j; goto end; } } } end: cout << s << endl; } }
#include <iostream> #include <vector> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < n; i++) ll dp[55][35]; bool ok[55][35]; int main() { int n, m, t; while (cin >> n, n) { int s = -1; rep(i, 31) { rep(j, n) { dp[j][i] = 0; ok[j][i] = false; } } for (int i = 0; i < n; i++) { cin >> m; for (int j = 0; j < m; j++) { cin >> t; ok[i][t] = true; } } rep(i, n) { dp[i][0] |= 1ll << i; } for (int j = 1; j < 31; j++) { rep(i, n) { if (ok[i][j] == true) rep(k, n) { if (ok[k][j] == true) { dp[i][j] |= dp[k][j - 1]; } } else dp[i][j] |= dp[i][j - 1]; } } rep(j, 31) { rep(i, n) { if (dp[i][j] == (1ll << n) - 1) { s = j; goto end; } } } end: cout << s << endl; } }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["-", 51, 16, 12, 16, 31, 23, 0, 16, 31, 13], ["+", 51, 16, 12, 16, 31, 23, 0, 16, 31, 13]]
1
321
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll n; while (cin >> n, n) { bool a[50][30] = {}; ll g[50] = {}; ll m; for (ll i = 0; i < n; i++) { cin >> m; for (ll j = 0, x; j < m; j++) { cin >> x, x--; a[i][x] = 1; g[x] |= 1LL << i; } } ll ans = -1, f = 1, c[50] = {}; for (ll i = 0; i < 30 && f; i++) for (ll j = 0; j < n; j++) { if (a[j][i]) for (ll k = 0; k < n; k++) if (a[k][i]) c[j] |= c[i] | g[i]; if (c[j] + 1 == (1LL << n)) f = 0, ans = i + 1; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll n; while (cin >> n, n) { bool a[50][30] = {}; ll m, g[50] = {}; for (ll i = 0; i < n; i++) { cin >> m; for (ll j = 0, x; j < m; j++) { cin >> x, x--; a[i][x] = 1; g[x] |= 1LL << i; } } ll ans = -1, f = 1, c[50] = {}; for (ll i = 0; i < 30 && f; i++) for (ll j = 0; j < n; j++) { if (a[j][i]) for (ll k = 0; k < n; k++) if (a[k][i]) c[j] |= c[k] | g[i]; if (c[j] + 1 == (1LL << n)) f = 0, ans = i + 1; } cout << ans << endl; } return 0; }
[["+", 8, 9, 0, 52, 8, 9, 0, 43, 49, 22], ["+", 8, 9, 0, 52, 8, 9, 0, 43, 0, 21], ["-", 8, 9, 0, 52, 8, 9, 0, 43, 39, 78], ["-", 8, 9, 0, 52, 8, 9, 0, 43, 49, 22], ["-", 8, 9, 0, 52, 8, 9, 0, 43, 0, 35], ["-", 0, 11, 12, 16, 31, 69, 341, 342, 0, 22], ["+", 0, 11, 12, 16, 31, 69, 341, 342, 0, 22]]
1
250
#include <cstdio> #include <cstring> using namespace std; typedef long long STATES; int main(void) { STATES free_days[31], dp[51], goal_state, next; STATES *p; short int i, j, n, answer, free_day, k; while (scanf("%d", &n) == 1 && n) { memset(free_days, 0, sizeof(free_days)); memset(dp, 0, sizeof(dp)); goal_state = 0; for (i = 0; i < n; i++) goal_state |= 1LL << i; for (i = 0; i < n; i++) { scanf("%d", &k); for (j = 0; j < k; j++) { scanf("%d", &free_day); free_days[free_day] |= 1LL << i; } } answer = -1; for (i = 0; i < n; i++) dp[i] = 1LL << i; p = free_days + 1; for (i = 1; i <= 30; i++, p++) { next = 0; for (j = 0; j < n; j++) if (*p & 1LL << j) next |= dp[j]; if (next == goal_state) { answer = i; break; } for (j = 0; j < n; j++) if (*p & 1LL << j) dp[j] |= next; } printf("%d\n", answer); } return 0; }
#include <cstdio> #include <cstring> using namespace std; typedef long long STATES; int main(void) { STATES free_days[31], dp[51], goal_state, next; STATES *p; int i, j, n, free_day, k; short answer; while (scanf("%d", &n) == 1 && n) { memset(free_days, 0, sizeof(free_days)); memset(dp, 0, sizeof(dp)); goal_state = 0; for (i = 0; i < n; i++) goal_state |= 1LL << i; for (i = 0; i < n; i++) { scanf("%d", &k); for (j = 0; j < k; j++) { scanf("%d", &free_day); free_days[free_day] |= 1LL << i; } } answer = -1; for (i = 0; i < n; i++) dp[i] = 1LL << i; p = free_days + 1; for (i = 1; i <= 30; i++, p++) { next = 0; for (j = 0; j < n; j++) if (*p & 1LL << j) next |= dp[j]; if (next == goal_state) { answer = i; break; } for (j = 0; j < n; j++) if (*p & 1LL << j) dp[j] |= next; } printf("%d\n", answer); } return 0; }
[["-", 0, 14, 8, 9, 0, 43, 39, 86, 0, 133], ["-", 0, 30, 0, 14, 8, 9, 0, 43, 0, 21], ["-", 0, 30, 0, 14, 8, 9, 0, 43, 49, 22], ["+", 0, 30, 0, 14, 8, 9, 0, 43, 0, 35], ["+", 0, 14, 8, 9, 0, 43, 39, 86, 0, 133], ["+", 0, 30, 0, 14, 8, 9, 0, 43, 49, 22]]
1
316
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <functional> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #ifdef _MSC_VER #define __typeof__ decltype template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; } #endif #define foreach(it, c) \ for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define CLEAR(arr, val) memset(arr, val, sizeof(arr)) #define rep(i, n) for (int i = 0; i < n; ++i) template <class T> void max_swap(T &a, const T &b) { a = max(a, b); } template <class T> void min_swap(T &a, const T &b) { a = min(a, b); } typedef long long ll; typedef pair<int, int> pint; const double PI = acos(-1.0); const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; int main() { int n; while (cin >> n, n) { bool s[64][32]; CLEAR(s, 0); for (int i = 0; i < n; ++i) { int f; cin >> f; while (f--) { int t; cin >> t; s[i][t] = true; } } int part[64]; for (int i = 0; i < n; ++i) part[i] = 1 << i; int res = -1; for (int d = 0; d <= 30; ++d) { int p = 0; for (int i = 0; i < n; ++i) if (s[i][d]) p |= part[i]; for (int i = 0; i < n; ++i) if (s[i][d]) part[i] = p; if (p == (1 << n) - 1) { res = d; break; } } cout << res << endl; } }
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <functional> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #ifdef _MSC_VER #define __typeof__ decltype template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; } #endif #define foreach(it, c) \ for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define CLEAR(arr, val) memset(arr, val, sizeof(arr)) #define rep(i, n) for (int i = 0; i < n; ++i) template <class T> void max_swap(T &a, const T &b) { a = max(a, b); } template <class T> void min_swap(T &a, const T &b) { a = min(a, b); } typedef long long ll; typedef pair<int, int> pint; const double PI = acos(-1.0); const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; int main() { int n; while (cin >> n, n) { bool s[64][32]; CLEAR(s, 0); for (int i = 0; i < n; ++i) { int f; cin >> f; while (f--) { int t; cin >> t; s[i][t] = true; } } ll part[64]; for (int i = 0; i < n; ++i) part[i] = 1LL << i; int res = -1; for (int d = 1; d <= 30; ++d) { ll p = 0; for (int i = 0; i < n; ++i) if (s[i][d]) p |= part[i]; for (int i = 0; i < n; ++i) if (s[i][d]) part[i] = p; if (p == (1LL << n) - 1) { res = d; break; } } cout << res << endl; } }
[["-", 8, 9, 0, 52, 8, 9, 0, 43, 39, 40], ["+", 8, 9, 0, 52, 8, 9, 0, 43, 39, 78], ["-", 0, 7, 8, 1, 0, 11, 12, 16, 31, 13], ["+", 0, 7, 8, 1, 0, 11, 12, 16, 31, 13], ["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["-", 8, 9, 0, 7, 8, 9, 0, 43, 39, 40], ["+", 8, 9, 0, 7, 8, 9, 0, 43, 39, 78], ["-", 51, 16, 12, 16, 31, 23, 0, 16, 31, 13], ["+", 51, 16, 12, 16, 31, 23, 0, 16, 31, 13]]
1
458
#include <cstring> #include <iostream> #include <set> using namespace std; void solve() { int d[51][31]; int m[51][51]; int n; while (cin >> n, n) { memset(d, 0, sizeof(d)); memset(m, 0, sizeof(m)); for (int i = 0; i < n; ++i) { int num; cin >> num; for (int j = 0; j < num; ++j) { int t; cin >> t; d[i][t] = 1; } m[i][i] = 1; } for (int t = 1; t < n; ++t) { set<int> Set; for (int i = 0; i < n; ++i) { if (!d[i][t]) { continue; } for (int j = 0; j < n; ++j) { if (m[i][j]) { Set.insert(j); } } } for (int i = 0; i < n; ++i) { if (!d[i][t]) { continue; } for (set<int>::iterator It = Set.begin(); It != Set.end(); ++It) { m[i][*It] = 1; } } if (Set.size() == n) { cout << t << endl; goto NEXT; } } cout << -1 << endl; NEXT:; } } int main() { solve(); return (0); }
#include <cstring> #include <iostream> #include <set> using namespace std; void solve() { int d[51][31]; int m[51][51]; int n; while (cin >> n, n) { memset(d, 0, sizeof(d)); memset(m, 0, sizeof(m)); for (int i = 0; i < n; ++i) { int num; cin >> num; for (int j = 0; j < num; ++j) { int t; cin >> t; d[i][t] = 1; } m[i][i] = 1; } for (int t = 1; t <= 30; ++t) { set<int> Set; for (int i = 0; i < n; ++i) { if (!d[i][t]) { continue; } for (int j = 0; j < n; ++j) { if (m[i][j]) { Set.insert(j); } } } for (int i = 0; i < n; ++i) { if (!d[i][t]) { continue; } for (set<int>::iterator It = Set.begin(); It != Set.end(); ++It) { m[i][*It] = 1; } } if (Set.size() == n) { cout << t << endl; goto NEXT; } } cout << -1 << endl; NEXT:; } } int main() { solve(); return (0); }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13]]
1
341
#include <algorithm> #include <iostream> using namespace std; int x[1000000]; int y[1000000]; int main() { int t; int all_length; int n; int k; cin >> t; for (int j = 0; j < t; ++j) { cin >> n >> k; for (int i = 0; i < n; ++i) { cin >> x[i]; } if (n == 1) { all_length = 0; } else { all_length = x[n - 1] - x[0]; for (int i = 0; i < n - 1; ++i) { y[i] = x[i + 1] - x[i]; } sort(y, y + n); for (int i = 0; i < k - 1; ++i) { all_length = all_length - y[n - 1 - i]; } } cout << all_length; cout << endl; } }
#include <algorithm> #include <iostream> using namespace std; int x[1000000]; int y[1000000]; int main() { int t; int all_length; int n; int k; cin >> t; for (int j = 0; j < t; ++j) { cin >> n >> k; for (int i = 0; i < n; ++i) { cin >> x[i]; } if (n <= k) { all_length = 0; } else { all_length = x[n - 1] - x[0]; for (int i = 0; i < n - 1; ++i) { y[i] = x[i + 1] - x[i]; } sort(y, y + n - 1); for (int i = 0; i < k - 1; ++i) { all_length = all_length - y[n - 2 - i]; } } cout << all_length; cout << endl; } }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 33], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["-", 12, 69, 341, 342, 0, 16, 31, 16, 12, 13], ["+", 12, 69, 341, 342, 0, 16, 31, 16, 12, 13]]
1
199
#include <algorithm> #include <cstring> #include <iostream> int T, N, K, X[100000 + 10], A[100000 + 10]; using namespace std; int main() { cin >> T; for (int t = 0; t < T; ++t) { cin >> N; cin >> K; for (int i = 0; i < N; ++i) cin >> X[i]; for (int i = 0; i + 1 < N; ++i) { A[i] = X[i + 1] - X[i]; } sort(A, A + N + 1); int result = 0; if (N > K) { for (int i = 0; i < N - K; i++) { result += A[i]; } } cout << endl << result; } return 0; }
#include <algorithm> #include <cstring> #include <iostream> int T, N, K, X[100000 + 10], A[100000 + 10]; using namespace std; int main() { cin >> T; for (int t = 0; t < T; ++t) { cin >> N; cin >> K; for (int i = 0; i < N; ++i) cin >> X[i]; for (int i = 0; i + 1 < N; ++i) { A[i] = X[i + 1] - X[i]; } sort(A, A + N - 1); int result = 0; if (N > K) { for (int i = 0; i < N - K; i++) { result += A[i]; } } cout << result << endl; } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 33], ["-", 8, 9, 0, 1, 0, 16, 31, 16, 17, 151], ["-", 8, 9, 0, 1, 0, 16, 31, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 1, 0, 16, 17, 151], ["+", 0, 7, 8, 9, 0, 1, 0, 16, 12, 22]]
1
179
#include <algorithm> #include <iostream> using namespace std; int T, N, K, X[100000 + 10], A[100000 + 10]; int main() { int sum = 0; int i, j, t; cin >> T; //データセットの数を詠みこむ for (t = 0; t < T; t++) { sum = 0; cin >> N >> K; for (i = 0; i < N; i++) { cin >> X[i]; } for (i = 0; i < N - 1; i++) { A[i] = X[i + 1] - X[i]; } sort(A, A + i); for (j = 0; j < N - K; j++) { sum += A[j]; } cout << sum << "\n" << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; int T, N, K, X[100000 + 10], A[100000 + 10]; int main() { int sum = 0; int i, j, t; cin >> T; //データセットの数を詠みこむ for (t = 0; t < T; t++) { sum = 0; cin >> N >> K; for (i = 0; i < N; i++) { cin >> X[i]; } for (i = 0; i < N - 1; i++) { A[i] = X[i + 1] - X[i]; } sort(A, A + i); for (j = 0; j < N - K; j++) { sum += A[j]; } cout << sum << endl; } return 0; }
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 62], ["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 44], ["-", 0, 7, 8, 9, 0, 1, 0, 16, 17, 151]]
1
179
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int z = 0; z < t; z++) { int n, k; cin >> n >> k; vector<int> x(n); for (int i = 0; i < n; i++) cin >> x[i]; vector<int> sub(n - 1); for (int i = 0; i < n - 1; i++) sub[i] = x[i + 1] - x[i]; sort(sub.begin(), sub.end()); int ans = 0; for (int i = k - 1; i < n - 1; i++) ans += sub[i]; cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int z = 0; z < t; z++) { int n, k; cin >> n >> k; vector<int> x(n); for (int i = 0; i < n; i++) cin >> x[i]; vector<int> sub(n - 1); for (int i = 0; i < n - 1; i++) sub[i] = x[i + 1] - x[i]; sort(sub.begin(), sub.end()); int ans = 0; for (int i = 0; i < n - k; i++) ans += sub[i]; cout << ans << endl; } }
[["-", 0, 7, 10, 43, 49, 50, 51, 16, 31, 22], ["-", 0, 7, 10, 43, 49, 50, 51, 16, 17, 33], ["-", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 22]]
1
171
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int n, k; int sum = 0; cin >> n >> k; vector<int> a(n); vector<int> b(n - 1); for (int j = 0; j < n; j++) { cin >> a.at(j); } for (int m = 0; m < n - 1; m++) { b.at(m) = a.at(m + 1) - a.at(m); } sort(b.begin(), b.end()); int c = n - k; for (int l = 0; l < c; l++) { sum += b.at(l); cout << sum << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int n, k; int sum = 0; cin >> n >> k; vector<int> a(n); vector<int> b(n - 1); for (int j = 0; j < n; j++) { cin >> a.at(j); } for (int m = 0; m < n - 1; m++) { b.at(m) = a.at(m + 1) - a.at(m); } sort(b.begin(), b.end()); int c = n - k; for (int l = 0; l < c; l++) { sum += b.at(l); } cout << sum << endl; } }
[["+", 0, 7, 8, 9, 0, 7, 8, 9, 0, 46], ["-", 0, 7, 8, 9, 0, 7, 8, 9, 0, 46]]
1
190
#include <algorithm> #include <iostream> using namespace std; int t, n, k; int x[100010]; int d[100010]; int main() { cin >> t; while (t) { t--; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> x[i]; } int ans = x[n - 1] - x[n]; for (int i = 1; i < n; i++) { d[i - 1] = x[i] - x[i - 1]; } sort(d, d + n - 1); if (n <= k) { ans = 0; } else { for (int i = 0; i < k - 1; i++) { ans -= d[i]; } } cout << ans << endl; } }
#include <algorithm> #include <iostream> using namespace std; int t, n, k; int x[100010]; int d[100010]; int main() { cin >> t; while (t) { t--; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> x[i]; } int ans = x[n - 1] - x[0]; for (int i = 1; i < n; i++) { d[i - 1] = x[i] - x[i - 1]; } sort(d, d + n - 1); if (n <= k) { ans = 0; } else { for (int i = 0; i < k - 1; i++) { ans -= d[n - 2 - i]; } } cout << ans << endl; } }
[["-", 49, 50, 51, 16, 12, 69, 341, 342, 0, 22], ["+", 49, 50, 51, 16, 12, 69, 341, 342, 0, 13], ["+", 12, 69, 341, 342, 0, 16, 31, 16, 31, 22], ["+", 12, 69, 341, 342, 0, 16, 31, 16, 17, 33], ["+", 12, 69, 341, 342, 0, 16, 31, 16, 12, 13], ["+", 0, 11, 12, 69, 341, 342, 0, 16, 17, 33]]
1
182
#include <algorithm> #include <iostream> using namespace std; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int house, generator; cin >> house >> generator; int a[house]; for (int j = 0; j < house; j++) { cin >> a[j]; } int b[house - 1]; int sum = 0; for (int j = 0; j < house - 1; j++) { b[j] = a[j + 1] - a[j]; sum += b[j]; } sort(b, b + house - 1); reverse(b, b + house - 1); int j = 0; while (generator <= house && generator - 1 != 0 && sum != 0) { sum -= b[j]; j++; generator--; } cout << sum << endl; } }
#include <algorithm> #include <iostream> using namespace std; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int house, generator; cin >> house >> generator; int a[house]; for (int j = 0; j < house; j++) { cin >> a[j]; } int b[house - 1]; int sum = 0; for (int j = 0; j < house - 1; j++) { b[j] = a[j + 1] - a[j]; sum += b[j]; } sort(b, b + house - 1); reverse(b, b + house - 1); int j = 0; while (generator - 1 != 0 && sum != 0) { sum -= b[j]; j++; generator--; } cout << sum << endl; } }
[["-", 15, 339, 51, 16, 31, 16, 31, 16, 17, 19], ["-", 15, 339, 51, 16, 31, 16, 31, 16, 12, 22], ["-", 0, 52, 15, 339, 51, 16, 31, 16, 17, 98], ["-", 51, 16, 31, 16, 12, 16, 31, 16, 31, 22]]
1
196
#include <algorithm> #include <iostream> #include <math.h> using namespace std; long long n, k, m, x[100010], d[100010], result, l = 0; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { cin >> n >> k; for (int j = 0; j < n; j++) { cin >> x[j]; } for (int j = 0; j + 1 < n; j++) { d[j] = x[j + 1] - x[j]; } sort(d, d + n - 1); m = max(l, n - k); for (int j = 0; j < m; j++) result += d[j]; cout << result << endl; } }
#include <algorithm> #include <iostream> #include <math.h> using namespace std; long long n, k, m, x[100010], d[100010], result, l = 0; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { result = 0; cin >> n >> k; for (int j = 0; j < n; j++) { cin >> x[j]; } for (int j = 0; j + 1 < n; j++) { d[j] = x[j + 1] - x[j]; } sort(d, d + n - 1); m = max(l, n - k); for (int j = 0; j < m; j++) result += d[j]; cout << result << endl; } }
[["+", 0, 7, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 7, 8, 9, 0, 1, 0, 35]]
1
176
#include <algorithm> #include <iostream> using namespace std; int T, N, K, X[100010], d[100010]; int main() { cin >> T; for (int t = 0; t <= T; t++) { cin >> N >> K; for (int i = 0; i < N; i++) cin >> X[i]; for (int i = 0; i + 1 < N; i++) d[i] = X[i + 1] - X[i]; sort(d, d + N - 1); int l = max(0, N - K); long long sum = 0; for (int i = 0; i < l; i++) sum += d[i]; cout << sum; } }
#include <algorithm> #include <iostream> using namespace std; int T, N, K, X[100010], d[100010]; int main() { cin >> T; for (int t = 0; t < T; t++) { cin >> N >> K; for (int i = 0; i < N; i++) cin >> X[i]; for (int i = 0; i + 1 < N; i++) d[i] = X[i + 1] - X[i]; sort(d, d + N - 1); int l = max(0, N - K); long long sum = 0; for (int i = 0; i < l; i++) sum += d[i]; cout << sum << endl; } }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 1, 0, 16, 17, 151], ["+", 0, 7, 8, 9, 0, 1, 0, 16, 12, 22]]
1
165
#include <algorithm> #include <iostream> using namespace std; int q, n, m, a[100000], b[99999]; int main() { cin >> q; for (int i = 0; i < q; i++) { cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) b[i - 1] = a[i] - a[i - 1]; sort(b, b + n - 1); int ret = a[n - 1] - a[0]; for (int i = n - 2; i >= n - m - 1 && i >= 0; i--) ret -= b[i]; cout << ret << endl; } }
#include <algorithm> #include <iostream> using namespace std; int q, n, m, a[100000], b[99999]; int main() { cin >> q; for (int i = 0; i < q; i++) { cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) b[i - 1] = a[i] - a[i - 1]; sort(b, b + n - 1); int ret = a[n - 1] - a[0]; for (int i = n - 2; i >= n - m && i >= 0; i--) ret -= b[i]; cout << ret << endl; } }
[["-", 0, 7, 15, 16, 31, 16, 12, 16, 17, 33], ["-", 0, 7, 15, 16, 31, 16, 12, 16, 12, 13]]
1
174
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int n, k; int sum = 0; cin >> n >> k; vector<int> x(n); vector<int> dis; int n_k; n_k = n - k; for (int i = 0; i < n; ++i) cin >> x[i]; for (int i = 0; i < n - 1; ++i) dis.push_back(x[i + 1] - x[i]); sort(dis.begin(), dis.end()); for (int i = 0; i < n_k; i++) { sum += dis[i]; } cout << sum; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int n, k; int sum = 0; cin >> n >> k; vector<int> x(n); vector<int> dis; int n_k; n_k = n - k; for (int i = 0; i < n; ++i) cin >> x[i]; for (int i = 0; i < n - 1; ++i) dis.push_back(x[i + 1] - x[i]); sort(dis.begin(), dis.end()); for (int i = 0; i < n_k; i++) { sum += dis[i]; } cout << sum << endl; } }
[["+", 0, 7, 8, 9, 0, 1, 0, 16, 17, 151], ["+", 0, 7, 8, 9, 0, 1, 0, 16, 12, 22]]
1
171
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int t; cin >> t; for (int a = 0; a < t; a++) { int n, k, x[100000], sum; vector<int> vec; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> x[i]; if (i) vec.push_back(x[i] - x[i - 1]); } sum = x[n - 1] - x[0]; sort(vec.begin(), vec.end(), greater<int>()); for (int i = 0; i < k - 1; i++) { sum -= vec[i]; } cout << sum << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int t; cin >> t; for (int a = 0; a < t; a++) { int n, k, x[100000], sum; vector<int> vec; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> x[i]; if (i) vec.push_back(x[i] - x[i - 1]); } sum = x[n - 1] - x[0]; sort(vec.begin(), vec.end(), greater<int>()); for (int i = 0; i < min(n, k) - 1; i++) { sum -= vec[i]; } cout << sum << endl; } return 0; }
[["+", 0, 7, 15, 16, 12, 16, 31, 2, 63, 22], ["+", 15, 16, 12, 16, 31, 2, 3, 4, 0, 24], ["+", 15, 16, 12, 16, 31, 2, 3, 4, 0, 22], ["+", 15, 16, 12, 16, 31, 2, 3, 4, 0, 21], ["+", 15, 16, 12, 16, 31, 2, 3, 4, 0, 25]]
1
177
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define SIZE 100005 #define INF 1000000005LL using namespace std; typedef long long int ll; typedef pair<int, int> P; ll K; ll T[SIZE]; ll S[SIZE]; void solve(int n, int k) { if (n <= k) { printf("0\n"); return; } else if (k == 1) { printf("%lld\n", T[n - 1] - T[0]); return; } ll road = T[n - 1] - T[0]; for (int i = 1; i < n; i++) { S[i] = T[i] - T[i - 1]; } sort(S, S + n - 1); for (int i = 0; i < (k - 1); i++) { road -= S[n - 2 - i]; } printf("%lld\n", road); } void imain() { int n, k; scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) scanf("%lld", &T[i]); solve(n, k); } int main() { int N; scanf("%d", &N); for (int i = 0; i < N; i++) { imain(); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define SIZE 100005 #define INF 1000000005LL using namespace std; typedef long long int ll; typedef pair<int, int> P; ll K; ll T[SIZE]; ll S[SIZE]; void solve(int n, int k) { if (n <= k) { printf("0\n"); return; } else if (k == 1) { printf("%lld\n", T[n - 1] - T[0]); return; } ll road = T[n - 1] - T[0]; for (int i = 1; i < n; i++) { S[i - 1] = T[i] - T[i - 1]; } sort(S, S + n - 1); for (int i = 0; i < (k - 1); i++) { road -= S[n - 2 - i]; } printf("%lld\n", road); } void imain() { int n, k; scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) scanf("%lld", &T[i]); solve(n, k); } int main() { int N; scanf("%d", &N); for (int i = 0; i < N; i++) { imain(); } }
[["+", 0, 11, 31, 69, 341, 342, 0, 16, 17, 33], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13]]
1
319
#include <algorithm> #include <iostream> int main() { int t; std::cin >> t; for (int i = 0; i < t; i++) { // 入力 int n, k; std::cin >> n >> k; int x[100000]; for (int j = 0; j < n; j++) { std::cin >> x[j]; } // 演算 int a[100000]; for (int j = 0; j < n - 1; j++) { a[j] = x[j + 1] - x[j]; } std::sort(a, a + n - 1); int savedTotal = 0; for (int j = 0; j < k - 1; j++) { savedTotal += a[n - 1 - j]; } // 出力 std::cout << x[n - 1] - x[0] - savedTotal << std::endl; } return 0; }
#include <algorithm> #include <iostream> int main() { int t; std::cin >> t; for (int i = 0; i < t; i++) { // 入力 int n, k; std::cin >> n >> k; int x[100000]; for (int j = 0; j < n; j++) { std::cin >> x[j]; } // 演算 int a[100000]; for (int j = 0; j < n - 1; j++) { a[j] = x[j + 1] - x[j]; } std::sort(a, a + n - 1); int savedTotal = 0; for (int j = 0; j < k - 1; j++) { savedTotal += a[n - 1 - j - 1]; } // 出力 std::cout << x[n - 1] - x[0] - savedTotal << std::endl; } return 0; }
[["+", 0, 11, 12, 69, 341, 342, 0, 16, 17, 33], ["+", 0, 11, 12, 69, 341, 342, 0, 16, 12, 13]]
1
195
#include <algorithm> #include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; #define REP(i, b, n) for (int i = b; i < n; i++) #define rep(i, n) REP(i, 0, n) #define ALL(C) (C).begin(), (C).end() #define pb push_back #define mp make_pair typedef long long ll; ll mypow(ll n, ll a) { if (n == 0) return 1; ll ret = mypow(n / 2, a); ret = ret * ret; if (n % 2 == 1) ret *= a; return ret; } ll val[255]; // div=27 46 -> 27 47 ll compute(string &a, ll div, int base) { /* ll nowbase=1; ll ret=0; for(int i=a.size()-1;i>=0;i--){ cout << i <<" " << a[i] <<" "<< val[a[i]]*nowbase << " " << div << " "<< (val[a[i]]+nowbase)/div <<endl; ret+=(val[a[i]]*nowbase)/div; nowbase*=base; } */ ll nowbase = mypow((int)a.size() - 1, base); ll ret = 0; ll rem = 0; // for(int i=a.size()-1;i>=0;i--){ for (int i = 0; i < a.size(); i++) { // cout << i <<" " << a[i] <<" "<< val[a[i]]*nowbase << // " " << div << " "<< (val[a[i]]+nowbase)/div <<endl; ret += (rem + val[a[i]] * nowbase) / div; rem = (val[a[i]] * nowbase) % div; nowbase /= base; } // cout << ret <<" " << div << endl; return ret; } void fact(int n, int *base, int *basenum, int &p) { for (int i = 2; i <= n; i++) { if (n % i == 0) { base[p] = i; basenum[p] = 0; while (true) { if (n / i == 0 || n % i != 0) break; n /= i; basenum[p]++; } p++; } } } ll count(int p, int *basenum, ll *cnt) { ll ret = (1LL << 62); rep(i, p) { ret = min(ret, cnt[i] / basenum[i]); } return ret; } main() { // check(); for (int i = 0; i < 10; i++) { val['0' + i] = i; } for (int i = 'A'; i <= 'Z'; i++) { val[i] = i - 'A' + 10; } int base[100]; int basenum[100]; ll cnt[100]; int num[100]; int in; string inp; while (cin >> in >> inp && in) { int p = 0; fact(in, base, basenum, p); rep(i, p) { ll now = base[i]; cnt[i] = 0; while (true) { ll tmp = compute(inp, now, in); if (tmp == 0) break; cnt[i] += tmp; now *= base[i]; } } cout << count(p, basenum, cnt) << endl; } return false; }
#include <algorithm> #include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; #define REP(i, b, n) for (int i = b; i < n; i++) #define rep(i, n) REP(i, 0, n) #define ALL(C) (C).begin(), (C).end() #define pb push_back #define mp make_pair typedef long long ll; ll mypow(ll n, ll a) { if (n == 0) return 1; ll ret = mypow(n / 2, a); ret = ret * ret; if (n % 2 == 1) ret *= a; return ret; } ll val[255]; // div=27 46 -> 27 47 ll compute(string &a, ll div, int base) { ll nowbase = mypow((int)a.size() - 1, base); ll ret = 0; ll rem = 0; for (int i = 0; i < a.size(); i++) { ret += (rem + val[a[i]] * nowbase) / div; rem = (rem + val[a[i]] * nowbase) % div; nowbase /= base; } return ret; } void fact(int n, int *base, int *basenum, int &p) { for (int i = 2; i <= n; i++) { if (n % i == 0) { base[p] = i; basenum[p] = 0; while (true) { if (n / i == 0 || n % i != 0) break; n /= i; basenum[p]++; } p++; } } } ll count(int p, int *basenum, ll *cnt) { ll ret = (1LL << 62); rep(i, p) { ret = min(ret, cnt[i] / basenum[i]); } return ret; } main() { // check(); for (int i = 0; i < 10; i++) { val['0' + i] = i; } for (int i = 'A'; i <= 'Z'; i++) { val[i] = i - 'A' + 10; } int base[100]; int basenum[100]; ll cnt[100]; int num[100]; int in; string inp; while (cin >> in >> inp && in) { int p = 0; fact(in, base, basenum, p); rep(i, p) { ll now = base[i]; cnt[i] = 0; while (true) { ll tmp = compute(inp, now, in); if (tmp == 0) break; cnt[i] += tmp; now *= base[i]; } } cout << count(p, basenum, cnt) << endl; } return false; }
[["+", 0, 11, 12, 16, 31, 23, 0, 16, 31, 22], ["+", 0, 11, 12, 16, 31, 23, 0, 16, 17, 72]]
1
570
#include <algorithm> #include <assert.h> #include <iostream> #include <math.h> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; static const double EPS = 1e-9; static const double PI = acos(-1.0); #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) #define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++) #define FORIT(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++) #define MEMSET(v, h) memset((v), h, sizeof(v)) vector<pair<int, int>> PrimeFactor(int n) { vector<pair<int, int>> ret; FOREQ(i, 2, n) { int cnt = 0; while (n % i == 0) { n /= i; cnt++; } if (cnt != 0) { ret.push_back(make_pair(i, cnt)); } } return ret; } ll calc(ll m, ll v) { if (m < v) { return 0; } return m / v + calc(m / v, v); } ll convert(string s, ll base) { ll ret = 0; FORIT(it, s) { ll v = 0; if (isdigit(*it)) { v = *it - '0'; } else { v = *it - 'A' + 10; } ret = ret * 10 + v; } return ret; } ll n; string str; int main() { while (cin >> n >> str, n) { ll m = convert(str, n); vector<pair<int, int>> factor = PrimeFactor(n); ll ans = 1LL << 60; FORIT(it, factor) { ans = min(ans, calc(m, it->first) / it->second); } printf("%lld\n", ans); } }
#include <algorithm> #include <assert.h> #include <iostream> #include <math.h> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; static const double EPS = 1e-9; static const double PI = acos(-1.0); #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) #define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++) #define FORIT(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++) #define MEMSET(v, h) memset((v), h, sizeof(v)) vector<pair<int, int>> PrimeFactor(int n) { vector<pair<int, int>> ret; FOREQ(i, 2, n) { int cnt = 0; while (n % i == 0) { n /= i; cnt++; } if (cnt != 0) { ret.push_back(make_pair(i, cnt)); } } return ret; } ll calc(ll m, ll v) { if (m < v) { return 0; } return m / v + calc(m / v, v); } ll convert(string s, ll base) { ll ret = 0; FORIT(it, s) { ll v = 0; if (isdigit(*it)) { v = *it - '0'; } else { v = *it - 'A' + 10; } ret = ret * base + v; } return ret; } ll n; string str; int main() { while (cin >> n >> str, n) { ll m = convert(str, n); vector<pair<int, int>> factor = PrimeFactor(n); ll ans = 1LL << 62; FORIT(it, factor) { ans = min(ans, calc(m, it->first) / it->second); } printf("%lld\n", ans); } }
[["-", 0, 1, 0, 11, 12, 16, 31, 16, 12, 13], ["+", 0, 1, 0, 11, 12, 16, 31, 16, 12, 22], ["-", 8, 9, 0, 43, 49, 50, 51, 16, 12, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 16, 12, 13]]
1
385
h={};2.step(36){|i|h[i]=i.prime_division} while(l=gets.chomp)!="0 0" l=l.split n=l[0].to_i;m=l[1].to_i(n) p h[n].map{|a,b|z=m;r=0;r+=(z/=a) while z>0;r/b}.min end
require'prime' h={};2.step(36){|i|h[i]=i.prime_division} while(l=gets.chomp)!="0 0" l=l.split n=l[0].to_i;m=l[1].to_i(n) p h[n].map{|a,b|z=m;r=0;r+=(z/=a) while z>0;r/b}.min end
[["+", 36, 36, 36, 36, 0, 493, 0, 652, 735, 22], ["+", 0, 493, 0, 652, 3, 4, 0, 557, 0, 62], ["+", 0, 493, 0, 652, 3, 4, 0, 557, 0, 6]]
4
101
#include <cstdio> #include <cstdlib> #include <stack> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; int f[128]; struct card { char rank, suit; int strength() const { return 4 * f[rank] + f[suit]; } bool neighbor(const card &c) const { if (rank == 'A') { return c.rank == '2' || c.rank == 'K'; } else if (c.rank == 'A') { return rank == '2' || rank == 'K'; } else { return abs(f[rank] - f[c.rank]) == 1; } } }; card parse(const char *s) { return (card){s[1], s[0]}; } struct action { int type, i; // 0: deck からカードを引く, 1: 右の table にカード i を置く, 2: // 左の table にカード i を置く, 3: 位置 i にカードを戻す int t; // この動作を終える時刻 }; bool gameover(const stack<card> &S1, const stack<card> &S2, const bool *b1, const bool *b2, const card &last1, const card &last2) { bool end1 = S1.empty(), end2 = S2.empty(); rep(i, 4) { if (b1[i]) end1 = false; if (b2[i]) end2 = false; } if (end1 && end2) { puts(last1.strength() > last2.strength() ? "A wins." : "B wins."); return true; } if (end1) { puts("A wins."); return true; } if (end2) { puts("B wins."); return true; } return false; } void cold_move(stack<card> &S, bool *b, card *tab, card &R) { if (!S.empty()) R = S.top(), S.pop(); else { // rightmost card rep(i, 4) if (b[i]) { R = tab[i]; b[i] = false; break; } } } bool get_next_action(int t, const stack<card> &S, const bool *b, const card *tab, const card &R, const card &L, action &res) { // type 0 if (!S.empty()) { rep(i, 4) if (!b[i]) { res.type = 0; res.t = t + 3; return true; } } // type 1 int i0 = -1; rep(i, 4) if (b[i] && R.neighbor(tab[i])) if (i0 == -1 || tab[i].strength() < tab[i0].strength()) i0 = i; if (i0 != -1) { res.type = 1; res.t = t + 5; res.i = i0; return true; } // type 2 i0 = -1; rep(i, 4) if (b[i] && L.neighbor(tab[i])) if (i0 == -1 || tab[i].strength() < tab[i0].strength()) i0 = i; if (i0 != -1) { res.type = 2; res.t = t + 7; res.i = i0; return true; } // type 3 or wait return false; } void do_action(const action &a, int &t, stack<card> &S, bool *b, card *tab, card &R, card &L) { t = a.t; if (a.type == 0) { b[a.i] = true; tab[a.i] = S.top(); S.pop(); } else if (a.type == 1) { R = tab[a.i]; b[a.i] = false; } else if (a.type == 2) { L = tab[a.i]; b[a.i] = false; } } int main() { const int list[] = {77, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 3, 2, 1, 0}; rep(i, 17) f["A23456789XJKQSHDC"[i]] = list[i]; while (1) { stack<card> S1, S2; // deck card L, R; // table の一番上のカード bool b1[4] = {}, b2[4] = {}; // tableau の i 番目にカードがあるかどうか card tab1[4], tab2[4]; // tableau ( 手札 ) // input char tmp[52][3]; int n1; scanf("%d", &n1); rep(i, n1) scanf("%s", tmp[i]); for (int i = n1 - 1; i >= 0; i--) S1.push(parse(tmp[i])); int n2; scanf("%d", &n2); rep(i, n2) scanf("%s", tmp[i]); for (int i = n2 - 1; i >= 0; i--) S2.push(parse(tmp[i])); if (n1 == 0) break; // draw 4 cards rep(i, 4) { if (!S1.empty()) { card c = S1.top(); S1.pop(); b1[i] = true; tab1[i] = c; } if (!S2.empty()) { card c = S2.top(); S2.pop(); b2[i] = true; tab2[i] = c; } } // gao while (1) { // cold cold_move(S1, b1, tab1, R); cold_move(S2, b2, tab2, L); if (gameover(S1, S2, b1, b2, R, L)) goto END; // jakajaka int t = 0; action a1, a2; bool ok1 = get_next_action(t, S1, b1, tab1, R, L, a1); bool ok2 = get_next_action(t, S2, b2, tab2, L, R, a2); card last1, last2; // 最後に置いたカード while (ok1 || ok2) { if (ok1 && (a1.type == 1 || a1.type == 2)) last1 = tab1[a1.i]; if (ok2 && (a2.type == 1 || a2.type == 2)) last2 = tab2[a2.i]; if (ok1 && ok2) { if (a1.t < a2.t) { do_action(a1, t, S1, b1, tab1, R, L); if (a1.type == 1 && a2.type == 2 || a1.type == 2 && a2.type == 1) a2.type = 3, a2.t = t + 5; ok1 = get_next_action(t, S1, b1, tab1, R, L, a1); } else if (a2.t < a1.t) { do_action(a2, t, S2, b2, tab2, L, R); if (a1.type == 1 && a2.type == 2 || a1.type == 2 && a2.type == 1) a1.type = 3, a1.t = t + 5; ok2 = get_next_action(t, S2, b2, tab2, L, R, a2); } else { // a1.t==a2.t if (a1.type == 1 && a2.type == 2) { do_action(a2, t, S2, b2, tab2, L, R); a1.type = 3; a1.t = t + 5; ok2 = get_next_action(t, S2, b2, tab2, L, R, a2); } else if (a1.type == 2 && a2.type == 1) { do_action(a1, t, S1, b1, tab1, R, L); a2.type = 3; a2.t = t + 5; ok1 = get_next_action(t, S1, b1, tab1, R, L, a1); } else { do_action(a1, t, S1, b1, tab1, R, L); do_action(a2, t, S2, b2, tab2, L, R); ok1 = get_next_action(t, S1, b1, tab1, R, L, a1); ok2 = get_next_action(t, S2, b2, tab2, L, R, a2); } } } else if (ok1) { do_action(a1, t, S1, b1, tab1, R, L); ok1 = get_next_action(t, S1, b1, tab1, R, L, a1); ok2 = get_next_action(t, S2, b2, tab2, L, R, a2); } else if (ok2) { do_action(a2, t, S2, b2, tab2, L, R); ok1 = get_next_action(t, S1, b1, tab1, R, L, a1); ok2 = get_next_action(t, S2, b2, tab2, L, R, a2); } if (gameover(S1, S2, b1, b2, last1, last2)) goto END; } } END:; } return 0; }
#include <cstdio> #include <cstdlib> #include <stack> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; int f[128]; struct card { char rank, suit; int strength() const { return 4 * f[rank] + f[suit]; } bool neighbor(const card &c) const { if (rank == 'A') { return c.rank == '2' || c.rank == 'K'; } else if (c.rank == 'A') { return rank == '2' || rank == 'K'; } else { return abs(f[rank] - f[c.rank]) == 1; } } }; card parse(const char *s) { return (card){s[1], s[0]}; } struct action { int type, i; // 0: deck からカードを引く, 1: 右の table にカード i を置く, 2: // 左の table にカード i を置く, 3: 位置 i にカードを戻す int t; // この動作を終える時刻 }; bool gameover(const stack<card> &S1, const stack<card> &S2, const bool *b1, const bool *b2, const card &last1, const card &last2) { bool end1 = S1.empty(), end2 = S2.empty(); rep(i, 4) { if (b1[i]) end1 = false; if (b2[i]) end2 = false; } if (end1 && end2) { puts(last1.strength() > last2.strength() ? "A wins." : "B wins."); return true; } if (end1) { puts("A wins."); return true; } if (end2) { puts("B wins."); return true; } return false; } void cold_move(stack<card> &S, bool *b, card *tab, card &R) { if (!S.empty()) R = S.top(), S.pop(); else { // rightmost card rep(i, 4) if (b[i]) { R = tab[i]; b[i] = false; break; } } } bool get_next_action(int t, const stack<card> &S, const bool *b, const card *tab, const card &R, const card &L, action &res) { // type 0 if (!S.empty()) { rep(i, 4) if (!b[i]) { res.type = 0; res.t = t + 3; return true; } } // type 1 int i0 = -1; rep(i, 4) if (b[i] && R.neighbor(tab[i])) if (i0 == -1 || tab[i].strength() < tab[i0].strength()) i0 = i; if (i0 != -1) { res.type = 1; res.t = t + 5; res.i = i0; return true; } // type 2 i0 = -1; rep(i, 4) if (b[i] && L.neighbor(tab[i])) if (i0 == -1 || tab[i].strength() < tab[i0].strength()) i0 = i; if (i0 != -1) { res.type = 2; res.t = t + 7; res.i = i0; return true; } // type 3 or wait return false; } void do_action(const action &a, int &t, stack<card> &S, bool *b, card *tab, card &R, card &L) { t = a.t; if (a.type == 0) { b[a.i] = true; tab[a.i] = S.top(); S.pop(); } else if (a.type == 1) { R = tab[a.i]; b[a.i] = false; } else if (a.type == 2) { L = tab[a.i]; b[a.i] = false; } } int main() { const int list[] = {77, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 3, 2, 1, 0}; rep(i, 17) f["A23456789XJQKSHDC"[i]] = list[i]; while (1) { stack<card> S1, S2; // deck card L, R; // table の一番上のカード bool b1[4] = {}, b2[4] = {}; // tableau の i 番目にカードがあるかどうか card tab1[4], tab2[4]; // tableau ( 手札 ) // input char tmp[52][3]; int n1; scanf("%d", &n1); rep(i, n1) scanf("%s", tmp[i]); for (int i = n1 - 1; i >= 0; i--) S1.push(parse(tmp[i])); int n2; scanf("%d", &n2); rep(i, n2) scanf("%s", tmp[i]); for (int i = n2 - 1; i >= 0; i--) S2.push(parse(tmp[i])); if (n1 == 0) break; // draw 4 cards rep(i, 4) { if (!S1.empty()) { card c = S1.top(); S1.pop(); b1[i] = true; tab1[i] = c; } if (!S2.empty()) { card c = S2.top(); S2.pop(); b2[i] = true; tab2[i] = c; } } // gao while (1) { // cold cold_move(S1, b1, tab1, R); cold_move(S2, b2, tab2, L); if (gameover(S1, S2, b1, b2, R, L)) goto END; // jakajaka int t = 0; action a1, a2; bool ok1 = get_next_action(t, S1, b1, tab1, R, L, a1); bool ok2 = get_next_action(t, S2, b2, tab2, L, R, a2); card last1, last2; // 最後に置いたカード while (ok1 || ok2) { if (ok1 && (a1.type == 1 || a1.type == 2)) last1 = tab1[a1.i]; if (ok2 && (a2.type == 1 || a2.type == 2)) last2 = tab2[a2.i]; if (ok1 && ok2) { if (a1.t < a2.t) { do_action(a1, t, S1, b1, tab1, R, L); if (a1.type == 1 && a2.type == 2 || a1.type == 2 && a2.type == 1) a2.type = 3, a2.t = t + 5; ok1 = get_next_action(t, S1, b1, tab1, R, L, a1); } else if (a2.t < a1.t) { do_action(a2, t, S2, b2, tab2, L, R); if (a1.type == 1 && a2.type == 2 || a1.type == 2 && a2.type == 1) a1.type = 3, a1.t = t + 5; ok2 = get_next_action(t, S2, b2, tab2, L, R, a2); } else { // a1.t==a2.t if (a1.type == 1 && a2.type == 2) { do_action(a2, t, S2, b2, tab2, L, R); a1.type = 3; a1.t = t + 5; ok2 = get_next_action(t, S2, b2, tab2, L, R, a2); } else if (a1.type == 2 && a2.type == 1) { do_action(a1, t, S1, b1, tab1, R, L); a2.type = 3; a2.t = t + 5; ok1 = get_next_action(t, S1, b1, tab1, R, L, a1); } else { do_action(a1, t, S1, b1, tab1, R, L); do_action(a2, t, S2, b2, tab2, L, R); ok1 = get_next_action(t, S1, b1, tab1, R, L, a1); ok2 = get_next_action(t, S2, b2, tab2, L, R, a2); } } } else if (ok1) { do_action(a1, t, S1, b1, tab1, R, L); ok1 = get_next_action(t, S1, b1, tab1, R, L, a1); ok2 = get_next_action(t, S2, b2, tab2, L, R, a2); } else if (ok2) { do_action(a2, t, S2, b2, tab2, L, R); ok1 = get_next_action(t, S1, b1, tab1, R, L, a1); ok2 = get_next_action(t, S2, b2, tab2, L, R, a2); } if (gameover(S1, S2, b1, b2, last1, last2)) goto END; } } END:; } return 0; }
[["-", 31, 69, 341, 342, 0, 69, 28, 5, 0, 6], ["+", 31, 69, 341, 342, 0, 69, 28, 5, 0, 6]]
1
1,892
#include <iostream> #include <string> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; string LCS(const string &s, const string &t) { const int LMAX = 300; int m = s.length(), n = t.length(); static int dp[LMAX + 1][LMAX + 1], from[LMAX + 1][LMAX + 1]; rep(i, m + 1) dp[i][0] = 0, from[i][0] = -1; rep(j, n + 1) dp[0][j] = 0, from[0][j] = -1; rep(i, m) rep(j, n) { if (s[i] == t[j]) { dp[i + 1][j + 1] = dp[i][j] + 1; from[i + 1][j + 1] = 2; } else { if (dp[i][j + 1] < dp[i + 1][j]) { dp[i + 1][j + 1] = dp[i + 1][j]; from[i + 1][j + 1] = 1; } else { dp[i + 1][j + 1] = dp[i][j + 1]; from[i + 1][j + 1] = 0; } } } int idx = dp[m][n]; string ans(idx, '@'); for (int i = m, j = n; ~from[i][j];) { switch (from[i][j]) { case 0: j--; break; case 1: i--; break; case 2: i--; j--; idx--; ans[idx] = s[i]; break; } } return ans; } int main() { for (string s; cin >> s, s != "#END";) { int len = s.length(); string ans; for (int i = 1; i < len; i++) { if (min(i, len - i) <= ans.length()) break; string tmp = LCS(s.substr(0, i), s.substr(i)); if (ans.length() < tmp.length()) ans = tmp; } cout << ans << endl; } return 0; }
#include <iostream> #include <string> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; string LCS(const string &s, const string &t) { const int LMAX = 300; int m = s.length(), n = t.length(); static int dp[LMAX + 1][LMAX + 1], from[LMAX + 1][LMAX + 1]; rep(i, m + 1) dp[i][0] = 0, from[i][0] = -1; rep(j, n + 1) dp[0][j] = 0, from[0][j] = -1; rep(i, m) rep(j, n) { if (s[i] == t[j]) { dp[i + 1][j + 1] = dp[i][j] + 1; from[i + 1][j + 1] = 2; } else { if (dp[i][j + 1] < dp[i + 1][j]) { dp[i + 1][j + 1] = dp[i + 1][j]; from[i + 1][j + 1] = 1; } else { dp[i + 1][j + 1] = dp[i][j + 1]; from[i + 1][j + 1] = 0; } } } int idx = dp[m][n]; string ans(idx, '@'); for (int i = m, j = n; ~from[i][j];) { switch (from[i][j]) { case 0: i--; break; case 1: j--; break; case 2: i--; j--; idx--; ans[idx] = s[i]; break; } } return ans; } int main() { for (string s; cin >> s, s != "#END";) { int len = s.length(); string ans; for (int i = 1; i < len; i++) { if (min(i, len - i) <= ans.length()) break; string tmp = LCS(s.substr(0, i), s.substr(i)); if (ans.length() < tmp.length()) ans = tmp; } cout << ans << endl; } return 0; }
[["-", 8, 9, 0, 100, 0, 1, 0, 27, 28, 22], ["+", 8, 9, 0, 100, 0, 1, 0, 27, 28, 22]]
1
522
#include <cstring> #include <iostream> #include <string> #include <vector> using namespace std; typedef pair<int, int> pii; int dp[301][301]; pii nxt[301][301]; char s1[501]; char s2[501]; int s1s; int s2s; char res[501]; int main() { string str; while (cin >> str && str != "#END") { int ms = 0; for (int i = 0; i < (int)str.size() - 1; i++) { s1s = i + 1; s2s = str.size() - i; for (int j = 0; j < i + 1; j++) s1[j] = str[j]; for (int j = i + 1; j < (int)str.size(); j++) s2[j - i - 1] = str[j]; memset(dp, 0, sizeof(dp)); for (int j = s1s; j >= 0; j--) { for (int k = s2s; k >= 0; k--) { int res = 0; int a = j; int b = k; if (a == s1s || b == s2s) res = 0; else if (s1[a] == s2[b]) { int tmp = dp[a + 1][b + 1] + 1; if (tmp > res) { nxt[a][b].first = a + 1; nxt[a][b].second = b + 1; res = tmp; } } else { int tmp = dp[a][b + 1]; if (tmp > res) { nxt[a][b].first = a; nxt[a][b].second = b + 1; res = tmp; } tmp = dp[a + 1][b]; if (tmp > res) { nxt[a][b].first = a + 1; nxt[a][b].second = b; res = tmp; } } dp[a][b] = res; } } // int tmp=dfs(0,0); int tmp = dp[0][0]; if (tmp > ms) { ms = tmp; int ca, cb; ca = cb = 0; int cnt = 0; while (1) { if (ca == s1s || cb == s2s) break; int na = nxt[ca][cb].first; int nb = nxt[ca][cb].second; if (na - ca == 1 && nb - cb == 1) { res[cnt] = s1[ca]; cnt++; } ca = na; cb = nb; } res[cnt] = '\0'; } } cout << res << endl; } return 0; }
#include <cstring> #include <iostream> #include <string> #include <vector> using namespace std; typedef pair<int, int> pii; int dp[301][301]; pii nxt[301][301]; char s1[501]; char s2[501]; int s1s; int s2s; char res[501]; int main() { string str; while (cin >> str && str != "#END") { int ms = 0; for (int i = 0; i < (int)str.size() - 1; i++) { s1s = i + 1; s2s = str.size() - i - 1; for (int j = 0; j < i + 1; j++) s1[j] = str[j]; for (int j = i + 1; j < (int)str.size(); j++) s2[j - i - 1] = str[j]; memset(dp, 0, sizeof(dp)); for (int j = s1s; j >= 0; j--) { for (int k = s2s; k >= 0; k--) { int res = 0; int a = j; int b = k; if (a == s1s || b == s2s) res = 0; else if (s1[a] == s2[b]) { int tmp = dp[a + 1][b + 1] + 1; if (tmp > res) { nxt[a][b].first = a + 1; nxt[a][b].second = b + 1; res = tmp; } } else { int tmp = dp[a][b + 1]; if (tmp > res) { nxt[a][b].first = a; nxt[a][b].second = b + 1; res = tmp; } tmp = dp[a + 1][b]; if (tmp > res) { nxt[a][b].first = a + 1; nxt[a][b].second = b; res = tmp; } } dp[a][b] = res; } } // int tmp=dfs(0,0); int tmp = dp[0][0]; if (tmp > ms) { ms = tmp; int ca, cb; ca = cb = 0; int cnt = 0; while (1) { if (ca == s1s || cb == s2s) break; int na = nxt[ca][cb].first; int nb = nxt[ca][cb].second; if (na - ca == 1 && nb - cb == 1) { res[cnt] = s1[ca]; cnt++; } ca = na; cb = nb; } res[cnt] = '\0'; } } cout << res << endl; } return 0; }
[["+", 8, 9, 0, 1, 0, 11, 12, 16, 17, 33], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 12, 13]]
1
593
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <utility> #include <vector> using namespace std; /* constant */ const int MAX_N = 100; const int MAX_GN = MAX_N + 9; const long long LLINF = 1LL << 62; const string wdnames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; /* typedef */ typedef long long ll; struct Edge { int ui, vi; ll c; Edge(int _ui, int _vi, ll _c) : ui(_ui), vi(_vi), c(_c) {} }; typedef vector<Edge> ve; /* global variables */ int n, gn, st, gl; ll w, sumti; map<string, int> wdnhash; ve nbrs[MAX_GN]; ll minfs[MAX_GN], flows[MAX_GN][MAX_GN]; int prvs[MAX_GN]; /* subroutines */ /* main */ int main() { for (int i = 0; i < 7; i++) wdnhash[wdnames[i]] = i; for (;;) { cin >> n >> w; if (n == 0) break; gn = n; st = n + 7; gl = n + 8; for (int i = 0; i < gn; i++) nbrs[i].clear(); sumti = 0; for (int i = 0; i < n; i++) { ll ti; int ci; cin >> ti >> ci; sumti += ti; nbrs[st].push_back(Edge(st, i, ti)); for (int j = 0; j < ci; j++) { string wdname; cin >> wdname; int wj = n + wdnhash[wdname]; nbrs[i].push_back(Edge(i, wj, LLINF)); nbrs[wj].push_back(Edge(wj, i, 0)); } } for (int i = 0; i < 7; i++) nbrs[n + i].push_back(Edge(n + i, gl, w)); memset(flows, 0, sizeof(flows)); ll max_flow = 0; for (;;) { // printf("max_flow = %lld\n", max_flow); memset(prvs, -1, sizeof(prvs)); prvs[st] = st; minfs[st] = LLINF; queue<int> q; q.push(st); while (!q.empty()) { int ui = q.front(); q.pop(); if (ui == gl) break; ve &nbru = nbrs[ui]; for (ve::iterator vit = nbru.begin(); vit != nbru.end(); vit++) { int vi = vit->vi; ll vc = vit->c - flows[ui][vi]; if (prvs[vi] < 0 && vc > 0) { prvs[vi] = ui; minfs[vi] = (minfs[ui] < vc) ? minfs[ui] : vc; q.push(vi); } } } if (prvs[gl] < 0) break; ll min_flow = minfs[gl]; for (int j = gl; j != st;) { int i = prvs[j]; flows[i][j] += min_flow; flows[j][i] -= min_flow; j = i; } max_flow += min_flow; } cout << (max_flow >= sumti ? "Yes" : "No") << endl; } return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <utility> #include <vector> using namespace std; /* constant */ const int MAX_N = 100; const int MAX_GN = MAX_N + 9; const long long LLINF = 1LL << 62; const string wdnames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; /* typedef */ typedef long long ll; struct Edge { int ui, vi; ll c; Edge(int _ui, int _vi, ll _c) : ui(_ui), vi(_vi), c(_c) {} }; typedef vector<Edge> ve; /* global variables */ int n, gn, st, gl; ll w, sumti; map<string, int> wdnhash; ve nbrs[MAX_GN]; ll minfs[MAX_GN], flows[MAX_GN][MAX_GN]; int prvs[MAX_GN]; /* subroutines */ /* main */ int main() { for (int i = 0; i < 7; i++) wdnhash[wdnames[i]] = i; for (;;) { cin >> n >> w; if (n == 0) break; gn = n + 9; st = n + 7; gl = n + 8; for (int i = 0; i < gn; i++) nbrs[i].clear(); sumti = 0; for (int i = 0; i < n; i++) { ll ti; int ci; cin >> ti >> ci; sumti += ti; nbrs[st].push_back(Edge(st, i, ti)); for (int j = 0; j < ci; j++) { string wdname; cin >> wdname; int wj = n + wdnhash[wdname]; nbrs[i].push_back(Edge(i, wj, LLINF)); nbrs[wj].push_back(Edge(wj, i, 0)); } } for (int i = 0; i < 7; i++) nbrs[n + i].push_back(Edge(n + i, gl, w)); memset(flows, 0, sizeof(flows)); ll max_flow = 0; for (;;) { // printf("max_flow = %lld\n", max_flow); memset(prvs, -1, sizeof(prvs)); prvs[st] = st; minfs[st] = LLINF; queue<int> q; q.push(st); while (!q.empty()) { int ui = q.front(); q.pop(); if (ui == gl) break; ve &nbru = nbrs[ui]; for (ve::iterator vit = nbru.begin(); vit != nbru.end(); vit++) { int vi = vit->vi; ll vc = vit->c - flows[ui][vi]; if (prvs[vi] < 0 && vc > 0) { prvs[vi] = ui; minfs[vi] = (minfs[ui] < vc) ? minfs[ui] : vc; q.push(vi); } } } if (prvs[gl] < 0) break; ll min_flow = minfs[gl]; for (int j = gl; j != st;) { int i = prvs[j]; flows[i][j] += min_flow; flows[j][i] -= min_flow; j = i; } max_flow += min_flow; } cout << (max_flow >= sumti ? "Yes" : "No") << endl; } return 0; }
[["+", 8, 9, 0, 1, 0, 11, 12, 16, 17, 72], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 12, 13]]
1
743
#include <bits/stdc++.h> using namespace std; typedef complex<double> P; double eps = 1e-6; double PI = acos(-1); P getCrossPoint(P a, P b, P c, P d) { a -= d; b -= d; c -= d; return d + a + (b - a) * imag(a / c) / imag(a / c - b / c); } bool isParallel(P a, P b, P c, P d) { return (abs(imag((b - a) * conj(d - c))) < eps); } int lcm(int a, int b) { return a / __gcd(a, b) * b; } int n; int main() { while (1) { cin >> n; if (n == 0) break; double R = 1.0 / sqrt(3.0); P a = P(R, 0); P b = P(R - 0.5 * sqrt(3.0), 0.5); double lc = lcm(n, 3); double ti = 2.0 * PI / lc; P c = a, d = b; if (lc == 3) { printf("0.433013\n"); continue; } double T = 1e9; for (int i = 1; i <= lc / 3; i++) { c = a * P(cos(ti * i), sin(ti * i)); d = a * P(cos(ti * i), sin(ti * i)); if (!isParallel(a, b, c, d)) { P target = getCrossPoint(a, b, c, d); double k = min(abs(target - a), abs(target - b)); if (k > eps) T = min(T, k); } // if(i==2)i=lc/3-2; } double ans = T * R * 0.5; printf("%.8f\n", ans * lc); } }
#include <bits/stdc++.h> using namespace std; typedef complex<double> P; double eps = 1e-6; double PI = acos(-1); P getCrossPoint(P a, P b, P c, P d) { a -= d; b -= d; c -= d; return d + a + (b - a) * imag(a / c) / imag(a / c - b / c); } bool isParallel(P a, P b, P c, P d) { return (abs(imag((b - a) * conj(d - c))) < eps); } int lcm(int a, int b) { return a / __gcd(a, b) * b; } int n; int main() { while (1) { cin >> n; if (n == 0) break; double R = 1.0 / sqrt(3.0); P a = P(R, 0); P b = P(R - 0.5 * sqrt(3.0), 0.5); double lc = lcm(n, 3); double ti = 2.0 * PI / lc; P c = a, d = b; if (lc == 3) { printf("0.433013\n"); continue; } double T = 1e9; for (int i = 1; i <= lc / 3; i++) { c = a * P(cos(ti * i), sin(ti * i)); d = b * P(cos(ti * i), sin(ti * i)); if (!isParallel(a, b, c, d)) { P target = getCrossPoint(a, b, c, d); double k = min(abs(target - a), abs(target - b)); if (k > eps) T = min(T, k); } // if(i==2)i=lc/3-2; } double ans = T * R * 0.5; printf("%.8f\n", ans * lc); } }
[["-", 8, 9, 0, 1, 0, 11, 12, 16, 31, 22], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 31, 22]]
1
410
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; const double EPS = 1.0e-10; const double PI = acos(-1.0); class Point { public: double y, x; Point() { y = x = 0.0; } Point(double y0, double x0) { y = y0; x = x0; } Point(double theta) { y = sin(theta); x = cos(theta); } Point operator+(const Point &p) const { return Point(y + p.y, x + p.x); } Point operator-(const Point &p) const { return Point(y - p.y, x - p.x); } Point operator*(double a) const { return Point(y * a, x * a); } Point operator/(double a) const { return Point(y / a, x / a); } double length() const { return sqrt(y * y + x * x); } double dist(const Point &p) const { return sqrt(pow(y - p.y, 2) + pow(x - p.x, 2)); } double dot(const Point &p) const { return y * p.y + x * p.x; // |a|*|b|*cosθ } double cross(const Point &p) const { return x * p.y - y * p.x; // |a|*|b|*sinθ } }; class Line { public: double a, b, c; // a*x + b*y + c = 0 Line() { a = c = 0.0; b = 1.0; } Line(double a0, double b0, double c0) { a = a0; b = b0; c = c0; } Line(const Point &p1, const Point &p2) { // 2点を結ぶ直線 double dy = p2.y - p1.y; double dx = p2.x - p1.x; if (abs(dy / dx) < EPS) { a = 0.0; b = 1.0; c = -p1.y; } else if (abs(dx / dy) < EPS) { a = 1.0; b = 0.0; c = -p1.x; } else { a = -dy / dx; b = 1.0; c = dy / dx * p1.x - p1.y; } } }; bool linesIntersection(const Line &l1, const Line &l2, Point &intersection) { double s = l1.a * l2.b - l2.a * l1.b; if (abs(s) < EPS) return false; intersection = Point(l2.a * l1.c - l1.a * l2.c, l1.b * l2.c - l2.b * l1.c) / s; return true; } double polygonArea(const vector<Point> &p) { int n = p.size(); double ret = 0.0; for (int i = 0; i < n; ++i) ret += p[i].cross(p[(i + 1) % n]); return abs(ret) / 2.0; } int main() { double r = 1.0 / (2.0 * cos(PI / 6.0)); for (;;) { int n; cin >> n; if (n == 0) return 0; while (n % 3 == 0) n /= 3; if (n == 1) { vector<Point> p(3); p[0] = Point(0) * r; p[1] = Point(2.0 / 3.0 * PI) * r; p[2] = Point(4.0 / 3.0 * PI) * r; double ret = polygonArea(p); printf("%.10f\n", ret); continue; } Line l1(Point(0), Point(2.0 / 3.0 * PI)); Line l2(Point(2.0 / 3.0 * PI / n), Point(4.0 / 3.0 * PI + 2.0 / 3.0 * PI / n)); Point intersection; linesIntersection(l1, l2, intersection); vector<Point> p(4); p[0] = Point(0) * r; p[1] = Point(0, 0) * r; p[2] = Point(2.0 / 3.0 * PI / n) * r; p[3] = intersection * r; double ret = polygonArea(p); ret *= 3 * n; printf("%.10f\n", ret); } }
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; const double EPS = 1.0e-10; const double PI = acos(-1.0); class Point { public: double y, x; Point() { y = x = 0.0; } Point(double y0, double x0) { y = y0; x = x0; } Point(double theta) { y = sin(theta); x = cos(theta); } Point operator+(const Point &p) const { return Point(y + p.y, x + p.x); } Point operator-(const Point &p) const { return Point(y - p.y, x - p.x); } Point operator*(double a) const { return Point(y * a, x * a); } Point operator/(double a) const { return Point(y / a, x / a); } double length() const { return sqrt(y * y + x * x); } double dist(const Point &p) const { return sqrt(pow(y - p.y, 2) + pow(x - p.x, 2)); } double dot(const Point &p) const { return y * p.y + x * p.x; // |a|*|b|*cosθ } double cross(const Point &p) const { return x * p.y - y * p.x; // |a|*|b|*sinθ } }; class Line { public: double a, b, c; // a*x + b*y + c = 0 Line() { a = c = 0.0; b = 1.0; } Line(double a0, double b0, double c0) { a = a0; b = b0; c = c0; } Line(const Point &p1, const Point &p2) { // 2点を結ぶ直線 double dy = p2.y - p1.y; double dx = p2.x - p1.x; if (abs(dy / dx) < EPS) { a = 0.0; b = 1.0; c = -p1.y; } else if (abs(dx / dy) < EPS) { a = 1.0; b = 0.0; c = -p1.x; } else { a = -dy / dx; b = 1.0; c = dy / dx * p1.x - p1.y; } } }; bool linesIntersection(const Line &l1, const Line &l2, Point &intersection) { double s = l1.a * l2.b - l2.a * l1.b; if (abs(s) < EPS) return false; intersection = Point(l2.a * l1.c - l1.a * l2.c, l1.b * l2.c - l2.b * l1.c) / s; return true; } double polygonArea(const vector<Point> &p) { int n = p.size(); double ret = 0.0; for (int i = 0; i < n; ++i) ret += p[i].cross(p[(i + 1) % n]); return abs(ret) / 2.0; } int main() { double r = 1.0 / (2.0 * cos(PI / 6.0)); for (;;) { int n; cin >> n; if (n == 0) return 0; if (n % 3 == 0) n /= 3; if (n == 1) { vector<Point> p(3); p[0] = Point(0) * r; p[1] = Point(2.0 / 3.0 * PI) * r; p[2] = Point(4.0 / 3.0 * PI) * r; double ret = polygonArea(p); printf("%.10f\n", ret); continue; } Line l1(Point(0), Point(2.0 / 3.0 * PI)); Line l2(Point(2.0 / 3.0 * PI / n), Point(4.0 / 3.0 * PI + 2.0 / 3.0 * PI / n)); Point intersection; linesIntersection(l1, l2, intersection); vector<Point> p(4); p[0] = Point(0) * r; p[1] = Point(0, 0) * r; p[2] = Point(2.0 / 3.0 * PI / n) * r; p[3] = intersection * r; double ret = polygonArea(p); ret *= 3 * n; printf("%.10f\n", ret); } }
[["-", 8, 9, 0, 7, 8, 9, 0, 52, 0, 89], ["+", 8, 9, 0, 7, 8, 9, 0, 57, 0, 121]]
1
946
import java.util.Scanner; public class Main { public static Scanner sc; public static void main(String[] args) throws Exception { sc = new Scanner(System.in); while (sc.hasNext()) { int u = 0; int d = 0; int n = sc.nextInt(); int a = sc.nextInt(); for (int i = 0; i < n - 1; i++) { int b = sc.nextInt(); int diff = b - a; if (diff > u) { u = diff; } else if (diff < d) { d = diff; } a = b; } System.out.println(u + " " + d * -1); } } }
import java.util.Scanner; public class Main { public static Scanner sc; public static void main(String[] args) throws Exception { sc = new Scanner(System.in); sc.nextInt(); while (sc.hasNext()) { int u = 0; int d = 0; int n = sc.nextInt(); int a = sc.nextInt(); for (int i = 0; i < n - 1; i++) { int b = sc.nextInt(); int diff = b - a; if (diff > u) { u = diff; } else if (diff < d) { d = diff; } a = b; } System.out.println(u + " " + d * -1); } } }
[["+", 0, 195, 8, 196, 0, 1, 0, 492, 500, 22], ["+", 0, 195, 8, 196, 0, 1, 0, 492, 0, 131], ["+", 0, 195, 8, 196, 0, 1, 0, 492, 141, 22], ["+", 8, 196, 0, 1, 0, 492, 3, 4, 0, 24], ["+", 8, 196, 0, 1, 0, 492, 3, 4, 0, 25], ["+", 8, 498, 0, 195, 8, 196, 0, 1, 0, 35]]
3
160
#include <stdio.h> #include <stdlib.h> int main() { int t, n, h[100], i, j, k, s; scanf("%d", &t); for (i = 0; i < t; i++) { scanf("%d", &n); for (j = 0; j < n; j++) { scanf("%d", &h[j]); } for (j = 0; j < n - 1; j++) { h[j] = h[j + 1] - h[j]; } if (n = 2) { if (h[0] >= 0) printf("%d 0\n", h[0]); else printf("0 %d\n", abs(h[0])); } else { for (j = 0; j < n - 2; j++) { for (k = 0; k < n - 2; k++) { if (h[k] < h[k + 1]) { s = h[k]; h[k] = h[k + 1]; h[k + 1] = s; } } } if (h[0] < 0) h[0] = 0; if (h[n - 2] > 0) h[n - 2] = 0; printf("%d %d\n", h[0], abs(h[n - 2])); } } return 0; }
#include <stdio.h> #include <stdlib.h> int main() { int t, n, h[100], i, j, k, s; scanf("%d", &t); for (i = 0; i < t; i++) { scanf("%d", &n); for (j = 0; j < n; j++) { scanf("%d", &h[j]); } for (j = 0; j < n - 1; j++) { h[j] = h[j + 1] - h[j]; } if (n == 2) { if (h[0] >= 0) printf("%d 0\n", h[0]); else printf("0 %d\n", abs(h[0])); } else { for (j = 0; j < n - 2; j++) { for (k = 0; k < n - 2; k++) { if (h[k] < h[k + 1]) { s = h[k]; h[k] = h[k + 1]; h[k + 1] = s; } } } if (h[0] < 0) h[0] = 0; if (h[n - 2] > 0) h[n - 2] = 0; printf("%d %d\n", h[0], abs(h[n - 2])); } } return 0; }
[["-", 8, 9, 0, 57, 15, 23, 0, 11, 17, 32], ["+", 8, 9, 0, 57, 15, 23, 0, 16, 17, 60]]
0
315
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; int main() { int n, m; cin >> m; while (m--) { cin >> n; int b, a; cin >> b; int Max = 0, Min = 100000000; while (--n) { cin >> a; int dif = a - b; Max = max(Max, dif); Min = min(Min, dif); b = a; } if (Min < 0) Min *= -1; cout << Max << " " << Min << endl; } }
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; int main() { int n, m; cin >> m; while (m--) { cin >> n; int b, a; cin >> b; int Max = 0, Min = 100000000; while (--n) { cin >> a; int dif = a - b; Max = max(Max, dif); Min = min(Min, dif); b = a; } if (Min < 0) Min *= -1; else Min = 0; cout << Max << " " << Min << endl; } }
[["+", 0, 52, 8, 9, 0, 57, 75, 76, 0, 95], ["+", 0, 57, 75, 76, 0, 1, 0, 11, 31, 22], ["+", 0, 57, 75, 76, 0, 1, 0, 11, 17, 32], ["+", 0, 57, 75, 76, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 57, 75, 76, 0, 1, 0, 35]]
1
118
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int n; cin >> n; int h[100]; for (int j = 0; j < n; j++) { cin >> h[j]; } int max1 = 0, min1 = 0; for (int j = 1; j < n; j++) { if (h[j - 1] < h[j]) max1 = max(max1, h[j] - h[j - 1]); if (h[j - 1] > h[j]) min1 = max(min1, h[j - 1] - h[j]); cout << max1 << ' ' << min1 << endl; } // cout << max1 << ' ' << min1 << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int n; cin >> n; int h[100]; for (int j = 0; j < n; j++) { cin >> h[j]; } int max1 = 0, min1 = 0; for (int j = 1; j < n; j++) { if (h[j - 1] < h[j]) max1 = max(max1, h[j] - h[j - 1]); if (h[j - 1] > h[j]) min1 = max(min1, h[j - 1] - h[j]); // cout << max1 << ' ' << min1 << endl; } cout << max1 << ' ' << min1 << endl; } return 0; }
[["+", 0, 7, 8, 9, 0, 7, 8, 9, 0, 46], ["-", 0, 7, 8, 9, 0, 7, 8, 9, 0, 46]]
1
182
#include <iostream> using namespace std; int main() { int t, n; cin >> t; while (t--) { int mup = 0, mdown = 0, height[101] = {0}; cin >> n; for (int i = 0; i < n; ++i) { cin >> height[i]; } for (int i = 1; i < n; ++i) { int diff = height[i] - height[i - 1]; if (diff > 0 && diff > mup) mup = diff; if (diff < 0 && diff < mdown) mdown = -diff; } cout << mup << " " << mdown << endl; } return 0; }
#include <iostream> using namespace std; int main() { int t, n; cin >> t; while (t--) { int mup = 0, mdown = 0, height[101] = {0}; cin >> n; for (int i = 0; i < n; ++i) { cin >> height[i]; } for (int i = 1; i < n; ++i) { int diff = height[i] - height[i - 1]; if (diff > 0 && diff > mup) mup = diff; if (diff < 0 && -diff > mdown) mdown = -diff; } cout << mup << " " << mdown << endl; } return 0; }
[["+", 15, 339, 51, 16, 12, 16, 31, 91, 17, 33], ["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 18], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 47]]
1
148
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; int main() { for (;;) { int n; cin >> n; if (n == 0) return 0; vector<string> s1(n), s2(n); vector<int> t1(n), t2(n); map<string, int> index; for (int i = 0; i < n; ++i) { cin >> s1[i] >> t1[i] >> s2[i] >> t2[i]; index[s1[i]] = i; } vector<int> sup(n); vector<vector<int>> edges(n); for (int i = 0; i < n; ++i) { sup[i] = index[s2[i]]; if (s1[i] != s2[i]) edges[index[s2[i]]].push_back(i); } int ret = 0; vector<bool> make(n, false); for (int i = 0; i < n; ++i) { if (make[i]) continue; int j = i; vector<bool> check(n, false); while (!check[j]) { check[j] = true; j = sup[j]; } check.assign(n, false); int k = j; while (!check[j]) { if (t1[j] - t2[j] > t1[k] - t2[k]) k = j; check[j] = true; j = sup[j]; } ret += t1[k] - t2[k]; queue<int> q; q.push(k); while (!q.empty()) { int a = q.front(); q.pop(); ret += t2[a]; make[a] = true; for (unsigned l = 0; l < edges[a].size(); ++l) { int b = edges[a][l]; if (!make[b]) q.push(b); } } } cout << ret << endl; } }
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; int main() { for (;;) { int n; cin >> n; if (n == 0) return 0; vector<string> s1(n), s2(n); vector<int> t1(n), t2(n); map<string, int> index; for (int i = 0; i < n; ++i) { cin >> s1[i] >> t1[i] >> s2[i] >> t2[i]; index[s1[i]] = i; } vector<int> sup(n); vector<vector<int>> edges(n); for (int i = 0; i < n; ++i) { sup[i] = index[s2[i]]; if (s1[i] != s2[i]) edges[index[s2[i]]].push_back(i); } int ret = 0; vector<bool> make(n, false); for (int i = 0; i < n; ++i) { if (make[i]) continue; int j = i; vector<bool> check(n, false); while (!check[j]) { check[j] = true; j = sup[j]; } check.assign(n, false); int k = j; while (!check[j]) { if (t1[j] - t2[j] < t1[k] - t2[k]) k = j; check[j] = true; j = sup[j]; } ret += t1[k] - t2[k]; queue<int> q; q.push(k); while (!q.empty()) { int a = q.front(); q.pop(); ret += t2[a]; make[a] = true; for (unsigned l = 0; l < edges[a].size(); ++l) { int b = edges[a][l]; if (!make[b]) q.push(b); } } } cout << ret << endl; } }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 47], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18]]
1
494
#include <algorithm> #include <cmath> #include <complex> #include <cstring> #include <iostream> #include <vector> using namespace std; const double EPS = (1e-10); bool EQ(double a, double b) { return abs(a - b) < EPS; } typedef complex<double> P; typedef pair<double, double> pdd; double calc(double a1, double b1, double a2, double b2, double x, double r) { return (a2 * a1 * x * x * x) / 3.0 + (x * x * (a1 * a2 * r + b2 * a1 + a2 * b1)) / 2.0 + (a2 * b1 * r + b1 * b2) * x; } int main() { int n; double r; while (cin >> n >> r && !(n == 0 && EQ(r, 0))) { vector<P> dots; vector<pdd> co; dots.push_back(P(-1000, 0)); for (int i = 0; i < n; i++) { double x, y; cin >> x >> y; dots.push_back(P(x, y)); } dots.push_back(P(1000, 0)); double sum = 0; for (int i = 1; i <= n + 1; i++) { double a = (dots[i - 1].imag() - dots[i].imag()) / (dots[i - 1].real() - dots[i].real()); double b = dots[i - 1].imag() - a * dots[i - 1].real(); co.push_back(pdd(a, b)); } double ax = dots[1].real(); double bx = ax + r; int nxtApos = n + 2; int nxtBpos = n + 2; // rツづ個値ツつェツづづ個凝ヲツ甘板づ可甘慊づ慊づェツづつ「ツづゥツつゥツづーツ探ツ催オ for (int i = 0; i <= n + 1; i++) { if (dots[i].real() > bx && !EQ(dots[i].real(), bx)) { nxtBpos = i; break; } } for (int i = 0; i <= n + 1; i++) { if (dots[i].real() > ax && !EQ(dots[i].real(), ax)) { nxtApos = i; break; } } bool rev = false; if (ax > bx) { swap(ax, bx); swap(nxtApos, nxtBpos); rev = true; } // nxtBposツつェnツづ可づ按づゥツづ慊づ堕アツつッツづゥ while (nxtBpos != n + 2) { // ツ篠淞づ個湘ェツ渉環づ鳴づ個仰猟猟」ツつェツ渉ャツつウツつ「ツづ個づ債づつソツづァツつゥ double adis = dots[nxtApos].real() - ax; double bdis = dots[nxtBpos].real() - bx; double dis = min(adis, bdis); double t = ax + dis; double s = ax; if (!rev) { t = bx + dis; s = bx; sum += calc(co[nxtBpos - 1].first, co[nxtBpos - 1].second, co[nxtApos - 1].first, co[nxtApos - 1].second, t, r); sum -= calc(co[nxtBpos - 1].first, co[nxtBpos - 1].second, co[nxtApos - 1].first, co[nxtApos - 1].second, s, r); } else { sum += calc(co[nxtApos - 1].first, co[nxtApos - 1].second, co[nxtBpos - 1].first, co[nxtBpos - 1].second, t, r); sum -= calc(co[nxtApos - 1].first, co[nxtApos - 1].second, co[nxtBpos - 1].first, co[nxtBpos - 1].second, s, r); } ax += dis; bx += dis; if (EQ(adis, bdis)) { nxtApos++; nxtBpos++; } else if (adis < bdis) nxtApos++; else nxtBpos++; } printf("%.10f\n", sum); } return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstring> #include <iostream> #include <vector> using namespace std; const double EPS = (1e-10); bool EQ(double a, double b) { return abs(a - b) < EPS; } typedef complex<double> P; typedef pair<double, double> pdd; double calc(double a1, double b1, double a2, double b2, double x, double r) { return (a2 * a1 * x * x * x) / 3.0 + (x * x * (a1 * a2 * r + b2 * a1 + a2 * b1)) / 2.0 + (a2 * b1 * r + b1 * b2) * x; } int main() { int n; double r; while (cin >> n >> r && !(n == 0 && EQ(r, 0))) { vector<P> dots; vector<pdd> co; dots.push_back(P(-1000, 0)); for (int i = 0; i < n; i++) { double x, y; cin >> x >> y; dots.push_back(P(x, y)); } dots.push_back(P(1000, 0)); double sum = 0; for (int i = 1; i <= n + 1; i++) { double a = (dots[i - 1].imag() - dots[i].imag()) / (dots[i - 1].real() - dots[i].real()); double b = dots[i - 1].imag() - a * dots[i - 1].real(); co.push_back(pdd(a, b)); } double ax = dots[1].real(); double bx = ax + r; int nxtApos = n + 2; int nxtBpos = n + 2; // rツづ個値ツつェツづづ個凝ヲツ甘板づ可甘慊づ慊づェツづつ「ツづゥツつゥツづーツ探ツ催オ for (int i = 0; i <= n + 1; i++) { if (dots[i].real() > bx && !EQ(dots[i].real(), bx)) { nxtBpos = i; break; } } for (int i = 0; i <= n + 1; i++) { if (dots[i].real() > ax && !EQ(dots[i].real(), ax)) { nxtApos = i; break; } } bool rev = false; if (ax > bx) { swap(ax, bx); swap(nxtApos, nxtBpos); rev = true; } // nxtBposツつェnツづ可づ按づゥツづ慊づ堕アツつッツづゥ while (nxtBpos != n + 2) { // ツ篠淞づ個湘ェツ渉環づ鳴づ個仰猟猟」ツつェツ渉ャツつウツつ「ツづ個づ債づつソツづァツつゥ double adis = dots[nxtApos].real() - ax; double bdis = dots[nxtBpos].real() - bx; double dis = min(adis, bdis); double t = ax + dis; double s = ax; if (rev) { t = bx + dis; s = bx; sum += calc(co[nxtBpos - 1].first, co[nxtBpos - 1].second, co[nxtApos - 1].first, co[nxtApos - 1].second, t, r); sum -= calc(co[nxtBpos - 1].first, co[nxtBpos - 1].second, co[nxtApos - 1].first, co[nxtApos - 1].second, s, r); } else { sum += calc(co[nxtApos - 1].first, co[nxtApos - 1].second, co[nxtBpos - 1].first, co[nxtBpos - 1].second, t, r); sum -= calc(co[nxtApos - 1].first, co[nxtApos - 1].second, co[nxtBpos - 1].first, co[nxtBpos - 1].second, s, r); } ax += dis; bx += dis; if (EQ(adis, bdis)) { nxtApos++; nxtBpos++; } else if (adis < bdis) nxtApos++; else nxtBpos++; } printf("%.10f\n", sum); } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 91, 17, 111]]
1
844