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
while True: M,T,P,R=list(map(int,input().split())) if M==T==P==R==0:break PB=[[False]*P for i in range(T+1)] PN=[[0]*P for i in range(T+1)] TM=[M]*(T+1) for i in range(R): m,t,p,r=list(map(int,input().split())) if r==0: PB[t][p-1]=True TM[t]=m else: PN[t][p-1]+=1 for i in range(1,T+1): for j in range(P): if PB[i][j]: TM[i]+=PN[i][j]*20 solved=[sum(i) for i in PB] ans=[] for i in range(1,T+1): ans.append((-solved[i],TM[i],i)) import operator ans=ans[::-1] ans.sort(key=operator.itemgetter(1)) ans.sort(key=operator.itemgetter(0)) s=str(ans[0][2]) for i in range(1,T): if (ans[i][0]!=ans[i-1][0]) or (ans[i][1]!=ans[i-1][1]): s+=","+str(ans[i][2]) continue s+="="+str(ans[i][2]) print(s)
while True: M,T,P,R=list(map(int,input().split())) if M==T==P==R==0:break PB=[[False]*P for i in range(T+1)] PN=[[0]*P for i in range(T+1)] TM=[M]*(T+1) for i in range(R): m,t,p,r=list(map(int,input().split())) if r==0: PB[t][p-1]=True TM[t]+=m else: PN[t][p-1]+=1 for i in range(1,T+1): for j in range(P): if PB[i][j]: TM[i]+=PN[i][j]*20 solved=[sum(i) for i in PB] ans=[] for i in range(1,T+1): ans.append((-solved[i],TM[i],i)) import operator ans=ans[::-1] ans.sort(key=operator.itemgetter(1)) ans.sort(key=operator.itemgetter(0)) s=str(ans[0][2]) for i in range(1,T): if (ans[i][0]!=ans[i-1][0]) or (ans[i][1]!=ans[i-1][1]): s+=","+str(ans[i][2]) continue s+="="+str(ans[i][2]) print(s)
[["-", 0, 57, 64, 196, 0, 1, 0, 662, 0, 32], ["+", 0, 57, 64, 196, 0, 1, 0, 677, 17, 107]]
5
378
#include <algorithm> #include <cctype> #include <iostream> #include <string> #include <vector> using namespace std; const char *p; string str; int parse() { int num; char *e; p++; if (*p == '[') { vector<int> v; for (;;) { if (*p == '[') { num = parse(); v.push_back(num); p++; } else break; } p++; sort(v.begin(), v.end()); int sum = 0; for (int i = 0; i < v.size() / 2 + 1; i++) sum += v[i]; return sum; } if (isdigit(*p)) { num = strtol(p, &e, 10); p = e; return num / 2 + 1; } } int main() { int n; cin >> n; cin.ignore(); for (int i = 0; i < n; i++) { getline(cin, str); p = str.c_str(); cout << parse() << endl; } return 0; }
#include <algorithm> #include <cctype> #include <iostream> #include <string> #include <vector> using namespace std; const char *p; string str; int parse() { int num; char *e; p++; if (*p == '[') { vector<int> v; for (;;) { if (*p == '[') { num = parse(); v.push_back(num); p++; } else break; } sort(v.begin(), v.end()); int sum = 0; for (int i = 0; i < v.size() / 2 + 1; i++) sum += v[i]; return sum; } if (isdigit(*p)) { num = strtol(p, &e, 10); p = e; return num / 2 + 1; } } int main() { int n; cin >> n; cin.ignore(); for (int i = 0; i < n; i++) { getline(cin, str); p = str.c_str(); cout << parse() << endl; } return 0; }
[["-", 0, 57, 64, 9, 0, 1, 0, 27, 28, 22], ["-", 0, 57, 64, 9, 0, 1, 0, 27, 17, 29], ["-", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35]]
1
241
#include <algorithm> #include <cctype> #include <iostream> #include <string> #include <vector> using namespace std; string S; size_t cur; int digit() { int n = S[cur++] - '0'; return n; } int number() { int n = digit(); while (isdigit(S[cur])) n = n * 10 + digit(); return n; } int expression() { vector<int> list; int ans = 0; while (S[cur] != ']') { if (S[cur] == '[') cur++, list.push_back(expression()); else if (isdigit(S[cur])) list.push_back(number()); else if (cur == S.size()) return list[0]; } if (S[cur] == ']') { cur++; sort(list.begin(), list.end()); if (list.size() == 1) { ans += (list[0] / 2 + 1); } else { int n = (list.size() / 2 + 1); for (int i = 0; i < n; i++) ans += list[i]; } } return ans; } int main(void) { int n; for (int i = 0; i < n; i++) { cin >> S; cur = 0; int ans = expression(); cout << ans << endl; } return 0; }
#include <algorithm> #include <cctype> #include <iostream> #include <string> #include <vector> using namespace std; string S; size_t cur; int digit() { int n = S[cur++] - '0'; return n; } int number() { int n = digit(); while (isdigit(S[cur])) n = n * 10 + digit(); return n; } int expression() { vector<int> list; int ans = 0; while (S[cur] != ']') { if (S[cur] == '[') cur++, list.push_back(expression()); else if (isdigit(S[cur])) list.push_back(number()); else if (cur == S.size()) return list[0]; } if (S[cur] == ']') { cur++; sort(list.begin(), list.end()); if (list.size() == 1) { ans += (list[0] / 2 + 1); } else { int n = (list.size() / 2 + 1); for (int i = 0; i < n; i++) ans += list[i]; } } return ans; } int main(void) { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> S; cur = 0; int ans = expression(); cout << ans << endl; } return 0; }
[["+", 0, 14, 8, 9, 0, 1, 0, 16, 31, 22], ["+", 0, 14, 8, 9, 0, 1, 0, 16, 17, 152], ["+", 0, 14, 8, 9, 0, 1, 0, 16, 12, 22], ["+", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35]]
1
316
#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> using namespace std; int expr(string &str, int &itr); int number(string &str, int &itr); int main() { int N; int start; string str; cin >> N; for (int i = 0; i < N; i++) { cin >> str; start = 0; cout << expr(str, start) << endl; } return 0; } int expr(string &str, int &itr) { int ans = 0; vector<int> vect(0); for (; str[itr] == '[';) { itr++; if (isdigit(str[itr])) { vect.push_back((int)ceil(number(str, itr) / 2.0)); } else { vect.push_back(expr(str, itr)); } for (; str[itr] == ']';) itr++; } sort(vect.begin(), vect.end()); for (int i = 0; i < vect.size() / 2 + 1; i++) { ans += vect[i]; } return ans; } int number(string &str, int &itr) { int num = 0; for (; isdigit(str[itr]); itr++) { num = num * 10 + (str[itr] - '0'); } return num; }
#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> using namespace std; int expr(string &str, int &itr); int number(string &str, int &itr); int main() { int N; int start; string str; cin >> N; for (int i = 0; i < N; i++) { cin >> str; start = 0; cout << expr(str, start) << endl; } return 0; } int expr(string &str, int &itr) { int ans = 0; vector<int> vect(0); for (; str[itr] == '[';) { itr++; if (isdigit(str[itr])) { vect.push_back((int)ceil(number(str, itr) / 2.0)); } else { vect.push_back(expr(str, itr)); } if (str[itr] == ']') itr++; } sort(vect.begin(), vect.end()); for (int i = 0; i < vect.size() / 2 + 1; i++) { ans += vect[i]; } return ans; } int number(string &str, int &itr) { int num = 0; for (; isdigit(str[itr]); itr++) { num = num * 10 + (str[itr] - '0'); } return num; }
[["-", 8, 9, 0, 7, 8, 9, 0, 7, 0, 88], ["+", 8, 9, 0, 7, 8, 9, 0, 57, 0, 121], ["-", 8, 9, 0, 7, 8, 9, 0, 7, 0, 35]]
1
306
//============================================================================ // Name : aoj1188.cpp // Author : afterCmidday // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <algorithm> #include <cctype> #include <cstdlib> #include <functional> #include <iostream> #include <string> #include <vector> using namespace std; string s; int solve(int l, int r) { if (s[l] == '[') { int i, cnt = 0; // 次に要素番号 l の後ろに'['があった場合 +1,']'があった場合 // -1 とするcntである vector<int> each; while (l < r) { for (i = l + 1; i < r && !(s[i] == ']' && cnt == 0); i++) { // そのcntが0でかつs[l]が']'(この選挙区の終端)であるときに終了 if (s[i] == '[') { cnt++; } else if (s[i] == ']') { cnt--; } } each.push_back(solve(l + 1, i)); l = i + 1; } sort(each.begin(), each.end()); int sum = 0; for (int i = 0; i < (each.size() + 1) / 2; i++) { // cout << each[i] << " "; sum += each[i]; } // cout << endl; // cout << sum << endl; return sum; } if (isdigit(s[l])) { int d = 0; for (int i = l; i < r && isdigit(s[i]); i++) { d = d * 10 + (s[i] - '0'); } return (d + 1) / 2; } } int main(void) { int n; cin >> n; cin.get(); cin.get(); for (int i = 0; i < n; i++) { getline(cin, s); cout << solve(1, s.size() - 2) << endl; } }
//============================================================================ // Name : aoj1188.cpp // Author : afterCmidday // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <algorithm> #include <cctype> #include <cstdlib> #include <functional> #include <iostream> #include <string> #include <vector> using namespace std; string s; int solve(int l, int r) { if (s[l] == '[') { int i, cnt = 0; // 次に要素番号 l の後ろに'['があった場合 +1,']'があった場合 // -1 とするcntである vector<int> each; while (l < r) { for (i = l + 1; i < r && !(s[i] == ']' && cnt == 0); i++) { // そのcntが0でかつs[l]が']'(この選挙区の終端)であるときに終了 if (s[i] == '[') { cnt++; } else if (s[i] == ']') { cnt--; } } each.push_back(solve(l + 1, i)); l = i + 1; } sort(each.begin(), each.end()); int sum = 0; for (int i = 0; i < (each.size() + 1) / 2; i++) { // cout << each[i] << " "; sum += each[i]; } // cout << endl; // cout << sum << endl; return sum; } if (isdigit(s[l])) { int d = 0; for (int i = l; i < r && isdigit(s[i]); i++) { d = d * 10 + (s[i] - '0'); } return (d + 1) / 2; // 過半数 } } int main(void) { int n; cin >> n; cin.get(); for (int i = 0; i < n; i++) { getline(cin, s); cout << solve(1, s.size() - 2) << endl; } }
[["-", 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], ["-", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35]]
1
354
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; int main() { while (1) { int n, m, c, s, g; int a[110][110][30]; fill(&a[0][0][0], &a[109][109][29], 1000000000); cin >> n >> m >> c >> s >> g; if (n == 0 && m == 0 && c == 0 && s == 0 && g == 0) { break; } for (int i = 0; i < m; i++) { int p, q, d, com; cin >> p >> q >> d >> com; a[p - 1][q - 1][com - 1] = min(d, a[p - 1][q - 1][com - 1]); a[p - 1][q - 1][com - 1] = a[p - 1][q - 1][com - 1]; } int p[30] = {}; for (int i = 0; i < c; i++) { cin >> p[i]; } int coin[30][50][2] = {}; for (int i = 0; i < c; i++) { for (int t = 0; t < p[i] - 1; t++) { cin >> coin[i][t][0]; } for (int t = 0; t < p[i]; t++) { cin >> coin[i][t][1]; } } for (int i = 0; i < c; i++) { for (int t = 0; t < n; t++) { for (int h = 0; h < n; h++) { for (int k = 0; k < n; k++) { if (a[h][t][i] != 1000000000 && a[t][k][i] != 1000000000) { a[h][k][i] = min(a[h][t][i] + a[t][k][i], a[h][k][i]); } } } } } /* cout<<endl; for(int i=0;i<c;i++){ for(int t=0;t<n;t++){ for(int h=0;h<n;h++){ if(a[t][h][i]==1000000000){cout<<-1<<" ";} else{ int k=0; if(a[t][h][i]>coin[i][p[i]-2][0]){ k=coin[i][p[i]-1][1]*(a[t][h][i]-coin[i][p[i]-2][0]); } for(int q=0;q<p[i]-1;q++){ int w=a[t][h][i]; if(w>coin[i][q][0]){w=coin[i][q][0];} if(q!=0){w-=coin[i][q-1][0];} if(w<0){break;} k+=w*coin[i][q][1]; } cout<<a[t][h][i]<<" ";} } cout<<endl; } cout<<endl; } */ /* cout<<endl; for(int i=0;i<c;i++){ for(int t=0;t<n;t++){ for(int h=0;h<n;h++){ if(a[t][h][i]==1000000000){cout<<-1<<" ";} else{ int k=0; if(a[t][h][i]>coin[i][p[i]-2][0]){ k=coin[i][p[i]-1][1]*(a[t][h][i]-coin[i][p[i]-2][0]); } for(int q=0;q<p[i]-1;q++){ int w=a[t][h][i]; if(w>coin[i][q][0]){w=coin[i][q][0];} if(q!=0){w-=coin[i][q-1][0];} if(w<0){break;} k+=w*coin[i][q][1]; } cout<<k<<" ";} } cout<<endl; } cout<<endl; } */ int dis[110][110]; fill(&dis[0][0], &dis[109][109], 1000000000); for (int i = 0; i < c; i++) { for (int t = 0; t < n; t++) { for (int h = 0; h < n; h++) { if (a[t][h][i] != 1000000000) { int k = 0; if (a[t][h][i] > coin[i][p[i] - 2][0]) { k = coin[i][p[i] - 1][1] * (a[t][h][i] - coin[i][p[i] - 2][0]); } for (int q = 0; q < p[i] - 1; q++) { int w = a[t][h][i]; if (w > coin[i][q][0]) { w = coin[i][q][0]; } if (q != 0) { w -= coin[i][q - 1][0]; } if (w < 0) { break; } k += w * coin[i][q][1]; } dis[t][h] = min(k, dis[t][h]); } } } } /* for(int i=0;i<n;i++){ for(int t=0;t<n;t++){ if(dis[i][t]==1000000000){cout<<-1<<" ";} else{cout<<dis[i][t]<<" ";} } cout<<endl; } cout<<endl; */ for (int i = 0; i < n; i++) { for (int t = 0; t < n; t++) { for (int h = 0; h < n; h++) { dis[t][h] = min(dis[t][h], dis[t][i] + dis[i][h]); } } } if (dis[s - 1][g - 1] == 1000000000) { cout << "-1" << endl; } else { cout << dis[s - 1][g - 1] << endl; } } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; int main() { while (1) { int n, m, c, s, g; int a[110][110][30]; fill(&a[0][0][0], &a[109][109][29], 1000000000); cin >> n >> m >> c >> s >> g; if (n == 0 && m == 0 && c == 0 && s == 0 && g == 0) { break; } for (int i = 0; i < m; i++) { int p, q, d, com; cin >> p >> q >> d >> com; a[p - 1][q - 1][com - 1] = min(d, a[p - 1][q - 1][com - 1]); a[q - 1][p - 1][com - 1] = a[p - 1][q - 1][com - 1]; } int p[30] = {}; for (int i = 0; i < c; i++) { cin >> p[i]; } int coin[30][50][2] = {}; for (int i = 0; i < c; i++) { for (int t = 0; t < p[i] - 1; t++) { cin >> coin[i][t][0]; } for (int t = 0; t < p[i]; t++) { cin >> coin[i][t][1]; } } for (int i = 0; i < c; i++) { for (int t = 0; t < n; t++) { for (int h = 0; h < n; h++) { for (int k = 0; k < n; k++) { if (a[h][t][i] != 1000000000 && a[t][k][i] != 1000000000) { a[h][k][i] = min(a[h][t][i] + a[t][k][i], a[h][k][i]); } } } } } /* cout<<endl; for(int i=0;i<c;i++){ for(int t=0;t<n;t++){ for(int h=0;h<n;h++){ if(a[t][h][i]==1000000000){cout<<-1<<" ";} else{ int k=0; if(a[t][h][i]>coin[i][p[i]-2][0]){ k=coin[i][p[i]-1][1]*(a[t][h][i]-coin[i][p[i]-2][0]); } for(int q=0;q<p[i]-1;q++){ int w=a[t][h][i]; if(w>coin[i][q][0]){w=coin[i][q][0];} if(q!=0){w-=coin[i][q-1][0];} if(w<0){break;} k+=w*coin[i][q][1]; } cout<<a[t][h][i]<<" ";} } cout<<endl; } cout<<endl; } */ /* cout<<endl; for(int i=0;i<c;i++){ for(int t=0;t<n;t++){ for(int h=0;h<n;h++){ if(a[t][h][i]==1000000000){cout<<-1<<" ";} else{ int k=0; if(a[t][h][i]>coin[i][p[i]-2][0]){ k=coin[i][p[i]-1][1]*(a[t][h][i]-coin[i][p[i]-2][0]); } for(int q=0;q<p[i]-1;q++){ int w=a[t][h][i]; if(w>coin[i][q][0]){w=coin[i][q][0];} if(q!=0){w-=coin[i][q-1][0];} if(w<0){break;} k+=w*coin[i][q][1]; } cout<<k<<" ";} } cout<<endl; } cout<<endl; } */ int dis[110][110]; fill(&dis[0][0], &dis[109][109], 1000000000); for (int i = 0; i < c; i++) { for (int t = 0; t < n; t++) { for (int h = 0; h < n; h++) { if (a[t][h][i] != 1000000000) { int k = 0; if (a[t][h][i] > coin[i][p[i] - 2][0]) { k = coin[i][p[i] - 1][1] * (a[t][h][i] - coin[i][p[i] - 2][0]); } for (int q = 0; q < p[i] - 1; q++) { int w = a[t][h][i]; if (w > coin[i][q][0]) { w = coin[i][q][0]; } if (q != 0) { w -= coin[i][q - 1][0]; } if (w < 0) { break; } k += w * coin[i][q][1]; } dis[t][h] = min(k, dis[t][h]); } } } } /* for(int i=0;i<n;i++){ for(int t=0;t<n;t++){ if(dis[i][t]==1000000000){cout<<-1<<" ";} else{cout<<dis[i][t]<<" ";} } cout<<endl; } cout<<endl; */ for (int i = 0; i < n; i++) { for (int t = 0; t < n; t++) { for (int h = 0; h < n; h++) { dis[t][h] = min(dis[t][h], dis[t][i] + dis[i][h]); } } } if (dis[s - 1][g - 1] == 1000000000) { cout << "-1" << endl; } else { cout << dis[s - 1][g - 1] << endl; } } return 0; }
[["-", 28, 69, 28, 69, 341, 342, 0, 16, 31, 22], ["+", 28, 69, 28, 69, 341, 342, 0, 16, 31, 22], ["-", 31, 69, 28, 69, 341, 342, 0, 16, 31, 22], ["+", 31, 69, 28, 69, 341, 342, 0, 16, 31, 22]]
1
952
#include <cmath> #include <complex> #include <iomanip> #include <iostream> #include <queue> #include <tuple> #include <vector> using namespace std; typedef complex<double> Point; #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(c) (c).begin(), (c).end() const double eps = 1e-8, inf = 1e+8; template <typename T> T sqr(T x) { return x * x; } double cross(const Point &a, const Point &b) { return imag(conj(a) * b); } struct Line : vector<Point> { Line(const Point &a, const Point &b) { push_back(a); push_back(b); } }; bool interLS(const Line &l, const Line &s) { return cross(l[1] - l[0], s[0] - l[0]) * cross(l[1] - l[0], s[1] - l[0]) < eps; } struct Circle : Point { double r; Circle(const Point &a, double r) : Point(a), r(r) {} }; vector<Point> xptCC(const Circle &c1, const Circle &c2) { vector<Point> res; double d = abs(c1 - c2); if (d < eps) return res; double rc = (sqr(d) + sqr(c1.r) - sqr(c2.r)) / (2. * d); double rs = sqrt(sqr(c1.r) - sqr(rc)); Point diff = (c2 - c1) / d; res.push_back(c1 + diff * Point(rc, rs)); res.push_back(c1 + diff * Point(rc, -rs)); return res; } int n; vector<Circle> cir; double solve() { vector<Point> ps{cir[0]}; rep(i, n - 1) { vector<Point> xpt(xptCC(cir[i], cir[i + 1])); ps.insert(ps.end(), all(xpt)); } ps.push_back(cir[n - 1]); int m = (int)ps.size(); vector<vector<pair<int, double>>> G(m); rep(i, m) rep(j, i) { bool ok = true; int s = j | 1, t = i - (~i & 1); for (int k = s; k < t; k += 2) { if (interLS(Line(ps[i], ps[j]), Line(ps[k], ps[k + 1]))) continue; ok = false; break; } if (ok) G[j].emplace_back(i, abs(ps[i] - ps[j])); } vector<double> mem(n, inf); mem[0] = 0; priority_queue<pair<double, int>> q; for (q.emplace(0, 0); !q.empty();) { double c; int v; tie(c, v) = q.top(); q.pop(); if (mem[v] + c < -eps) continue; for (auto e : G[v]) { double nc = e.second - c; if (mem[e.first] + eps <= nc) continue; mem[e.first] = nc; q.emplace(-nc, e.first); } } return mem[n - 1]; } int main() { while (cin >> n, n) { cir.clear(); rep(i, n) { int x, y, r; cin >> x >> y >> r; cir.emplace_back(Point(x, y), r); } cout << fixed << setprecision(6) << solve() << '\n'; } return 0; }
#include <cmath> #include <complex> #include <iomanip> #include <iostream> #include <queue> #include <tuple> #include <vector> using namespace std; typedef complex<double> Point; #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(c) (c).begin(), (c).end() const double eps = 1e-8, inf = 1e+8; template <typename T> T sqr(T x) { return x * x; } double cross(const Point &a, const Point &b) { return imag(conj(a) * b); } struct Line : vector<Point> { Line(const Point &a, const Point &b) { push_back(a); push_back(b); } }; bool interLS(const Line &l, const Line &s) { return cross(l[1] - l[0], s[0] - l[0]) * cross(l[1] - l[0], s[1] - l[0]) < eps; } struct Circle : Point { double r; Circle(const Point &a, double r) : Point(a), r(r) {} }; vector<Point> xptCC(const Circle &c1, const Circle &c2) { vector<Point> res; double d = abs(c1 - c2); if (d < eps) return res; double rc = (sqr(d) + sqr(c1.r) - sqr(c2.r)) / (2. * d); double rs = sqrt(sqr(c1.r) - sqr(rc)); Point diff = (c2 - c1) / d; res.push_back(c1 + diff * Point(rc, rs)); res.push_back(c1 + diff * Point(rc, -rs)); return res; } int n; vector<Circle> cir; double solve() { vector<Point> ps{cir[0]}; rep(i, n - 1) { vector<Point> xpt(xptCC(cir[i], cir[i + 1])); ps.insert(ps.end(), all(xpt)); } ps.push_back(cir[n - 1]); int m = (int)ps.size(); vector<vector<pair<int, double>>> G(m); rep(i, m) rep(j, i) { bool ok = true; int s = j | 1, t = i - (!i & 1); for (int k = s; k < t; k += 2) { if (interLS(Line(ps[i], ps[j]), Line(ps[k], ps[k + 1]))) continue; ok = false; break; } if (ok) G[j].emplace_back(i, abs(ps[i] - ps[j])); } vector<double> mem(m, inf); mem[0] = 0; priority_queue<pair<double, int>> q; for (q.emplace(0, 0); !q.empty();) { double c; int v; tie(c, v) = q.top(); q.pop(); if (mem[v] + c < -eps) continue; for (auto e : G[v]) { double nc = e.second - c; if (mem[e.first] + eps <= nc) continue; mem[e.first] = nc; q.emplace(-nc, e.first); } } return mem[m - 1]; } int main() { while (cin >> n, n) { cir.clear(); rep(i, n) { int x, y, r; cin >> x >> y >> r; cir.emplace_back(Point(x, y), r); } cout << fixed << setprecision(6) << solve() << '\n'; } return 0; }
[["-", 51, 16, 12, 23, 0, 16, 31, 91, 17, 92], ["+", 51, 16, 12, 23, 0, 16, 31, 91, 17, 111], ["-", 0, 43, 49, 53, 54, 55, 0, 56, 39, 78], ["+", 0, 43, 49, 53, 54, 55, 0, 56, 39, 78], ["-", 0, 37, 0, 69, 341, 342, 0, 16, 31, 22], ["+", 0, 37, 0, 69, 341, 342, 0, 16, 31, 22]]
1
834
#define _USE_MATH_DEFINES #include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define EPS 1e-8 #define INF 1000000 struct Point { double x, y; Point() {} Point(double _x, double _y) { x = _x; y = _y; } Point operator+(const Point p) const { return Point(x + p.x, y + p.y); } Point operator-(const Point p) const { return Point(x - p.x, y - p.y); } Point operator*(const double d) const { return Point(x * d, y * d); } bool operator<(const Point &p) const { if (x == p.x) return y < p.y; return x < p.x; } double norm() { return x * x + y * y; } bool input() { if (cin >> x >> y) return true; return false; } }; struct Line { Point a, b; Line() {} Line(Point _a, Point _b) { a = _a; b = _b; } bool input() { if (a.input() && b.input()) return true; return false; } }; struct Circle { Point c; double r; Circle() {} Circle(Point _c, double _r) { c = _c; r = _r; } }; typedef vector<Point> Polygon; double dot(Point p, Point q) { return p.x * q.x + p.y * q.y; } double cross(Point p, Point q) { return p.x * q.y - q.x * p.y; } int ccw(Point a, Point b, Point c) { Point v1 = b - a; Point v2 = c - a; if (cross(v1, v2) > EPS) return +1; if (cross(v1, v2) < -EPS) return -1; if (dot(v1, v2) < -EPS) return +2; if (v1.norm() < v2.norm()) return -2; return 0; } bool intersect_ss(Line l, Line m) { return ccw(l.a, l.b, m.a) * ccw(l.a, l.b, m.b) <= 0 && ccw(m.a, m.b, l.a) * ccw(m.a, m.b, l.b) <= 0; } bool crosspoint_ll(Line l, Line m, Point &p) { Point a1, a2, b1, b2; a1 = l.b - l.a; a2 = m.b - m.a; b1 = m.a - l.a; b2 = l.a - m.b; double s1, s2; s1 = cross(a1, b1) / 2; s2 = cross(a1, b2) / 2; if (s1 + s2 < EPS) return false; p = Point(m.a.x + a2.x * s1 / (s1 + s2), m.a.y + a2.y * s1 / (s1 + s2)); return true; } int crosspoint_ss(Line l, Line m, Point &p) { if (intersect_ss(l, m) == false) return 0; if (crosspoint_ll(l, m, p) == true) return 1; return -1; } int crosspoint_cc(Circle c1, Circle c2, Point &p1, Point &p2) { double d, a, t; d = sqrt((c2.c - c1.c).norm()); if (abs(c1.c.x - c2.c.x) < EPS && abs(c1.c.y - c2.c.y) < EPS && abs(c1.r - c2.r) < EPS) return -1; if (d < abs(c1.r - c2.r) || c1.r + c2.r < d) return 0; a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); t = atan2(c2.c.y - c1.c.y, c2.c.x - c1.c.x); p1 = Point(c1.c.x + c1.r * cos(t + a), c1.c.y + c1.r * sin(t + a)); p2 = Point(c1.c.x + c1.r * cos(t - a), c1.c.y + c1.r * sin(t - a)); if (abs(p1.x - p2.x) < EPS && abs(p1.y - p2.y) < EPS) return 1; return 2; } int contains(Polygon g, Point p) { Line l = Line(p, Point(INF, p.y)); int cnt = 0, n = g.size(); for (int i = 0; i < n; i++) { Point a = g[i] - p; Point b = g[(i + 1) % n] - p; if (ccw(g[i], g[(i + 1) % n], p) == 0) return 1; if (a.y > b.y) swap(a, b); if (a.y <= EPS && EPS < b.y && cross(a, b) > EPS) cnt++; } if ((cnt & 1) == 1) return 2; return 0; } Polygon andrewScan(Polygon s) { if (s.size() <= 2) return s; sort(s.begin(), s.end()); Polygon g; for (int i = 0; i < s.size(); i++) { for (int n = g.size(); n >= 2 && ccw(g[n - 2], g[n - 1], s[i]) != -1; n--) { g.pop_back(); } g.push_back(s[i]); } int upper_n = g.size(); for (int i = s.size() - 2; i >= 0; i--) { for (int n = g.size(); n > upper_n && ccw(g[n - 2], g[n - 1], s[i]) != -1; n--) { g.pop_back(); } g.push_back(s[i]); } reverse(g.begin(), g.end()); g.pop_back(); return g; } int main() { int N; Circle C[100]; while (cin >> N, N) { for (int i = 0; i < N; i++) { cin >> C[i].c.x >> C[i].c.y >> C[i].r; } Polygon way[2]; way[0].push_back(C[0].c); way[1].push_back(C[1].c); for (int i = 0; i < N - 1; i++) { Point p1, p2; crosspoint_cc(C[i], C[i + 1], p1, p2); way[0].push_back(p1); way[1].push_back(p2); } way[0].push_back(C[N - 1].c); way[1].push_back(C[N - 1].c); double dp[101][2]; fill_n((double *)dp, 101 * 2, INF); dp[0][0] = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < 2; j++) { for (int k = i + 1; k <= N; k++) { for (int l = 0; l < 2; l++) { Line l1(way[j][i], way[l][k]); bool flag = true; for (int m = i + 1; m < k; m++) { if (intersect_ss(l1, Line(way[0][m], way[1][m])) == false) flag = false; } if (flag == true) { double d = sqrt((way[l][k] - way[j][i]).norm()); dp[k][l] = min(dp[k][l], dp[i][j] + d); } } } } } printf("%d\n", dp[N][0]); } }
#define _USE_MATH_DEFINES #include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define EPS 1e-8 #define INF 1000000 struct Point { double x, y; Point() {} Point(double _x, double _y) { x = _x; y = _y; } Point operator+(const Point p) const { return Point(x + p.x, y + p.y); } Point operator-(const Point p) const { return Point(x - p.x, y - p.y); } Point operator*(const double d) const { return Point(x * d, y * d); } bool operator<(const Point &p) const { if (x == p.x) return y < p.y; return x < p.x; } double norm() { return x * x + y * y; } bool input() { if (cin >> x >> y) return true; return false; } }; struct Line { Point a, b; Line() {} Line(Point _a, Point _b) { a = _a; b = _b; } bool input() { if (a.input() && b.input()) return true; return false; } }; struct Circle { Point c; double r; Circle() {} Circle(Point _c, double _r) { c = _c; r = _r; } }; typedef vector<Point> Polygon; double dot(Point p, Point q) { return p.x * q.x + p.y * q.y; } double cross(Point p, Point q) { return p.x * q.y - q.x * p.y; } int ccw(Point a, Point b, Point c) { Point v1 = b - a; Point v2 = c - a; if (cross(v1, v2) > EPS) return +1; if (cross(v1, v2) < -EPS) return -1; if (dot(v1, v2) < -EPS) return +2; if (v1.norm() < v2.norm()) return -2; return 0; } bool intersect_ss(Line l, Line m) { return ccw(l.a, l.b, m.a) * ccw(l.a, l.b, m.b) <= 0 && ccw(m.a, m.b, l.a) * ccw(m.a, m.b, l.b) <= 0; } bool crosspoint_ll(Line l, Line m, Point &p) { Point a1, a2, b1, b2; a1 = l.b - l.a; a2 = m.b - m.a; b1 = m.a - l.a; b2 = l.a - m.b; double s1, s2; s1 = cross(a1, b1) / 2; s2 = cross(a1, b2) / 2; if (s1 + s2 < EPS) return false; p = Point(m.a.x + a2.x * s1 / (s1 + s2), m.a.y + a2.y * s1 / (s1 + s2)); return true; } int crosspoint_ss(Line l, Line m, Point &p) { if (intersect_ss(l, m) == false) return 0; if (crosspoint_ll(l, m, p) == true) return 1; return -1; } int crosspoint_cc(Circle c1, Circle c2, Point &p1, Point &p2) { double d, a, t; d = sqrt((c2.c - c1.c).norm()); if (abs(c1.c.x - c2.c.x) < EPS && abs(c1.c.y - c2.c.y) < EPS && abs(c1.r - c2.r) < EPS) return -1; if (d < abs(c1.r - c2.r) || c1.r + c2.r < d) return 0; a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); t = atan2(c2.c.y - c1.c.y, c2.c.x - c1.c.x); p1 = Point(c1.c.x + c1.r * cos(t + a), c1.c.y + c1.r * sin(t + a)); p2 = Point(c1.c.x + c1.r * cos(t - a), c1.c.y + c1.r * sin(t - a)); if (abs(p1.x - p2.x) < EPS && abs(p1.y - p2.y) < EPS) return 1; return 2; } int contains(Polygon g, Point p) { Line l = Line(p, Point(INF, p.y)); int cnt = 0, n = g.size(); for (int i = 0; i < n; i++) { Point a = g[i] - p; Point b = g[(i + 1) % n] - p; if (ccw(g[i], g[(i + 1) % n], p) == 0) return 1; if (a.y > b.y) swap(a, b); if (a.y <= EPS && EPS < b.y && cross(a, b) > EPS) cnt++; } if ((cnt & 1) == 1) return 2; return 0; } Polygon andrewScan(Polygon s) { if (s.size() <= 2) return s; sort(s.begin(), s.end()); Polygon g; for (int i = 0; i < s.size(); i++) { for (int n = g.size(); n >= 2 && ccw(g[n - 2], g[n - 1], s[i]) != -1; n--) { g.pop_back(); } g.push_back(s[i]); } int upper_n = g.size(); for (int i = s.size() - 2; i >= 0; i--) { for (int n = g.size(); n > upper_n && ccw(g[n - 2], g[n - 1], s[i]) != -1; n--) { g.pop_back(); } g.push_back(s[i]); } reverse(g.begin(), g.end()); g.pop_back(); return g; } int main() { int N; Circle C[100]; while (cin >> N, N) { for (int i = 0; i < N; i++) { cin >> C[i].c.x >> C[i].c.y >> C[i].r; } Polygon way[2]; way[0].push_back(C[0].c); way[1].push_back(C[1].c); for (int i = 0; i < N - 1; i++) { Point p1, p2; crosspoint_cc(C[i], C[i + 1], p1, p2); way[0].push_back(p1); way[1].push_back(p2); } way[0].push_back(C[N - 1].c); way[1].push_back(C[N - 1].c); double dp[101][2]; fill_n((double *)dp, 101 * 2, INF); dp[0][0] = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < 2; j++) { for (int k = i + 1; k <= N; k++) { for (int l = 0; l < 2; l++) { Line l1(way[j][i], way[l][k]); bool flag = true; for (int m = i + 1; m < k; m++) { if (intersect_ss(l1, Line(way[0][m], way[1][m])) == false) flag = false; } if (flag == true) { double d = sqrt((way[l][k] - way[j][i]).norm()); dp[k][l] = min(dp[k][l], dp[i][j] + d); } } } } } printf("%.4lf\n", dp[N][0]); } }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
1,898
#include <cmath> #include <complex> #include <iostream> #include <queue> #include <utility> #include <vector> using namespace std; typedef complex<double> Point; typedef pair<Point, Point> Line; #define EPS 1e-9 #define INF 1e9 // ?????¢, ??????, ?????? double dis(Point a, Point b) { return abs(a - b); } double dot(Point a, Point b) { return a.real() * b.real() + a.imag() * b.imag(); } double cross(Point a, Point b) { return a.real() * b.imag() - a.imag() * b.real(); } // ccw // 1: c?????????ab??????????????´????????? (a?????????, b????\\??¨????????¨???) // -1: c?????????ab??????????????´????????? // 2: c?????????ab?????¶????????????a????????????????????? // -2: c?????????ab?????¶????????????b???????\\????????? // 0: c?????????ab???????????? int ccw(Point a, Point b, Point c) { if (cross(b - a, c - a) > EPS) return 1; if (cross(b - a, c - a) < -EPS) return -1; if (dot(b - a, c - a) < -EPS) return 2; if (dis(a, b) + EPS < dis(c, a)) return -2; return 0; } int ccw(Line a, Point b) { return ccw(a.first, a.second, b); } // ??????????????????(??????2???????????¶??´????????????) Line CircleCross(Point a, double ra, Point b, double rb) { double d = dis(a, b); double rc = (d * d + ra * ra - rb * rb) / (2 * d); double rs = sqrt(ra * ra - rc * rc); Point diff = (b - a) / d; return make_pair(a + diff * Point(rc, rs), a + diff * Point(rc, -rs)); } vector<double> dijk(int s, vector<vector<pair<double, int>>> adj) { int n = adj.size(); vector<double> result(n, INF); priority_queue<pair<double, int>> wait; result[s] = 0; wait.push(make_pair(0, s)); while (!wait.empty()) { double nowcost = -wait.top().first; int nowpoint = wait.top().second; wait.pop(); if (result[nowpoint] < nowcost) { continue; } for (int i = 0; i < (int)adj[nowpoint].size(); i++) { double nextcost = adj[nowpoint][i].first + nowcost; int nextpoint = adj[nowpoint][i].second; if (result[nextpoint] > nextcost) { wait.push(make_pair(-nextcost, nextpoint)); result[nextpoint] = nextcost; } } } return result; } int main(void) { while (true) { int n; cin >> n; if (n == 0) { break; } // ??????????????¨????????????????????? double x, y, r; vector<pair<Point, double>> circles; for (int i = 0; i < n; i++) { cin >> x >> y >> r; circles.push_back(make_pair(Point(x, y), r)); } // ????????????(+?§??????¨??????)????????? // second??????????????????????????????????????????????¨???? vector<pair<Point, int>> vertices; vertices.push_back(make_pair(circles[0].first, 0)); for (int i = 0; i < n - 1; i++) { Line tmp = CircleCross(circles[i].first, circles[i].second, circles[i + 1].first, circles[i + 1].second); vertices.push_back(make_pair(tmp.first, 1)); vertices.push_back(make_pair(tmp.second, -1)); } vertices.push_back(make_pair(circles[n - 1].first, 0)); // ??????i??¨j???????????????????????? // ??????k(i+1???j-1)????????????????????????????????????????????°OK vector<vector<pair<double, int>>> adj(vertices.size()); for (int i = 0; i < (int)vertices.size(); i++) { for (int j = i + 1; j < (int)vertices.size(); j++) { bool flag = true; for (int k = i + 1; k < j; k++) { int tmp = ccw(vertices[i].first, vertices[j].first, vertices[k].first); if (tmp != vertices[k].second) { flag = false; } } if (flag) { adj[i].push_back( make_pair(dis(vertices[i].first, vertices[j].first), j)); } } } // ?????????????????????????????????????????? vector<double> result = dijk(0, adj); printf("%.10f", result[result.size() - 1]); } return 0; }
#include <cmath> #include <complex> #include <iostream> #include <queue> #include <utility> #include <vector> using namespace std; typedef complex<double> Point; typedef pair<Point, Point> Line; #define EPS 1e-9 #define INF 1e9 // ?????¢, ??????, ?????? double dis(Point a, Point b) { return abs(a - b); } double dot(Point a, Point b) { return a.real() * b.real() + a.imag() * b.imag(); } double cross(Point a, Point b) { return a.real() * b.imag() - a.imag() * b.real(); } // ccw // 1: c?????????ab??????????????´????????? (a?????????, b????\\??¨????????¨???) // -1: c?????????ab??????????????´????????? // 2: c?????????ab?????¶????????????a????????????????????? // -2: c?????????ab?????¶????????????b???????\\????????? // 0: c?????????ab???????????? int ccw(Point a, Point b, Point c) { if (cross(b - a, c - a) > EPS) return 1; if (cross(b - a, c - a) < -EPS) return -1; if (dot(b - a, c - a) < -EPS) return 2; if (dis(a, b) + EPS < dis(c, a)) return -2; return 0; } int ccw(Line a, Point b) { return ccw(a.first, a.second, b); } // ??????????????????(??????2???????????¶??´????????????) Line CircleCross(Point a, double ra, Point b, double rb) { double d = dis(a, b); double rc = (d * d + ra * ra - rb * rb) / (2 * d); double rs = sqrt(ra * ra - rc * rc); Point diff = (b - a) / d; return make_pair(a + diff * Point(rc, rs), a + diff * Point(rc, -rs)); } vector<double> dijk(int s, vector<vector<pair<double, int>>> adj) { int n = adj.size(); vector<double> result(n, INF); priority_queue<pair<double, int>> wait; result[s] = 0; wait.push(make_pair(0, s)); while (!wait.empty()) { double nowcost = -wait.top().first; int nowpoint = wait.top().second; wait.pop(); if (result[nowpoint] < nowcost) { continue; } for (int i = 0; i < (int)adj[nowpoint].size(); i++) { double nextcost = adj[nowpoint][i].first + nowcost; int nextpoint = adj[nowpoint][i].second; if (result[nextpoint] > nextcost) { wait.push(make_pair(-nextcost, nextpoint)); result[nextpoint] = nextcost; } } } return result; } int main(void) { while (true) { int n; cin >> n; if (n == 0) { break; } // ??????????????¨????????????????????? double x, y, r; vector<pair<Point, double>> circles; for (int i = 0; i < n; i++) { cin >> x >> y >> r; circles.push_back(make_pair(Point(x, y), r)); } // ????????????(+?§??????¨??????)????????? // second??????????????????????????????????????????????¨???? vector<pair<Point, int>> vertices; vertices.push_back(make_pair(circles[0].first, 0)); for (int i = 0; i < n - 1; i++) { Line tmp = CircleCross(circles[i].first, circles[i].second, circles[i + 1].first, circles[i + 1].second); vertices.push_back(make_pair(tmp.first, 1)); vertices.push_back(make_pair(tmp.second, -1)); } vertices.push_back(make_pair(circles[n - 1].first, 0)); // ??????i??¨j???????????????????????? // ??????k(i+1???j-1)????????????????????????????????????????????°OK vector<vector<pair<double, int>>> adj(vertices.size()); for (int i = 0; i < (int)vertices.size(); i++) { for (int j = i + 1; j < (int)vertices.size(); j++) { bool flag = true; for (int k = i + 1; k < j; k++) { int tmp = ccw(vertices[i].first, vertices[j].first, vertices[k].first); if (tmp != vertices[k].second) { flag = false; } } if (flag) { adj[i].push_back( make_pair(dis(vertices[i].first, vertices[j].first), j)); } } } // ?????????????????????????????????????????? vector<double> result = dijk(0, adj); printf("%.4f\n", result[result.size() - 1]); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
1
1,006
import java.util.Arrays; import java.util.Scanner; public class Main { Scanner sc; int INF = 1 << 28; int ex(int v, int r) { return (v * (100 + r)) / 100; } void run() { for (;;) { int x = ni(); int y = ni(); int s = ni(); if ((x | y | s) == 0) break; int max = 0; for (int i = 1; i <= s; ++i) { for (int j = s - i; 0 <= j; --j) { int bef = ex(i, x) + ex(j, x); if (bef == s) { max = Math.max(max, ex(i, y) + ex(j, y)); } } } System.out.println(max); } } Main() { sc = new Scanner(System.in); } int ni() { return sc.nextInt(); } public static void main(String[] args) { new Main().run(); } void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } }
import java.util.Arrays; import java.util.Scanner; public class Main { Scanner sc; int INF = 1 << 28; int ex(int v, int r) { return (v * (100 + r)) / 100; } void run() { for (;;) { int x = ni(); int y = ni(); int s = ni(); if ((x | y | s) == 0) break; int max = 0; for (int i = 1; i <= s; ++i) { for (int j = s - i; 1 <= j; --j) { int bef = ex(i, x) + ex(j, x); if (bef == s) { max = Math.max(max, ex(i, y) + ex(j, y)); } } } System.out.println(max); } } Main() { sc = new Scanner(System.in); } int ni() { return sc.nextInt(); } public static void main(String[] args) { new Main().run(); } void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } }
[["-", 0, 7, 8, 196, 0, 7, 15, 16, 31, 499], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 31, 499]]
3
269
import java.util.Scanner; public class Main { public static void main(String[] args) { try (Scanner sc = new Scanner(System.in)) { while (sc.hasNext()) { int x = sc.nextInt(); int y = sc.nextInt(); int s = sc.nextInt(); if (x == 0 && y == 0 && s == 0) break; int max = 0; for (int i = 1; i < 1000; i++) { for (int j = i + 1; j < 1000; j++) { // System.out.println(Math.floor((i*(100+x)/100+j*(100+x)/100))); if (Math.floor((i * (100 + x) / 100 + j * (100 + x) / 100)) == s) { max = Math.max( (int)Math.floor((i * (100 + y) / 100 + j * (100 + y) / 100)), max); } } } System.out.println(max); } } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { try (Scanner sc = new Scanner(System.in)) { while (sc.hasNext()) { int x = sc.nextInt(); int y = sc.nextInt(); int s = sc.nextInt(); if (x == 0 && y == 0 && s == 0) break; int max = 0; for (int i = 1; i < 1000; i++) { for (int j = 1; j < 1000; j++) { // System.out.println(Math.floor((i*(100+x)/100+j*(100+x)/100))); if (Math.floor((i * (100 + x) / 100 + j * (100 + x) / 100)) == s) { max = Math.max( (int)Math.floor((i * (100 + y) / 100 + j * (100 + y) / 100)), max); } } } System.out.println(max); } } } }
[["-", 0, 7, 502, 503, 49, 200, 51, 16, 31, 22], ["-", 0, 7, 502, 503, 49, 200, 51, 16, 17, 72]]
3
213
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <functional> #include <iostream> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; #define MP make_pair long gcd(long a, long b) { if (a % b == 0) return b; else return gcd(b, a % b); } long lcm(long a, long b) { return a * b / gcd(a, b); } int main() { while (1) { int x, y, s; cin >> x >> y >> s; if ((x | y | s) == 0) break; int res = 0; for (int a = 0; a < s; a++) { for (int b = 0; b < s; b++) { int vx = a * (100 + x) / 100 + b * (100 + x) / 100; int vy = a * (100 + y) / 100 + b * (100 + y) / 100; if (vx == s && res < vy) res = vy; } } cout << res << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <functional> #include <iostream> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; #define MP make_pair long gcd(long a, long b) { if (a % b == 0) return b; else return gcd(b, a % b); } long lcm(long a, long b) { return a * b / gcd(a, b); } int main() { while (1) { int x, y, s; cin >> x >> y >> s; if (((x | y) | s) == 0) break; int res = 0; for (int a = 1; a < s; a++) { for (int b = 1; b < s; b++) { int vx = a * (100 + x) / 100 + b * (100 + x) / 100; int vy = a * (100 + y) / 100 + b * (100 + y) / 100; if (vx == s && res < vy) res = vy; } } cout << res << endl; } return 0; }
[["+", 51, 16, 31, 23, 0, 16, 31, 23, 0, 24], ["+", 51, 16, 31, 23, 0, 16, 31, 23, 0, 25], ["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
232
#include <bits/stdc++.h> typedef long long LL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define DECIM8 fixed << setprecision(8) #define SZ(a) int((a).size()) #define SORT(c) sort((c).begin(), (c).end()) // repetition //------------------------------------------ #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) // constant //-------------------------------------------- const double EPS = 1e-10; const double PI = acos(-1.0); // clear memory #define CLR(a) memset((a), 0, sizeof(a)) using namespace std; int addtax(int a, int p) { return a * (p + 100) / 100; } int main(void) { for (;;) { int x, y, s; int answer = 0; cin >> x >> y >> s; if (!x && !y && !s) return 0; REP(i, s) { REP(j, s) { if (addtax(i, x) + addtax(j, x) == s) answer = max(answer, addtax(i, y) + addtax(j, y)); } } cout << answer << endl; } }
#include <bits/stdc++.h> typedef long long LL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define DECIM8 fixed << setprecision(8) #define SZ(a) int((a).size()) #define SORT(c) sort((c).begin(), (c).end()) // repetition //------------------------------------------ #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) // constant //-------------------------------------------- const double EPS = 1e-10; const double PI = acos(-1.0); // clear memory #define CLR(a) memset((a), 0, sizeof(a)) using namespace std; int addtax(int a, int p) { return a * (p + 100) / 100; } int main(void) { for (;;) { int x, y, s; int answer = 0; cin >> x >> y >> s; if (!x && !y && !s) return 0; FOR(i, 1, s) { FOR(j, 1, s) { if (addtax(i, x) + addtax(j, x) == s) answer = max(answer, addtax(i, y) + addtax(j, y)); } } cout << answer << endl; } }
[["-", 0, 7, 8, 9, 0, 14, 49, 53, 49, 22], ["+", 0, 7, 8, 9, 0, 1, 0, 2, 63, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 21], ["+", 0, 1, 0, 2, 3, 4, 0, 25, 0, 35], ["-", 0, 14, 8, 9, 0, 14, 49, 53, 49, 22], ["+", 8, 9, 0, 9, 0, 1, 0, 2, 63, 22], ["+", 0, 9, 0, 1, 0, 2, 3, 4, 0, 21], ["+", 0, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
1
221
#include <bits/stdc++.h> using namespace std; inline int calc(int p, int x) { return p * (100 + x) / 100; } int main() { for (int x, y, s; cin >> x >> y >> s && (x | y | s);) { int res = 0; for (int a = 1; a < s; ++a) { for (int b = 1; b < s; ++b) { if (calc(a, x) + calc(b, y) != s) continue; res = max(res, calc(a, y) + calc(b, y)); } } cout << res << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; inline int calc(int p, int x) { return p * (100 + x) / 100; } int main() { for (int x, y, s; cin >> x >> y >> s && (x | y | s);) { int res = 0; for (int a = 1; a < s; ++a) { for (int b = 1; b < s; ++b) { if (calc(a, x) + calc(b, x) != s) continue; res = max(res, calc(a, y) + calc(b, y)); } } cout << res << endl; } return 0; }
[["-", 51, 16, 31, 16, 12, 2, 3, 4, 0, 22], ["+", 51, 16, 31, 16, 12, 2, 3, 4, 0, 22]]
1
150
#include <stdio.h> int main() { int x, y, s; while (1) { scanf("%d%d%d", &x, &y, &s); if (x == 0 && y == 0 && s == 0) break; int X = 100 + x; int Y = 100 + y; int max = 0; for (int i = 1; i < 100; i++) for (int j = 1; j < 100; j++) { if (i * X / 100 + j * X / 100 == s && max < i * Y / 100 + j * Y / 100) max = i * Y / 100 + j * Y / 100; } printf("%d\n", max); } return 0; }
#include <stdio.h> int main() { int x, y, s; while (1) { scanf("%d%d%d", &x, &y, &s); if (x == 0 && y == 0 && s == 0) break; int X = 100 + x; int Y = 100 + y; int max = 0; for (int i = 1; i < s; i++) for (int j = 1; j < s; j++) { if (i * X / 100 + j * X / 100 == s && max < i * Y / 100 + j * Y / 100) max = i * Y / 100 + j * Y / 100; } printf("%d\n", max); } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["-", 8, 9, 0, 7, 8, 7, 15, 16, 12, 13], ["+", 8, 9, 0, 7, 8, 7, 15, 16, 12, 22]]
1
159
#include <iostream> using namespace std; int f(int v, int per) { return v * (100 + per) / 100; } int main() { int x, y, s; while (cin >> x >> y >> s, x || y || s) { int ans = 0; for (int i = 1; i <= s - 1; ++i) { for (int j = 0; j <= s - 1; ++j) { if (f(i, x) + f(j, x) == s) { ans = max(ans, f(i, y) + f(j, y)); } } } cout << ans << endl; } }
#include <iostream> using namespace std; int f(int v, int per) { return v * (100 + per) / 100; } int main() { int x, y, s; while (cin >> x >> y >> s, x || y || s) { int ans = 0; for (int i = 1; i <= s - 1; ++i) { for (int j = 1; j <= s - 1; ++j) { if (f(i, x) + f(j, x) == s) { ans = max(ans, f(i, y) + f(j, y)); } } } cout << ans << endl; } }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
147
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <string> #include <vector> using namespace std; double net_of_tax(int n, int rate) { double ret, tmp1, tmp2; ret = (double)(n * 100) / (rate + 100); tmp1 = floor(ret); tmp2 = ceil(ret); if ((floor((double)(tmp1 * (rate + 100) / 100))) != (double)n) { if ((floor((double)(tmp2 * (rate + 100) / 100))) != (double)n) { return ret; } else { ret = tmp2; } } else { ret = tmp1; } return ret; } int main() { double n1, n2, tmp, max = 0.; int x, y, s; while (1) { cin >> x >> y >> s; if (x == 0 && y == 0 && s == 0) break; for (int i = 1; i <= s / 2; ++i) { n1 = net_of_tax(i, x); n2 = net_of_tax((s - i), x); n1 = floor((double)((n1 * (y + 100)) / 100)); n2 = floor((double)((n2 * (y + 100)) / 100)); if ((n1 + n2) > max) max = n1 + n2; } cout << max << endl; n1 = n2 = max = 0; } return 0; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <string> #include <vector> using namespace std; double net_of_tax(int n, int rate) { double ret, tmp1, tmp2; ret = (double)(n * 100) / (rate + 100); tmp1 = floor(ret); tmp2 = ceil(ret); if ((floor((double)(tmp1 * (rate + 100) / 100))) != (double)n) { if ((floor((double)(tmp2 * (rate + 100) / 100))) != (double)n) { return ret - 1.; } else { ret = tmp2; } } else { ret = tmp1; } return ret; } int main() { double n1, n2, tmp, max = 0.; int x, y, s; while (1) { cin >> x >> y >> s; if (x == 0 && y == 0 && s == 0) break; for (int i = 1; i <= s / 2; ++i) { n1 = net_of_tax(i, x); n2 = net_of_tax((s - i), x); n1 = floor((double)((n1 * (y + 100)) / 100)); n2 = floor((double)((n2 * (y + 100)) / 100)); if ((n1 + n2) > max) max = n1 + n2; } cout << max << endl; n1 = n2 = max = 0; } return 0; }
[["+", 0, 57, 64, 9, 0, 37, 0, 16, 17, 33], ["+", 0, 57, 64, 9, 0, 37, 0, 16, 12, 13]]
1
314
#include <iostream> using namespace std; int main() { int a, b, c, d, e, f; while (true) { b = 0; cin >> c >> d >> e; if ((c == 0 && d == 0) && e == 0) { break; } for (int i = 0; i <= e; i++) { for (int j = 0; j <= e; j++) { a = (i * (100 + c)) / 100 + (j * (100 + c)) / 100; if (a == e) { f = (i * (100 + d)) / 100 + (j * (100 + d)) / 100; if (b < f) { b = f; } } } } cout << b << endl; } return 0; }
#include <iostream> using namespace std; int main() { int a, b, c, d, e, f; while (true) { b = 0; cin >> c >> d >> e; if ((c == 0 && d == 0) && e == 0) { break; } for (int i = 1; i <= e; i++) { for (int j = 1; j <= e; j++) { a = (i * (100 + c)) / 100 + (j * (100 + c)) / 100; if (a == e) { f = (i * (100 + d)) / 100 + (j * (100 + d)) / 100; if (b < f) { b = f; } } } } cout << b << endl; } return 0; }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
176
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <set> #include <stack> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define mp(a, b) make_pair(a, b) #define pb(a) push_back(a) #define dbg(x) cout << #x "=" << x << endl typedef long long ll; typedef pair<int, int> pii; typedef pair<pii, int> ppi; #define INF 1000000000 int exc2inc(int y, int p) { return y * (100 + p) / 100; } int inc2exc(int y, int p) { if (y * 100 % (100 + p) == 0) return y * 100 / (100 + p); else { int tmp = y * 100 / (100 + p); while (exc2inc(tmp + 1, p) <= y) { tmp++; } return tmp; } } int main() { while (true) { int x, y, s; cin >> x >> y >> s; if (x == 0 && y == 0 && s == 0) break; int ans = 0; rep(i, s / 2) { if (i == 0) continue; int tmp = inc2exc(s - exc2inc(i, x), x); // i and tmp are tax-excluded value if (exc2inc(i, x) + exc2inc(tmp, x) != s) continue; ans = max(ans, exc2inc(i, y) + exc2inc(tmp, y)); // dbg(i);dbg(y); } cout << ans << endl; } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <set> #include <stack> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define mp(a, b) make_pair(a, b) #define pb(a) push_back(a) #define dbg(x) cout << #x "=" << x << endl typedef long long ll; typedef pair<int, int> pii; typedef pair<pii, int> ppi; #define INF 1000000000 int exc2inc(int y, int p) { return y * (100 + p) / 100; } int inc2exc(int y, int p) { if (y * 100 % (100 + p) == 0) return y * 100 / (100 + p); else { int tmp = y * 100 / (100 + p); while (exc2inc(tmp + 1, p) <= y) { tmp++; } return tmp; } } int main() { while (true) { int x, y, s; cin >> x >> y >> s; if (x == 0 && y == 0 && s == 0) break; int ans = 0; rep(i, s / 2 + 1) { if (i == 0) continue; int tmp = inc2exc(s - exc2inc(i, x), x); // i and tmp are tax-excluded value if (exc2inc(i, x) + exc2inc(tmp, x) != s) continue; ans = max(ans, exc2inc(i, y) + exc2inc(tmp, y)); // dbg(i);dbg(y); } cout << ans << endl; } }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
304
#include <bits/stdc++.h> using namespace std; int addTax(int base, int tax) { return base * (100 + tax) / 100; } int sumPrice(int item1, int item2, int tax) { return addTax(item1, tax) + addTax(item2, tax); } int main() { int x, y, s; while (cin >> x >> y >> s, x || y || s) { int ans = 0; for (int p = 1; p <= s / 2; p++) { for (int q = 1; q <= s / 2; q++) { if (sumPrice(p, q, x) == s) ans = max(ans, sumPrice(p, q, y)); } } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int addTax(int base, int tax) { return base * (100 + tax) / 100; } int sumPrice(int item1, int item2, int tax) { return addTax(item1, tax) + addTax(item2, tax); } int main() { int x, y, s; while (cin >> x >> y >> s, x || y || s) { int ans = 0; for (int p = 1; p <= s; p++) { for (int q = 1; q <= s; q++) { // cout << p << " " << q << " " << sumPrice(p, q, x) << endl; if (sumPrice(p, q, x) == s) ans = max(ans, sumPrice(p, q, y)); } } cout << ans << endl; } return 0; }
[["-", 8, 9, 0, 7, 15, 16, 12, 16, 17, 85], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
167
#include <cstdio> #include <iostream> #include <string> using namespace std; string str; int main() { int i, j; // 2????????????????¨???? int x, y, s; int max = 0; int a, b; // 2????????????????¨???? while (1) { cin >> x >> y >> s; if (x == 0 && y == 0 && s == 0) break; max = 0; for (i = 1; i < s; i++) // cout<<'B'<<endl; for (j = 1; j < i; j++) { a = i * (100 + x) / 100; b = j * (100 + x) / 100; // cout<<'C'<<endl; if (a + b == s) { // cout<<'D'<<endl; a = i * (100 + y) / 100; b = j * (100 + y) / 100; if (max < a + b) max = a + b; // cout<<max<<'A'<<endl; } } cout << max << endl; } }
#include <cstdio> #include <iostream> #include <string> using namespace std; string str; int main() { int i, j; // 2テ」ツ?、テ」ツ?ョテ・ツ閉?・ツ督?」ツ?ョテァツィツ偲ヲツ環? int x, y, s; int max = 0; int a, b; // 2テ」ツ?、テ」ツ?ョテ・ツ閉?・ツ督?」ツ?ョテァツィツ偲ィツセツシ while (1) { cin >> x >> y >> s; if (x == 0 && y == 0 && s == 0) break; max = 0; for (i = 1; i < s; i++) // cout<<'B'<<endl; for (j = 1; j <= i; j++) { a = i * (100 + x) / 100; b = j * (100 + x) / 100; // cout<<'C'<<endl; if (a + b == s) { // cout<<'D'<<endl; // cout<<'A'<<a<<" "<<b<<endl; a = i * (100 + y) / 100; b = j * (100 + y) / 100; if (max < a + b) max = a + b; // cout<<max<<'M'<<endl; // cout<<'I'<<i<<" "<<j<<endl; } } cout << max << endl; } }
[["-", 8, 9, 0, 7, 8, 7, 15, 16, 17, 18], ["+", 8, 9, 0, 7, 8, 7, 15, 16, 17, 19]]
1
187
#include <algorithm> #include <iostream> using namespace std; inline int deTax(int rate, int yen) { return yen * 100 / (100 + rate); } inline int enTax(int rate, int yen) { return yen * (100 + rate) / 100; } int main() { while (1) { int x, y, s; cin >> x >> y >> s; if (!x && !y && !s) break; int maximum = 0; for (double i = 1; i < s / 2 + 1; ++i) { double j = s - i + 0.99999999999999; double k = i + 0.99999999999999; int nx = deTax(x, k), ny = deTax(x, j); if (enTax(x, nx) + enTax(x, ny) == s) { maximum = max(maximum, enTax(y, nx) + enTax(y, ny)); } } cout << maximum << endl; } }
#include <algorithm> #include <iostream> using namespace std; inline int deTax(int rate, double yen) { return yen * 100 / (100 + rate); } inline int enTax(int rate, double yen) { return yen * (100 + rate) / 100; } int main() { while (1) { int x, y, s; cin >> x >> y >> s; if (!x && !y && !s) break; int maximum = 0; for (double i = 1.0; i < s / 2 + 1; ++i) { double j = s - i + 0.99999999999999; double k = i + 0.99999999999999; int nx = deTax(x, k), ny = deTax(x, j); if (enTax(x, nx) + enTax(x, ny) == s) { maximum = max(maximum, enTax(y, nx) + enTax(y, ny)); } } cout << maximum << endl; } }
[["-", 0, 14, 49, 53, 54, 55, 0, 56, 39, 40], ["+", 0, 14, 49, 53, 54, 55, 0, 56, 39, 40], ["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
201
#include <bits/stdc++.h> using namespace std; typedef complex<double> P; double eps = 1e-8; bool eq(double a, double b) { return (-eps < a - b && a - b < eps); } bool eq(P a, P b) { return (abs(a - b) < eps); } double dot(P a, P b) { return real(b * conj(a)); } double cross(P a, P b) { return imag(b * conj(a)); } bool isParallel(P a, P b) { return eq(cross(a, b), 0); } struct S { P p, q; S(P a = P(0, 0), P b = P(0, 0)) : p(a), q(b) {} P base() { return q - p; } }; typedef S L; double getDistanceLP(L l, P p) { P a = l.p, b = l.q; return abs(cross(b - a, p - a)) / abs(b - a); } double getDistanceSP(S s, P p) { P a = s.p; P b = s.q; if (dot(b - a, p - a) < eps) return abs(p - a); if (dot(a - b, p - b) < eps) return abs(p - b); return getDistanceLP(s, p); } P reflect(S s, P p) { P a = s.p; P b = s.q; p -= a; b -= a; return a + conj(p / b) * b; } bool onLine(L l, P p) { P a = l.p; P b = l.q; return eq(0, cross(p - a, b - a)); } bool onSegment(S s, P p) { P a = s.p; P b = s.q; return eq(abs(a - b), abs(p - a) + abs(p - b)); } P getCrossPoint(S s1, S s2) { P a = s1.p, b = s1.q; P c = s2.p, d = s2.q; a -= d, b -= d, c -= d; return d + a + (b - a) * imag(a / c) / imag(a / c - b / c); } bool isIntersectLS(L li, S se) { P a = li.p; P b = li.q; double A = cross(b - a, se.p - a); double B = cross(b - a, se.q - a); if (A * B < eps) return true; } P input() { double x, y; cin >> x >> y; return P(x, y); } int N; S t[5]; P si, ti; int tl[10]; double ans; double calc(P p, P v, int depth) { if (eq(p, ti)) return 0; P np, nv; if (depth > 6) return 1e20; double mini = 1e20; for (int i = 0; i < N; i++) { S seg = t[i]; if (onSegment(seg, p)) continue; L li = L(p, p + v); if (!isIntersectLS(li, seg)) continue; P k = getCrossPoint(li, seg); assert(onSegment(seg, k)); assert(onLine(li, k)); /* cout<<"i="<<i<<endl; cout<<"k="<<k<<endl; cout<<"v="<<v<<endl; cout<<"p="<<p<<endl; cout<<endl; */ if (dot(k - p, v) < 0) continue; if (mini > abs(k - p)) { mini = abs(k - p); np = k; nv = reflect(seg, k + v) - k; } } if (isParallel(v, ti - p) && dot(v, ti - p) > 0) { if (mini > abs(ti - p)) { return abs(ti - p); } } if (mini == 1e20) return 1e20; return mini + calc(np, nv, depth + 1); } P ansve; void check(int size, P target) { P v = target - si; if (abs(v) < eps) return; double dist = calc(si, v, 0); if (ans > dist) { ans = dist; ansve = v; } } void dfs(int depth, P target) { check(depth, target); if (depth == 6) return; for (int i = 0; i < N; i++) { dfs(depth + 1, reflect(t[i], target)); } } int main() { while (1) { cin >> N; if (N == 0) break; for (int i = 0; i < N; i++) { P a = input(); P b = input(); t[i] = S(a, b); } ti = input(); si = input(); ans = 1e20; dfs(0, ti); printf("%.10f\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef complex<double> P; double eps = 1e-8; bool eq(double a, double b) { return (-eps < a - b && a - b < eps); } bool eq(P a, P b) { return (abs(a - b) < eps); } double dot(P a, P b) { return real(b * conj(a)); } double cross(P a, P b) { return imag(b * conj(a)); } bool isParallel(P a, P b) { return eq(cross(a, b), 0); } struct S { P p, q; S(P a = P(0, 0), P b = P(0, 0)) : p(a), q(b) {} P base() { return q - p; } }; typedef S L; double getDistanceLP(L l, P p) { P a = l.p, b = l.q; return abs(cross(b - a, p - a)) / abs(b - a); } double getDistanceSP(S s, P p) { P a = s.p; P b = s.q; if (dot(b - a, p - a) < eps) return abs(p - a); if (dot(a - b, p - b) < eps) return abs(p - b); return getDistanceLP(s, p); } P reflect(S s, P p) { P a = s.p; P b = s.q; p -= a; b -= a; return a + conj(p / b) * b; } bool onLine(L l, P p) { P a = l.p; P b = l.q; return eq(0, cross(p - a, b - a)); } bool onSegment(S s, P p) { P a = s.p; P b = s.q; return eq(abs(a - b), abs(p - a) + abs(p - b)); } P getCrossPoint(S s1, S s2) { P a = s1.p, b = s1.q; P c = s2.p, d = s2.q; a -= d, b -= d, c -= d; return d + a + (b - a) * imag(a / c) / imag(a / c - b / c); } bool isIntersectLS(L li, S se) { P a = li.p; P b = li.q; double A = cross(b - a, se.p - a); double B = cross(b - a, se.q - a); return (A * B < eps); } P input() { double x, y; cin >> x >> y; return P(x, y); } int N; S t[5]; P si, ti; int tl[10]; double ans; double calc(P p, P v, int depth) { if (eq(p, ti)) return 0; P np, nv; if (depth > 6) return 1e20; double mini = 1e20; for (int i = 0; i < N; i++) { S seg = t[i]; if (onSegment(seg, p)) continue; L li = L(p, p + v); if (!isIntersectLS(li, seg)) continue; P k = getCrossPoint(li, seg); assert(onSegment(seg, k)); assert(onLine(li, k)); /* cout<<"i="<<i<<endl; cout<<"k="<<k<<endl; cout<<"v="<<v<<endl; cout<<"p="<<p<<endl; cout<<endl; */ if (dot(k - p, v) < 0) continue; if (mini > abs(k - p)) { mini = abs(k - p); np = k; nv = reflect(seg, k + v) - k; } } if (isParallel(v, ti - p) && dot(v, ti - p) > 0) { if (mini > abs(ti - p)) { return abs(ti - p); } } if (mini == 1e20) return 1e20; return mini + calc(np, nv, depth + 1); } P ansve; void check(int size, P target) { P v = target - si; if (abs(v) < eps) return; double dist = calc(si, v, 0); if (ans > dist) { ans = dist; ansve = v; } } void dfs(int depth, P target) { check(depth, target); if (depth == 6) return; for (int i = 0; i < N; i++) { dfs(depth + 1, reflect(t[i], target)); } } int main() { while (1) { cin >> N; if (N == 0) break; for (int i = 0; i < N; i++) { P a = input(); P b = input(); t[i] = S(a, b); } ti = input(); si = input(); ans = 1e20; dfs(0, ti); printf("%.10f\n", ans); } return 0; }
[["-", 0, 30, 0, 14, 8, 9, 0, 57, 0, 121], ["+", 0, 30, 0, 14, 8, 9, 0, 37, 0, 38], ["-", 0, 14, 8, 9, 0, 57, 64, 37, 0, 38], ["-", 0, 14, 8, 9, 0, 57, 64, 37, 0, 146]]
1
1,129
import java.util.Arrays; import java.util.Scanner; public class Main { final static int MAX = 200000; public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 目的の大きさよりも大きいものを作る boolean[] s = new boolean[MAX]; Arrays.fill(s, true); s[0] = false; s[1] = false; //エラトステネスの篩 for (int i = 2; i * i < MAX; i++) { if (s[i]) { for (int j = i + i; j < MAX; j += i) { if (j % i == 0) { s[j] = false; } } } } // System.out.println(s[17]); // System.out.println(s[19]); // System.out.println(s[23]); while (true) { int n = sc.nextInt(); if (n == 0) { break; } int cnt = 0; for (int i = n + 1; i <= 2 * n; i++) { if (s[i]) { cnt++; } } System.out.println(cnt); } } }
import java.util.Arrays; import java.util.Scanner; public class Main { final static int MAX = 300000; public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 目的の大きさよりも大きいものを作る boolean[] s = new boolean[MAX]; Arrays.fill(s, true); s[0] = false; s[1] = false; //エラトステネスの篩 for (int i = 2; i * i < MAX; i++) { if (s[i]) { for (int j = i + i; j < MAX; j += i) { if (j % i == 0) { s[j] = false; } } } } // System.out.println(s[17]); // System.out.println(s[19]); // System.out.println(s[23]); while (true) { int n = sc.nextInt(); if (n == 0) { break; } int cnt = 0; for (int i = n + 1; i <= 2 * n; i++) { if (s[i]) { cnt++; } } System.out.println(cnt); } } }
[["-", 0, 235, 8, 498, 0, 124, 49, 200, 51, 499], ["+", 0, 235, 8, 498, 0, 124, 49, 200, 51, 499]]
3
222
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(isr); Main app = new Main(); boolean prime[] = app.primeSet(); int n, res; while (true) { if ((n = Integer.valueOf(reader.readLine())) == 0) break; res = 0; for (int i = n; i <= 2 * n; i++) { if (!prime[i]) res++; } System.out.println(res); } reader.close(); } public boolean[] primeSet() { boolean res[] = new boolean[123456 * 2 + 1]; res[0] = res[1] = true; for (int i = 2; i * i < 123457 * 2; i++) { for (int j = 2; i * j < 123457 * 2; j++) { res[i * j] = true; } } return res; } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(isr); Main app = new Main(); boolean prime[] = app.primeSet(); int n, res; while (true) { if ((n = Integer.valueOf(reader.readLine())) == 0) break; res = 0; for (int i = n + 1; i <= 2 * n; i++) { if (!prime[i]) res++; } System.out.println(res); } reader.close(); } public boolean[] primeSet() { boolean res[] = new boolean[123456 * 2 + 1]; res[0] = res[1] = true; for (int i = 2; i * i < 123457 * 2; i++) { for (int j = 2; i * j < 123457 * 2; j++) { res[i * j] = true; } } return res; } }
[["+", 0, 7, 502, 503, 49, 200, 51, 16, 17, 72], ["+", 0, 7, 502, 503, 49, 200, 51, 16, 12, 499]]
3
247
import java.util.Arrays; import java.util.Scanner; public class Main { static boolean p[]; public static void main(String[] args) { // TODO // ツ篠ゥツ督ョツ青カツ青ャツつウツづェツつスツδソツッツドツ・ツスツタツブ Scanner sc = new Scanner(System.in); int n, i, count; while (true) { n = sc.nextInt(); if (n == 0) break; count = 0; p = new boolean[2 * n + 1]; Arrays.fill(p, true); p[0] = p[1] = false; prime(p, n); for (i = n; i <= 2 * n; i++) { if (p[i]) count++; } System.out.println(count); } } static void prime(boolean p[], int n) { int i, j; for (i = 2; i <= 2 * n; i++) { if (p[i]) { for (j = 2 * i; j <= 2 * n; j += i) { p[j] = false; } } } } }
import java.util.Arrays; import java.util.Scanner; public class Main { static boolean p[]; public static void main(String[] args) { // TODO // ツ篠ゥツ督ョツ青カツ青ャツつウツづェツつスツδソツッツドツ・ツスツタツブ Scanner sc = new Scanner(System.in); int n, i, count; while (true) { n = sc.nextInt(); if (n == 0) break; count = 0; p = new boolean[2 * n + 1]; Arrays.fill(p, true); p[0] = p[1] = false; prime(p, n); for (i = n + 1; i <= 2 * n; i++) { if (p[i]) count++; } System.out.println(count); } } static void prime(boolean p[], int n) { int i, j; for (i = 2; i <= 2 * n; i++) { if (p[i]) { for (j = 2 * i; j <= 2 * n; j += i) { p[j] = false; } } } } }
[["+", 8, 196, 0, 7, 502, 11, 12, 16, 17, 72], ["+", 8, 196, 0, 7, 502, 11, 12, 16, 12, 499]]
3
231
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); if (n == 0) break; int count = 0; for (int i = n + 1; i <= 2 * n; i++) { int num = (int)Math.sqrt(i) + 1; if (i == 2) num = 2; int yaku_count = 0; for (int j = 1; j < num; j++) { if (i % j == 0) yaku_count += 2; } if (yaku_count == num * num) { yaku_count -= 1; } if (i == 2) yaku_count -= 1; if (yaku_count == 2) { count += 1; } } System.out.println(count); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); if (n == 0) break; int count = 0; for (int i = n + 1; i <= 2 * n; i++) { int num = (int)Math.sqrt(i); if (i == 2) num = 2; int yaku_count = 0; for (int j = 1; j < num + 1; j++) { if (i % j == 0) yaku_count += 2; } if (yaku_count == num * num) { yaku_count -= 1; } if (i == 2) yaku_count -= 1; if (yaku_count == 2) { count += 1; } } System.out.println(count); } } }
[["-", 8, 196, 0, 503, 49, 200, 51, 16, 17, 72], ["-", 8, 196, 0, 503, 49, 200, 51, 16, 12, 499], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 17, 72], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 12, 499]]
3
186
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String arg[]) { Scanner sc = new Scanner(System.in); boolean a[] = new boolean[123456 * 2 + 1]; Arrays.fill(a, true); a[0] = a[1] = false; for (int i = 2; i * i <= 123456 * 2; i++) { if (!a[i]) continue; for (int j = i * 2; j <= 123456 * 2; j += i) a[j] = false; } while (sc.hasNext()) { int ans = 0; int n = sc.nextInt(); if (n == 0) return; for (int i = n; i <= n * 2; i++) if (a[i]) ans++; System.out.println(ans); } } }
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String arg[]) { Scanner sc = new Scanner(System.in); boolean a[] = new boolean[123456 * 2 + 1]; Arrays.fill(a, true); a[0] = a[1] = false; for (int i = 2; i * i <= 123456 * 2; i++) { if (!a[i]) continue; for (int j = i * 2; j <= 123456 * 2; j += i) a[j] = false; } while (sc.hasNext()) { int ans = 0; int n = sc.nextInt(); if (n == 0) return; for (int i = n + 1; i <= n * 2; i++) if (a[i]) ans++; System.out.println(ans); } } }
[["+", 0, 7, 502, 503, 49, 200, 51, 16, 17, 72], ["+", 0, 7, 502, 503, 49, 200, 51, 16, 12, 499]]
3
201
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { ArrayList<Integer> numlist = new ArrayList<Integer>(); numlist.add(2); for (int i = 3; i < 123457; i += 2) { boolean TF = true; for (Integer integer : numlist) { if (integer * 2 > i) { break; } if ((i % integer) == 0) { TF = false; break; } else { continue; } } if (TF) { numlist.add(i); } } Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); if (n == 0) { break; } int count = 0; for (Integer integer : numlist) { if (n <= integer) { if (integer <= 2 * n) { count++; } else { break; } } } System.out.println(count); } } }
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { ArrayList<Integer> numlist = new ArrayList<Integer>(); numlist.add(2); for (int i = 3; i < 123457 * 2; i += 2) { boolean TF = true; for (Integer integer : numlist) { if (integer * 2 > i) { break; } if ((i % integer) == 0) { TF = false; break; } else { continue; } } if (TF) { numlist.add(i); } } Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); if (n == 0) { break; } int count = 0; for (Integer integer : numlist) { if (n < integer) { if (integer <= 2 * n) { count++; } else { break; } } } System.out.println(count); } } }
[["+", 8, 196, 0, 7, 15, 16, 12, 16, 17, 48], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 12, 499], ["-", 8, 196, 0, 57, 15, 15, 0, 16, 17, 19], ["+", 8, 196, 0, 57, 15, 15, 0, 16, 17, 18]]
3
216
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = 0, count = 0; boolean check = true; while (sc.hasNext()) { n = sc.nextInt(); count = 0; //テ・ツ按敕ヲツ慊淌・ツ個? check = true; if (n == 0) break; for (int i = n + 1; i <= 2 * n; i++) { check = true; for (int j = 2; j * j < i; j++) { if (i % j == 0) check = false; } if (check) count++; } System.out.println(count); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = 0, count = 0; boolean check = true; while (sc.hasNext()) { n = sc.nextInt(); count = 0; //テ・ツ按敕ヲツ慊淌・ツ個? check = true; if (n == 0) break; for (int i = n + 1; i <= 2 * n; i++) { check = true; for (int j = 2; j * j <= i; j++) { if (i % j == 0) check = false; } if (check) count++; } System.out.println(count); } } }
[["-", 0, 7, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 17, 19]]
3
154
#include <stdio.h> int main(void) { int n, i, j, c, f; while (scanf("%d", &n), n) { c = 0; for (i = n + 1; i <= 2 * n; i++) { f = 1; for (j = 2; j * j <= i; j++) { if (!(i & j)) { f = 0; break; } } if (f) c++; } printf("%d\n", c); } return 0; }
#include <stdio.h> int main(void) { int n, i, j, c, f; while (scanf("%d", &n), n) { c = 0; for (i = n + 1; i <= 2 * n; i++) { f = 1; for (j = 2; j * j <= i; j++) { if (!(i % j)) { f = 0; break; } } if (f) c++; } printf("%d\n", c); } return 0; }
[["-", 15, 23, 0, 91, 28, 23, 0, 16, 17, 67], ["+", 15, 23, 0, 91, 28, 23, 0, 16, 17, 109]]
0
117
#include <stdbool.h> #include <stdio.h> #define N 123456 int main() { int i, j, k, count; bool p[N * 2 + 1]; for (i = 0; i < N * 2 + 1; i++) { p[i] = 1; } p[0] = 0; p[1] = 0; for (i = 0; i < N; i++) { if (p[i] == 1) { for (j = 2; i * j < 2 * N + 1; j++) { p[i * j] = 0; } } } do { scanf("%d", &k); count = 0; if (k == 0 || k > 123456) { break; } for (i = k + 1; i <= 2 * k + 1; i++) { if (p[i]) { count++; } } printf("%d\n", count); } while (1); return 0; }
#include <stdbool.h> #include <stdio.h> #define N 123456 int main() { int i, j, k, count; bool p[N * 2 + 1]; for (i = 0; i < N * 2 + 1; i++) { p[i] = 1; } p[0] = 0; p[1] = 0; for (i = 0; i < N; i++) { if (p[i] == 1) { for (j = 2; i * j < 2 * N + 1; j++) { p[i * j] = 0; } } } do { scanf("%d", &k); count = 0; if (k == 0 || k > 123456) { break; } for (i = k + 1; i <= 2 * k; i++) { if (p[i]) { count++; } } printf("%d\n", count); } while (1); return 0; }
[["-", 8, 9, 0, 7, 15, 16, 12, 16, 17, 72], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
0
210
#include <stdio.h> #define N 123456 * 2 + 5 int main(void) { int a[N], i, j, l = 1, count; for (i = 0; i < N; i++) a[i] = 1; a[0] = a[1] = 0; for (i = 2; i * i < N; i++) { if (a[i]) for (j = i + i; j < N; j = j + i) { a[j] = 0; } } while (1) { scanf("%d", &l); if (l == 0) break; count = 0; for (i = l + 1; i < 2 * l; i++) if (a[i]) count++; printf("%d\n", count); } return 0; }
#include <stdio.h> #define N 123456 * 2 + 5 int main(void) { int a[N], i, j, l = 1, count; for (i = 0; i < N; i++) a[i] = 1; a[0] = a[1] = 0; for (i = 2; i * i < N; i++) { if (a[i]) for (j = i + i; j < N; j = j + i) { a[j] = 0; } } while (1) { scanf("%d", &l); if (l == 0) break; count = 0; for (i = l + 1; i <= 2 * l; i++) if (a[i]) count++; printf("%d\n", count); } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19]]
0
179
#include <math.h> #include <stdio.h> #include <string.h> int sosu(int n) { int i; if (n <= 1) return 0; ; for (i = 2; i <= sqrt(n); i++) { if (n % i == 0) return 0; } return 1; } int main() { int n, i; int ans; while (1) { scanf("%d", &n); if (n == 0) break; for (i = n; i <= n * 2; i++) { if (sosu(i)) ans++; } printf("%d\n", ans); } return 0; }
#include <math.h> #include <stdio.h> #include <string.h> int sosu(int n) { int i; if (n <= 1) return 0; ; for (i = 2; i <= sqrt(n); i++) { if (n % i == 0) return 0; } return 1; } int main() { int n, i; int ans; while (1) { scanf("%d", &n); if (n == 0) break; ans = 0; for (i = n + 1; i <= n * 2; i++) { if (sosu(i)) ans++; } printf("%d\n", ans); } return 0; }
[["+", 0, 52, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35], ["+", 8, 9, 0, 7, 10, 11, 12, 16, 17, 72], ["+", 8, 9, 0, 7, 10, 11, 12, 16, 12, 13]]
0
137
#include <math.h> #include <stdio.h> int isprime(int x) { if (x == 2) return 1; if (x < 2 || x % 2 == 0) return 0; int i; for (i = 3; i <= sqrt(x); i += 2) if (x % i == 0) return 0; return 1; } int main(void) { int n, x, i; for (;;) { scanf("%d", &n); if (n == 0) break; int num = 0; for (i = n; i <= 2 * n; i++) if (isprime(i)) num++; printf("%d\n", num); } return 0; }
#include <math.h> #include <stdio.h> int isprime(int x) { if (x == 2) return 1; if (x < 2 || x % 2 == 0) return 0; int i; for (i = 3; i <= sqrt(x); i += 2) if (x % i == 0) return 0; return 1; } int main(void) { int n, x, i; for (;;) { scanf("%d", &n); if (n == 0) break; int num = 0; for (i = n + 1; i <= 2 * n; i++) if (isprime(i)) num++; printf("%d\n", num); } return 0; }
[["+", 8, 9, 0, 7, 10, 11, 12, 16, 17, 72], ["+", 8, 9, 0, 7, 10, 11, 12, 16, 12, 13]]
0
152
#include <stdio.h> int main() { int n, i, j, l, d, c; while (1) { c = 0; scanf("%d", &n); if (n == 0) break; if (n == 1) printf("1\n"); else { for (i = n + 1; i <= n * 2; i++) { d = 0; for (j = 2; j * j < i; j++) { if (i % j == 0) { d++; break; } } if (d == 0) c++; } printf("%d\n", c); } } return 0; }
#include <stdio.h> int main() { int n, i, j, l, d, c; while (1) { c = 0; scanf("%d", &n); if (n == 0) break; if (n == 1) printf("1\n"); else { for (i = n + 1; i <= n * 2; i++) { d = 0; for (j = 2; j * j <= i; j++) { if (i % j == 0) { d++; break; } } if (d == 0) c++; } printf("%d\n", c); } } return 0; }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19]]
0
143
#include <stdio.h> #define MAX 123456 int main() { int n, n2, flag[MAX], i, j, count; while (1) { scanf("%d", &n); if (n == 0) break; n2 = 2 * n; for (i = 0; i < n2 + 1; i++) { flag[i] = 0; } count = 0; flag[0] -= 1; flag[1] -= 1; for (i = 2; i < n2 + 1; i++) { if (flag[i] < 0) continue; if (n2 < i * i) break; for (j = i * i; j < n2 + 1; j += i) { // 倍数を全て消していく flag[j] -= 1; } } for (i = 0; i < n2 + 1; i++) { if ((0 == flag[i]) && (n < i)) { // printf("%d ", i); count++; } } printf("%d\n", count); } return 0; }
#include <stdio.h> #define MAX 246912 int main() { int n, n2, flag[MAX], i, j, count; while (1) { scanf("%d", &n); if (n == 0) break; n2 = 2 * n; for (i = 0; i < n2 + 1; i++) { flag[i] = 0; } count = 0; flag[0] -= 1; flag[1] -= 1; for (i = 2; i < n2 + 1; i++) { if (flag[i] < 0) continue; if (n2 < i * i) break; for (j = i * i; j < n2 + 1; j += i) { flag[j] -= 1; } } for (i = 0; i < n2 + 1; i++) { if ((0 == flag[i]) && (n < i)) { count++; } } printf("%d\n", count); } return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59]]
0
218
a['___'], c; main(i, j) { for (; ++i * i < 1e6;) for (j = i; j < 1e6;) a[j += i] = 1; for (; scanf("%d", &j), i = j; c = !printf("%d\n", c)) for (; j++ <= i * 2;) c += !a[j]; }
a['___'], c; main(i, j) { for (; ++i * i < 1e6;) for (j = i; j < 1e6;) a[j += i] = 1; for (; scanf("%d", &j), i = j; c = !printf("%d\n", c)) for (; ++j <= i * 2;) c += !a[j]; }
[["-", 0, 7, 8, 7, 15, 16, 31, 27, 28, 22], ["+", 0, 7, 8, 7, 15, 16, 31, 27, 28, 22]]
0
99
#include <math.h> #include <stdio.h> int IsPrime(int n); int main() { int n, i, j, count, ans[1000000]; j = 0; while (1) { scanf("%d", &n); if (n == 0) break; count = 0; for (i = n; i <= 2 * n; i++) { if (IsPrime(i)) { count++; } } ans[j] = count; j++; } for (i = 0; i < j; i++) printf("%d\n", ans[i]); return 0; } int IsPrime(int n) { int i, limit; if (n == 2) return 1; if (n < 2 || n % 2 == 0) return 0; limit = (int)sqrt((double)n); for (i = 3; i <= limit; i += 2) if (n % i == 0) return 0; return 1; }
#include <math.h> #include <stdio.h> int IsPrime(int n); int main() { int n, i, j, count, ans[1000000]; j = 0; while (1) { scanf("%d", &n); if (n == 0) break; count = 0; for (i = n + 1; i <= 2 * n; i++) { if (IsPrime(i)) { count++; } } ans[j] = count; j++; } for (i = 0; i < j; i++) printf("%d\n", ans[i]); return 0; } int IsPrime(int n) { int i, limit; if (n == 2) return 1; if (n < 2 || n % 2 == 0) return 0; limit = (int)sqrt((double)n); for (i = 3; i <= limit; i += 2) if (n % i == 0) return 0; return 1; }
[["+", 8, 9, 0, 7, 10, 11, 12, 16, 17, 72], ["+", 8, 9, 0, 7, 10, 11, 12, 16, 12, 13]]
0
209
#include <stdio.h> int makelist(int *list, int kazu) { int i, j; for (i = 0; i < kazu; i++) { list[i] = 1; } list[0] = 0; list[1] = 0; for (i = 2; i < kazu; i++) { if (list[i]) { for (j = 2 * i; j < kazu; j += i) { list[j] = 0; } } } return 0; } int main() { int list[300000]; int n, ans; int i; makelist(list, 300000); while (1) { scanf("%d", &n); if (n == 0) { break; } ans = 0; for (i = n; i <= 2 * n; i++) { if (list[i] == 1) ans++; } printf("%d\n", ans); } return 0; }
#include <stdio.h> int makelist(int *list, int kazu) { int i, j; for (i = 0; i < kazu; i++) { list[i] = 1; } list[0] = 0; list[1] = 0; for (i = 2; i < kazu; i++) { if (list[i]) { for (j = 2 * i; j < kazu; j += i) { list[j] = 0; } } } return 0; } int main() { int list[300000]; int n, ans; int i; makelist(list, 300000); while (1) { scanf("%d", &n); if (n == 0) { break; } ans = 0; for (i = n + 1; i <= 2 * n; i++) { if (list[i] == 1) ans++; } printf("%d\n", ans); } return 0; }
[["+", 8, 9, 0, 7, 10, 11, 12, 16, 17, 72], ["+", 8, 9, 0, 7, 10, 11, 12, 16, 12, 13]]
0
206
#include <bits/stdc++.h> using namespace std; #define INF_LL (int64)1e18 #define INF (int32)1e9 #define REP(i, n) for (int64 i = 0; i < (n); i++) #define FOR(i, a, b) for (int64 i = (a); i < (b); i++) #define all(x) x.begin(), x.end() #define fs first #define sc second using int32 = int_fast32_t; using uint32 = uint_fast32_t; using int64 = int_fast64_t; using uint64 = uint_fast64_t; using PII = pair<int32, int32>; using PLL = pair<int64, int64>; const double eps = 1e-10; template <typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b; } bool notPrime[312345] = {}; int main(void) { notPrime[0] = notPrime[1] = 1; FOR(i, 2, 312345) { if (!notPrime[i]) { for (int32 j = 2; i * j < 312345; j++) { notPrime[i * j] = 1; } } } int32 n; while (cin >> n && n) { int32 cnt = 0; FOR(i, n + 1, 2 * n) { cnt += !notPrime[i]; } cout << cnt << endl; } }
#include <bits/stdc++.h> using namespace std; #define INF_LL (int64)1e18 #define INF (int32)1e9 #define REP(i, n) for (int64 i = 0; i < (n); i++) #define FOR(i, a, b) for (int64 i = (a); i < (b); i++) #define all(x) x.begin(), x.end() #define fs first #define sc second using int32 = int_fast32_t; using uint32 = uint_fast32_t; using int64 = int_fast64_t; using uint64 = uint_fast64_t; using PII = pair<int32, int32>; using PLL = pair<int64, int64>; const double eps = 1e-10; template <typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b; } bool notPrime[312345] = {}; int main(void) { notPrime[0] = notPrime[1] = 1; FOR(i, 2, 312345) { if (!notPrime[i]) { for (int32 j = 2; i * j < 312345; j++) { notPrime[i * j] = 1; } } } int32 n; while (cin >> n && n) { int32 cnt = 0; FOR(i, n + 1, 2 * n + 1) { cnt += !notPrime[i]; } cout << cnt << endl; } }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
273
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // conversion //------------------------------------------ inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } // math //------------------------------------------- template <class T> inline T sqr(T x) { return x * x; } // typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define EXISTch(s, c) \ ((((s).find_first_of(c)) != std::string::npos) ? 1 : 0) // cがあれば1 if(1) #define SORT(c) sort((c).begin(), (c).end()) // repetition //------------------------------------------ #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define loop(n) FOR(i, 0, n) #define rrep(i, a, b) for (int i = (a); i >= (b); --i) // constant //-------------------------------------------- const double EPS = 1e-10; const double PI = acos(-1.0); const int INF = (int)1000000007; const LL MOD = (LL)1000000007; // 10^9+7 const LL INF2 = (LL)100000000000000000; // 10^18 bool isPrime(int n) { if (n == 1) return false; if (n == 2) return true; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) { return false; } } return true; } int main() { while (true) { int n; cin >> n; if (n == 0) return 0; int cnt = 0; for (int i = n + 1; i <= 2 * n; i++) { if (isPrime(i)) cnt++; } cout << cnt << endl; } return 0; }
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // conversion //------------------------------------------ inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } // math //------------------------------------------- template <class T> inline T sqr(T x) { return x * x; } // typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define EXISTch(s, c) \ ((((s).find_first_of(c)) != std::string::npos) ? 1 : 0) // cがあれば1 if(1) #define SORT(c) sort((c).begin(), (c).end()) // repetition //------------------------------------------ #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define loop(n) FOR(i, 0, n) #define rrep(i, a, b) for (int i = (a); i >= (b); --i) // constant //-------------------------------------------- const double EPS = 1e-10; const double PI = acos(-1.0); const int INF = (int)1000000007; const LL MOD = (LL)1000000007; // 10^9+7 const LL INF2 = (LL)100000000000000000; // 10^18 bool isPrime(int n) { if (n == 1) return false; if (n == 2) return true; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } int main() { while (true) { int n; cin >> n; if (n == 0) return 0; int cnt = 0; for (int i = n + 1; i <= 2 * n; i++) { if (isPrime(i)) cnt++; } cout << cnt << endl; } return 0; }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["-", 0, 14, 8, 9, 0, 7, 26, 11, 17, 107], ["-", 0, 14, 8, 9, 0, 7, 26, 11, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 26, 27, 17, 29]]
1
436
#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<int> sprime(21800, 0); sprime[0] = 2; int a = 1; for (int i = 3; i < 123456 * 2; i += 2) { int k = 3; while (k <= sqrt(i)) { if (i % k == 0) break; k += 2; } if (k > sqrt(i)) { sprime[a] = i; a++; } } int n; while (cin >> n && n) { int cnt = 0; for (int i = 0; i < n; ++i) if (n < sprime[i] && sprime[i] <= 2 * n) cnt++; cout << cnt << endl; } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<int> sprime(200000, 0); sprime[0] = 2; int a = 1; for (int i = 3; i < 123456 * 2; i += 2) { int k = 3; while (k <= sqrt(i)) { if (i % k == 0) break; k += 2; } if (k > sqrt(i)) { sprime[a] = i; a++; } } int n; while (cin >> n && n) { int cnt = 0; for (int i = 0; i < n; ++i) if (n < sprime[i] && sprime[i] <= 2 * n) cnt++; cout << cnt << endl; } return 0; }
[["-", 8, 9, 0, 43, 49, 50, 51, 4, 0, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 4, 0, 13]]
1
175
#include <iostream> #include <list> #include <math.h> #include <vector> using namespace std; using vec = vector<int>; using lst = list<int>; bool check(int x, lst &sosu) { for (auto s : sosu) { if (sqrt(x) < s) return true; if (x % s == 0) return false; } return true; } int main() { vec count(123456 * 2 + 1); lst sosu; count[0] = 0; count[1] = 0; count[2] = 1; sosu.emplace_back(2); for (int i = 3; i < count.size(); ++i) { count[i] = count[i - 1]; if (check(i, sosu)) { ++count[i]; sosu.emplace_back(i); } } int n; while (true) { cin >> n; if (n == 0) return 0; cout << count[2 * n] - count[n - 1] << endl; } }
#include <iostream> #include <list> #include <math.h> #include <vector> using namespace std; using vec = vector<int>; using lst = list<int>; bool check(int x, lst &sosu) { for (auto s : sosu) { if (sqrt(x) < s) return true; if (x % s == 0) return false; } return true; } int main() { vec count(123456 * 2 + 1); lst sosu; count[0] = 0; count[1] = 0; count[2] = 1; sosu.emplace_back(2); for (int i = 3; i < count.size(); ++i) { count[i] = count[i - 1]; if (check(i, sosu)) { ++count[i]; sosu.emplace_back(i); } } int n; while (true) { cin >> n; if (n == 0) return 0; cout << count[2 * n] - count[n] << endl; } }
[["-", 12, 16, 12, 69, 341, 342, 0, 16, 17, 33], ["-", 12, 16, 12, 69, 341, 342, 0, 16, 12, 13]]
1
218
loop do h = gets.chomp.to_i if h == 0 break end cells = Array.new(h) H.times do |i| cells[i] = gets.chomp.split(" ").map(&:to_i) end ans = 0 loop do flg = false for i in 0...H for j in 0...5 if cells[i][j] == -1 next end cnt = 0 for k in j...5 if cells[i][j] != cells[i][k] break end cnt += 1 end if cnt < 3 next end ans += cells[i][j] * cnt for k in 0...cnt cells[i][j + k] = -1 end flg = true end end if !flg break end for j in 0...5 for i in 0...H idx = H - i - 1 if cells[idx][j] == -1 next end while idx + 1 < H && cells[idx + 1][j] == -1 do cells[idx][j], cells[idx + 1][j] = cells[idx + 1][j], cells[idx][j] idx += 1 end end end end puts ans end
loop do h = gets.chomp.to_i if h == 0 break end cells = Array.new(h) h.times do |i| cells[i] = gets.chomp.split(" ").map(&:to_i) end ans = 0 loop do flg = false for i in 0...h for j in 0...5 if cells[i][j] == -1 next end cnt = 0 for k in j...5 if cells[i][j] != cells[i][k] break end cnt += 1 end if cnt < 3 next end ans += cells[i][j] * cnt for k in 0...cnt cells[i][j + k] = -1 end flg = true end end if !flg break end for j in 0...5 for i in 0...h idx = h - i - 1 if cells[idx][j] == -1 next end while idx + 1 < h && cells[idx + 1][j] == -1 do cells[idx][j], cells[idx + 1][j] = cells[idx + 1][j], cells[idx][j] idx += 1 end end end end puts ans end
[["-", 0, 652, 196, 737, 8, 736, 0, 652, 486, 743], ["+", 0, 652, 196, 737, 8, 736, 0, 652, 486, 22], ["-", 8, 736, 0, 88, 51, 267, 0, 475, 444, 743], ["+", 8, 736, 0, 88, 51, 267, 0, 475, 444, 22], ["-", 8, 170, 0, 88, 51, 267, 0, 475, 444, 743], ["+", 8, 170, 0, 88, 51, 267, 0, 475, 444, 22], ["-", 8, 170, 0, 662, 12, 738, 31, 738, 31, 743], ["+", 8, 170, 0, 662, 12, 738, 31, 738, 31, 22], ["-", 8, 170, 0, 89, 15, 738, 31, 738, 12, 743], ["+", 8, 170, 0, 89, 15, 738, 31, 738, 12, 22]]
4
259
a = int(input()) global s s = 0 def de(ppp,h): re = False co = 0 global s for lo in ppp: for j ,cell in enumerate(lo): k = j p = j if k <= 2: while cell == lo[k+1] and cell != None: co += 1 if k == 3: j = k+1 break k += 1 j = k if co >= 2: re = True s += (co+1)*cell for l in range(5): if l >= p and l <= j: lo[l] = None co = 0 return re def mo(ppp,h): for i ,roe in enumerate(ppp): for j , cell in enumerate(roe): kkk = i while i != 0 and ppp[kkk-1][j] == None: ppp[kkk-1][j] = ppp[kkk][j] ppp[kkk][j] = None if kkk == 1: break kkk -= 1 while a != 0: ppp = [list(map(int,input().split())) for i in range(a)] ppp.reverse() while(de(ppp,a) == True): mo(ppp,a) print(ppp) print(s) s = 0 a = int(input())
a = int(input()) global s s = 0 def de(ppp,h): re = False co = 0 global s for lo in ppp: for j ,cell in enumerate(lo): k = j p = j if k <= 2: while cell == lo[k+1] and cell != None: co += 1 if k == 3: j = k+1 break k += 1 j = k if co >= 2: re = True s += (co+1)*cell for l in range(5): if l >= p and l <= j: lo[l] = None co = 0 return re def mo(ppp,h): for i ,roe in enumerate(ppp): for j , cell in enumerate(roe): kkk = i while i != 0 and ppp[kkk-1][j] == None: ppp[kkk-1][j] = ppp[kkk][j] ppp[kkk][j] = None if kkk == 1: break kkk -= 1 while a != 0: ppp = [list(map(int,input().split())) for i in range(a)] ppp.reverse() while(de(ppp,a) == True): mo(ppp,a) print(s) s = 0 a = int(input())
[["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 22], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 25], ["-", 0, 52, 8, 196, 0, 1, 0, 652, 63, 22], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 24]]
5
290
import java.awt.Point; import java.io.FileInputStream; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; public class Main { static int r, n; static int[] xl, xr, h; private static void start() { int i, j; Point[] height = new Point[41]; for (i = 0; i < 41; i++) height[i] = new Point(0, 0); for (i = 0; i < n; i++) for (j = xl[i]; j <= xr[i]; j++) { if (j != xl[i]) height[j].x = Math.max(height[j].x, h[i]); if (j != xr[i]) height[j].y = Math.max(height[j].y, h[i]); } double min = Double.MAX_VALUE; for (i = (20 - r); i <= (20 + r); i++) { if (i != (20 - r)) { double w = Math.abs(i - 20); double p = Math.sqrt(r * r - w * w); double t = (double)height[i].y - p + (double)r; min = Math.min(min, t); } if (i != (20 + r)) { double w = Math.abs(i - 20); double p = Math.sqrt(r * r - w * w); double t = (double)height[i].x - p + (double)r; min = Math.min(min, t); } } System.out.printf("%.04f\n", min); } public static void main(String[] args) { Scanner sca = new Scanner(System.in); while (true) { r = sca.nextInt(); n = sca.nextInt(); if (r == 0) break; xl = new int[n]; xr = new int[n]; h = new int[n]; for (int i = 0; i < n; i++) { xl[i] = sca.nextInt() + 20; xr[i] = sca.nextInt() + 20; h[i] = sca.nextInt(); } start(); } } }
import java.awt.Point; import java.io.FileInputStream; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; public class Main { static int r, n; static int[] xl, xr, h; private static void start() { int i, j; Point[] height = new Point[41]; for (i = 0; i < 41; i++) height[i] = new Point(0, 0); for (i = 0; i < n; i++) for (j = xl[i]; j <= xr[i]; j++) { if (j != xl[i]) height[j].x = Math.max(height[j].x, h[i]); if (j != xr[i]) height[j].y = Math.max(height[j].y, h[i]); } double min = Double.MAX_VALUE; for (i = (20 - r); i <= (20 + r); i++) { if (i != (20 - r)) { double w = Math.abs(i - 20); double p = Math.sqrt(r * r - w * w); double t = (double)height[i].x - p + (double)r; min = Math.min(min, t); } if (i != (20 + r)) { double w = Math.abs(i - 20); double p = Math.sqrt(r * r - w * w); double t = (double)height[i].y - p + (double)r; min = Math.min(min, t); } } System.out.printf("%.04f\n", min); } public static void main(String[] args) { Scanner sca = new Scanner(System.in); while (true) { r = sca.nextInt(); n = sca.nextInt(); if (r == 0) break; xl = new int[n]; xr = new int[n]; h = new int[n]; for (int i = 0; i < n; i++) { xl[i] = sca.nextInt() + 20; xr[i] = sca.nextInt() + 20; h[i] = sca.nextInt(); } start(); } } }
[["-", 51, 16, 31, 16, 31, 74, 51, 509, 119, 22], ["+", 51, 16, 31, 16, 31, 74, 51, 509, 119, 22]]
3
526
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; int hm[43]; int r, n; int main() { while (cin >> r >> n, r) { REP(i, 43) hm[i] = 0; REP(i, n) { int xl, xr, h; cin >> xl >> xr >> h; for (int i = xl + 21; i < xr + 21; ++i) hm[i] = max(hm[i], h); } double mx = 21; for (int i = 21 - r; i <= 21 + r; ++i) mx = min(mx, min(hm[i - 1], hm[i]) + r - sqrt(r * r - (i - 21) * (i - 21))); cout << setprecision(10) << fixed << mx << endl; } return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; int hm[43]; int r, n; int main() { while (cin >> r >> n, r) { REP(i, 43) hm[i] = 0; REP(i, n) { int xl, xr, h; cin >> xl >> xr >> h; for (int i = xl + 21; i < xr + 21; ++i) hm[i] = max(hm[i], h); } double mx = 21; for (int i = 21 - r + 1; i < 21 + r; ++i) mx = min(mx, min(hm[i - 1], hm[i]) + r - sqrt(r * r - (i - 21) * (i - 21))); cout << setprecision(10) << fixed << mx << endl; } return 0; }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18]]
1
194
#include <cmath> #include <iomanip> #include <iostream> #include <vector> using namespace std; int main() { int n, r, diff = 20; while (std::cin >> r >> n, r) { vector<int> hl(50, 0), hr(50, 0); for (int i = 0; i < n; i++) { int xl, xr, h; std::cin >> xl >> xr >> h; for (int j = xl + 1; j < xr; j++) { hr[j + diff] = max(hr[j + diff], h); } for (int j = xl; j <= xr; j++) { hl[j + diff] = max(hl[j + diff], h); } } double ans = 1e9; for (int i = -r + 1; i < r; ++i) { ans = min(ans, min(hr[i + diff], hl[i + diff]) + r - sqrt(r * r - i * i)); } std::cout << setprecision(12) << fixed << ans << std::endl; } return 0; }
#include <cmath> #include <iomanip> #include <iostream> #include <vector> using namespace std; int main() { int n, r, diff = 20; while (std::cin >> r >> n, r) { vector<int> hl(50, 0), hr(50, 0); for (int i = 0; i < n; i++) { int xl, xr, h; std::cin >> xl >> xr >> h; for (int j = xl; j < xr; j++) { hr[j + diff] = max(hr[j + diff], h); } for (int j = xl + 1; j <= xr; j++) { hl[j + diff] = max(hl[j + diff], h); } } double ans = 1e9; for (int i = -r + 1; i < r; ++i) { ans = min(ans, min(hr[i + diff], hl[i + diff]) + r - sqrt(r * r - i * i)); } std::cout << setprecision(12) << fixed << ans << std::endl; } return 0; }
[["-", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["-", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
245
#include <algorithm> #include <cmath> #include <iostream> using namespace std; int main() { int r, n, sl, sr, h; int height[40]; double sunrise[40]; while (true) { cin >> r >> n; if (r == 0 && n == 0) { break; } for (int i = 0; i < 40; i++) { height[i] = 0; sunrise[i] = -5000.0; } for (int i = 0; i < n; i++) { cin >> sl >> sr >> h; for (int j = sl; j < sr; j++) { height[j + 20] = max(h, height[i]); } } for (int i = 20 - r; i < 20 + r; i++) { if (i < 20) { sunrise[i] = sqrt(r * r - (19 - i) * (19 - i)) - r; } else { sunrise[i] = sqrt(r * r - (i - 20) * (i - 20)) - r; } } double Answer = 10000.0; for (int i = 0; i < 40; i++) { Answer = min(height[i] - sunrise[i], Answer); } printf("%.4f\n", Answer); } return 0; }
#include <algorithm> #include <cmath> #include <iostream> using namespace std; int main() { int r, n, sl, sr, h; int height[40]; double sunrise[40]; while (true) { cin >> r >> n; if (r == 0 && n == 0) { break; } for (int i = 0; i < 40; i++) { height[i] = 0; sunrise[i] = -5000.0; } for (int i = 0; i < n; i++) { cin >> sl >> sr >> h; for (int j = sl; j < sr; j++) { height[j + 20] = max(h, height[j + 20]); } } for (int i = 20 - r; i < 20 + r; i++) { if (i < 20) { sunrise[i] = sqrt(r * r - (19 - i) * (19 - i)) - r; } else { sunrise[i] = sqrt(r * r - (i - 20) * (i - 20)) - r; } } double Answer = 10000.0; for (int i = 0; i < 40; i++) { Answer = min(height[i] - sunrise[i], Answer); } printf("%.4f\n", Answer); } return 0; }
[["-", 12, 2, 3, 4, 0, 69, 341, 342, 0, 22], ["+", 3, 4, 0, 69, 341, 342, 0, 16, 31, 22], ["+", 3, 4, 0, 69, 341, 342, 0, 16, 17, 72], ["+", 3, 4, 0, 69, 341, 342, 0, 16, 12, 13]]
1
286
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define LOG(...) printf(__VA_ARGS__) //#define LOG(...) #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) #define RSORT(c) sort((c).rbegin(), (c).rend()) #define CLR(a) memset((a), 0, sizeof(a)) typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; struct UnionFind { vector<int> v; UnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; } int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); } void unite(int x, int y) { v[find(x)] = find(y); } }; int main() { int r; int n; while (cin >> r >> n, r) { double hight[40] = {0}; double suny[41]; REP(i, 41) suny[i] = -1e9; REP(i, 41) { if (-r <= i - 20 && i - 20 <= r) suny[i] = -(double)r + sqrt((double)r * (double)r - (double)(i - 20) * (double)(i - 20)); } REP(i, n) { int x, x2, h; cin >> x >> x2 >> h; REP(i, x2 - x) { hight[x + 20 + i] = max(hight[x + 20 + i], (double)h); } } double mi = 1e9; REP(i, 41) { if (abs(i - 20) <= r) { if (i - 20 == -r) mi = min(mi, hight[i] - suny[i]); if (i - 20 == r) mi = min(mi, hight[i - 1] - suny[i]); else mi = min(mi, min(hight[i], hight[i - 1]) - suny[i]); } } printf("%.4lf\n", mi); } }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define LOG(...) printf(__VA_ARGS__) //#define LOG(...) #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) #define RSORT(c) sort((c).rbegin(), (c).rend()) #define CLR(a) memset((a), 0, sizeof(a)) typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; struct UnionFind { vector<int> v; UnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; } int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); } void unite(int x, int y) { v[find(x)] = find(y); } }; int main() { int r; int n; while (cin >> r >> n, r) { double hight[40] = {0}; double suny[41]; REP(i, 41) suny[i] = -1e9; REP(i, 41) { if (-r <= i - 20 && i - 20 <= r) suny[i] = -(double)r + sqrt((double)r * (double)r - (double)(i - 20) * (double)(i - 20)); } REP(i, n) { int x, x2, h; cin >> x >> x2 >> h; REP(i, x2 - x) { hight[x + 20 + i] = max(hight[x + 20 + i], (double)h); } } double mi = 100.0; REP(i, 41) { if (abs(i - 20) <= r) { if (i - 20 == -r) mi = min(mi, hight[i] - suny[i]); else if (i - 20 == r) mi = min(mi, hight[i - 1] - suny[i]); else mi = min(mi, min(hight[i], hight[i - 1]) - suny[i]); } } printf("%.4lf\n", mi); } }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 57, 64, 9, 0, 57, 75, 76, 0, 95]]
1
602
#include "bits/stdc++.h" using namespace std; //#define int long long #define REP(i, s, n) for (signed i = (s); i < signed(n); i++) #define rep(i, n) REP(i, 0, n) #define all(c) (c).begin(), (c).end() using VV = vector<vector<int>>; using V = vector<int>; using P = pair<int, int>; using IP = pair<int, P>; signed main() { int r, n; while (cin >> r >> n && r) { V height(r * 2); rep(i, n) { int lx, rx, h; cin >> lx >> rx >> h; lx += r; rx += r; REP(i, max(0, lx), min(r * 2, rx)) { height[i] = max(height[i], h); } } double ans = r * 2; rep(x, r * 2) { int h = height[x]; double a = sqrt(r * r - pow(abs(r - x) - (r - x < 0), 2)); double sun = (double)(h + r) - a; ans = min(ans, sun); } printf("%.8f\n", ans); } }
#include "bits/stdc++.h" using namespace std; //#define int long long #define REP(i, s, n) for (signed i = (s); i < signed(n); i++) #define rep(i, n) REP(i, 0, n) #define all(c) (c).begin(), (c).end() using VV = vector<vector<int>>; using V = vector<int>; using P = pair<int, int>; using IP = pair<int, P>; signed main() { int r, n; while (cin >> r >> n && r) { V height(r * 2); rep(i, n) { int lx, rx, h; cin >> lx >> rx >> h; lx += r; rx += r; REP(i, max(0, lx), min(r * 2, rx)) { height[i] = max(height[i], h); } } double ans = 1 << 27; rep(x, r * 2) { int h = height[x]; double a = sqrt(r * r - pow(abs(r - x) - (r - x > 0), 2)); double sun = (double)(h + r) - a; ans = min(ans, sun); } printf("%.8f\n", ans); } }
[["-", 8, 9, 0, 43, 49, 50, 51, 16, 31, 22], ["-", 8, 9, 0, 43, 49, 50, 51, 16, 17, 48], ["-", 8, 9, 0, 43, 49, 50, 51, 16, 12, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 16, 31, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 16, 17, 151], ["+", 8, 9, 0, 43, 49, 50, 51, 16, 12, 13], ["-", 3, 4, 0, 16, 12, 23, 0, 16, 17, 18], ["+", 3, 4, 0, 16, 12, 23, 0, 16, 17, 47]]
1
261
#include <cmath> #include <iostream> using namespace std; int r, n, L, R, h, i, x; int main() { while (cin >> r >> n, r) { int A[41] = {}, B[41] = {}; for (i = 0; i < n; i++) { cin >> L >> R >> h; for (x = L; x <= R; x++) { if (x != R) A[x + 20] = max(A[x + 20], h); if (x != L) B[x + 20] = max(B[x + 20], h); } } double res = 99; for (i = -r; i <= r - 1;) { res = min(res, min(r - sqrt(r * r - i * i) + A[i + 20], r - sqrt(r * r - (i + 1) * (i + 1)) + B[i++ + 1 + 20])); } cout << res << "\n"; } return 0; }
#include <cmath> #include <iostream> using namespace std; int r, n, L, R, h, i, x; int main() { while (cin >> r >> n, r) { int A[41] = {}, B[41] = {}; for (i = 0; i < n; i++) { cin >> L >> R >> h; for (x = L; x <= R; x++) { if (x != R) A[x + 20] = max(A[x + 20], h); if (x != L) B[x + 20] = max(B[x + 20], h); } } double res = 99; for (i = -r; i <= r - 1; i++) { res = min(res, min(r - sqrt(r * r - i * i) + A[i + 20], r - sqrt(r * r - (i + 1) * (i + 1)) + B[i + 1 + 20])); } cout << res << "\n"; } return 0; }
[["+", 0, 52, 8, 9, 0, 7, 26, 27, 28, 22], ["+", 0, 52, 8, 9, 0, 7, 26, 27, 17, 29], ["-", 341, 342, 0, 16, 31, 16, 31, 27, 17, 29]]
1
239
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) int s[21][41]; int main(void) { int r, n, xl, xr, h; while (cin >> r >> n && r) { rep(i, 41) rep(j, 41) s[i][j] = 0; rep(i, n) { cin >> xl >> xr >> h; for (int y = 0; y < h; ++y) { for (int x = xl + 20; x < xr + 20; ++x) { s[y][x] = 1; } } } /*for(int y=20; y>=0; --y) { rep(x, 41) cout << s[y][x]; cout << endl; }*/ double mint = numeric_limits<double>::max(); rep(y, 21) { for (int x = -r + 20; x < r + 20; ++x) { if (s[y][x] == 0) { double t; if (x < 20) t = (double)y + r - sqrt(pow((double)r, 2.0) - pow(x - 19.0, 2.0)); else t = (double)y + r - sqrt(pow((double)r, 2.0) - pow(x - 20.0, 2.0)); mint = min(mint, t); } } } printf("%.4lf\n", mint); } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) int s[21][41]; int main(void) { int r, n, xl, xr, h; while (cin >> r >> n && r) { rep(i, 21) rep(j, 41) s[i][j] = 0; rep(i, n) { cin >> xl >> xr >> h; for (int y = 0; y < h; ++y) { for (int x = xl + 20; x < xr + 20; ++x) { s[y][x] = 1; } } } double mint = numeric_limits<double>::max(); rep(y, 21) { for (int x = -r + 20; x < r + 20; ++x) { if (s[y][x] == 0) { double t; if (x < 20) t = (double)y + r - sqrt(pow((double)r, 2.0) - pow(x - 19.0, 2.0)); else t = (double)y + r - sqrt(pow((double)r, 2.0) - pow(x - 20.0, 2.0)); mint = min(mint, t); } } } printf("%.4lf\n", mint); } }
[["-", 8, 9, 0, 42, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 42, 0, 2, 3, 4, 0, 13]]
1
285
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> using namespace std; int main() { while (true) { int r, n; cin >> r >> n; if (r == 0 && n == 0) { break; } bool grid[21][41]; for (int i = 0; i < 21; i++) { for (int j = 0; j < 41; j++) { grid[i][j] = false; } } for (int i = 0; i < n; i++) { int lx, rx, y; cin >> lx >> rx >> y; for (int j = 0; j < y; j++) { for (int k = lx + 20; k < rx + 20; k++) { grid[j][k] = true; } } } double ans = 10000000; for (int y = 0; y < 21; y++) { for (int x = -r + 20; x < r + 20; x++) { if (grid[y][x] == false) { double temp; if (x < 20) { temp = (double)r - sqrt((double)r * 2 - abs(x - 19) * abs(x - 19)) + y; } else { temp = (double)r - sqrt((double)r * 2 - abs(x - 20) * abs(x - 20)) + y; } ans = min(ans, temp); } } } printf("%.4f\n", ans); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> using namespace std; int main() { while (true) { int r, n; cin >> r >> n; if (r == 0 && n == 0) { break; } bool grid[21][41]; for (int i = 0; i < 21; i++) { for (int j = 0; j < 41; j++) { grid[i][j] = false; } } for (int i = 0; i < n; i++) { int lx, rx, y; cin >> lx >> rx >> y; for (int j = 0; j < y; j++) { for (int k = lx + 20; k < rx + 20; k++) { grid[j][k] = true; } } } double ans = 10000000; for (int y = 0; y < 21; y++) { for (int x = -r + 20; x < r + 20; x++) { if (grid[y][x] == false) { double temp; if (x < 20) { temp = (double)r - sqrt((double)r * r - abs(x - 19) * abs(x - 19)) + y; } else { temp = (double)r - sqrt((double)r * r - abs(x - 20) * abs(x - 20)) + y; } ans = min(ans, temp); } } } printf("%.4f\n", ans); } return 0; }
[["-", 12, 2, 3, 4, 0, 16, 31, 16, 12, 13], ["+", 12, 2, 3, 4, 0, 16, 31, 16, 12, 22]]
1
335
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define repr(i, n) for (int i = (int)(n - 1); i >= 0; i--) #define rep(i, n) repl(i, 0, n) #define each(itr, v) for (auto itr : v) #define pb(s) push_back(s) #define all(x) (x).begin(), (x).end() #define dbg(x) cout << #x " = " << x << endl #define print(x) cout << x << endl #define maxch(x, y) x = max(x, y) #define minch(x, y) x = min(x, y) #define uni(x) x.erase(unique(all(x)), x.end()) #define exist(x, y) (find(all(x), y) != x.end()) #define bcnt(x) bitset<32>(x).count() typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> P; typedef pair<P, int> PPI; typedef pair<int, P> PIP; typedef pair<ll, ll> PL; typedef pair<P, ll> PPL; typedef set<int> S; #define INF INT_MAX / 3 #define MAX_N 1000000001 int x[22][42]; int r, n; int dx[] = {0, -1, -1, 0}; int dy[] = {0, 0, -1, -1}; bool check(double t) { repl(i, 1, 21) repl(j, 1, 41) { if ((i - 1 - t + r) * (i - 1 - t + r) + (j - 21) * (j - 21) <= r * r) { rep(k, 4) { if (x[i + dy[k]][j + dx[k]] == 0) { return false; } } } } return true; } int main() { cin.sync_with_stdio(false); rep(i, 22) rep(j, 42) x[i][j] = 1; while (cin >> r >> n, r) { repl(i, 1, 21) repl(j, 1, 41) x[i][j] = 0; rep(i, n) { int l, r, h; cin >> l >> r >> h; repl(k, 1, h + 1) repl(j, l + 21, r + 21) x[k][j] = 1; } double ok = 0, ng = 20, md; rep(i, 100) { md = (ok + ng) / 2; if (check(md)) ok = md; else ng = md; } printf("%.10f\n", ok); } return 0; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define repr(i, n) for (int i = (int)(n - 1); i >= 0; i--) #define rep(i, n) repl(i, 0, n) #define each(itr, v) for (auto itr : v) #define pb(s) push_back(s) #define all(x) (x).begin(), (x).end() #define dbg(x) cout << #x " = " << x << endl #define print(x) cout << x << endl #define maxch(x, y) x = max(x, y) #define minch(x, y) x = min(x, y) #define uni(x) x.erase(unique(all(x)), x.end()) #define exist(x, y) (find(all(x), y) != x.end()) #define bcnt(x) bitset<32>(x).count() typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> P; typedef pair<P, int> PPI; typedef pair<int, P> PIP; typedef pair<ll, ll> PL; typedef pair<P, ll> PPL; typedef set<int> S; #define INF INT_MAX / 3 #define MAX_N 1000000001 int x[22][42]; int r, n; int dx[] = {0, -1, -1, 0}; int dy[] = {0, 0, -1, -1}; bool check(double t) { repl(i, 1, 21) repl(j, 1, 41) { if ((i - 1 - t + r) * (i - 1 - t + r) + (j - 21) * (j - 21) < r * r) { rep(k, 4) { if (x[i + dy[k]][j + dx[k]] == 0) { return false; } } } } return true; } int main() { cin.sync_with_stdio(false); rep(i, 22) rep(j, 42) x[i][j] = 1; while (cin >> r >> n, r) { repl(i, 1, 21) repl(j, 1, 41) x[i][j] = 0; rep(i, n) { int l, r, h; cin >> l >> r >> h; repl(k, 1, h + 1) repl(j, l + 21, r + 21) x[k][j] = 1; } double ok = 0, ng = 20, md; rep(i, 100) { md = (ok + ng) / 2; if (check(md)) ok = md; else ng = md; } printf("%.10f\n", ok); } return 0; }
[["-", 0, 9, 0, 57, 15, 339, 51, 16, 17, 19], ["+", 0, 9, 0, 57, 15, 339, 51, 16, 17, 18]]
1
515
//#define __USE_MINGW_ANSI_STDIO 0 #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VL; typedef vector<VL> VVL; typedef pair<int, int> PII; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() #define IN(a, b, x) (a <= x && x < b) #define MP make_pair #define PB push_back #define MOD 1000000007 #define INF (1LL << 30) #define LLINF (1LL << 60) #define PI 3.14159265359 #define EPS 1e-12 //#define int ll int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; int xl[25], xr[25], h[25]; signed main(void) { while (true) { int r, n; cin >> r >> n; if (!r && !n) break; REP(i, n) cin >> xl[i] >> xr[i] >> h[i]; double hi = 60, low = 0; REP(i, 20) { double mid = (hi + low) / 2; // cout << hi << " " << mid << " " << low << endl; bool flag = true; REP(j, 200000) { double x = -r + (double)(2 + r) * j / 200000; int ma = 0; REP(k, n) { if (xl[k] <= x && x <= xr[k]) { ma = max(ma, h[k]); } } double H = sqrt(r * r - x * x) + mid - r; // cout << ma << " " << H << endls if (ma < H) { flag = false; break; } } if (flag) { low = mid; } else { hi = mid; } } cout << fixed << setprecision(10) << low << endl; } return 0; }
//#define __USE_MINGW_ANSI_STDIO 0 #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VL; typedef vector<VL> VVL; typedef pair<int, int> PII; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() #define IN(a, b, x) (a <= x && x < b) #define MP make_pair #define PB push_back #define MOD 1000000007 #define INF (1LL << 30) #define LLINF (1LL << 60) #define PI 3.14159265359 #define EPS 1e-12 //#define int ll int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; int xl[25], xr[25], h[25]; signed main(void) { while (true) { int r, n; cin >> r >> n; if (!r && !n) break; REP(i, n) cin >> xl[i] >> xr[i] >> h[i]; double hi = 60, low = 0; REP(i, 20) { double mid = (hi + low) / 2; // cout << hi << " " << mid << " " << low << endl; bool flag = true; REP(j, 200000) { double x = -r + (double)(2 * r) * j / 200000; int ma = 0; REP(k, n) { if (xl[k] <= x && x <= xr[k]) { ma = max(ma, h[k]); } } double H = sqrt(r * r - x * x) + mid - r; // cout << ma << " " << H << endls if (ma < H) { flag = false; break; } } if (flag) { low = mid; } else { hi = mid; } } cout << fixed << setprecision(10) << low << endl; } return 0; }
[["-", 31, 16, 31, 74, 51, 23, 0, 16, 17, 72], ["+", 31, 16, 31, 74, 51, 23, 0, 16, 17, 48]]
1
380
#include <bits/stdc++.h> using namespace std; #ifdef DEBUG_MODE #define DBG(n) n; #else #define DBG(n) ; #endif #define REP(i, n) for (ll(i) = (0); (i) < (n); ++i) #define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i) #define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i)) #define PB push_back #define MP make_pair #define FI first #define SE second #define SHOW1d(v, n) \ { \ for (int W = 0; W < (n); W++) \ cerr << v[W] << ' '; \ cerr << endl << endl; \ } #define SHOW2d(v, i, j) \ { \ for (int aaa = 0; aaa < i; aaa++) { \ for (int bbb = 0; bbb < j; bbb++) \ cerr << v[aaa][bbb] << ' '; \ cerr << endl; \ } \ cerr << endl; \ } #define ALL(v) v.begin(), v.end() #define Decimal fixed << setprecision(20) #define INF 1000000000 #define MOD 1000000007 #define EPS 1e-7 typedef long long ll; typedef pair<ll, ll> P; double high[66]; int n, r; bool check(double mid) { for (int i = 30 - r; i < 30 + r; i++) { DBG(cerr << "hidh " << i << ' ' << high[i] << ' ' << high[i + 1] << endl;) if (i != 30 - r && i != 30 + r && high[i] < mid) return false; double A = sqrt((i - 30) * (i - 30) + (high[i] - mid) * (high[i] - mid)); double B = sqrt((i - 29) * (i - 29) + (high[i] - mid) * (high[i] - mid)); DBG(cerr << "AB " << A << ' ' << B << endl;) if (min(A, B) < r + EPS) return false; } return true; } int main() { while (cin >> r >> n, r | n) { //初期 REP(i, 66) high[i] = 0; REP(i, n) { int a, b; double c; cin >> a >> b >> c; for (int j = a; j < b; j++) { high[j + 30] = max(high[j + 30], c); } } double tb = 100; double ub = -10; DBG(SHOW1d(high, 66)); REP(i, 1000) { double mid = (tb + ub) / 2; DBG(cerr << endl << "mid " << mid << endl << endl;) if (check(mid)) ub = mid; else tb = mid; } cout << Decimal << (double)(ub + r) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef DEBUG_MODE #define DBG(n) n; #else #define DBG(n) ; #endif #define REP(i, n) for (ll(i) = (0); (i) < (n); ++i) #define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i) #define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i)) #define PB push_back #define MP make_pair #define FI first #define SE second #define SHOW1d(v, n) \ { \ for (int W = 0; W < (n); W++) \ cerr << v[W] << ' '; \ cerr << endl << endl; \ } #define SHOW2d(v, i, j) \ { \ for (int aaa = 0; aaa < i; aaa++) { \ for (int bbb = 0; bbb < j; bbb++) \ cerr << v[aaa][bbb] << ' '; \ cerr << endl; \ } \ cerr << endl; \ } #define ALL(v) v.begin(), v.end() #define Decimal fixed << setprecision(20) #define INF 1000000000 #define MOD 1000000007 #define EPS 1e-7 typedef long long ll; typedef pair<ll, ll> P; double high[66]; int n, r; bool check(double mid) { for (int i = 30 - r; i < 30 + r; i++) { DBG(cerr << "hidh " << i << ' ' << high[i] << ' ' << high[i + 1] << endl;) if (i != 30 - r && i != 30 + r && high[i] < mid) return false; double A = sqrt((i - 30) * (i - 30) + (high[i] - mid) * (high[i] - mid)); double B = sqrt((i - 29) * (i - 29) + (high[i] - mid) * (high[i] - mid)); DBG(cerr << "AB " << A << ' ' << B << endl;) if (min(A, B) < r + EPS) return false; } return true; } int main() { while (cin >> r >> n, r | n) { //初期 REP(i, 66) high[i] = 0; REP(i, n) { int a, b; double c; cin >> a >> b >> c; for (int j = a; j < b; j++) { high[j + 30] = max(high[j + 30], c); } } double tb = 100; double ub = -100; DBG(SHOW1d(high, 66)); REP(i, 1000) { double mid = (tb + ub) / 2; DBG(cerr << endl << "mid " << mid << endl << endl;) if (check(mid)) ub = mid; else tb = mid; } cout << Decimal << (double)(ub + r) << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
1
507
#include <algorithm> #include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <math.h> #include <queue> #include <string> #include <vector> // #include <boost/multiprecision/cpp_int.hpp> #include <functional> // #define int long long #define inf 1000000007 #define pa pair<int, int> #define ll long long #define pal pair<double, pa> #define ppa pair<pa, int> #define ppap pair<int, pa> #define ssa pair<string, int> #define mp make_pair #define pb push_back #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) using namespace std; // priority_queue<int, vector<int>, greater<int> > que; class Point { public: double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(x * a, y * a); } Point operator/(double a) { return Point(x / a, y / a); } double absv() { return sqrt(norm()); } double norm() { return x * x + y * y; } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } }; typedef Point Vector; struct Segment { Point p1, p2; }; double hen(Vector a) { if (fabs(a.x) < EPS && a.y > 0) return acos(0); else if (fabs(a.x) < EPS && a.y < 0) return 3 * acos(0); else if (fabs(a.y) < EPS && a.x < 0) return 2 * acos(0); else if (fabs(a.y) < EPS && a.x > 0) return 0.0; else if (a.y > 0) return acos(a.x / a.absv()); else return 2 * acos(0) + acos(-a.x / a.absv()); } string itos(int i) { ostringstream s; s << i; return s.str(); } int gcd(int v, int b) { if (v > b) return gcd(b, v); if (v == b) return b; if (b % v == 0) return v; return gcd(v, b % v); } double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; } double distans(double x1, double y1, double x2, double y2) { double rr = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); return sqrt(rr); } /*} int pr[100010]; //int inv[100010]; int beki(int wa,int rr){ if(rr==0) return 1ll; if(rr==1) return wa; if(rr%2==1) return (beki(wa,rr-1)*wa)%inf; int zx=beki(wa,rr/2); return (zx*zx)%inf; } void gya(){ pr[0]=1; for(int i=1;i<100010;i++){ pr[i]=(pr[i-1]*i)%inf; } for(int i=0;i<100010;i++) inv[i]=beki(pr[i],inf-2); } */ //----------------kokomade tenpure------------ string s[50], t[50]; signed main() { int n, m, r; /* double d; cin>>d; double ans=0; for(int i=0;i<=10;i++){ if(i>d) break; double ii=i+0.0; ans=max(ans,sqrt(d*d-ii*ii)); } printf("%.10lf\n",ans); */ while (1) { cin >> r >> n; // int sumi[200]={0}; if (n == 0 && r == 0) return 0; int a[100] = {0}; for (int i = 0; i < n; i++) { int x2, x1, h; cin >> x1 >> x2 >> h; for (int i = x1 + 50 + 1; i <= x2 + 50; i++) a[i] = max(a[i], h); } int aa[100] = {0}; for (int i = 50 - r - 2; i <= r + 50 + 2; i++) aa[i] = min(a[i], a[i + 1]); double ans = 1000000000; for (int i = 50 - r; i <= 50 + r; i++) { int d = i - 50; double w = (r + 0.0) - sqrt((r + 0.0) * (r + 0.0) - (d + 0.0) * (d + 0.0)); ans = min(ans, aa[i] + w); } printf("%.10lf\n", ans); } return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <math.h> #include <queue> #include <string> #include <vector> // #include <boost/multiprecision/cpp_int.hpp> #include <functional> // #define int long long #define inf 1000000007 #define pa pair<int, int> #define ll long long #define pal pair<double, pa> #define ppa pair<pa, int> #define ppap pair<int, pa> #define ssa pair<string, int> #define mp make_pair #define pb push_back #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) using namespace std; // priority_queue<int, vector<int>, greater<int> > que; class Point { public: double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(x * a, y * a); } Point operator/(double a) { return Point(x / a, y / a); } double absv() { return sqrt(norm()); } double norm() { return x * x + y * y; } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } }; typedef Point Vector; struct Segment { Point p1, p2; }; double hen(Vector a) { if (fabs(a.x) < EPS && a.y > 0) return acos(0); else if (fabs(a.x) < EPS && a.y < 0) return 3 * acos(0); else if (fabs(a.y) < EPS && a.x < 0) return 2 * acos(0); else if (fabs(a.y) < EPS && a.x > 0) return 0.0; else if (a.y > 0) return acos(a.x / a.absv()); else return 2 * acos(0) + acos(-a.x / a.absv()); } string itos(int i) { ostringstream s; s << i; return s.str(); } int gcd(int v, int b) { if (v > b) return gcd(b, v); if (v == b) return b; if (b % v == 0) return v; return gcd(v, b % v); } double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; } double distans(double x1, double y1, double x2, double y2) { double rr = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); return sqrt(rr); } /*} int pr[100010]; //int inv[100010]; int beki(int wa,int rr){ if(rr==0) return 1ll; if(rr==1) return wa; if(rr%2==1) return (beki(wa,rr-1)*wa)%inf; int zx=beki(wa,rr/2); return (zx*zx)%inf; } void gya(){ pr[0]=1; for(int i=1;i<100010;i++){ pr[i]=(pr[i-1]*i)%inf; } for(int i=0;i<100010;i++) inv[i]=beki(pr[i],inf-2); } */ //----------------kokomade tenpure------------ string s[50], t[50]; signed main() { int n, m, r; /* double d; cin>>d; double ans=0; for(int i=0;i<=10;i++){ if(i>d) break; double ii=i+0.0; ans=max(ans,sqrt(d*d-ii*ii)); } printf("%.10lf\n",ans); */ while (1) { cin >> r >> n; // int sumi[200]={0}; if (n == 0 && r == 0) return 0; int a[100] = {0}; for (int i = 0; i < n; i++) { int x2, x1, h; cin >> x1 >> x2 >> h; for (int i = x1 + 50 + 1; i <= x2 + 50; i++) a[i] = max(a[i], h); } int aa[100] = {0}; // for(int i=50-r;i<=50+r;i++) cout<<aa[i]<<endl; for (int i = 50 - r - 2; i <= r + 50 + 2; i++) aa[i] = min(a[i], a[i + 1]); // for(int i=50-r;i<=50+r;i++) cout<<i-50<<" "<<aa[i]<<endl; double ans = 1000000000; for (int i = 50 - r + 1; i <= 50 + r - 1; i++) { int d = i - 50; double w = (r + 0.0) - sqrt((r + 0.0) * (r + 0.0) - (d + 0.0) * (d + 0.0)); ans = min(ans, aa[i] + w); } printf("%.10lf\n", ans); } return 0; }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
913
#include <bits/stdc++.h> using namespace std; int hs[100][100]; int main() { int n; double r; while (cin >> r >> n, r + n) { memset(hs, 0, sizeof(hs)); for (int i = 0; i < n; i++) { int x1, x2, h; cin >> x1 >> x2 >> h; x1 += 20; x2 += 20; for (int i = x1; i < x2; i++) { for (int j = 0; j <= h; j++) hs[i][j] = 1; } } auto check = [&](double t) { for (int i = 0; i < r; i++) { double x = i; int y = sqrt(r * r - x * x) + t + 1 - r; // cout<<i<<" "<<t<<" "<<y<<endl; if (y < 0) continue; if (!hs[i + 20][y]) return false; if (!hs[-i + 20 - 1][y]) return false; } return true; }; double ub = 100, lb = 0, mid; for (int i = 0; i < 200; i++) { mid = (ub + lb) / 2; if (check(mid)) lb = mid; else ub = mid; } cout << fixed << setprecision(12) << lb << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int hs[100][100]; int main() { int n; double r; while (cin >> r >> n, r + n) { memset(hs, 0, sizeof(hs)); for (int i = 0; i < n; i++) { int x1, x2, h; cin >> x1 >> x2 >> h; x1 += 20; x2 += 20; for (int i = x1; i < x2; i++) { for (int j = 0; j <= h; j++) hs[i][j] = 1; } } auto check = [&](double t) { for (int i = 0; i < r; i++) { double x = i; int y = sqrt(r * r - x * x) + t + 1 - r; // cout<<i<<" "<<t<<" "<<y<<endl; if (y <= 0) continue; if (!hs[i + 20][y]) return false; if (!hs[-i + 20 - 1][y]) return false; } return true; }; double ub = 100, lb = 0, mid; for (int i = 0; i < 200; i++) { mid = (ub + lb) / 2; if (check(mid)) lb = mid; else ub = mid; } cout << fixed << setprecision(12) << lb << endl; } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19]]
1
302
#include <algorithm> #include <cmath> #include <cstdlib> #include <iostream> using namespace std; //#define int long long #define rep(i, n) for (int i = 0; i < (n); ++i) int main(void) { cin.tie(0); ios::sync_with_stdio(false); int x, y, s, a, b, ans; while (cin >> x >> y >> s, x | y | s) { ans = 0; for (int i = 1; i < s; ++i) { for (int j = 1; j < i; ++j) { a = i * (100.0 + x) / 100.0; b = j * (100.0 + x) / 100.0; if (a + b == s) { a = i * (100.0 + y) / 100.0; b = j * (100.0 + y) / 100.0; ans = max(ans, a + b); } } } cout << ans << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdlib> #include <iostream> using namespace std; //#define int long long #define rep(i, n) for (int i = 0; i < (n); ++i) int main(void) { cin.tie(0); ios::sync_with_stdio(false); int x, y, s, a, b, ans; while (cin >> x >> y >> s, x | y | s) { ans = 0; for (int i = 1; i <= s; ++i) { for (int j = 1; j <= i; ++j) { a = i * (100.0 + x) / 100.0; b = j * (100.0 + x) / 100.0; if (a + b == s) { a = i * (100.0 + y) / 100.0; b = j * (100.0 + y) / 100.0; ans = max(ans, a + b); } } } cout << ans << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 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]]
1
187
#include <bits/stdc++.h> using namespace std; main() { int x, y, s; while (cin >> x >> y >> s, x + y + s) { int mx = 0; for (int i = 1; i < s - 1; i++) { for (int j = i + 1; j < s; j++) { if (i * (100 + x) / 100 + j * (100 + x) / 100 == s) { mx = max(mx, i * (100 + y) / 100 + j * (100 + y) / 100); } } } cout << mx << endl; } }
#include <bits/stdc++.h> using namespace std; main() { int x, y, s; while (cin >> x >> y >> s, x + y + s) { int mx = 0; for (int i = 1; i < s - 1; i++) { for (int j = i; j < s; j++) { if (i * (100 + x) / 100 + j * (100 + x) / 100 == s) { mx = max(mx, i * (100 + y) / 100 + j * (100 + y) / 100); } } } cout << mx << endl; } }
[["-", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["-", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
136
#include <iostream> using namespace std; int main() { int x, y, s; int max = 0, ss; while (cin >> x >> y >> s && !(x == 0 && y == 0 && s == 0)) { for (int i = 1; i < s; i++) { for (int j = 1; j < s; j++) { if (i * (100 + x) / 100 + j * (100 + x) / 100 == s) { ss = i * (100 + y) / 100 + j * (100 + y) / 100; if (max < ss) max = ss; } } } cout << max << endl; } return 0; }
#include <iostream> using namespace std; int main() { int x, y, s; int max, ss; while (cin >> x >> y >> s && !(x == 0 && y == 0 && s == 0)) { max = 0; for (int i = 1; i < s; i++) { for (int j = 1; j < s; j++) { if (i * (100 + x) / 100 + j * (100 + x) / 100 == s) { ss = i * (100 + y) / 100 + j * (100 + y) / 100; if (max < ss) max = ss; } } } cout << max << endl; } return 0; }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 0, 32], ["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
152
#include <iostream> using namespace std; int func(int a, int x) { return a + (a * x / 100); } int main(void) { int x, y, s; while (cin >> x >> y >> s, x | y | s) { int ans = 0; for (int i = 1; i < 100; i++) { for (int j = 1; j < 100; j++) { if (func(i, x) + func(j, x) == s) { ans = max(ans, func(i, y) + func(j, y)); } } } cout << ans << endl; } return 0; }
#include <iostream> using namespace std; int func(int a, int x) { return a + (a * x / 100); } int main(void) { int x, y, s; while (cin >> x >> y >> s, x | y | s) { int ans = 0; for (int i = 1; i < 1000; i++) { for (int j = 1; j < 1000; j++) { if (func(i, x) + func(j, x) == s) { ans = max(ans, func(i, y) + func(j, y)); } } } cout << ans << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13]]
1
147
#include <iostream> using namespace std; int x, y, s; int main() { while (true) { cin >> x; cin >> y; cin >> s; if (x == 0 && y == 0 && s == 0) break; int bef, aft, max; max = 0; for (int i = 0; i <= s; ++i) { for (int j = 0; j <= s; ++j) { bef = i * (100 + x) / 100 + j * (100 + x) / 100; if (bef == s) { aft = i * (100 + y) / 100 + j * (100 + y) / 100; if (aft >= max) max = aft; } } } cout << max << endl; } }
#include <iostream> using namespace std; int x, y, s; int main() { while (true) { cin >> x; cin >> y; cin >> s; if (x == 0 && y == 0 && s == 0) break; int bef, aft, max; max = 0; for (int i = 1; i <= s; ++i) { for (int j = 1; j <= s; ++j) { bef = i * (100 + x) / 100 + j * (100 + x) / 100; if (bef == s) { aft = i * (100 + y) / 100 + j * (100 + y) / 100; if (aft >= max) { max = aft; // cout << "ijmax" << i << " " << j << " " << max << endl; } } } } cout << max << endl; } }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 0, 57, 64, 9, 0, 57, 64, 9, 0, 45], ["+", 0, 57, 64, 9, 0, 57, 64, 9, 0, 46]]
1
164
#include <algorithm> #include <cmath> #include <iostream> #include <queue> #include <string> #include <utility> #include <vector> #define INF 2147483647 #define pb push_back #define mp make_pair #define F first #define S second #define ll long long using namespace std; int main() { int x, y, s; while (cin >> x >> y >> s, x + y + s) { vector<pair<int, int>> pii; pii.clear(); for (int i = 1; i < s; i++) { for (int j = i + 1; j < s; j++) { // cout<<((i*(100+x))/(100))+((j*(100+x))/(100))<<endl; if ((i * (100 + x)) / (100) + (j * (100 + x)) / (100) == s) { pii.pb(mp(i, j)); // cout<<"!"<<endl; } } } int ans = 0; for (int i = 0; i < pii.size(); i++) { // cout<<pii[i].F<<" "<<pii[i].S<<endl; ans = max(ans, (pii[i].F * (100 + y)) / (100) + (pii[i].S * (100 + y)) / (100)); } cout << ans << endl; } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <queue> #include <string> #include <utility> #include <vector> #define INF 2147483647 #define pb push_back #define mp make_pair #define F first #define S second #define ll long long using namespace std; int main() { int x, y, s; while (cin >> x >> y >> s, x + y + s) { vector<pair<int, int>> pii; pii.clear(); for (int i = 1; i < s; i++) { for (int j = i; j < s; j++) { // cout<<((i*(100+x))/(100))+((j*(100+x))/(100))<<endl; if ((i * (100 + x)) / (100) + (j * (100 + x)) / (100) == s) { pii.pb(mp(i, j)); // cout<<"!"<<endl; } } } int ans = 0; for (int i = 0; i < pii.size(); i++) { // cout<<pii[i].F<<" "<<pii[i].S<<endl; ans = max(ans, (pii[i].F * (100 + y)) / (100) + (pii[i].S * (100 + y)) / (100)); } cout << ans << endl; } return 0; }
[["-", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["-", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
246
#include <bits/stdc++.h> // Shrotening #define fst first #define snd second #define pb push_back #define mp make_pair // Loop #define FOR(i, a, b) for (auto i = (a); i < (b); ++i) #define RFOR(i, a, b) for (auto i = (a); i >= (b); --i) #define REP(i, a) for (long i = 0; i < (a); ++i) #define RREP(i, a) for (long i = (a); i >= 0; --i) #define EACH(i, a) for (auto(i) = (a).begin(), _END = (a).end(); i != _END; ++i) #define REACH(i, a) \ for (auto(i) = (a).rbegin(), _END = (a).rend(); i != _END; ++i) // Algorithm #define ALL(a) (a).begin(), a.end() #define RALL(a) (a).rbegin(), a.rend() #define EXIST(a, x) ((a).find(x) != (a).end()) #define SORT(a) std::sort((a).begin(), (a).end()) #define UNIQUE(a) \ std::sort((a).begin(), a.end()), \ a.erase(std::unique((a).begin(), a.end()), a.end()); #define SUM(a) std::accumulate((a).begin(), (a).end(), 0); // Setting #define OPT \ std::cin.tie(0); \ std::ios::sync_with_stdio(false); // debug message bool debug = true; #define MSG(s) \ if (debug) { \ std::cout << s << std::endl; \ } #define DEBUG(x) \ if (debug) { \ std::cout << "debug(" << #x << "): " << x << std::endl; \ } // alias typedef long long LL; typedef std::vector<char> VC; typedef std::vector<int> VI; typedef std::vector<long> VL; typedef std::vector<long long> VLL; typedef std::vector<VC> VC2; typedef std::vector<VI> VI2; typedef std::vector<VL> VL2; typedef std::vector<VLL> VLL2; typedef std::pair<int, int> PII; inline int calc(int p, int x) { return p * (100 + x) / 100; } int x, y, s; int main() { while (std::cin >> x >> y >> s) { if (x == 0 && y == 0 && s == 0) continue; int ans = 0; FOR(i, 1, s) { FOR(j, 1, s) { if (calc(i, x) + calc(j, y) == s) { int i2 = calc(i, y); int j2 = calc(j, y); ans = std::max(i2 + j2, ans); } } } std::cout << ans << std::endl; } }
#include <bits/stdc++.h> // Shrotening #define fst first #define snd second #define pb push_back #define mp make_pair // Loop #define FOR(i, a, b) for (auto i = (a); i < (b); ++i) #define RFOR(i, a, b) for (auto i = (a); i >= (b); --i) #define REP(i, a) for (long i = 0; i < (a); ++i) #define RREP(i, a) for (long i = (a); i >= 0; --i) #define EACH(i, a) for (auto(i) = (a).begin(), _END = (a).end(); i != _END; ++i) #define REACH(i, a) \ for (auto(i) = (a).rbegin(), _END = (a).rend(); i != _END; ++i) // Algorithm #define ALL(a) (a).begin(), a.end() #define RALL(a) (a).rbegin(), a.rend() #define EXIST(a, x) ((a).find(x) != (a).end()) #define SORT(a) std::sort((a).begin(), (a).end()) #define UNIQUE(a) \ std::sort((a).begin(), a.end()), \ a.erase(std::unique((a).begin(), a.end()), a.end()); #define SUM(a) std::accumulate((a).begin(), (a).end(), 0); // Setting #define OPT \ std::cin.tie(0); \ std::ios::sync_with_stdio(false); // debug message bool debug = true; #define MSG(s) \ if (debug) { \ std::cout << s << std::endl; \ } #define DEBUG(x) \ if (debug) { \ std::cout << "debug(" << #x << "): " << x << std::endl; \ } // alias typedef long long LL; typedef std::vector<char> VC; typedef std::vector<int> VI; typedef std::vector<long> VL; typedef std::vector<long long> VLL; typedef std::vector<VC> VC2; typedef std::vector<VI> VI2; typedef std::vector<VL> VL2; typedef std::vector<VLL> VLL2; typedef std::pair<int, int> PII; inline int calc(int p, int x) { return p * (100 + x) / 100; } int x, y, s; int main() { while (std::cin >> x >> y >> s) { if (x == 0 && y == 0 && s == 0) continue; int ans = 0; FOR(i, 1, s) { FOR(j, 1, s) { if (calc(i, x) + calc(j, x) == s) { int i2 = calc(i, y); int j2 = calc(j, y); ans = std::max(i2 + j2, ans); } } } std::cout << ans << std::endl; } }
[["-", 51, 16, 31, 16, 12, 2, 3, 4, 0, 22], ["+", 51, 16, 31, 16, 12, 2, 3, 4, 0, 22]]
1
380
#include <algorithm> #include <iostream> using namespace std; int main() { while (1) { int x, y, z, ans = 0; cin >> x >> y >> z; if (!x && !y && !z) break; for (int i = 0; i < z; i++) { for (int j = 0; j < z; j++) { if (int(i * (100 + x) / 100) + int(j * (100 + x) / 100) == z) ans = max(ans, int(i * (100 + y) / 100) + int(j * (100 + y) / 100)); } } cout << ans << endl; } }
#include <algorithm> #include <iostream> using namespace std; int main() { while (1) { int x, y, z, ans = 0; cin >> x >> y >> z; if (!x && !y && !z) break; for (int i = 1; i < z; i++) { for (int j = 1; j < z; j++) { if (int(i * (100 + x) / 100) + int(j * (100 + x) / 100) == z) ans = max(ans, int(i * (100 + y) / 100) + int(j * (100 + y) / 100)); } } cout << ans << endl; } }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
153
#include <bits/stdc++.h> using namespace std; long getPrice(long orig, long r) { double d = double(orig) * (100 + r) / 100; return floor(d); } long getOrig(long pr, long r) { double d = double(pr) / (100 + r) * 100; if (getPrice(floor(d), r) == pr) { return floor(d); } if (getPrice(ceil(d), r) == pr) { return ceil(d); } return -1; } void solve(long x, long y, long s) { long ans = 0; for (int pr1 = 0; pr1 < s; pr1++) { long or1 = getOrig(pr1, x); long or2 = getOrig(s - pr1, x); if (or1 != -1 && or2 != -1) { ans = max(ans, getPrice(or1, y) + getPrice(or2, y)); } } cout << ans << endl; } int main() { long x, y, s; while (1) { cin >> x >> y >> s; if (x + y + s) { solve(x, y, s); } else { return 0; } } }
#include <bits/stdc++.h> using namespace std; long getPrice(long orig, long r) { double d = double(orig) * (100 + r) / 100; return floor(d); } long getOrig(long pr, long r) { double d = double(pr) / (100 + r) * 100; if (getPrice(floor(d), r) == pr) { return floor(d); } if (getPrice(ceil(d), r) == pr) { return ceil(d); } return -1; } void solve(long x, long y, long s) { long ans = 0; for (int pr1 = 1; pr1 < s; pr1++) { long or1 = getOrig(pr1, x); long or2 = getOrig(s - pr1, x); if (or1 != -1 && or2 != -1) { ans = max(ans, getPrice(or1, y) + getPrice(or2, y)); } } cout << ans << endl; } int main() { long x, y, s; while (1) { cin >> x >> y >> s; if (x + y + s) { solve(x, y, s); } else { return 0; } } }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
261
while true rateA,rateB,s = gets.split(" ").map(&:to_i) break if rateA ==0 && rateB == 0 && s == 0 a=[] $roop = 1 for i in 1..s/2 sa = i*(100+rateA)/100 (s/2..s).to_a.reverse.each do |j| sb = j*(100+rateA)/100 if sa + sb < s break end if sa + sb == s a.push([i,j]) break end end break if $roop == 0 end maximum = 0 for i in 0..a.length-1 b = [maximum, a[i][0]*(100+rateB)/100 + a[i][1]*(100+rateB)/100] maximum = b.max end puts(maximum) end
while true rateA,rateB,s = gets.split(" ").map(&:to_i) break if rateA ==0 && rateB == 0 && s == 0 a=[] $roop = 1 for i in 1..s/2 sa = i*(100+rateA)/100 (1..s-sa).to_a.reverse.each do |j| sb = j*(100+rateA)/100 if sa + sb < s break end if sa + sb == s a.push([i,j]) break end end break if $roop == 0 end maximum = 0 for i in 0..a.length-1 b = [maximum, a[i][0]*(100+rateB)/100 + a[i][1]*(100+rateB)/100] maximum = b.max end puts(maximum) end
[["-", 486, 652, 486, 739, 0, 475, 756, 738, 31, 22], ["-", 486, 652, 486, 739, 0, 475, 756, 738, 17, 85], ["-", 486, 652, 486, 739, 0, 475, 756, 738, 12, 612], ["+", 486, 652, 486, 652, 486, 739, 0, 475, 756, 612], ["+", 486, 652, 486, 739, 0, 475, 444, 738, 17, 33], ["+", 486, 652, 486, 739, 0, 475, 444, 738, 12, 22]]
4
183
def tax_exclude(price, tax) return nil if ((price + 1) * 100) % (100 + tax) == 0 possible_price = ((price + 1) / ((100 + tax) / 100.0)).to_i return nil if possible_price < ((price) / ((100 + tax) / 100.0)) possible_price end while true old_tax, new_tax, sum = gets.split(" ").map(&:to_i) break if [old_tax, new_tax, sum] == [0, 0, 0] max_price = 0 for i in 1..(sum / 2) do low_price = tax_exclude(i, old_tax) high_price = tax_exclude(sum - i, old_tax) next unless low_price && high_price new_tax_total = (low_price * ((100 + new_tax) / 100.0)).to_i + (high_price * ((100 + new_tax) / 100.0)).to_i max_price = new_tax_total if new_tax_total > max_price end puts max_price end
def tax_exclude(price, tax) return nil if ((price + 1) * 100) % (100 + tax) == 0 possible_price = ((price + 1) / ((100 + tax) / 100.0)).to_i return nil if possible_price < ((price) / ((100 + tax) / 100.0)) possible_price end while true old_tax, new_tax, sum = gets.split(" ").map(&:to_i) break if [old_tax, new_tax, sum] == [0, 0, 0] max_price = 0 for i in 1..(sum / 2) do low_price = tax_exclude(i, old_tax) high_price = tax_exclude(sum - i, old_tax) next unless low_price && high_price new_tax_total = (low_price * (100 + new_tax) / 100.0).to_i + (high_price * (100 + new_tax) / 100.0).to_i max_price = new_tax_total if new_tax_total > max_price end puts max_price end
[["-", 0, 738, 12, 739, 0, 738, 31, 739, 0, 24], ["-", 31, 652, 486, 739, 0, 738, 12, 739, 0, 25], ["-", 12, 652, 486, 739, 0, 738, 12, 739, 0, 24], ["-", 12, 652, 486, 739, 0, 738, 12, 739, 0, 25]]
4
190
import math def f(s, x): return int(math.floor(s * (100 + x) / 100.0)) while True: (x, y, s) = list(map(int, input().split())) if x + y + s == 0: break ans = 0 for i in range(1, s): for j in range(1, s - i): if f(i, x) + f(j, x) == s: ans = max(ans, f(i, y) + f(j, y)) print(ans)
import math def f(s, x): return int(math.floor(s * (100 + x) / 100.0)) while True: (x, y, s) = list(map(int, input().split())) if x + y + s == 0: break ans = 0 for i in range(1, s): for j in range(1, s - i + 1): if f(i, x) + f(j, x) == s: ans = max(ans, f(i, y) + f(j, y)) print(ans)
[["+", 0, 7, 12, 652, 3, 4, 0, 657, 17, 72], ["+", 0, 7, 12, 652, 3, 4, 0, 657, 12, 612]]
5
130
#!/usr/bin/env python # -*- coding: utf-8 -*- while True: x,y,s = map(int,input().split()) if (x,y,s) == (0,0,0): break ans = 0 for i in range(1,s-1): for j in range(1,s-1): if i*(100+x)//100 + j*(100+x)//100== s: ans = max(ans,i*(100+y)//100 + j*(100+y)//100) print(ans)
#!/usr/bin/env python # -*- coding: utf-8 -*- while True: x,y,s = map(int,input().split()) if (x,y,s) == (0,0,0): break ans = 0 for i in range(1,s-1): for j in range(1,s-i + 1): if i*(100+x)//100 + j*(100+x)//100== s: ans = max(ans,i*(100+y)//100 + j*(100+y)//100) print(ans)
[["+", 12, 652, 3, 4, 0, 657, 31, 657, 12, 22], ["+", 0, 7, 12, 652, 3, 4, 0, 657, 17, 72]]
5
121
#!/usr/bin/env python # -*- coding: utf-8 -*- while True: x,y,s = map(int,input().split()) if (x,y,s) == (0,0,0): break ans = 0 for i in range(1,s-1): for j in range(1,s-1): if i*(100+x)//100 + j*(100+x)//100== s: ans = max(ans,i*(100+y)//100 + j*(100+y)//100) print(ans)
#!/usr/bin/env python # -*- coding: utf-8 -*- while True: x,y,s = map(int,input().split()) if (x,y,s) == (0,0,0): break ans = 0 for i in range(1,s//2 + 1): for j in range(1,s-i + 1): if i*(100+x)//100 + j*(100+x)//100== s: ans = max(ans,i*(100+y)//100 + j*(100+y)//100) print(ans)
[["-", 0, 7, 12, 652, 3, 4, 0, 657, 17, 33], ["+", 12, 652, 3, 4, 0, 657, 31, 657, 17, 676], ["+", 12, 652, 3, 4, 0, 657, 31, 657, 12, 612], ["+", 0, 7, 12, 652, 3, 4, 0, 657, 17, 72], ["+", 12, 652, 3, 4, 0, 657, 31, 657, 12, 22]]
5
121
import java.util.Arrays; import java.util.Scanner; public class Main { Scanner sc; int ex(int src, int ratio) { return (src * (100 + ratio)) / 100; } void run() { for (;;) { int h = ni(); if (h == 0) { break; } int[][] f = new int[h][5]; for (int i = 0; i < h; ++i) { for (int j = 0; j < 5; ++j) { f[i][j] = ni(); } } int score = 0; for (;;) { int cnt = 0; for (int i = 0; i < h; ++i) { for (int j = 0; j < 3; ++j) { int v = f[i][j]; boolean flag = true; for (int k = 0; k < 3; ++k) { flag &= f[i][j + k] == v; } if (flag) { for (int k = j; k < 5; ++k) { if (f[i][k] != v) { break; } cnt += v; for (int l = i; l > 0; --l) { f[l][k] = f[l - 1][k]; } f[0][k] = 0; } } } } if (cnt == 0) { break; } score += cnt; } System.out.println(score); } } Main() { sc = new Scanner(System.in); } int ni() { return sc.nextInt(); } public static void main(String[] args) { new Main().run(); } void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } }
import java.util.Arrays; import java.util.Scanner; public class Main { Scanner sc; int ex(int src, int ratio) { return (src * (100 + ratio)) / 100; } void run() { for (;;) { int h = ni(); if (h == 0) { break; } int[][] f = new int[h][5]; for (int i = 0; i < h; ++i) { for (int j = 0; j < 5; ++j) { f[i][j] = ni(); } } int score = 0; for (;;) { int cnt = 0; for (int i = 0; i < h; ++i) { for (int j = 0; j < 3; ++j) { int v = f[i][j]; boolean flag = true; for (int k = 0; k < 3; ++k) { flag &= f[i][j + k] == v; } if (flag) { for (int k = j; k < 5; ++k) { if (f[i][k] != v) { break; } cnt += v; for (int l = i; l > 0; --l) { f[l][k] = f[l - 1][k]; } f[0][k] = 0; } break; } } } if (cnt == 0) { break; } score += cnt; } System.out.println(score); } } Main() { sc = new Scanner(System.in); } int ni() { return sc.nextInt(); } public static void main(String[] args) { new Main().run(); } void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } }
[["+", 8, 196, 0, 57, 64, 196, 0, 93, 0, 94], ["+", 8, 196, 0, 57, 64, 196, 0, 93, 0, 35]]
3
412
#include <stdio.h> int main() { int h, i, j, c, s, f, k; while (scanf("%d", &h), h) { int d[20][6] = {0}; for (i = s = 0; i < h; i++) { for (j = 0; j < 5; j++) scanf("%d", &d[h - i - 1][j]); } /*for(i=0;i<20;i++){ for(j=0;j<5;j++){ printf("%d ",d[i][j]); } printf("\n"); }printf("\n");//*/ f = 1; while (f) { for (i = f = 0; i < h; i++) { c = 1; for (j = 0; j < 5; j++) { if (d[i][j] == d[i][j + 1] && d[i][j]) c++; else { if (c > 2) { f = 1; for (k = 0; k < c; k++) { s += d[i][j - k]; d[i][j - k] = 0; } } c = 1; } // printf("%d ",c); } // printf("\n"); } // printf("\n"); for (i = 0; i < 5; i++) { for (j = c = 0; j < 10; j++) { d[j - c][i] = d[j][i]; if (d[j][i] == 0 && j < h) c++; } } /*for(i=0;i<20;i++){ for(j=0;j<5;j++)printf("%d ",d[i][j]); printf("\n"); }printf("\n");//*/ } printf("%d\n", s); } return 0; }
#include <stdio.h> int main() { int h, i, j, c, s, f, k; while (scanf("%d", &h), h) { int d[20][6] = {0}; for (i = s = 0; i < h; i++) { for (j = 0; j < 5; j++) scanf("%d", &d[h - i - 1][j]); } /*for(i=0;i<20;i++){ for(j=0;j<5;j++){ printf("%d ",d[i][j]); } printf("\n"); }printf("\n");//*/ f = 1; while (f) { for (i = f = 0; i < h; i++) { c = 1; for (j = 0; j < 5; j++) { if (d[i][j] == d[i][j + 1] && d[i][j]) c++; else { if (c > 2) { f = 1; for (k = 0; k < c; k++) { s += d[i][j - k]; d[i][j - k] = 0; } } c = 1; } // printf("%d ",c); } // printf("\n"); } // printf("\n"); for (i = 0; i < 5; i++) { for (j = c = 0; j - c < h; j++) { d[j - c][i] = d[j][i]; if (d[j][i] == 0 && j < h) c++; } } /*for(i=0;i<20;i++){ for(j=0;j<5;j++)printf("%d ",d[i][j]); printf("\n"); }printf("\n");//*/ } printf("%d\n", s); } return 0; }
[["+", 8, 9, 0, 7, 15, 16, 31, 16, 17, 33], ["+", 8, 9, 0, 7, 15, 16, 31, 16, 12, 22], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22]]
0
324
#include <stdio.h> int main(void) { int H, stone[10][5] = {0}; int a, i, j, k, score = 0; while (1) { scanf("%d", &H); if (H == 0) { break; } score = 0; for (i = 0; i < H; i++) { scanf("%d %d %d %d %d", &stone[i][0], &stone[i][1], &stone[i][2], &stone[i][3], &stone[i][4]); } for (a = 0; a < 100; a++) { for (i = 0; i < H; i++) { if (stone[i][0] == stone[i][1] && stone[i][1] == stone[i][2] && stone[i][2] == stone[i][3] && stone[i][3] == stone[i][4]) { score += stone[i][2] * 5; for (j = 0; j < 5; j++) { stone[i][j] = 0; } } } for (i = 0; i < H; i++) { for (j = 0; j < 2; j++) { if (stone[i][j] == stone[i][1 + j] && stone[i][1 + j] == stone[i][2 + j] && stone[i][2 + j] == stone[i][3 + j]) { score += stone[i][2] * 4; for (k = j; k < j + 4; k++) { stone[i][k] = 0; } } } } for (i = 0; i < H; i++) { for (j = 0; j < 3; j++) { if (stone[i][0 + j] == stone[i][1 + j] && stone[i][1 + j] == stone[i][2 + j]) { score += stone[i][2] * 3; for (k = j; k < j + 3; k++) { stone[i][k] = 0; } } } } for (k = 0; k < 100; k++) { for (i = 1; i < H; i++) { for (j = 0; j < 5; j++) { if (stone[i][j] == 0 && stone[i - 1][j] != 0) { stone[i][j] = stone[i - 1][j]; stone[i - 1][j] = 0; } } } } } printf("%d", score); } return 0; }
#include <stdio.h> int main(void) { int H, stone[10][5] = {0}; int a, i, j, k, score = 0; while (1) { scanf("%d", &H); if (H == 0) { break; } score = 0; for (i = 0; i < H; i++) { scanf("%d %d %d %d %d", &stone[i][0], &stone[i][1], &stone[i][2], &stone[i][3], &stone[i][4]); } for (a = 0; a < 50; a++) { for (i = 0; i < H; i++) { if (stone[i][0] == stone[i][1] && stone[i][1] == stone[i][2] && stone[i][2] == stone[i][3] && stone[i][3] == stone[i][4]) { score += stone[i][2] * 5; for (j = 0; j < 5; j++) { stone[i][j] = 0; } } } for (i = 0; i < H; i++) { for (j = 0; j < 2; j++) { if (stone[i][j] == stone[i][1 + j] && stone[i][1 + j] == stone[i][2 + j] && stone[i][2 + j] == stone[i][3 + j]) { score += stone[i][2] * 4; for (k = j; k < j + 4; k++) { stone[i][k] = 0; } } } } for (i = 0; i < H; i++) { for (j = 0; j < 3; j++) { if (stone[i][0 + j] == stone[i][1 + j] && stone[i][1 + j] == stone[i][2 + j]) { score += stone[i][2] * 3; for (k = j; k < j + 3; k++) { stone[i][k] = 0; } } } } for (k = 0; k < 10; k++) { for (i = 1; i < H; i++) { for (j = 0; j < 5; j++) { if (stone[i][j] == 0 && stone[i - 1][j] != 0) { stone[i][j] = stone[i - 1][j]; stone[i - 1][j] = 0; } } } } } printf("%d\n", score); } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
0
626
#include <stdio.h> #define NUM 10 void drop(int puz[][5], int i, int j) { int k; for (k = i; k > 0; k--) puz[k][j] = puz[k - 1][j]; puz[0][j] = 0; } int main(void) { int score = 0; int tscore = 0; int count = 0; int h, i, j; int puz[NUM][5] = {0}; replay: while (1) { scanf("%d", &h); if (h == 0) break; for (i = 0; i < h; i++) { for (j = 0; j < 5; j++) { scanf("%d", &puz[i][j]); } } while (1) { for (i = 0; i < h; i++) { for (j = 0; j < 5; j++) { if (puz[i][j] == puz[i][j + 1]) count++; else if (count == 2) { score = score + (3 * puz[i][j]); puz[i][j] = 0; puz[i][j - 1] = 0; puz[i][j - 2] = 0; count = 0; } else if (count == 3) { score = score + (4 * puz[i][j]); puz[i][j] = 0; puz[i][j - 1] = 0; puz[i][j - 2] = 0; puz[i][j - 3] = 0; count = 0; } else count = 0; } if (count == 2) { score = score + (3 * puz[i][j]); puz[i][j] = 0; puz[i][j - 1] = 0; puz[i][j - 2] = 0; count = 0; } else if (count == 3) { score = score + (4 * puz[i][j]); puz[i][j] = 0; puz[i][j - 1] = 0; puz[i][j - 2] = 0; puz[i][j - 3] = 0; count = 0; } else if (count == 4) { score = score + (5 * puz[i][j]); puz[i][j] = 0; puz[i][j - 1] = 0; puz[i][j - 2] = 0; puz[i][j - 3] = 0; puz[i][j - 4] = 0; count = 0; } else count = 0; } for (i = 0; i < h; i++) { for (j = 0; j < 5; j++) { if (puz[i][j] == 0) drop(puz, i, j); } } if (score == 0) { printf("%d\n", tscore); tscore = 0; goto replay; } tscore = tscore + score; score = 0; } } return 0; }
#include <stdio.h> #define NUM 10 void drop(int puz[][5], int i, int j) { int k; for (k = i; k > 0; k--) puz[k][j] = puz[k - 1][j]; puz[0][j] = 0; } int main(void) { int score = 0; int tscore = 0; int count = 0; int h, i, j; int puz[NUM][5] = {0}; replay: while (1) { scanf("%d", &h); if (h == 0) break; for (i = 0; i < h; i++) { for (j = 0; j < 5; j++) { scanf("%d", &puz[i][j]); } } while (1) { for (i = 0; i < h; i++) { for (j = 0; j < 4; j++) { if (puz[i][j] == puz[i][j + 1]) count++; else if (count == 2) { score = score + (3 * puz[i][j]); puz[i][j] = 0; puz[i][j - 1] = 0; puz[i][j - 2] = 0; count = 0; } else if (count == 3) { score = score + (4 * puz[i][j]); puz[i][j] = 0; puz[i][j - 1] = 0; puz[i][j - 2] = 0; puz[i][j - 3] = 0; count = 0; } else count = 0; } if (count == 2) { score = score + (3 * puz[i][j]); puz[i][j] = 0; puz[i][j - 1] = 0; puz[i][j - 2] = 0; count = 0; } else if (count == 3) { score = score + (4 * puz[i][j]); puz[i][j] = 0; puz[i][j - 1] = 0; puz[i][j - 2] = 0; puz[i][j - 3] = 0; count = 0; } else if (count == 4) { score = score + (5 * puz[i][j]); puz[i][j] = 0; puz[i][j - 1] = 0; puz[i][j - 2] = 0; puz[i][j - 3] = 0; puz[i][j - 4] = 0; count = 0; } else count = 0; } for (i = 0; i < h; i++) { for (j = 0; j < 5; j++) { if (puz[i][j] == 0) drop(puz, i, j); } } if (score == 0) { printf("%d\n", tscore); tscore = 0; goto replay; } tscore = tscore + score; score = 0; } } return 0; }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13]]
0
702
#include <stdio.h> int h; int memo[11][5]; int bf, ans; int main() { int i, j, k; while (1) { scanf("%d", &h); if (h == 0) break; bf = -1; ans = 0; for (i = 0; i < h; i++) for (j = 0; j < 5; j++) scanf("%d", &memo[i][j]); while (ans != bf) { bf = ans; //消す作業 for (i = 0; i < h; i++) for (j = 0; j < 3; j++) { int now = j; while (memo[i][now] == memo[i][j] && j < 5) j++; if (j - now >= 3 && memo[i][now] != -1) { ans += (j - now) * memo[i][now]; for (k = now; k < j; k++) memo[i][k] = -1; } j--; } //落とす作業 for (j = 0; j < 5; j++) for (i = h - 1; i >= 0; i--) { int now = i; while (memo[i][j] == -1 && i >= 0) i--; if (i >= 0 && now != i) { memo[now][j] = memo[i][j]; memo[i][j] = -1; i++; } } /*for(i=0;i<h;i++){ for(j=0;j<5;j++)printf("%d ",memo[i][j]); printf("\n"); } printf("\n");*/ } printf("%d\n", ans); } return 0; }
#include <stdio.h> int h; int memo[11][5]; int bf, ans; int main() { int i, j, k; while (1) { scanf("%d", &h); if (h == 0) break; bf = -1; ans = 0; for (i = 0; i < h; i++) for (j = 0; j < 5; j++) scanf("%d", &memo[i][j]); while (ans != bf) { bf = ans; //消す作業 for (i = 0; i < h; i++) for (j = 0; j < 3; j++) { int now = j; while (memo[i][now] == memo[i][j] && j < 5) j++; if (j - now >= 3 && memo[i][now] != -1) { ans += (j - now) * memo[i][now]; for (k = now; k < j; k++) memo[i][k] = -1; } j--; } //落とす作業 for (j = 0; j < 5; j++) for (i = h - 1; i >= 0; i--) { int now = i; while (memo[i][j] == -1 && i >= 0) i--; if (i >= 0 && now != i) { memo[now][j] = memo[i][j]; memo[i][j] = -1; i = now; } } /*for(i=0;i<h;i++){ for(j=0;j<5;j++)printf("%d ",memo[i][j]); printf("\n"); } printf("\n");*/ } printf("%d\n", ans); } return 0; }
[["-", 0, 57, 64, 9, 0, 1, 0, 27, 17, 29], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 12, 22]]
0
349
// Name: Chain Disappearance Puzzle // Level: 2 // Category: シミュレーション // Note: /** * 素直にシミュレーションすればよい。 * 消える石にマークする処理と、消えたところに石を詰める処理は別々に行わないといけないことに注意する。 * * オーダーは O(H^2)。 */ #include <algorithm> #include <iostream> #include <vector> using namespace std; void mark(vector<vector<int>> &field, int r, int c, int cnt) { for (int cc = c - cnt + 1; cc <= c; ++cc) { field[r][cc] = 0; } } int sim(vector<vector<int>> &field) { const int H = field.size(); int score = 0; for (int r = 0; r < H; ++r) { int cnt = 0; int prev = -1; for (int c = 0; c < 5; ++c) { if (field[r][c] == prev) { ++cnt; } else { if (cnt >= 3 && prev != 0) { score += cnt; mark(field, r, c - 1, cnt); } cnt = 1; } prev = field[r][c]; } if (cnt >= 3 && prev != 0) { score += cnt; mark(field, r, 4, cnt); } } if (score > 0) { for (int r = H - 1; r >= 0; --r) { for (int c = 0; c < 5; ++c) { if (field[r][c] == 0) { for (int rr = r - 1; rr >= 0; --rr) { if (field[rr][c] != 0) { field[r][c] = field[rr][c]; field[rr][c] = 0; break; } } } } } } return score; } bool solve() { int H; if (!(cin >> H)) return false; if (!H) return false; vector<vector<int>> field(H); for (int i = 0; i < H; ++i) { for (int j = 0; j < 5; ++j) { int v; cin >> v; field[i].push_back(v); } } int ans = 0; while (true) { const int score = sim(field); if (score == 0) break; ans += score; } cout << ans << endl; return true; } int main() { cin.tie(0); ios::sync_with_stdio(0); while (solve()) ; return 0; }
// Name: Chain Disappearance Puzzle // Level: 2 // Category: シミュレーション // Note: /** * 素直にシミュレーションすればよい。 * 消える石にマークする処理と、消えたところに石を詰める処理は別々に行わないといけないことに注意する。 * * オーダーは O(H^2)。 */ #include <algorithm> #include <iostream> #include <vector> using namespace std; void mark(vector<vector<int>> &field, int r, int c, int cnt) { for (int cc = c - cnt + 1; cc <= c; ++cc) { field[r][cc] = 0; } } int sim(vector<vector<int>> &field) { const int H = field.size(); int score = 0; for (int r = 0; r < H; ++r) { int cnt = 0; int prev = -1; for (int c = 0; c < 5; ++c) { if (field[r][c] == prev) { ++cnt; } else { if (cnt >= 3 && prev != 0) { score += cnt * prev; mark(field, r, c - 1, cnt); } cnt = 1; } prev = field[r][c]; } if (cnt >= 3 && prev != 0) { score += cnt * prev; mark(field, r, 4, cnt); } } if (score > 0) { for (int r = H - 1; r >= 0; --r) { for (int c = 0; c < 5; ++c) { if (field[r][c] == 0) { for (int rr = r - 1; rr >= 0; --rr) { if (field[rr][c] != 0) { field[r][c] = field[rr][c]; field[rr][c] = 0; break; } } } } } } return score; } bool solve() { int H; if (!(cin >> H)) return false; if (!H) return false; vector<vector<int>> field(H); for (int i = 0; i < H; ++i) { for (int j = 0; j < 5; ++j) { int v; cin >> v; field[i].push_back(v); } } int ans = 0; while (true) { const int score = sim(field); if (score == 0) break; ans += score; } cout << ans << endl; return true; } int main() { cin.tie(0); ios::sync_with_stdio(0); while (solve()) ; return 0; }
[["+", 64, 9, 0, 1, 0, 11, 12, 16, 17, 48], ["+", 64, 9, 0, 1, 0, 11, 12, 16, 12, 22]]
1
511
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; #define loop(i, a, b) for (int i = (a); i < ull(b); ++i) #define rep(i, n) loop(i, 0, n) #define all(a) (a).begin(), (a).end() const double eps = 1e-10; const double pi = acos(-1.0); const double inf = (int)1e8; int main() { int n; while (cin >> n, n) { vector<vector<int>> v(n); for (int i = 0; i < n; i++) { for (int j = 0; j < 5; j++) { int m; cin >> m; v[i].push_back(m); } } int ret = 0; while (true) { bool p = false; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { if (v[i][j] == -1) continue; int count = 0; while (v[i][j] == v[i][j + count] && j + count < 5) count++; if (3 <= count) { p = true; ret += v[i][j] * count; rep(k, 3) v[i][j + k] = -1; } } } if (!p) break; else { for (int i = n - 1; 1 <= i; i--) { for (int j = 0; j < 5; j++) { if (v[i][j] == -1) { for (int k = 0; 0 <= i - k; k++) if (v[i - k][j] != -1) { v[i][j] = v[i - k][j]; v[i - k][j] = -1; break; } } } } } } cout << ret << endl; } }
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; #define loop(i, a, b) for (int i = (a); i < ull(b); ++i) #define rep(i, n) loop(i, 0, n) #define all(a) (a).begin(), (a).end() const double eps = 1e-10; const double pi = acos(-1.0); const double inf = (int)1e8; int main() { int n; while (cin >> n, n) { vector<vector<int>> v(n); for (int i = 0; i < n; i++) { for (int j = 0; j < 5; j++) { int m; cin >> m; v[i].push_back(m); } } int ret = 0; while (true) { bool p = false; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { if (v[i][j] == -1) continue; int count = 0; while (v[i][j] == v[i][j + count] && j + count < 5) count++; if (3 <= count) { p = true; ret += v[i][j] * count; rep(k, count) v[i][j + k] = -1; } } } if (!p) break; else { for (int i = n - 1; 1 <= i; i--) { for (int j = 0; j < 5; j++) { if (v[i][j] == -1) { for (int k = 0; 0 <= i - k; k++) if (v[i - k][j] != -1) { v[i][j] = v[i - k][j]; v[i - k][j] = -1; break; } } } } } } cout << ret << endl; } }
[["-", 31, 69, 28, 69, 28, 2, 3, 4, 0, 13], ["+", 31, 69, 28, 69, 28, 2, 3, 4, 0, 22]]
1
406
//連鎖消滅パズル #include <iostream> using namespace std; #define ISEQUAL(i, a, b, c) \ Data[a][i] == Data[b][i] && Data[b][i] == Data[c][i] && Data[a][i] != 0 int main() { int RowNum = 0; //行数 int *Data[5]; //数値データ[列][行] int Points = 0; //ポイント bool IsComprete = false; //ルーチン終了判定用 bool Checker[5] = {false}; //行ごと消去判定用 bool IsFall = true; //落下判定 int FirstZeroPos = 0; //落下判定時ゼロ位置保持用 while (1) { cin >> RowNum; if (RowNum == 0) break; //データ代入 for (int i = 0; i < 5; ++i) { Data[i] = new int[RowNum]; } for (int i = 0; i < RowNum; ++i) { for (int j = 0; j < 5; ++j) { cin >> Data[j][i]; } } Points = 0; //ゲームルーチン while (!IsComprete) { IsComprete = true; for (int i = 0; i < RowNum; ++i) { //上から行ごとに for (int j = 0; j < 5; ++j) { //判定用変数初期化 Checker[j] = false; } for (int j = 0; j < 3; ++j) { //消滅判定 if (ISEQUAL(i, j, j + 1, j + 2)) { Checker[j] = true; Checker[j + 1] = true; Checker[j + 2] = true; IsComprete = false; //消滅判定時未終了と判断 } } for (int j = 0; j < 5; ++j) { //消滅処理 if (Checker[j]) { Points += Data[j][i]; Data[j][i] = 0; } } } //落下処理 for (int j = 0; j < 5; ++j) { //列ごとに IsFall = true; while (IsFall) { FirstZeroPos = -1; IsFall = false; for (int i = RowNum - 1; i >= 0; --i) { //下から if (FirstZeroPos == -1) { if (Data[j][i] == 0) FirstZeroPos = i; } else { if (Data[j][i] != 0) { Data[j][FirstZeroPos] = Data[j][i]; Data[j][i] = 0; IsFall = true; break; } } } } } } cout << Points << endl; for (int i = 0; i < 5; ++i) { delete[] Data[i]; } } return 0; }
#include <iostream> using namespace std; #define ISEQUAL(i, a, b, c) \ Data[a][i] == Data[b][i] && Data[b][i] == Data[c][i] && Data[a][i] != 0 int main() { int RowNum = 0; //行数 int *Data[5]; //数値データ[列][行] int Points = 0; //ポイント bool IsComprete = false; //ルーチン終了判定用 bool Checker[5] = {false}; //行ごと消去判定用 bool IsFall = true; //落下判定 int FirstZeroPos = 0; //落下判定時ゼロ位置保持用 while (1) { cin >> RowNum; if (RowNum == 0) break; //データ代入 for (int i = 0; i < 5; ++i) { Data[i] = new int[RowNum]; } for (int i = 0; i < RowNum; ++i) { for (int j = 0; j < 5; ++j) { cin >> Data[j][i]; } } Points = 0; IsComprete = false; //ゲームルーチン while (!IsComprete) { IsComprete = true; for (int i = 0; i < RowNum; ++i) { //上から行ごとに for (int j = 0; j < 5; ++j) { //判定用変数初期化 Checker[j] = false; } for (int j = 0; j < 3; ++j) { //消滅判定 if (ISEQUAL(i, j, j + 1, j + 2)) { Checker[j] = true; Checker[j + 1] = true; Checker[j + 2] = true; IsComprete = false; //消滅判定時未終了と判断 } } for (int j = 0; j < 5; ++j) { //消滅処理 if (Checker[j]) { Points += Data[j][i]; Data[j][i] = 0; } } } //落下処理 for (int j = 0; j < 5; ++j) { //列ごとに IsFall = true; while (IsFall) { FirstZeroPos = -1; IsFall = false; for (int i = RowNum - 1; i >= 0; --i) { //下から if (FirstZeroPos == -1) { if (Data[j][i] == 0) FirstZeroPos = i; } else { if (Data[j][i] != 0) { Data[j][FirstZeroPos] = Data[j][i]; Data[j][i] = 0; IsFall = true; break; } } } } } } cout << Points << endl; for (int i = 0; i < 5; ++i) { delete[] Data[i]; } } return 0; }
[["+", 0, 52, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 147], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
493
#include <bits/stdc++.h> using namespace std; // void pfield(vector<vector<int> >, int); int main() { int cols; while (cin >> cols, cols) { vector<vector<int>> field(5, vector<int>()); // [col][line] for (int il = 0; il < cols; ++il) { for (int ic = 0; ic < 5; ++ic) { int num; cin >> num; field[ic].push_back(num); } } for (int ic = 0; ic < 5; ++ic) { reverse(field[ic].begin(), field[ic].end()); } int score = 0; int lastScore = 0; do { lastScore = score; // Top down scan for (int il = cols; il >= 0; --il) { int rowCnt = 0; int lastNum = -1; for (int ic = 0; ic < 5; ++ic) { if (field[ic].size() <= il) { rowCnt = 0; continue; } (field[ic][il] == lastNum) ? ++rowCnt : (rowCnt = 0); int eraseNum = lastNum; lastNum = field[ic][il]; if (rowCnt == 2) { for (int dic = -2; dic <= 0; ++dic) { field[ic + dic].erase(field[ic + dic].begin() + il); score += eraseNum; } } else if (rowCnt > 2) { field[ic].erase(field[ic].begin() + il); score += eraseNum; } } } } while (score != lastScore); cout << score << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; // void pfield(vector<vector<int> >, int); int main() { int cols; while (cin >> cols, cols) { vector<vector<int>> field(5, vector<int>()); // [col][line] for (int il = 0; il < cols; ++il) { for (int ic = 0; ic < 5; ++ic) { int num; cin >> num; field[ic].push_back(num); } } for (int ic = 0; ic < 5; ++ic) { reverse(field[ic].begin(), field[ic].end()); } int score = 0; int lastScore = 0; do { lastScore = score; // Top down scan for (int il = cols; il >= 0; --il) { int rowCnt = 0; int lastNum = -1; for (int ic = 0; ic < 5; ++ic) { if (field[ic].size() <= il) { rowCnt = 0; lastNum = -1; continue; } (field[ic][il] == lastNum) ? ++rowCnt : (rowCnt = 0); int eraseNum = lastNum; lastNum = field[ic][il]; if (rowCnt == 2) { for (int dic = -2; dic <= 0; ++dic) { field[ic + dic].erase(field[ic + dic].begin() + il); score += eraseNum; } } else if (rowCnt > 2) { field[ic].erase(field[ic].begin() + il); score += eraseNum; } } // pfield(field, cols); } } while (score != lastScore); cout << score << endl; } return 0; }
[["+", 0, 57, 64, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35]]
1
348
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define mp make_pair #define pb push_back #define fi first #define se second #define INF 1000000005 #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long int ll; typedef pair<int, int> PI; int n; int m[11][5]; //??????????????? int erase() { int ret = 0; rep(i, n) { int now = 0; int left = 0; while (now < 5) { while (m[i][left] == m[i][now]) now++; if (now - left < 3) { left = now; } else { while (left < now) { ret += m[i][left]; m[i][left] = 0; left++; } } } } return ret; } void down() { rep(j, 5) { int down = 0; int now = 0; while (now < n) { while (now < n && !m[now][j]) now++; if (now == n) { break; } m[down][j] = m[now][j]; if (down < now) { m[now][j] = 0; } down++; now++; } } return; } void show() { rep(i, n) { rep(j, 5) { cout << m[n - 1 - i][j] << " "; } cout << endl; } cout << endl; } int main() { while (1) { cin >> n; if (!n) break; rep(i, 11) { rep(j, 5) m[i][j] = 0; } rep(i, n) { rep(j, 5) { cin >> m[n - 1 - i][j]; } } int ans = 0; int temp; while (1) { temp = erase(); // show(); ans += temp; if (!temp) { break; } down(); // show(); } cout << ans << endl; } }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define mp make_pair #define pb push_back #define fi first #define se second #define INF 1000000005 #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long int ll; typedef pair<int, int> PI; int n; int m[12][5]; //??????????????? int erase() { int ret = 0; rep(i, n) { int now = 0; int left = 0; while (now < 5) { while (now < 5 && m[i][left] == m[i][now]) now++; if (now - left < 3) { left = now; } else { while (left < now) { ret += m[i][left]; m[i][left] = 0; left++; } } } } return ret; } void down() { rep(j, 5) { int down = 0; int now = 0; while (now < n) { while (now < n && !m[now][j]) now++; if (now == n) { break; } m[down][j] = m[now][j]; if (down < now) { m[now][j] = 0; } down++; now++; } } return; } void show() { rep(i, n) { rep(j, 5) { cout << m[n - 1 - i][j] << " "; } cout << endl; } cout << endl; } int main() { while (1) { cin >> n; if (!n) break; rep(i, 11) { rep(j, 5) m[i][j] = 0; } rep(i, n) { rep(j, 5) { cin >> m[n - 1 - i][j]; } } int ans = 0; int temp; while (1) { temp = erase(); // show(); ans += temp; if (!temp) { break; } down(); // show(); } cout << ans << endl; } }
[["-", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["+", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["+", 0, 52, 15, 339, 51, 16, 31, 16, 31, 22], ["+", 0, 52, 15, 339, 51, 16, 31, 16, 17, 18], ["+", 0, 52, 15, 339, 51, 16, 31, 16, 12, 13], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 17, 98]]
1
461
#include <algorithm> #include <iostream> using namespace std; int map[20][5]; int h, ans; void DFS() { bool move = false; for (int i = 0; i < h; i++) { int cnt = 0, set = 0; for (int j = 0; j < 5; j++) { if (map[i][j] != set) { if (cnt >= 3 && set != 0) { for (int k = 0; k < cnt; k++) { ans += map[i][j - k - 1]; map[i][j - k - 1] = 0; } move = true; } cnt = 1; set = map[i][j]; } else cnt++; } if (cnt >= 3 && set != 0) { for (int k = 0; k < cnt; k++) { ans += map[i][4 - k]; map[i][4 - k] = 0; } move = true; } } for (int k = 0; k < 5; k++) { for (int j = 0; j < 5; j++) { for (int i = h - 1; i > 0; i--) { if (map[i - 1][j] == 0) { int tmp = map[i][j]; map[i][j] = 0; map[i - 1][j] = tmp; } } } } if (move) DFS(); } int main() { while (true) { cin >> h; if (h == 0) break; for (int i = 0; i < h; i++) { for (int j = 0; j < 5; j++) { cin >> map[h - i - 1][j]; } } ans = 0; DFS(); cout << ans << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; int map[20][5]; int h, ans; void DFS() { bool move = false; for (int i = 0; i < h; i++) { int cnt = 0, set = 0; for (int j = 0; j < 5; j++) { if (map[i][j] != set) { if (cnt >= 3 && set != 0) { for (int k = 0; k < cnt; k++) { ans += map[i][j - k - 1]; map[i][j - k - 1] = 0; } // cout<<"$"<<set<<" "<<cnt<<endl; move = true; } cnt = 1; set = map[i][j]; } else cnt++; } if (cnt >= 3 && set != 0) { for (int k = 0; k < cnt; k++) { ans += map[i][4 - k]; map[i][4 - k] = 0; } // cout<<"$"<<set<<" "<<cnt<<endl; move = true; } } for (int k = 0; k < h; k++) { for (int j = 0; j < 5; j++) { for (int i = h - 1; i > 0; i--) { if (map[i - 1][j] == 0) { int tmp = map[i][j]; map[i][j] = 0; map[i - 1][j] = tmp; } } } } if (move) DFS(); } int main() { while (true) { cin >> h; if (h == 0) break; for (int i = 0; i < h; i++) { for (int j = 0; j < 5; j++) { cin >> map[h - i - 1][j]; } } ans = 0; DFS(); cout << ans << endl; } return 0; }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 22]]
1
416
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, c; while (cin >> n, n != 0) { int a[n][5], sum = 0; ; for (i = 0; i < n; i++) for (j = 0; j < 5; j++) cin >> a[i][j]; while (1) { c = 0; for (i = 0; i < n; i++) { if (a[i][0] != 0 && a[i][0] == a[i][1] && a[i][1] == a[i][2] && a[i][2] == a[i][3] && a[i][3] == a[i][4]) { sum += a[i][0] * 5; c++; a[i][0] = 0; a[i][1] = 0; a[i][2] = 0; a[i][3] = 0; a[i][4] = 0; } if (a[i][0] != 0 && a[i][0] == a[i][1] && a[i][1] == a[i][2] && a[i][2] == a[i][3]) { sum += a[i][0] * 4; c++; a[i][0] = 0; a[i][1] = 0; a[i][2] = 0; a[i][3] = 0; } if (a[i][4] != 0 && a[i][3] == a[i][4] && a[i][1] == a[i][2] && a[i][2] == a[i][3]) { sum += a[i][1] * 4; c++; a[i][4] = 0; a[i][1] = 0; a[i][2] = 0; a[i][3] = 0; } if (a[i][2] != 0 && a[i][0] == a[i][1] && a[i][1] == a[i][2]) { sum += a[i][0] * 3; c++; a[i][0] = 0; a[i][1] = 0; a[i][2] = 0; } if (a[i][2] != 0 && a[i][3] == a[i][1] && a[i][1] == a[i][2]) { sum += a[i][1] * 3; c++; a[i][3] = 0; a[i][1] = 0; a[i][2] = 0; } if (a[i][2] != 0 && a[i][3] == a[i][4] && a[i][3] == a[i][2]) { sum += a[i][2] * 3; c++; a[i][3] = 0; a[i][4] = 0; a[i][2] = 0; } } if (c == 0) break; for (int h = 0; h < n; h++) for (i = n; i > 0; i--) for (j = 0; j < 4; j++) { if (a[i][j] == 0) { a[i][j] = a[i - 1][j]; a[i - 1][j] = 0; } } } cout << sum << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, c; while (cin >> n, n != 0) { int a[n][5], sum = 0; ; for (i = 0; i < n; i++) for (j = 0; j < 5; j++) cin >> a[i][j]; while (1) { c = 0; for (i = 0; i < n; i++) { if (a[i][0] != 0 && a[i][0] == a[i][1] && a[i][1] == a[i][2] && a[i][2] == a[i][3] && a[i][3] == a[i][4]) { sum += a[i][0] * 5; c++; a[i][0] = 0; a[i][1] = 0; a[i][2] = 0; a[i][3] = 0; a[i][4] = 0; } if (a[i][0] != 0 && a[i][0] == a[i][1] && a[i][1] == a[i][2] && a[i][2] == a[i][3]) { sum += a[i][0] * 4; c++; a[i][0] = 0; a[i][1] = 0; a[i][2] = 0; a[i][3] = 0; } if (a[i][4] != 0 && a[i][3] == a[i][4] && a[i][1] == a[i][2] && a[i][2] == a[i][3]) { sum += a[i][1] * 4; c++; a[i][4] = 0; a[i][1] = 0; a[i][2] = 0; a[i][3] = 0; } if (a[i][2] != 0 && a[i][0] == a[i][1] && a[i][1] == a[i][2]) { sum += a[i][0] * 3; c++; a[i][0] = 0; a[i][1] = 0; a[i][2] = 0; } if (a[i][2] != 0 && a[i][3] == a[i][1] && a[i][1] == a[i][2]) { sum += a[i][1] * 3; c++; a[i][3] = 0; a[i][1] = 0; a[i][2] = 0; } if (a[i][2] != 0 && a[i][3] == a[i][4] && a[i][3] == a[i][2]) { sum += a[i][2] * 3; c++; a[i][3] = 0; a[i][4] = 0; a[i][2] = 0; } } if (c == 0) break; for (int h = 0; h < n; h++) for (i = n - 1; i > 0; i--) for (j = 0; j < 5; j++) { if (a[i][j] == 0) { a[i][j] = a[i - 1][j]; a[i - 1][j] = 0; } } } cout << sum << endl; } return 0; }
[["+", 0, 7, 8, 7, 10, 11, 12, 16, 17, 33], ["+", 0, 7, 8, 7, 10, 11, 12, 16, 12, 13], ["-", 0, 7, 8, 7, 8, 7, 15, 16, 12, 13], ["+", 0, 7, 8, 7, 8, 7, 15, 16, 12, 13]]
1
861
#include <bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define pf push_front #define mp make_pair #define fr first #define sc second #define Rep(i, n) for (int i = 0; i < (n); i++) #define All(v) v.begin(), v.end() typedef pair<int, int> Pii; typedef pair<int, Pii> Pip; const int INF = 1107110711071107; main() { int H; while (cin >> H, H) { int data[11][5]; Rep(i, H) Rep(j, 5) cin >> data[i][j]; int sum = 0; while (1) { bool flag = false; Rep(i, H) Rep(j, 5) { if (data[i][j] == 0) continue; int r = j + 1; while (r < 5) { if (data[i][j] != data[i][r]) break; r++; } if (r - j >= 3) { // cout << i << " " << j << " " << r << endl; sum += (r - j) * data[i][j]; for (int k = j; k < r; k++) data[i][k] = 0; flag = true; } } if (flag) { bool flag2 = true; while (flag2) { flag2 = false; for (int i = H; i > 0; i--) { for (int j = 0; j < 5; j++) { if (data[i][j] == 0 && data[i - 1][j] != 0) { flag2 = true; data[i][j] = data[i - 1][j]; data[i - 1][j] = 0; } } } } } else { break; } } cout << sum << endl; } }
#include <bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define pf push_front #define mp make_pair #define fr first #define sc second #define Rep(i, n) for (int i = 0; i < (n); i++) #define All(v) v.begin(), v.end() typedef pair<int, int> Pii; typedef pair<int, Pii> Pip; const int INF = 1107110711071107; main() { int H; while (cin >> H, H) { int data[11][5]; Rep(i, H) Rep(j, 5) cin >> data[i][j]; int sum = 0; while (1) { bool flag = false; Rep(i, H) Rep(j, 5) { if (data[i][j] == 0) continue; int r = j + 1; while (r < 5) { if (data[i][j] != data[i][r]) break; r++; } if (r - j >= 3) { // cout << i << " " << j << " " << r << endl; sum += (r - j) * data[i][j]; for (int k = j; k < r; k++) data[i][k] = 0; flag = true; } } if (flag) { bool flag2 = true; while (flag2) { flag2 = false; for (int i = H - 1; i > 0; i--) { for (int j = 0; j < 5; j++) { if (data[i][j] == 0 && data[i - 1][j] != 0) { flag2 = true; data[i][j] = data[i - 1][j]; data[i - 1][j] = 0; } } } } } else { break; } } cout << sum << endl; } }
[["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 33], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
376
#include <bits/stdc++.h> using namespace std; using ll = long long; #define int ll #define FOR(i, a, b) for (int i = int(a); i < int(b); i++) #define REP(i, b) FOR(i, 0, b) int read() { int i; scanf("%lld", &i); return i; } signed main() { while (true) { int H = read(); if (H == 0) break; int table[H][5] = {0}; REP(i, H) { REP(j, 5) { table[i][j] = read(); } } int point = 0; while (true) { REP(i, H) { int count = 1; int tmpvalue = 0; FOR(j, 1, 5) { if (table[i][j] == table[i][j - 1]) { count++; tmpvalue = table[i][j]; } else if (count >= 3) { REP(k, count) { table[i][j - 1 - k] = 0; } point += count * tmpvalue; count = 1; } else count = 1; if (j == 4 && count >= 3) { REP(k, count) { table[i][j - k] = 0; } point += count * tmpvalue; count = 0; } } } bool flag = true; REP(j, 5) { for (int i = H - 2; i >= 0; i--) { for (int k = 0; k + i < 4; k++) { if (table[i + k][j] != 0 && table[i + 1 + k][j] == 0) { flag = false; table[i + 1 + k][j] = table[i + k][j]; table[i + k][j] = 0; } } } } /*REP(i,H){ REP(j,5){ cout << table[i][j]; } cout << endl; } cout << endl;*/ if (flag) break; } cout << point << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define int ll #define FOR(i, a, b) for (int i = int(a); i < int(b); i++) #define REP(i, b) FOR(i, 0, b) int read() { int i; scanf("%lld", &i); return i; } signed main() { while (true) { int H = read(); if (H == 0) break; int table[H][5] = {0}; REP(i, H) { REP(j, 5) { table[i][j] = read(); } } int point = 0; while (true) { REP(i, H) { int count = 1; int tmpvalue = 0; FOR(j, 1, 5) { if (table[i][j] == table[i][j - 1]) { count++; tmpvalue = table[i][j]; } else if (count >= 3) { REP(k, count) { table[i][j - 1 - k] = 0; } point += count * tmpvalue; count = 1; } else count = 1; if (j == 4 && count >= 3) { REP(k, count) { table[i][j - k] = 0; } point += count * tmpvalue; count = 0; } } } bool flag = true; REP(j, 5) { for (int i = H - 2; i >= 0; i--) { for (int k = 0; k + i < H - 1; k++) { if (table[i + k][j] != 0 && table[i + 1 + k][j] == 0) { flag = false; table[i + 1 + k][j] = table[i + k][j]; table[i + k][j] = 0; } } } } /*REP(i,H){ REP(j,5){ cout << table[i][j]; } cout << endl; } cout << endl;*/ if (flag) break; } cout << point << endl; } }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 31, 22], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
417
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < n; ++i) int tile[11][5]; int main() { int h; while (cin >> h, h) { REP(i, 11) REP(j, 5) { tile[i][j] = -1; } REP(i, h) REP(j, 5) { cin >> tile[h - 1 - i][j]; } int ans = 0; bool update = false; while (true) { update = false; REP(it, 11) { bool t1 = tile[it][0] == tile[it][1] && tile[it][0] != -1; bool t2 = tile[it][1] == tile[it][2] && tile[it][1] != -1; bool t3 = tile[it][2] == tile[it][3] && tile[it][2] != -1; bool t4 = tile[it][3] == tile[it][4] && tile[it][3] != -1; bool y1 = t1 && t2; bool y2 = t2 && t3; bool y3 = t3 && t4; if (y1 && y2 && y3) { ans += tile[it][0] * 5; REP(i, 5) { tile[it][i] = -1; } update = true; } else if (y1 && y2 || y2 && y3) { ans += tile[it][y3] * 4; REP(i, 4) { tile[it][i + y3] = -1; } update = true; } else if (y1 || y2 || y3) { ans += tile[it][2] * 3; REP(i, 3) { tile[it][i + y2 + ((int)y3) * 2] = -1; } update = true; } } REP(it, 11) { REP(i, 5) { if (tile[it][i] == -1) { int itt = it; while (tile[++itt][i] == -1 && itt != 10) ; swap(tile[it][i], tile[itt][i]); } } } if (!update) break; } cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < n; ++i) int tile[11][5]; int main() { int h; while (cin >> h, h) { REP(i, 11) REP(j, 5) { tile[i][j] = -1; } REP(i, h) REP(j, 5) { cin >> tile[h - 1 - i][j]; } int ans = 0; bool update = false; while (true) { update = false; REP(it, 11) { bool t1 = tile[it][0] == tile[it][1] && tile[it][0] != -1; bool t2 = tile[it][1] == tile[it][2] && tile[it][1] != -1; bool t3 = tile[it][2] == tile[it][3] && tile[it][2] != -1; bool t4 = tile[it][3] == tile[it][4] && tile[it][3] != -1; bool y1 = t1 && t2; bool y2 = t2 && t3; bool y3 = t3 && t4; if (y1 && y2 && y3) { ans += tile[it][0] * 5; REP(i, 5) { tile[it][i] = -1; } update = true; } else if (y1 && y2 || y2 && y3) { ans += tile[it][y3] * 4; REP(i, 4) { tile[it][i + y3] = -1; } update = true; } else if (y1 || y2 || y3) { ans += tile[it][2] * 3; REP(i, 3) { tile[it][i + y2 + ((int)y3) * 2] = -1; } update = true; } } REP(it, 10) { REP(i, 5) { if (tile[it][i] == -1) { int itt = it; while (tile[++itt][i] == -1 && itt != 10) ; swap(tile[it][i], tile[itt][i]); } } } if (!update) break; } cout << ans << endl; } }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
1
498
#include <iostream> using namespace std; int main() { while (1) { int H; cin >> H; if (H == 0) break; int S[10][5]; for (int y = 0; y < H; y++) { for (int x = 0; x < 5; x++) { cin >> S[y][x]; } } int sco = 0; while (1) { bool flg = false; for (int y = 0; y < H; y++) { int m = S[y][2]; if (m == 0) continue; if (S[y][0] == m && S[y][1] == m) { sco += m * 3; S[y][0] = S[y][1] = S[y][2] = 0; flg = true; if (S[y][3] == m) { sco += m; S[y][3] = 0; if (S[y][4] == m) { sco += m; S[y][4] = 0; } } } else if (S[y][1] == m && S[y][3] == m) { sco += m * 3; S[y][1] = S[y][2] = S[y][3] = 0; flg = true; if (S[y][4] == m) { sco += m; S[y][4] = 0; } } else if (S[y][3] == m && S[y][4] == m) { sco += m * 3; S[y][2] = S[y][3] = S[y][4] = 0; flg = true; } } if (!flg) break; for (int x = 0; x < 5; x++) { for (int y = H - 1; y >= 0; y--) { if (S[y][x] != 0) continue; for (int y2 = y - 1; y2 >= 0; y2--) { if (S[y2][x] != 0) { S[y][x] = S[y2][x]; S[2][x] = 0; break; } } } } } cout << sco << endl; } return 0; }
#include <iostream> using namespace std; int main() { while (1) { int H; cin >> H; if (H == 0) break; int S[10][5]; for (int y = 0; y < H; y++) { for (int x = 0; x < 5; x++) { cin >> S[y][x]; } } int sco = 0; while (1) { bool flg = false; for (int y = 0; y < H; y++) { int m = S[y][2]; if (m == 0) continue; if (S[y][0] == m && S[y][1] == m) { sco += m * 3; S[y][0] = S[y][1] = S[y][2] = 0; flg = true; if (S[y][3] == m) { sco += m; S[y][3] = 0; if (S[y][4] == m) { sco += m; S[y][4] = 0; } } } else if (S[y][1] == m && S[y][3] == m) { sco += m * 3; S[y][1] = S[y][2] = S[y][3] = 0; flg = true; if (S[y][4] == m) { sco += m; S[y][4] = 0; } } else if (S[y][3] == m && S[y][4] == m) { sco += m * 3; S[y][2] = S[y][3] = S[y][4] = 0; flg = true; } } if (!flg) break; for (int x = 0; x < 5; x++) { for (int y = H - 1; y >= 0; y--) { if (S[y][x] != 0) continue; for (int y2 = y - 1; y2 >= 0; y2--) { if (S[y2][x] != 0) { S[y][x] = S[y2][x]; S[y2][x] = 0; break; } } } } } cout << sco << endl; } return 0; }
[["-", 0, 11, 31, 69, 28, 69, 341, 342, 0, 13], ["+", 0, 11, 31, 69, 28, 69, 341, 342, 0, 22]]
1
525
#include <bits/stdc++.h> using namespace std; #define INF 1e9 signed main() { ios::sync_with_stdio(false); cin.tie(0); while (1) { int h; cin >> h; if (h == 0) break; int c[15][5] = {}; for (int i = 0; i < h; i++) { for (int j = 0; j < 5; j++) { cin >> c[i][j]; } } int ans = 0; while (1) { bool f = true; for (int i = 0; i < h; i++) { int t = 1; for (int j = 0; j < 4; j++) { if (c[i][j] == c[i][j + 1] && c[i][j] != 0) t++; else { if (t < 3) t = 1; else { ans += c[i][j - 1] * t; for (int k = 0; k < t; k++) c[i][j - k - 1] = 0; f = false; t = 1; } } } if (t > 2) { ans += c[i][4] * t; for (int j = 0; j < t; j++) { c[i][4 - j] = 0; } f = false; } } for (int i = h - 2; i >= 0; i--) { for (int j = 0; j < 5; j++) { for (int k = 1; i + k < h; k++) { if (c[i + k][j] == 0) { c[i + k][j] = c[i + k - 1][j]; c[i + k - 1][j] = 0; } } } } if (f) { cout << ans << endl; break; } } } }
#include <bits/stdc++.h> using namespace std; #define INF 1e9 signed main() { ios::sync_with_stdio(false); cin.tie(0); while (1) { int h; cin >> h; if (h == 0) break; int c[15][5] = {}; for (int i = 0; i < h; i++) { for (int j = 0; j < 5; j++) { cin >> c[i][j]; } } int ans = 0; while (1) { bool f = true; for (int i = 0; i < h; i++) { int t = 1; for (int j = 0; j < 4; j++) { if (c[i][j] == c[i][j + 1] && c[i][j] != 0) t++; else { if (t < 3) t = 1; else { ans += c[i][j - 1] * t; for (int k = 0; k < t; k++) c[i][j - k] = 0; f = false; t = 1; } } } if (t > 2) { ans += c[i][4] * t; for (int j = 0; j < t; j++) { c[i][4 - j] = 0; } f = false; } } for (int i = h - 2; i >= 0; i--) { for (int j = 0; j < 5; j++) { for (int k = 1; i + k < h; k++) { if (c[i + k][j] == 0) { c[i + k][j] = c[i + k - 1][j]; c[i + k - 1][j] = 0; } } } } if (f) { cout << ans << endl; break; } } } }
[["-", 0, 11, 31, 69, 341, 342, 0, 16, 17, 33], ["-", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13]]
1
426
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define INF 100000000 typedef long long ll; const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; const int MAXM = 1000100; bool isPrime[MAXM]; int dp[1500][1500]; int last[1500][1500]; class Board { public: vector<vector<int>> board; vector<pair<int, int>> pos; int maxDepth; int minWidth; int maxWidth; Board(int n) { maxDepth = 0; minWidth = 1500; maxWidth = 0; pos.resize(n + 10); for (int i = 0; i < 1500; i++) { vector<int> tmp(1500); board.push_back(tmp); } pair<int, int> position = make_pair(750, 750); int direction = 0; for (int cur = 1; cur <= n; cur++) { maxDepth = max(maxDepth, position.second); maxWidth = max(maxWidth, position.first); minWidth = min(minWidth, position.first); board[position.second][position.first] = cur; pos[cur] = position; int left = direction + 1; left %= 4; if (board[position.second + dy[left]][position.first + dx[left]] == 0) { direction = left; } position.first += dx[direction]; position.second += dy[direction]; } } }; void initPrime() { for (int i = 2; i < MAXM; i++) isPrime[i] = true; for (int i = 2; i * i < MAXM; i++) { if (isPrime[i]) { for (int j = 2; j * i < MAXM; j++) { isPrime[i * j] = false; } } } } int main(void) { int n, m; initPrime(); while (cin >> n >> m) { if (n == 0 && m == 0) break; Board board(n); // for (int i = 745; i < 755; i++) { // for (int j = 745; j < 755; j++) { // cout << board.board[i][j] << "\t"; // } // cout << endl; //} // cout << board.maxDepth << endl; for (int i = 0; i < 1500; i++) { for (int j = 0; j < 1500; j++) { dp[i][j] = -1; last[i][j] = -1; } } auto start = board.pos[m]; if (isPrime[m]) dp[start.second][start.first] = 1, last[start.second][start.first] = m; else dp[start.second][start.first] = 0; for (int i = start.second; i <= board.maxDepth; i++) { for (int j = board.minWidth; j < board.maxWidth; j++) { if (dp[i][j] == -1) continue; for (int k = -1; k <= 1; k++) { pair<int, int> next = make_pair(j + k, i + 1); if (next.first < board.minWidth || next.second > board.maxWidth || board.board[next.second][next.first] > n) continue; if (isPrime[board.board[next.second][next.first]]) { if (dp[next.second][next.first] <= dp[i][j] + 1) { dp[next.second][next.first] = dp[i][j] + 1; last[next.second][next.first] = board.board[next.second][next.first]; } } else { if (dp[next.second][next.first] <= dp[i][j]) { dp[next.second][next.first] = dp[i][j]; last[next.second][next.first] = last[i][j]; } } } } } // for (int i = 745; i < 755; i++) { // for (int j = 745; j < 755; j++) { // cout << dp[i][j] << "\t"; // } // cout << endl; // } // for (int i = 745; i < 755; i++) { // for (int j = 745; j < 755; j++) { // cout << last[i][j] << "\t"; // } // cout << endl; // } pair<int, int> ans; for (int i = board.minWidth; i <= board.maxWidth; i++) { ans = max(make_pair(dp[board.maxDepth][i], last[board.maxDepth][i]), ans); } cout << ans.first << " " << ans.second << endl; } return 0; }
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define INF 100000000 typedef long long ll; const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; const int MAXM = 1000100; bool isPrime[MAXM]; int dp[1500][1500]; int last[1500][1500]; class Board { public: vector<vector<int>> board; vector<pair<int, int>> pos; int maxDepth; int minWidth; int maxWidth; Board(int n) { maxDepth = 0; minWidth = 1500; maxWidth = 0; pos.resize(n + 10); for (int i = 0; i < 1500; i++) { vector<int> tmp(1500); board.push_back(tmp); } pair<int, int> position = make_pair(750, 750); int direction = 0; for (int cur = 1; cur <= n; cur++) { maxDepth = max(maxDepth, position.second); maxWidth = max(maxWidth, position.first); minWidth = min(minWidth, position.first); board[position.second][position.first] = cur; pos[cur] = position; int left = direction + 1; left %= 4; if (board[position.second + dy[left]][position.first + dx[left]] == 0) { direction = left; } position.first += dx[direction]; position.second += dy[direction]; } } }; void initPrime() { for (int i = 2; i < MAXM; i++) isPrime[i] = true; for (int i = 2; i * i < MAXM; i++) { if (isPrime[i]) { for (int j = 2; j * i < MAXM; j++) { isPrime[i * j] = false; } } } } int main(void) { int n, m; initPrime(); while (cin >> n >> m) { if (n == 0 && m == 0) break; Board board(n); // for (int i = 745; i < 755; i++) { // for (int j = 745; j < 755; j++) { // cout << board.board[i][j] << "\t"; // } // cout << endl; // } // cout << board.maxDepth << endl; for (int i = 0; i < 1500; i++) { for (int j = 0; j < 1500; j++) { dp[i][j] = -1; last[i][j] = -1; } } auto start = board.pos[m]; if (isPrime[m]) dp[start.second][start.first] = 1, last[start.second][start.first] = m; else dp[start.second][start.first] = 0; for (int i = start.second; i <= board.maxDepth; i++) { for (int j = board.minWidth; j <= board.maxWidth; j++) { if (dp[i][j] == -1) continue; for (int k = -1; k <= 1; k++) { pair<int, int> next = make_pair(j + k, i + 1); if (next.first < board.minWidth || next.second > board.maxWidth || board.board[next.second][next.first] > n) continue; if (isPrime[board.board[next.second][next.first]]) { if (dp[next.second][next.first] <= dp[i][j] + 1) { dp[next.second][next.first] = dp[i][j] + 1; last[next.second][next.first] = board.board[next.second][next.first]; } } else { if (dp[next.second][next.first] <= dp[i][j]) { dp[next.second][next.first] = dp[i][j]; last[next.second][next.first] = last[i][j]; } } } } } // for (int i = 745; i < 755; i++) { // for (int j = 745; j < 755; j++) { // cout << dp[i][j] << "\t"; // } // cout << endl; // } // for (int i = 745; i < 755; i++) { // for (int j = 745; j < 755; j++) { // cout << last[i][j] << "\t"; // } // cout << endl; // } pair<int, int> ans; for (int i = board.minWidth; i <= board.maxWidth; i++) { ans = max(make_pair(dp[board.maxDepth][i], last[board.maxDepth][i]), ans); } cout << ans.first << " " << ans.second << endl; } return 0; }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19]]
1
971
#include <algorithm> #include <iostream> using namespace std; typedef pair<int, int> P; bool prime[2160000]; int val[1200][1200]; P dp[1200][1200]; int xs[] = {1, 0, -1, 0}, ys[] = {0, 1, 0, -1}; bool solve() { int m, n; cin >> m >> n; if (m == 0 && n == 0) return false; for (int i = 0; i < 1200; i++) for (int j = 0; j < 1200; j++) dp[i][j] = P(0, 0); for (int j = 1; j < 1100; j++) { for (int i = 1; i < 1100; i++) { dp[i][j] = max(dp[i][j - 1], dp[i - 1][j - 1]); dp[i][j] = max(dp[i][j], dp[i + 1][j - 1]); if (prime[val[i][j]] && 1 <= val[i][j] && val[i][j] <= m) { if (dp[i][j].first == 0) { dp[i][j].second = val[i][j]; } dp[i][j].first++; } if (val[i][j] == n) { cout << dp[i][j].first << " " << dp[i][j].second << endl; return true; } } } } int main() { fill(prime, prime + 1080000, true); prime[0] = prime[1] = false; for (int i = 2; i < 1080000; i++) { if (prime[i]) { for (long long j = (long long)i * i; j < 1080000; j += i) prime[j] = false; } } for (int i = 0, p = 1, x = 550, y = 550; i < 1100; i++) { if (p >= 1080000) break; for (int j = 0; j < (i / 2) + 1; j++) { if (p >= 1080000) break; val[x][y] = p++; x += xs[i % 4]; y += ys[i % 4]; } } while (solve()) ; return 0; }
#include <algorithm> #include <iostream> using namespace std; typedef pair<int, int> P; bool prime[2160000]; int val[1200][1200]; P dp[1200][1200]; int xs[] = {1, 0, -1, 0}, ys[] = {0, 1, 0, -1}; bool solve() { int m, n; cin >> m >> n; if (m == 0 && n == 0) return false; for (int i = 0; i < 1200; i++) for (int j = 0; j < 1200; j++) dp[i][j] = P(0, 0); for (int j = 1; j < 1100; j++) { for (int i = 1; i < 1100; i++) { dp[i][j] = max(dp[i][j - 1], dp[i - 1][j - 1]); dp[i][j] = max(dp[i][j], dp[i + 1][j - 1]); if (prime[val[i][j]] && 1 <= val[i][j] && val[i][j] <= m) { if (dp[i][j].first == 0) { dp[i][j].second = val[i][j]; } dp[i][j].first++; } if (val[i][j] == n) { cout << dp[i][j].first << " " << dp[i][j].second << endl; return true; } } } } int main() { fill(prime, prime + 1080000, true); prime[0] = prime[1] = false; for (int i = 2; i < 1080000; i++) { if (prime[i]) { for (long long j = (long long)i * i; j < 1080000; j += i) prime[j] = false; } } for (int i = 0, p = 1, x = 550, y = 550; i < 2000; i++) { if (p >= 1080000) break; for (int j = 0; j < (i / 2) + 1; j++) { if (p >= 1080000) break; val[x][y] = p++; x += xs[i % 4]; y += ys[i % 4]; } } while (solve()) ; return 0; }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13]]
1
552
#include <bits/stdc++.h> using namespace std; bool isNotPrime[1000200] = {1, 1}; int prime() { for (int i = 2; i * i < 1000200; i++) { for (int j = 2 * i; j < 1000200; j += i) { isNotPrime[j] = 1; } } } int sc[1050][1050] = {}; int dp[1050][1050][2]; #define T_MAX 1000000 int main() { prime(); int c = 502, t = 1; int x = c, y = c; int w = -1, h = -1, _w = 1000000, _h = 1000000; for (int i = 1;; i += 2) { // cout<<t<<endl; for (int j = 1; j <= i; j++) { sc[y][x] = t++; w = max(x, w); h = max(y, h); _w = min(x, _w); _h = min(y, _h); x++; if (t > T_MAX) goto next; } for (int j = 1; j <= i; j++) { sc[y][x] = t++; w = max(x, w); h = max(y, h); _w = min(x, _w); _h = min(y, _h); y--; // if(x < 0 || y < 0)cout<<"!"<<endl; if (t > T_MAX) goto next; } for (int j = 1; j <= i + 1; j++) { sc[y][x] = t++; w = max(x, w); h = max(y, h); _w = min(x, _w); _h = min(y, _h); x--; // if(x < 0 || y < 0)cout<<"!"<<endl; if (t > T_MAX) goto next; } for (int j = 1; j <= i + 1; j++) { sc[y][x] = t++; w = max(x, w); h = max(y, h); _w = min(x, _w); _h = min(y, _h); y++; // if(x < 0 || y < 0)cout<<"!"<<endl; if (t > T_MAX) goto next; } } next:; int m, n; while (cin >> m >> n, m) { int sx, sy; for (int i = 0; i < 1050; i++) { for (int j = 0; j < 1050; j++) { if (sc[i][j] == n) sy = i, sx = j; dp[i][j][0] = dp[i][j][1] = -1; } } dp[sy][sx][0] = dp[sy][sx][1] = 0; if (!isNotPrime[sc[sy][sx]]) dp[sy][sx][0]++, dp[sy][sx][1] = sc[sy][sx]; int aval = dp[sy][sx][0], aidx = dp[sy][sx][1]; for (int i = sy; i <= h + 1; i++) { for (int j = 0; j <= w + 1; j++) { if (dp[i][j][0] == -1) continue; if (sc[i + 1][j] != 0 && sc[i + 1][j] <= m) { int n0 = dp[i][j][0] + !isNotPrime[sc[i + 1][j]]; int n1 = (!isNotPrime[sc[i + 1][j - 1]]) ? sc[i + 1][j] : dp[i][j][1]; if (dp[i + 1][j][0] == n0 ? dp[i + 1][j][1] < n1 : dp[i + 1][j][0] < n0) { dp[i + 1][j][0] = n0; dp[i + 1][j][1] = n1; } if (dp[i + 1][j][0] == aval ? dp[i + 1][j][1] > aidx : dp[i + 1][j][0] > aval) { aval = dp[i + 1][j][0]; aidx = dp[i + 1][j][1]; } } if (sc[i + 1][j - 1] != 0 && sc[i + 1][j - 1] <= m) { int n0 = dp[i][j][0] + !isNotPrime[sc[i + 1][j - 1]]; int n1 = (!isNotPrime[sc[i + 1][j - 1]]) ? sc[i + 1][j - 1] : dp[i][j][1]; if (dp[i + 1][j - 1][0] == n0 ? dp[i + 1][j - 1][1] < n1 : dp[i + 1][j - 1][0] < n0) { dp[i + 1][j - 1][0] = n0; dp[i + 1][j - 1][1] = n1; } if (dp[i + 1][j - 1][0] == aval ? dp[i + 1][j - 1][1] > aidx : dp[i + 1][j - 1][0] > aval) { aval = dp[i + 1][j - 1][0]; aidx = dp[i + 1][j - 1][1]; } } if (sc[i + 1][j + 1] != 0 && sc[i + 1][j + 1] <= m) { int n0 = dp[i][j][0] + !isNotPrime[sc[i + 1][j + 1]]; int n1 = (!isNotPrime[sc[i + 1][j + 1]]) ? sc[i + 1][j + 1] : dp[i][j][1]; if (dp[i + 1][j + 1][0] == n0 ? dp[i + 1][j + 1][1] < n1 : dp[i + 1][j + 1][0] < n0) { dp[i + 1][j + 1][0] = n0; dp[i + 1][j + 1][1] = n1; } if (dp[i + 1][j + 1][0] == aval ? dp[i + 1][j + 1][1] > aidx : dp[i + 1][j + 1][0] > aval) { aval = dp[i + 1][j + 1][0]; aidx = dp[i + 1][j + 1][1]; } } // cout<<i<<" "<<j<<" "<<dp[i][j][0]<<" "<<dp[i][j][1]<<endl; } } cout << aval << " " << aidx << endl; } }
#include <bits/stdc++.h> using namespace std; bool isNotPrime[1000200] = {1, 1}; int prime() { for (int i = 2; i * i < 1000200; i++) { for (int j = 2 * i; j < 1000200; j += i) { isNotPrime[j] = 1; } } } int sc[1050][1050] = {}; int dp[1050][1050][2]; #define T_MAX 1000000 int main() { prime(); int c = 502, t = 1; int x = c, y = c; int w = -1, h = -1, _w = 1000000, _h = 1000000; for (int i = 1;; i += 2) { // cout<<t<<endl; for (int j = 1; j <= i; j++) { sc[y][x] = t++; w = max(x, w); h = max(y, h); _w = min(x, _w); _h = min(y, _h); x++; if (t > T_MAX) goto next; } for (int j = 1; j <= i; j++) { sc[y][x] = t++; w = max(x, w); h = max(y, h); _w = min(x, _w); _h = min(y, _h); y--; // if(x < 0 || y < 0)cout<<"!"<<endl; if (t > T_MAX) goto next; } for (int j = 1; j <= i + 1; j++) { sc[y][x] = t++; w = max(x, w); h = max(y, h); _w = min(x, _w); _h = min(y, _h); x--; // if(x < 0 || y < 0)cout<<"!"<<endl; if (t > T_MAX) goto next; } for (int j = 1; j <= i + 1; j++) { sc[y][x] = t++; w = max(x, w); h = max(y, h); _w = min(x, _w); _h = min(y, _h); y++; // if(x < 0 || y < 0)cout<<"!"<<endl; if (t > T_MAX) goto next; } } next:; int m, n; while (cin >> m >> n, m) { int sx, sy; for (int i = 0; i < 1050; i++) { for (int j = 0; j < 1050; j++) { if (sc[i][j] == n) sy = i, sx = j; dp[i][j][0] = dp[i][j][1] = -1; } } dp[sy][sx][0] = dp[sy][sx][1] = 0; if (!isNotPrime[sc[sy][sx]]) dp[sy][sx][0]++, dp[sy][sx][1] = sc[sy][sx]; int aval = dp[sy][sx][0], aidx = dp[sy][sx][1]; for (int i = sy; i <= h + 1; i++) { for (int j = 0; j <= w + 1; j++) { if (dp[i][j][0] == -1) continue; if (sc[i + 1][j] != 0 && sc[i + 1][j] <= m) { int n0 = dp[i][j][0] + !isNotPrime[sc[i + 1][j]]; int n1 = (!isNotPrime[sc[i + 1][j]]) ? sc[i + 1][j] : dp[i][j][1]; if (dp[i + 1][j][0] == n0 ? dp[i + 1][j][1] < n1 : dp[i + 1][j][0] < n0) { dp[i + 1][j][0] = n0; dp[i + 1][j][1] = n1; } if (dp[i + 1][j][0] == aval ? dp[i + 1][j][1] > aidx : dp[i + 1][j][0] > aval) { aval = dp[i + 1][j][0]; aidx = dp[i + 1][j][1]; } } if (sc[i + 1][j - 1] != 0 && sc[i + 1][j - 1] <= m) { int n0 = dp[i][j][0] + !isNotPrime[sc[i + 1][j - 1]]; int n1 = (!isNotPrime[sc[i + 1][j - 1]]) ? sc[i + 1][j - 1] : dp[i][j][1]; if (dp[i + 1][j - 1][0] == n0 ? dp[i + 1][j - 1][1] < n1 : dp[i + 1][j - 1][0] < n0) { dp[i + 1][j - 1][0] = n0; dp[i + 1][j - 1][1] = n1; } if (dp[i + 1][j - 1][0] == aval ? dp[i + 1][j - 1][1] > aidx : dp[i + 1][j - 1][0] > aval) { aval = dp[i + 1][j - 1][0]; aidx = dp[i + 1][j - 1][1]; } } if (sc[i + 1][j + 1] != 0 && sc[i + 1][j + 1] <= m) { int n0 = dp[i][j][0] + !isNotPrime[sc[i + 1][j + 1]]; int n1 = (!isNotPrime[sc[i + 1][j + 1]]) ? sc[i + 1][j + 1] : dp[i][j][1]; if (dp[i + 1][j + 1][0] == n0 ? dp[i + 1][j + 1][1] < n1 : dp[i + 1][j + 1][0] < n0) { dp[i + 1][j + 1][0] = n0; dp[i + 1][j + 1][1] = n1; } if (dp[i + 1][j + 1][0] == aval ? dp[i + 1][j + 1][1] > aidx : dp[i + 1][j + 1][0] > aval) { aval = dp[i + 1][j + 1][0]; aidx = dp[i + 1][j + 1][1]; } } // cout<<i<<" "<<j<<" "<<dp[i][j][0]<<" "<<dp[i][j][1]<<endl; } } cout << aval << " " << aidx << endl; } }
[["-", 341, 342, 0, 69, 341, 342, 0, 16, 17, 33], ["-", 341, 342, 0, 69, 341, 342, 0, 16, 12, 13]]
1
1,549
#include "bits/stdc++.h" using namespace std; bool isso(long long int a) { if (a == 1 || a == 0) return false; for (long long int i = 2; i * i <= a; ++i) { if ((a % i)) { } else { return false; } } return true; } int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, 1, 0, -1}; pair<bool, int> caves[1199][1199]; pair<int, int> dp[1199][1199]; vector<pair<int, int>> places(1010000); int main() { { for (int i = 0; i < 1199; ++i) { for (int j = 0; j < 1199; ++j) { caves[i][j] = {false, -1}; } } int num = 1; int ny = 600; int nx = 600; int roopnum = 2; int way = 2; while (num <= 1000000) { for (int i = 0; i < roopnum / 2; ++i) { caves[ny][nx] = {isso(num), num}; places[num] = {ny, nx}; ny += dy[way]; nx += dx[way]; num++; } roopnum++; way = (way + 1) % 4; } } while (1) { int m, n; cin >> m >> n; if (!m) break; for (int i = 0; i < 1199; ++i) { for (int j = 0; j < 1199; ++j) { dp[i][j] = {-1, -999999}; } } int ny = places[n].first; int nx = places[n].second; if (caves[ny][nx].first) { dp[ny][nx] = {caves[ny][nx].second, 1}; } else { dp[ny][nx] = {-1, 0}; } for (int y = 0; y < 1198; ++y) { for (int x = 1; x < 1198; ++x) { for (int ax = -1; ax <= 1; ++ax) { const int newy = y + 1; const int newx = x + ax; bool so = (caves[newy][newx].first) && (caves[newy][newx].second <= m); const int newsonum = dp[y][x].second + so; if (dp[newy][newx].second < newsonum) { dp[newy][newx].second = newsonum; if (so) { dp[newy][newx].first = caves[newy][newx].second; } else { dp[newy][newx].first = dp[y][x].first; } } else if (dp[newy][newx].second == newsonum) { if (so) { dp[newy][newx].first = caves[newy][newx].second; } else { dp[newy][newx].first = max(dp[newy][newx].first, dp[y][x].first); } } } } } int maxso = 0; int maxnum = 0; for (int x = 0; x < 1199; ++x) { if (maxso == dp[1198][x].first) { maxnum = max(maxnum, dp[1198][x].first); } else if (maxso < dp[1198][x].second) { maxso = dp[1198][x].second; maxnum = dp[1198][x].first; } } cout << maxso << " " << maxnum << endl; } return 0; }
#include "bits/stdc++.h" using namespace std; bool isso(long long int a) { if (a == 1 || a == 0) return false; for (long long int i = 2; i * i <= a; ++i) { if ((a % i)) { } else { return false; } } return true; } int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, 1, 0, -1}; pair<bool, int> caves[1199][1199]; pair<int, int> dp[1199][1199]; vector<pair<int, int>> places(1010000); int main() { { for (int i = 0; i < 1199; ++i) { for (int j = 0; j < 1199; ++j) { caves[i][j] = {false, -1}; } } int num = 1; int ny = 600; int nx = 600; int roopnum = 2; int way = 2; while (num <= 1000000) { for (int i = 0; i < roopnum / 2; ++i) { caves[ny][nx] = {isso(num), num}; places[num] = {ny, nx}; ny += dy[way]; nx += dx[way]; num++; } roopnum++; way = (way + 1) % 4; } } while (1) { int m, n; cin >> m >> n; if (!m) break; for (int i = 0; i < 1199; ++i) { for (int j = 0; j < 1199; ++j) { dp[i][j] = {-1, -999999}; } } int ny = places[n].first; int nx = places[n].second; if (caves[ny][nx].first) { dp[ny][nx] = {caves[ny][nx].second, 1}; } else { dp[ny][nx] = {-1, 0}; } for (int y = 0; y < 1198; ++y) { for (int x = 1; x < 1198; ++x) { for (int ax = -1; ax <= 1; ++ax) { const int newy = y + 1; const int newx = x + ax; bool so = (caves[newy][newx].first) && (caves[newy][newx].second <= m); const int newsonum = dp[y][x].second + so; if (dp[newy][newx].second < newsonum) { dp[newy][newx].second = newsonum; if (so) { dp[newy][newx].first = caves[newy][newx].second; } else { dp[newy][newx].first = dp[y][x].first; } } else if (dp[newy][newx].second == newsonum) { if (so) { dp[newy][newx].first = caves[newy][newx].second; } else { dp[newy][newx].first = max(dp[newy][newx].first, dp[y][x].first); } } } } } int maxso = 0; int maxnum = 0; for (int x = 0; x < 1199; ++x) { if (maxso == dp[1198][x].second) { maxnum = max(maxnum, dp[1198][x].first); } else if (maxso < dp[1198][x].second) { maxso = dp[1198][x].second; maxnum = dp[1198][x].first; } } cout << maxso << " " << maxnum << endl; } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 12, 118, 119, 120], ["+", 0, 57, 15, 339, 51, 16, 12, 118, 119, 120]]
1
830
#include <algorithm> #include <cstdio> #include <iomanip> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; ++i) #define pii pair<int, int> #define MAXM 1000001 #define MAXSQM 1002 int diff[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; int map[MAXSQM][MAXSQM] = {}; bool furui[MAXM] = {}; pii dpmap[MAXSQM][2] = {}; void init_map(int m) { fill_n(*map, MAXSQM * MAXSQM, 0); fill_n(*dpmap, MAXSQM * 2, pii(-1, -1)); int idx = MAXSQM / 2, idy = MAXSQM / 2; int dir = 0; int c = 1; for (int i = 1; c <= m; i++) { for (int j = 0; j < i && c <= m; j++) { map[idx][idy] = c++; idx += diff[dir][0]; idy += diff[dir][1]; } dir = (dir + 1) % 4; for (int j = 0; j < i && c <= m; j++) { map[idx][idy] = c++; idx += diff[dir][0]; idy += diff[dir][1]; } dir = (dir + 1) % 4; } } pii dp(int n) { int nx, ny; for (nx = 0; nx < MAXSQM; nx++) { for (ny = 0; ny < MAXSQM; ny++) { if (n == map[nx][ny]) break; } if (n == map[nx][ny]) break; } pii ans = pii(0, 0); if (!furui[map[nx][ny]]) { dpmap[nx][ny % 2] = pii(1, map[nx][ny]); ans = dpmap[nx][ny % 2]; } else { dpmap[nx][ny % 2] = pii(0, 0); } for (int j = ny; j >= 1; j--) { for (int i = 0; i < MAXSQM; i++) { if (map[i][j] == 0 || dpmap[i][j % 2].first == -1) continue; for (int k = -1; k <= 1; k++) { if (map[i + k][j - 1] == 0) continue; if (!furui[map[i + k][j - 1]]) { dpmap[i + k][(j - 1) % 2] = max(pii(dpmap[i][j % 2].first + 1, map[i + k][j - 1]), dpmap[i + k][(j - 1) % 2]); } else { dpmap[i + k][(j - 1) % 2] = max(dpmap[i][j % 2], dpmap[i + k][(j - 1) % 2]); } ans = max(ans, dpmap[i + k][(j - 1) % 2]); } } } return ans; } int main() { while (1) { int m, n; cin >> m >> n; if (m == 0) break; furui[0] == furui[1] == true; for (int i = 2; i < MAXSQM; i++) { for (int j = 2; i * j < MAXM; j++) { if (!furui[i]) { furui[i * j] = true; } } } init_map(m); pii p = dp(n); /*int b=503,a=487; for(int i=a-5;i<a+5;i++){ for(int j=b-5;j<b+5;j++){ cout<<std::setw(4)<<map[j][i]; } cout<<endl; } for(int i=a-5;i<a+5;i++){ for(int j=b-5;j<b+5;j++){ cout<<setw(4)<<dpmap[j][i].first; } cout<<endl; }*/ cout << p.first << " " << p.second << endl; } return 0; }
#include <algorithm> #include <cstdio> #include <iomanip> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; ++i) #define pii pair<int, int> #define MAXM 1000001 #define MAXSQM 1003 int diff[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; int map[MAXSQM][MAXSQM] = {}; bool furui[MAXM] = {}; pii dpmap[MAXSQM][2] = {}; void init_map(int m) { fill_n(*map, MAXSQM * MAXSQM, 0); fill_n(*dpmap, MAXSQM * 2, pii(-1, -1)); int idx = MAXSQM / 2, idy = MAXSQM / 2; int dir = 0; int c = 1; for (int i = 1; c <= m; i++) { for (int j = 0; j < i && c <= m; j++) { map[idx][idy] = c++; idx += diff[dir][0]; idy += diff[dir][1]; } dir = (dir + 1) % 4; for (int j = 0; j < i && c <= m; j++) { map[idx][idy] = c++; idx += diff[dir][0]; idy += diff[dir][1]; } dir = (dir + 1) % 4; } } pii dp(int n) { int nx, ny; for (nx = 0; nx < MAXSQM; nx++) { for (ny = 0; ny < MAXSQM; ny++) { if (n == map[nx][ny]) break; } if (n == map[nx][ny]) break; } pii ans = pii(0, 0); if (!furui[map[nx][ny]]) { dpmap[nx][ny % 2] = pii(1, map[nx][ny]); ans = dpmap[nx][ny % 2]; } else { dpmap[nx][ny % 2] = pii(0, 0); } for (int j = ny; j >= 1; j--) { for (int i = 0; i < MAXSQM; i++) { if (map[i][j] == 0 || dpmap[i][j % 2].first == -1) continue; for (int k = -1; k <= 1; k++) { if (map[i + k][j - 1] == 0) continue; if (!furui[map[i + k][j - 1]]) { dpmap[i + k][(j - 1) % 2] = max(pii(dpmap[i][j % 2].first + 1, map[i + k][j - 1]), dpmap[i + k][(j - 1) % 2]); } else { dpmap[i + k][(j - 1) % 2] = max(dpmap[i][j % 2], dpmap[i + k][(j - 1) % 2]); } ans = max(ans, dpmap[i + k][(j - 1) % 2]); } } } return ans; } int main() { while (1) { int m, n; cin >> m >> n; if (m == 0) break; furui[0] = furui[1] = true; for (int i = 2; i < MAXSQM; i++) { for (int j = 2; i * j < MAXM; j++) { if (!furui[i]) { furui[i * j] = true; } } } init_map(m); pii p = dp(n); /*int b=MAXSQM/2,a=MAXSQM/2; for(int i=a-5;i<a+5;i++){ for(int j=b-5;j<b+5;j++){ cout<<std::setw(4)<<map[j][i]; } cout<<endl; } for(int i=a-5;i<a+5;i++){ for(int j=b-5;j<b+5;j++){ cout<<setw(4)<<dpmap[j][i].first; } cout<<endl; }*/ cout << p.first << " " << p.second << endl; } return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["-", 8, 9, 0, 1, 0, 16, 31, 16, 17, 60], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["-", 0, 52, 8, 9, 0, 1, 0, 16, 17, 60], ["+", 8, 9, 0, 1, 0, 11, 12, 11, 17, 32]]
1
830
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #define MAX_M 1000000 using namespace std; typedef struct { int num, prime, last; } HOLE; int M, N; HOLE h[1000 + 2][1000 + 2]; bool prime_judge(int x) { if (x == 0 || x == 1) return false; for (int i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return true; } void solve() { int flag = 0, d = 1; //?????????????????????, ???????????¢ int x = 501, y = 501, ox = 501, oy = 501; int ansx, ansy; for (int i = 0; i < 1002; i++) { for (int j = 0; j < 1002; j++) { h[i][j].num = 0; h[i][j].last = 0; h[i][j].prime = 0; } } for (int i = 1; i <= M; i++) { h[x][y].num = i; if (i == N) { ansx = x; ansy = y; } if (flag == 0) { x++; if (x - ox == d) { ox = x; flag = 1; } } else if (flag == 1) { y--; if (oy - y == d) { oy = y; d++; flag = 2; } } else if (flag == 2) { x--; if (ox - x == d) { ox = x; flag = 3; } } else if (flag == 3) { y++; if (y - oy == d) { oy = y; d++; flag = 0; } } } for (int i = 1001; i >= 0; i--) { for (int j = 0; j < 1002; j++) { if (h[j][i].num != 0) { h[j][i].prime = max(max(h[j - 1][i + 1].prime, h[j][i + 1].prime), h[j + 1][i + 1].prime); if (h[j - 1][i + 1].last == 0 && h[j][i + 1].last == 0 && h[j + 1][i + 1].last == 0) { if (prime_judge(h[j][i].num)) h[j][i].last = h[j][i].num; } else { int k; for (k = -1; k <= 1; k++) { if (h[j + k][i + 1].prime == h[j][i].prime) { h[j][i].last = h[j + k][i + 1].last; break; } } for (int l = -1; l <= 1; l++) { if (h[j + k][i + 1].prime == h[j + l][i + 1].prime) { h[j][i].last = max(h[j][i].last, h[j + l][i + 1].last); } } } if (prime_judge(h[j][i].num)) h[j][i].prime++; } } } if (h[ansx][ansy].prime == 0) cout << "0 0" << endl; else cout << h[ansx][ansy].prime << " " << h[ansx][ansy].last << endl; } int main() { while (1) { cin >> M >> N; if (M == 0 && N == 0) break; solve(); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #define MAX_M 1000000 using namespace std; typedef struct { int num, prime, last; } HOLE; int M, N; HOLE h[1000 + 2][1000 + 2]; bool prime_judge(int x) { if (x == 0 || x == 1) return false; for (int i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return true; } void solve() { int flag = 0, d = 1; //?????????????????????, ???????????¢ int x = 500, y = 500, ox = 500, oy = 500; int ansx, ansy; for (int i = 0; i < 1002; i++) { for (int j = 0; j < 1002; j++) { h[i][j].num = 0; h[i][j].last = 0; h[i][j].prime = 0; } } for (int i = 1; i <= M; i++) { h[x][y].num = i; if (i == N) { ansx = x; ansy = y; } if (flag == 0) { x++; if (x - ox == d) { ox = x; flag = 1; } } else if (flag == 1) { y--; if (oy - y == d) { oy = y; d++; flag = 2; } } else if (flag == 2) { x--; if (ox - x == d) { ox = x; flag = 3; } } else if (flag == 3) { y++; if (y - oy == d) { oy = y; d++; flag = 0; } } } for (int i = 1001; i >= 0; i--) { for (int j = 0; j < 1002; j++) { if (h[j][i].num != 0) { h[j][i].prime = max(max(h[j - 1][i + 1].prime, h[j][i + 1].prime), h[j + 1][i + 1].prime); if (h[j - 1][i + 1].last == 0 && h[j][i + 1].last == 0 && h[j + 1][i + 1].last == 0) { if (prime_judge(h[j][i].num)) h[j][i].last = h[j][i].num; } else { int k; for (k = -1; k <= 1; k++) { if (h[j + k][i + 1].prime == h[j][i].prime) { h[j][i].last = h[j + k][i + 1].last; break; } } for (int l = -1; l <= 1; l++) { if (h[j + k][i + 1].prime == h[j + l][i + 1].prime) { h[j][i].last = max(h[j][i].last, h[j + l][i + 1].last); } } } if (prime_judge(h[j][i].num)) h[j][i].prime++; } } } if (h[ansx][ansy].prime == 0) cout << "0 0" << endl; else cout << h[ansx][ansy].prime << " " << h[ansx][ansy].last << endl; } int main() { while (1) { cin >> M >> N; if (M == 0 && N == 0) break; solve(); } return 0; }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13]]
1
839
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> typedef long long ll; typedef unsigned long long ull; using namespace std; #define pb push_back int dy[] = {0, -1, 0, 1}; int dx[] = {-1, 0, 1, 0}; struct aaa { int maxi; int prime; }; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) int hole[1000][1000], m, n; bool isp[1000001]; aaa memo[1003][1000]; aaa search(int y, int x) { if (memo[y][x].maxi >= 0) return memo[y][x]; if ((y > 999) || hole[y][x] > m) { aaa ret; memo[y][x].maxi = memo[y][x].prime = 0; return memo[y][x]; } aaa ret; ret.maxi = ret.prime = 0; for (int i = x - 1; i < x + 2; i++) { if (i < 0 || i > 999) continue; aaa b = search(y + 1, i); if (b.maxi > ret.maxi || (b.maxi == ret.maxi && b.prime > ret.maxi)) ret = b; } if (ret.prime == 0 && isp[hole[y][x]]) ret.prime = hole[y][x]; ret.maxi += isp[hole[y][x]]; return memo[y][x] = ret; } int main() { int d = 2; int x = 0; int y = 0; RFOR(i, 1, 1000001) { hole[y][x] = i; if (x + dx[d] < 0 || x + dx[d] > 999 || y + dy[d] < 0 || y + dy[d] > 999 || hole[y + dy[d]][x + dx[d]] > 0) { d = (d + 1) % 4; } x += dx[d]; y += dy[d]; } memset(isp, true, sizeof(isp)); isp[1] = false; FOR(i, 2, 10000) { if (isp[i]) { for (int j = i * 2; j < 1000001; j += i) isp[j] = false; } } while (1) { scanf("%d%d", &m, &n); if (m == 0 && n == 0) break; aaa ans; REP(i, 1003) REP(j, 1000) memo[i][j].maxi = -1; REP(i, 1000) { REP(j, 1000) { if (hole[i][j] == n) ans = search(i, j); } } printf("%d %d\n", ans.maxi, ans.prime); } return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> typedef long long ll; typedef unsigned long long ull; using namespace std; #define pb push_back int dy[] = {0, -1, 0, 1}; int dx[] = {-1, 0, 1, 0}; struct aaa { int maxi; int prime; }; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) int hole[1000][1000], m, n; bool isp[1000001]; aaa memo[1003][1000]; aaa search(int y, int x) { if (memo[y][x].maxi >= 0) return memo[y][x]; if ((y > 999) || hole[y][x] > m) { aaa ret; memo[y][x].maxi = memo[y][x].prime = 0; return memo[y][x]; } aaa ret; ret.maxi = ret.prime = 0; for (int i = x - 1; i < x + 2; i++) { if (i < 0 || i > 999) continue; aaa b = search(y + 1, i); if (b.maxi > ret.maxi || (b.maxi == ret.maxi && b.prime > ret.prime)) ret = b; } if (ret.prime == 0 && isp[hole[y][x]]) ret.prime = hole[y][x]; ret.maxi += isp[hole[y][x]]; return memo[y][x] = ret; } int main() { int d = 2; int x = 0; int y = 0; RFOR(i, 1, 1000001) { hole[y][x] = i; if (x + dx[d] < 0 || x + dx[d] > 999 || y + dy[d] < 0 || y + dy[d] > 999 || hole[y + dy[d]][x + dx[d]] > 0) { d = (d + 1) % 4; } x += dx[d]; y += dy[d]; } memset(isp, true, sizeof(isp)); isp[1] = false; FOR(i, 2, 10000) { if (isp[i]) { for (int j = i * 2; j < 1000001; j += i) isp[j] = false; } } while (1) { scanf("%d%d", &m, &n); if (m == 0 && n == 0) break; aaa ans; REP(i, 1003) REP(j, 1000) memo[i][j].maxi = -1; REP(i, 1000) { REP(j, 1000) { if (hole[i][j] == n) ans = search(i, j); } } printf("%d %d\n", ans.maxi, ans.prime); } return 0; }
[["-", 12, 23, 0, 16, 12, 16, 12, 118, 119, 120], ["+", 12, 23, 0, 16, 12, 16, 12, 118, 119, 120]]
1
689
#include "bits/stdc++.h" // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1189&lang=jp using namespace std; typedef long long ll; typedef pair<int, int> pii; #define INF 1 << 30 #define MAX_M 1000010 bool Prime[MAX_M]; int Caves[1002][1002]; pii dp[1002][1002]; void init_prime() { Prime[2] = true; for (int i = 3; i < MAX_M; i += 2) { bool f = false; for (int j = 2; j * j < i; j++) { if (i % j == 0) { f = true; break; } } if (!f) Prime[i] = true; } } int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; pair<int, int> init_Caves(int m, int n) { /* Caves initialize */ memset(Caves, 0, sizeof(Caves)); /* initial state */ int x, y; x = y = 500; /* next direction */ int k = 0; /* step valiables */ int steps = 1; int now_steps = 0; int count = 0; /* return valiables */ pii ret; for (int i = 1; i <= m; i++) { /* n locate & Caves */ if (i == n) ret = {y, x}; Caves[y][x] = i; /* next locate */ x += dx[k]; y += dy[k]; /* step */ now_steps++; if (steps == now_steps) { now_steps = 0; k = (k + 1) % 4; count = (count + 1) % 2; if (count == 0) { steps++; } } } return ret; } int dcx[3] = {-1, 0, 1}; int dcy[3] = {-1, -1, -1}; int main(void) { cin.tie(0); ios::sync_with_stdio(false); init_prime(); int m, n; while (cin >> m >> n, m | n) { memset(dp, -1, sizeof(dp)); auto s = init_Caves(m, n); if (Prime[n]) { dp[s.first][s.second] = {1, n}; } else { dp[s.first][s.second] = {0, 0}; } pii ans = {0, 0}; for (int y = 1000; y >= 1; y--) { for (int x = 1; x <= 1000; x++) { if (Caves[y][x] == 0) continue; if (dp[y][x].first == -1 && dp[y][x].second == -1) continue; if (dp[y][x].first > ans.first) { ans.first = dp[y][x].first; ans.second = dp[y][x].second; } else if (dp[y][x].first == ans.first && dp[y][x].second > ans.second) { ans.second = dp[y][x].second; } int cave_count = dp[y][x].first; int last_prime = dp[y][x].second; for (int k = 0; k < 3; k++) { int ny = y + dcy[k], nx = x + dcx[k]; int next_cave_count = cave_count + (Prime[Caves[ny][nx]] == true); int next_last_prime = Prime[Caves[ny][nx]] ? Caves[ny][nx] : last_prime; if (next_cave_count > dp[ny][nx].first) { dp[ny][nx].first = next_cave_count; dp[ny][nx].second = next_last_prime; continue; } if (next_cave_count == dp[ny][nx].first && next_last_prime > dp[ny][nx].second) { dp[ny][nx].second = next_last_prime; continue; } } } } cout << ans.first << " " << ans.second << endl; } }
#include "bits/stdc++.h" // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1189&lang=jp using namespace std; typedef long long ll; typedef pair<int, int> pii; #define INF 1 << 30 #define MAX_M 1000010 bool Prime[MAX_M]; int Caves[1002][1002]; pii dp[1002][1002]; void init_prime() { Prime[2] = true; for (int i = 3; i < MAX_M; i += 2) { bool f = false; for (int j = 2; j * j <= i; j++) { if (i % j == 0) { f = true; break; } } if (!f) Prime[i] = true; } } int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; pair<int, int> init_Caves(int m, int n) { /* Caves initialize */ memset(Caves, 0, sizeof(Caves)); /* initial state */ int x, y; x = y = 500; /* next direction */ int k = 0; /* step valiables */ int steps = 1; int now_steps = 0; int count = 0; /* return valiables */ pii ret; for (int i = 1; i <= m; i++) { /* n locate & Caves */ if (i == n) ret = {y, x}; Caves[y][x] = i; /* next locate */ x += dx[k]; y += dy[k]; /* step */ now_steps++; if (steps == now_steps) { now_steps = 0; k = (k + 1) % 4; count = (count + 1) % 2; if (count == 0) { steps++; } } } return ret; } int dcx[3] = {-1, 0, 1}; int dcy[3] = {-1, -1, -1}; int main(void) { cin.tie(0); ios::sync_with_stdio(false); init_prime(); int m, n; while (cin >> m >> n, m | n) { memset(dp, -1, sizeof(dp)); auto s = init_Caves(m, n); if (Prime[n]) { dp[s.first][s.second] = {1, n}; } else { dp[s.first][s.second] = {0, 0}; } pii ans = {0, 0}; for (int y = 1000; y >= 1; y--) { for (int x = 1; x <= 1000; x++) { if (Caves[y][x] == 0) continue; if (dp[y][x].first == -1 && dp[y][x].second == -1) continue; if (dp[y][x].first > ans.first) { ans.first = dp[y][x].first; ans.second = dp[y][x].second; } else if (dp[y][x].first == ans.first && dp[y][x].second > ans.second) { ans.second = dp[y][x].second; } int cave_count = dp[y][x].first; int last_prime = dp[y][x].second; for (int k = 0; k < 3; k++) { int ny = y + dcy[k], nx = x + dcx[k]; int next_cave_count = cave_count + (Prime[Caves[ny][nx]] == true); int next_last_prime = Prime[Caves[ny][nx]] ? Caves[ny][nx] : last_prime; if (next_cave_count > dp[ny][nx].first) { dp[ny][nx].first = next_cave_count; dp[ny][nx].second = next_last_prime; continue; } if (next_cave_count == dp[ny][nx].first && next_last_prime > dp[ny][nx].second) { dp[ny][nx].second = next_last_prime; continue; } } } } cout << ans.first << " " << ans.second << endl; } }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19]]
1
862
#include <complex> #include <cstring> #include <iostream> #include <vector> using namespace std; #define loop(i, a, b) for (int i = (a); i < int(b); i++) #define rep(i, n) loop(i, 0, n) #define all(c) (c).begin(), (c).end() typedef complex<int> P; const int MAX_M = 1.5e6 + 10; const int SIZE = 1100; int ps[MAX_M / 2]; bool isp[MAX_M]; int grid[SIZE][SIZE] = {}; P itop[MAX_M]; pair<int, int> dp[SIZE][SIZE]; int cnt_p = 0; void make_es() { fill(isp, isp + MAX_M, true); isp[0] = isp[1] = false; rep(i, MAX_M) { if (!isp[i]) continue; ps[cnt_p++] = i; for (int j = i << 1; j < MAX_M; j += i) { isp[j] = false; } } } int n, m; pair<int, int> rec(int y, int x) { if (dp[y][x].first != -1) return dp[y][x]; pair<int, int> best(0, 0); loop(i, -1, 2) { if (grid[y + 1][x + i] <= m) { auto t = rec(y + 1, x + i); best = max(t, best); } } pair<int, int> res(0, 0); if (isp[grid[y][x]]) { res.first = 1; res.second = grid[y][x]; } if (best.first != 0) { res.first += best.first; res.second = best.second; } return dp[y][x] = res; } int main() { make_es(); P p(SIZE / 2, SIZE / 2); P dir(0, 1); for (int i = 1; i < MAX_M; i++) { grid[p.real()][p.imag()] = i; itop[i] = p; p += dir; P q = p + dir * P(0, 1); if (grid[q.real()][q.imag()] == 0) { dir *= P(0, 1); } } while (cin >> m >> n, n + m) { auto t = make_pair(-1, -1); rep(i, SIZE) rep(j, SIZE) dp[i][j] = t; auto u = itop[n]; auto ans = rec(u.real(), u.imag()); cout << ans.first << " " << ans.second << endl; } }
#include <complex> #include <cstring> #include <iostream> #include <vector> using namespace std; #define loop(i, a, b) for (int i = (a); i < int(b); i++) #define rep(i, n) loop(i, 0, n) #define all(c) (c).begin(), (c).end() typedef complex<int> P; const int MAX_M = 1.5e6 + 10; const int SIZE = 1500; int ps[MAX_M / 2]; bool isp[MAX_M]; int grid[SIZE][SIZE] = {}; P itop[MAX_M]; pair<int, int> dp[SIZE][SIZE]; int cnt_p = 0; void make_es() { fill(isp, isp + MAX_M, true); isp[0] = isp[1] = false; rep(i, MAX_M) { if (!isp[i]) continue; ps[cnt_p++] = i; for (int j = i << 1; j < MAX_M; j += i) { isp[j] = false; } } } int n, m; pair<int, int> rec(int y, int x) { if (dp[y][x].first != -1) return dp[y][x]; pair<int, int> best(0, 0); loop(i, -1, 2) { if (grid[y + 1][x + i] <= m) { auto t = rec(y + 1, x + i); best = max(t, best); } } pair<int, int> res(0, 0); if (isp[grid[y][x]]) { res.first = 1; res.second = grid[y][x]; } if (best.first != 0) { res.first += best.first; res.second = best.second; } return dp[y][x] = res; } int main() { make_es(); P p(SIZE / 2, SIZE / 2); P dir(0, 1); for (int i = 1; i < MAX_M; i++) { grid[p.real()][p.imag()] = i; itop[i] = p; p += dir; P q = p + dir * P(0, 1); if (grid[q.real()][q.imag()] == 0) { dir *= P(0, 1); } } while (cin >> m >> n, n + m) { auto t = make_pair(-1, -1); rep(i, SIZE) rep(j, SIZE) dp[i][j] = t; auto u = itop[n]; auto ans = rec(u.real(), u.imag()); cout << ans.first << " " << ans.second << endl; } }
[["-", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13]]
1
590