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
def aop() i = 0 b = [] a =[] c = [] while c = gets b = c.split(',').map(&:to_f) puts b a[i] = b i += 1 end t = 0 for i in 1..a.length-2 x = ((a[0][0]-a[i][0])**2 + (a[0][1]-a[i][1])**2)**(1/2.0) y = ((a[0][0]-a[i+1][0])**2 + (a[0][1]-a[i+1][1])**2)**(1/2.0) z = ((a[i+1][0]-a[i][0])**2 + (a[i+1][1]-a[i][1])**2)**(1/2.0) w = (x + y + z)/2.0 s = (w*(w-x)*(w-y)*(w-z))**(1/2.0) t += s end puts t end aop()
def aop() i = 0 b = [] a =[] c = [] while c = gets b = c.split(',').map(&:to_f) a[i] = b i += 1 end t = 0 for i in 1..a.length-2 x = ((a[0][0]-a[i][0])**2 + (a[0][1]-a[i][1])**2)**(1/2.0) y = ((a[0][0]-a[i+1][0])**2 + (a[0][1]-a[i+1][1])**2)**(1/2.0) z = ((a[i+1][0]-a[i][0])**2 + (a[i+1][1]-a[i][1])**2)**(1/2.0) w = (x + y + z)/2.0 s = (w*(w-x)*(w-y)*(w-z))**(1/2.0) t += s end puts t end aop()
[["-", 8, 736, 0, 89, 8, 170, 0, 652, 735, 22], ["-", 0, 89, 8, 170, 0, 652, 3, 4, 0, 22]]
4
269
point = Array.new def solve(point) source = point[0] point.delete_at(0) # point.sort{|p1,p2| p1[1] <=> p2[1]} sum = 0 while point.size > 1 do p1 = point[0] p2 = point[1] a = getDistance(source, p1) b = getDistance(source,p2) c = getDistance(p1, p2) z = (a + b + c) / 2 sum += getArea(a, b, c, z) #不要になった座標は削除する point.delete_at(0) end sum end def getDistance(p1, p2) Math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2) end def getArea(l1, l2, l3, z) Math.sqrt(z * (z - l1) * (z - l2) * (z - l3)) end while input = $stdin.gets do point << input.chomp.split(",").map(&:to_i) end printf "%.6f\n", solve(point)
point = Array.new def solve(point) source = point[0] point.delete_at(0) # point.sort{|p1,p2| p1[1] <=> p2[1]} sum = 0 while point.size > 1 do p1 = point[0] p2 = point[1] a = getDistance(source, p1) b = getDistance(source,p2) c = getDistance(p1, p2) z = (a + b + c) / 2 sum += getArea(a, b, c, z) #不要になった座標は削除する point.delete_at(0) end sum end def getDistance(p1, p2) Math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2) end def getArea(l1, l2, l3, z) Math.sqrt(z * (z - l1) * (z - l2) * (z - l3)) end while input = $stdin.gets do point << input.chomp.split(",").map(&:to_f) end printf "%.6f\n", solve(point)
[["-", 0, 738, 12, 652, 3, 4, 0, 752, 0, 753], ["+", 0, 738, 12, 652, 3, 4, 0, 752, 0, 753]]
4
214
import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Collections.*; import java.io.*; import java.util.*; public class Main { int INF = 1 << 28; // long INF = 1L << 62; double EPS = 1e-10; void run() { Scanner sc = new Scanner(System.in); for (;;) { int n = sc.nextInt(); if (n == 0) break; int m = sc.nextInt() - 1, c = sc.nextInt() - 1, d = sc.nextInt(); int g[] = new int[n]; for (int i = 0; i < n; i++) g[i] = i; HashSet<P> s = new HashSet<P>(); for (int i = 0; i < d; i++) { int l = Integer.valueOf( (new StringBuilder(sc.next()).reverse().toString()), 2) << 1; for (int j = 1; j <= n - 1; j++) { if (((l >> j) & 1) == 1) { swap(g, j, j - 1); } else if (((l >> (j - 1)) & 1) == 0 && ((l >> (j + 1)) & 1) == 0) { s.add(new P(g[j], g[j - 1], i, j)); } } } // debug(g); if (g[c] == m) System.out.println(1); else { int t = g[c]; if (t < m) { int tmp = t; t = m; m = tmp; } boolean f = false; for (P p : s) { if (p.v1 == m && p.v2 == t) { System.out.println((p.x + 1) + " " + p.y); f = true; } } if (!f) System.out.println(0); } } } void swap(int[] s, int a, int b) { int tmp = s[a]; s[a] = s[b]; s[b] = tmp; } class P { int v1, v2; int x, y; P(int v1, int v2, int x, int y) { this.v1 = min(v1, v2); this.v2 = max(v1, v2); this.x = x; this.y = y; } public boolean equals(Object o) { if (o instanceof P) { return v1 == ((P)o).v1 && v2 == ((P)o).v2; } return false; } public int hashCode() { return v1 * 100 + v2; } } void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } public static void main(String[] args) { new Main().run(); } }
import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Collections.*; import java.io.*; import java.util.*; public class Main { int INF = 1 << 28; // long INF = 1L << 62; double EPS = 1e-10; void run() { Scanner sc = new Scanner(System.in); for (;;) { int n = sc.nextInt(); if (n == 0) break; int m = sc.nextInt() - 1, c = sc.nextInt() - 1, d = sc.nextInt(); int g[] = new int[n]; for (int i = 0; i < n; i++) g[i] = i; HashSet<P> s = new HashSet<P>(); for (int i = 0; i < d; i++) { int l = Integer.valueOf( (new StringBuilder(sc.next()).reverse().toString()), 2) << 1; for (int j = 1; j <= n - 1; j++) { if (((l >> j) & 1) == 1) { swap(g, j, j - 1); } else if (((l >> (j - 1)) & 1) == 0 && ((l >> (j + 1)) & 1) == 0) { s.add(new P(g[j], g[j - 1], i, j)); // debug(g[j], g[j-1], i, //j); } // debug(g); } } // for(P p: s) debug(p.v1, p.v2, p.x, p.y); // debug(g); if (g[c] == m) System.out.println(0); else { int t = g[c]; if (t < m) { int tmp = t; t = m; m = tmp; } boolean f = false; for (P p : s) { if (p.v1 == m && p.v2 == t) { System.out.println((p.x + 1) + " " + p.y); f = true; } } if (!f) System.out.println(1); } } } void swap(int[] s, int a, int b) { int tmp = s[a]; s[a] = s[b]; s[b] = tmp; } class P { int v1, v2; int x, y; P(int v1, int v2, int x, int y) { this.v1 = min(v1, v2); this.v2 = max(v1, v2); this.x = x; this.y = y; } public boolean equals(Object o) { if (o instanceof P) { return v1 == ((P)o).v1 && v2 == ((P)o).v2; } return false; } public int hashCode() { return v1 * 100 + v2; } } void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } public static void main(String[] args) { new Main().run(); } }
[["-", 0, 57, 64, 1, 0, 492, 3, 4, 0, 499], ["+", 0, 57, 64, 1, 0, 492, 3, 4, 0, 499]]
3
661
#include <algorithm> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; #define REP(i, j) for (int i = 0; i < (int)(j); ++i) #define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i) #define P pair<int, int> #define SORT(v) sort((v).begin(), (v).end()) #define REVERSE(v) reverse((v).begin(), (v).end()) int N, S, G, D; bool check(vector<string> &v) { int now = S; REP(i, v.size()) { if (now != 0 && v[i][now - 1] == '1') --now; else if (now != N - 1 && v[i][now] == '1') ++now; } return now == G; } bool solve(vector<string> &v) { REP(i, v.size()) { REP(j, v[i].size()) { if (v[i][j] == '1') continue; if (j != 0 && v[i][j - 1] == '1') continue; if (j != N - 1 && v[i][j] == '1') continue; v[i][j] = '1'; if (check(v)) { cout << i + 1 << " " << j + 1 << endl; return 1; } v[i][j] = '0'; } } return false; } int main() { while (cin >> N && N) { cin >> S >> G >> D; --S; --G; vector<string> v(D); REP(i, D) cin >> v[i]; if (check(v)) cout << 0 << endl; else if (!solve(v)) cout << 1 << endl; } return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; #define REP(i, j) for (int i = 0; i < (int)(j); ++i) #define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i) #define P pair<int, int> #define SORT(v) sort((v).begin(), (v).end()) #define REVERSE(v) reverse((v).begin(), (v).end()) int N, S, G, D; bool check(vector<string> &v) { // cout <<"==========" <<endl; // REP(i, v.size()) cout <<v[i] <<endl; int now = S; REP(i, v.size()) { if (now != 0 && v[i][now - 1] == '1') --now; else if (now != N - 1 && v[i][now] == '1') ++now; } return now == G; } bool solve(vector<string> &v) { REP(i, v.size()) { REP(j, v[i].size()) { if (v[i][j] == '1') continue; if (j != 0 && v[i][j - 1] == '1') continue; if (j + 1 != N - 1 && v[i][j + 1] == '1') continue; v[i][j] = '1'; if (check(v)) { cout << i + 1 << " " << j + 1 << endl; return 1; } v[i][j] = '0'; } } return false; } int main() { while (cin >> N && N) { cin >> S >> G >> D; --S; --G; vector<string> v(D); REP(i, D) cin >> v[i]; if (check(v)) cout << 0 << endl; else if (!solve(v)) cout << 1 << endl; } return 0; }
[["+", 15, 339, 51, 16, 31, 16, 31, 16, 17, 72], ["+", 15, 339, 51, 16, 31, 16, 31, 16, 12, 13], ["+", 12, 16, 31, 69, 341, 342, 0, 16, 17, 72], ["+", 12, 16, 31, 69, 341, 342, 0, 16, 12, 13]]
1
406
#include <cstdio> #include <iostream> #include <string> #include <utility> using namespace std; int main() { while (1) { int n, m, atari, d; int a[10]; // amida string s[30]; scanf(" %d", &n); if (n == 0) break; for (int i = 0; i < n; ++i) a[i] = i + 1; scanf(" %d %d %d", &m, &atari, &d); for (int i = 0; i < d; ++i) cin >> s[i]; for (int i = 0; i < d; ++i) { for (int j = 0; j < s[i].size(); ++j) { if (s[i][j] == '1') swap(a[j], a[j + 1]); } } if (atari - 1 == a[m]) { // 線を引く必要がない printf("0\n"); continue; } bool flag = true; //横線を1つだけ足す for (int i = 0; i < d; ++i) { for (int j = 0; j < s[i].size(); ++j) { if (s[i][j] == '1') continue; int l = j - 1, r = j + 1; if (l >= 0 && s[i][l] == '1') continue; if (r < n && s[i][r] == '1') continue; for (int k = 0; k < n; ++k) a[k] = k + 1; string t[30]; for (int x = 0; x < d; ++x) t[x] = s[x]; t[i][j] = '1'; /* printf("\n"); printf("%d %d\n",i+1 ,j+1); for(int x=0; x<d; ++x) cout<<t[x]<<endl; */ for (int x = 0; x < d; ++x) { for (int y = 0; y < t[x].size(); ++y) { if (t[x][y] == '1') swap(a[y], a[y + 1]); } } /* for(int k=0; k<n; ++k) printf("%d ", a[k]); printf("\n"); */ if (m == a[atari - 1]) { // 線を引いたらうまくいった printf("%d %d\n", i + 1, j + 1); flag = false; } if (!flag) break; } if (!flag) break; } if (flag) printf("1\n"); } }
#include <cstdio> #include <iostream> #include <string> #include <utility> using namespace std; int main() { while (1) { int n, m, atari, d; int a[10]; // amida string s[30]; scanf(" %d", &n); if (n == 0) break; for (int i = 0; i < n; ++i) a[i] = i + 1; scanf(" %d %d %d", &m, &atari, &d); for (int i = 0; i < d; ++i) cin >> s[i]; for (int i = 0; i < d; ++i) { for (int j = 0; j < s[i].size(); ++j) { if (s[i][j] == '1') swap(a[j], a[j + 1]); } } if (m == a[atari - 1]) { // 線を引く必要がない printf("0\n"); continue; } bool flag = true; //横線を1つだけ足す for (int i = 0; i < d; ++i) { for (int j = 0; j < s[i].size(); ++j) { if (s[i][j] == '1') continue; int l = j - 1, r = j + 1; if (l >= 0 && s[i][l] == '1') continue; if (r < n && s[i][r] == '1') continue; for (int k = 0; k < n; ++k) a[k] = k + 1; string t[30]; for (int x = 0; x < d; ++x) t[x] = s[x]; t[i][j] = '1'; /* printf("\n"); printf("%d %d\n",i+1 ,j+1); for(int x=0; x<d; ++x) cout<<t[x]<<endl; printf("\n"); */ for (int x = 0; x < d; ++x) { for (int y = 0; y < t[x].size(); ++y) { if (t[x][y] == '1') swap(a[y], a[y + 1]); } } /* for(int k=0; k<n; ++k) printf("%d ", a[k]); printf("\n"); */ if (m == a[atari - 1]) { // 線を引いたらうまくいった printf("%d %d\n", i + 1, j + 1); flag = false; } if (!flag) break; } if (!flag) break; } if (flag) printf("1\n"); } }
[["-", 0, 57, 15, 339, 51, 16, 31, 16, 31, 22], ["-", 0, 57, 15, 339, 51, 16, 31, 16, 17, 33], ["-", 0, 57, 15, 339, 51, 16, 31, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22], ["-", 15, 339, 51, 16, 12, 69, 341, 342, 0, 22], ["+", 51, 16, 12, 69, 341, 342, 0, 16, 31, 22], ["+", 51, 16, 12, 69, 341, 342, 0, 16, 17, 33], ["+", 51, 16, 12, 69, 341, 342, 0, 16, 12, 13]]
1
525
#include <iostream> #include <string> #include <vector> using namespace std; int n, m, atari, d; bool check(vector<int> line, vector<vector<int>> sideline, int col = -1, int row = -1) { int i, j; if (col != -1 && row != -1) sideline[col][row] = 1; for (i = 0; i < d; i++) { for (j = 0; j < n - 1; j++) { if (sideline[i][j] == 1) { swap(line[j], line[j + 1]); } } } if (line[atari - 1] == m) { return true; } return false; } int main() { int i, j; bool flg; string str; while (true) { cin >> n; if (n == 0) break; cin >> m >> atari >> d; vector<int> line(n); for (i = 0; i < n; i++) line[i] = i + 1; vector<vector<int>> sideline(d, vector<int>(n - 1)); for (i = 0; i < d; i++) { cin >> str; for (j = 0; j < n - 1; j++) { sideline[i][j] = str[j] - '0'; } } if (check(line, sideline) == true) { cout << 0 << endl; continue; } flg = false; for (i = 0; i < d; i++) { for (j = 0; j < n - 1; j++) { if (j == 0) { if (sideline[i][j + 1] != 1) { flg = check(line, sideline, i, j); } } else if (0 < j && j < n - 2) { if ((sideline[i][j + 1] != 1) && (sideline[i][j - 1] != 1)) { flg = check(line, sideline, i, j); } } else if (j == n - 2) { if (sideline[i][j - 1] != 1) { flg = check(line, sideline, i, j); } } if (flg) break; } if (flg) break; } if (flg) { cout << i + 1 << " " << j + 1 << endl; } else { cout << "0" << endl; } } return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; int n, m, atari, d; bool check(vector<int> line, vector<vector<int>> sideline, int col = -1, int row = -1) { int i, j; if (col != -1 && row != -1) sideline[col][row] = 1; for (i = 0; i < d; i++) { for (j = 0; j < n - 1; j++) { if (sideline[i][j] == 1) { swap(line[j], line[j + 1]); } } } if (line[atari - 1] == m) { return true; } return false; } int main() { int i, j; bool flg; string str; while (true) { cin >> n; if (n == 0) break; cin >> m >> atari >> d; vector<int> line(n); for (i = 0; i < n; i++) line[i] = i + 1; vector<vector<int>> sideline(d, vector<int>(n - 1)); for (i = 0; i < d; i++) { cin >> str; for (j = 0; j < n - 1; j++) { sideline[i][j] = str[j] - '0'; } } if (check(line, sideline) == true) { cout << 0 << endl; continue; } flg = false; for (i = 0; i < d; i++) { for (j = 0; j < n - 1; j++) { if (j == 0) { if (sideline[i][j + 1] != 1) { flg = check(line, sideline, i, j); } } else if (0 < j && j < n - 2) { if ((sideline[i][j + 1] != 1) && (sideline[i][j - 1] != 1)) { flg = check(line, sideline, i, j); } } else if (j == n - 2) { if (sideline[i][j - 1] != 1) { flg = check(line, sideline, i, j); } } if (flg) break; } if (flg) break; } if (flg) { cout << i + 1 << " " << j + 1 << endl; } else { cout << "1" << endl; } } return 0; }
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
545
// Drawing Lots ? #include <bits/stdc++.h> using namespace std; vector<int> mv(string s) { vector<int> tmp; for (int i = 0; i < s.length(); i++) tmp.push_back(s[i]); return tmp; } int main() { int n, m, g, d; vector<vector<int>> input; while (cin >> n) { if (n == 0) break; cin >> m >> g >> d; input.clear(); for (int i = 0; i < d; i++) { string s; cin >> s; input.push_back(mv(s)); } vector<int> content; for (int i = 0; i < d; i++) { content.push_back(i + 1); } for (int i = 0; i < d; i++) { for (int j = 0; j < input[i].size(); j++) { if (input[i][j] == '1') swap(content[j], content[j + 1]); } } if (m == content[g]) { cout << "0" << endl; continue; } int gg = content[g - 1]; content.clear(); for (int i = 0; i < d; i++) { content.push_back(i + 1); } for (int i = 0; i < d; i++) { for (int j = 0; j < n - 1; j++) { if (input[i][j] == '1') swap(content[j], content[j + 1]); else if ((0 <= j - 1 ? input[i][j - 1] == '0' : true) && (j + 1 < n - 1 ? input[i][j + 1] == '0' : true)) { if ((content[j] == m && content[j + 1] == gg) || (content[j] == gg && content[j + 1] == m)) { cout << i + 1 << " " << j + 1 << endl; goto CONTINUE; } } } } cout << "1" << endl; CONTINUE:; } return 0; }
// Drawing Lots ? #include <bits/stdc++.h> using namespace std; vector<int> mv(string s) { vector<int> tmp; for (int i = 0; i < s.length(); i++) tmp.push_back(s[i]); return tmp; } int main() { int n, m, g, d; vector<vector<int>> input; while (cin >> n) { if (n == 0) break; cin >> m >> g >> d; input.clear(); for (int i = 0; i < d; i++) { string s; cin >> s; input.push_back(mv(s)); } vector<int> content; for (int i = 0; i < d; i++) { content.push_back(i + 1); } for (int i = 0; i < d; i++) { for (int j = 0; j < input[i].size(); j++) { if (input[i][j] == '1') swap(content[j], content[j + 1]); } } if (m == content[g - 1]) { cout << "0" << endl; continue; } int gg = content[g - 1]; content.clear(); for (int i = 0; i < d; i++) { content.push_back(i + 1); } for (int i = 0; i < d; i++) { for (int j = 0; j < n - 1; j++) { if (input[i][j] == '1') swap(content[j], content[j + 1]); else if ((0 <= j - 1 ? input[i][j - 1] == '0' : true) && (j + 1 < n - 1 ? input[i][j + 1] == '0' : true)) { if ((content[j] == m && content[j + 1] == gg) || (content[j] == gg && content[j + 1] == m)) { cout << i + 1 << " " << j + 1 << endl; goto CONTINUE; } } } } cout << "1" << endl; CONTINUE:; } return 0; }
[["+", 51, 16, 12, 69, 341, 342, 0, 16, 17, 33], ["+", 51, 16, 12, 69, 341, 342, 0, 16, 12, 13]]
1
494
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int, int> pii; #define REP(i, n) for (ll i = 0; i < n; ++i) #define REPR(i, n) for (ll i = 1; i < n; ++i) #define FOR(i, a, b) for (ll i = a; i < b; ++i) #define DEBUG(x) cout << #x << ": " << x << endl #define DEBUG_VEC(v) \ cout << #v << ":"; \ REP(i, v.size()) cout << " " << v[i]; \ cout << endl #define ALL(a) (a).begin(), (a).end() #define MOD (ll)(1e9 + 7) #define ADD(a, b) a = ((a) + (b)) % MOD #define FIX(a) ((a) % MOD + MOD) % MOD #define INF 1000000000 typedef long double Real; #define EPS 1e-11 #define EQ(x) (-EPS < x && x < EPS) typedef complex<Real> P; Real dot(P a, P b) { return real(conj(a) * b); } Real cross(P a, P b) { return imag(conj(a) * b); } int n, m, g, d; int mp[30][9]; bool calc() { int p = m; REP(i, d) { if (p != 0 && mp[i][p - 1] == 1) { --p; } else if (p != n - 1 && mp[i][p] == 1) { ++p; } } return p == g; } int main() { while (true) { cin >> n; if (n == 0) break; cin >> m >> g >> d; --m; --g; REP(i, d) { int x; cin >> x; REPR(j, n) { mp[i][n - 1 - j] = x % 10; x /= 10; } } if (calc()) { cout << 0 << endl; continue; } bool flag = false; REP(i, d) REP(j, n - 1) { if (flag) break; if (mp[i][j] == 1) continue; if (j != 0 && mp[i][j - 1] == 1) continue; if (j != n - 2 && mp[i][j + 1] == 1) continue; mp[i][j] = 1; if (calc()) { cout << (i + 1) << " " << (j + 1) << endl; flag = true; } mp[i][j] = 0; } if (!flag) cout << -1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int, int> pii; #define REP(i, n) for (ll i = 0; i < n; ++i) #define REPR(i, n) for (ll i = 1; i < n; ++i) #define FOR(i, a, b) for (ll i = a; i < b; ++i) #define DEBUG(x) cout << #x << ": " << x << endl #define DEBUG_VEC(v) \ cout << #v << ":"; \ REP(i, v.size()) cout << " " << v[i]; \ cout << endl #define ALL(a) (a).begin(), (a).end() #define MOD (ll)(1e9 + 7) #define ADD(a, b) a = ((a) + (b)) % MOD #define FIX(a) ((a) % MOD + MOD) % MOD #define INF 1000000000 typedef long double Real; #define EPS 1e-11 #define EQ(x) (-EPS < x && x < EPS) typedef complex<Real> P; Real dot(P a, P b) { return real(conj(a) * b); } Real cross(P a, P b) { return imag(conj(a) * b); } int n, m, g, d; int mp[30][9]; bool calc() { int p = m; REP(i, d) { if (p != 0 && mp[i][p - 1] == 1) { --p; } else if (p != n - 1 && mp[i][p] == 1) { ++p; } } return p == g; } int main() { while (true) { cin >> n; if (n == 0) break; cin >> m >> g >> d; --m; --g; REP(i, d) { int x; cin >> x; REPR(j, n) { mp[i][n - 1 - j] = x % 10; x /= 10; } } if (calc()) { cout << 0 << endl; continue; } bool flag = false; REP(i, d) REP(j, n - 1) { if (flag) break; if (mp[i][j] == 1) continue; if (j != 0 && mp[i][j - 1] == 1) continue; if (j != n - 2 && mp[i][j + 1] == 1) continue; mp[i][j] = 1; if (calc()) { cout << (i + 1) << " " << (j + 1) << endl; flag = true; } mp[i][j] = 0; } if (!flag) cout << 1 << endl; } return 0; }
[["-", 0, 57, 64, 1, 0, 16, 31, 16, 12, 13], ["+", 0, 57, 64, 1, 0, 16, 31, 16, 12, 13]]
1
501
#include <bits/stdc++.h> #define FOR(v, a, b) for (int v = (a); v < (b); ++v) #define FORE(v, a, b) for (int v = (a); v <= (b); ++v) #define REP(v, n) FOR(v, 0, n) #define UNWORDS(vec, str) \ for (int i = 0; i < (vec).size(); ++i) \ cout << (i ? str : "") << (vec)[i] #define UNLINES(vec) \ for (int i = 0; i < (vec).size(); ++i) \ cout << (vec)[i] << endl #define RS resize #define CLR clear #define PB push_back #define ALL(x) (x).begin(), (x).end() using namespace std; vector<vector<char>> amida; int n, m, t, d; int check() { int cur = m; REP(i, d) { if (amida[i][cur - 1] == 1) { --cur; } else if (amida[i][cur] == 1) { ++cur; } } return cur == t; } int main() { cin.tie(0); ios::sync_with_stdio(false); while (cin >> n, n) { cin >> m >> t >> d; amida.CLR(); amida.RS(d); REP(i, d) { amida[i].RS(n + 1); FORE(j, 1, n - 1) { cin >> amida[i][j]; amida[i][j] -= '0'; } } if (check()) { cout << 0 << endl; } else { bool suc = false; REP(i, d) { FORE(j, 1, n - 1) { if (amida[i][j] == 0 && amida[i][j - 1] == 0 && amida[i][j + 1] == 0) { amida[i][j] = 1; if (check()) { cout << i + 1 << " " << j << endl; suc = true; break; } amida[i][j] = 0; } } } if (!suc) cout << 1 << endl; } } return 0; }
#include <bits/stdc++.h> #define FOR(v, a, b) for (int v = (a); v < (b); ++v) #define FORE(v, a, b) for (int v = (a); v <= (b); ++v) #define REP(v, n) FOR(v, 0, n) #define UNWORDS(vec, str) \ for (int i = 0; i < (vec).size(); ++i) \ cout << (i ? str : "") << (vec)[i] #define UNLINES(vec) \ for (int i = 0; i < (vec).size(); ++i) \ cout << (vec)[i] << endl #define RS resize #define CLR clear #define PB push_back #define ALL(x) (x).begin(), (x).end() using namespace std; vector<vector<char>> amida; int n, m, t, d; int check() { int cur = m; REP(i, d) { if (amida[i][cur - 1] == 1) { --cur; } else if (amida[i][cur] == 1) { ++cur; } } return cur == t; } int main() { cin.tie(0); ios::sync_with_stdio(false); while (cin >> n, n) { cin >> m >> t >> d; amida.CLR(); amida.RS(d); REP(i, d) { amida[i].RS(n + 1); FORE(j, 1, n - 1) { cin >> amida[i][j]; amida[i][j] -= '0'; } } if (check()) { cout << 0 << endl; } else { bool suc = false; REP(i, d) { FORE(j, 1, n - 1) { if (amida[i][j] == 0 && amida[i][j - 1] == 0 && amida[i][j + 1] == 0) { amida[i][j] = 1; if (check()) { cout << i + 1 << " " << j << endl; suc = true; break; } amida[i][j] = 0; } } if (suc) break; } if (!suc) cout << 1 << endl; } } return 0; }
[["+", 0, 9, 0, 14, 8, 9, 0, 57, 0, 121], ["+", 0, 14, 8, 9, 0, 57, 15, 339, 0, 24], ["+", 0, 14, 8, 9, 0, 57, 15, 339, 51, 22], ["+", 0, 14, 8, 9, 0, 57, 15, 339, 0, 25], ["+", 0, 14, 8, 9, 0, 57, 64, 93, 0, 94], ["+", 0, 14, 8, 9, 0, 57, 64, 93, 0, 35]]
1
393
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) #define all(a) (a).begin(), (a).end() #define dump(o) \ { cerr << #o << " " << o << endl; } #define dumpc(o) \ { \ cerr << #o; \ for (auto &e : (o)) \ cerr << " " << e; \ cerr << endl; \ } #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL const int MOD = 1e9 + 7; signed main() { bool a[35][15]; for (int n; cin >> n && n;) { fill(a[0], a[35], false); int m, g, d; cin >> m >> g >> d; rep(h, 0, d) rep(i, 1, n) { char c; cin >> c; a[h][i] = c - '0'; } // rep(h, 0, d)dumpc(a[h]); { int cur = m; rep(j, 0, d) { if (a[j][m - 1]) cur--; else if (a[j][m]) cur++; } if (cur == g) { cout << 0 << endl; continue; } } bool flag = true; rep(h, 0, d) rep(i, 1, n) { if (a[h][i - 1] || a[h][i] || a[h][i + 1]) continue; a[h][i] = true; // cerr << h << " "; dumpc(a[h]); int cur = m; rep(j, 0, d) { if (a[j][cur - 1]) cur--; else if (a[j][cur]) cur++; // dump(cur); } if (cur == g) { cout << (h + 1) << " " << i << endl; i = n - 1; h = d; flag = false; break; } a[h][i] = false; } if (flag) cout << 1 << endl; } return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) #define all(a) (a).begin(), (a).end() #define dump(o) \ { cerr << #o << " " << o << endl; } #define dumpc(o) \ { \ cerr << #o; \ for (auto &e : (o)) \ cerr << " " << e; \ cerr << endl; \ } #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL const int MOD = 1e9 + 7; signed main() { bool a[35][15]; for (int n; cin >> n && n;) { fill(a[0], a[35], false); int m, g, d; cin >> m >> g >> d; rep(h, 0, d) rep(i, 1, n) { char c; cin >> c; a[h][i] = c - '0'; } // rep(h, 0, d)dumpc(a[h]); { int cur = m; rep(j, 0, d) { if (a[j][cur - 1]) cur--; else if (a[j][cur]) cur++; } if (cur == g) { cout << 0 << endl; continue; } } bool flag = true; rep(h, 0, d) rep(i, 1, n) { if (a[h][i - 1] || a[h][i] || a[h][i + 1]) continue; a[h][i] = true; // cerr << h << " "; dumpc(a[h]); int cur = m; rep(j, 0, d) { if (a[j][cur - 1]) cur--; else if (a[j][cur]) cur++; // dump(cur); } if (cur == g) { cout << (h + 1) << " " << i << endl; i = n - 1; h = d; flag = false; break; } a[h][i] = false; } if (flag) cout << 1 << endl; } return 0; }
[["-", 15, 339, 51, 69, 341, 342, 0, 16, 31, 22], ["+", 15, 339, 51, 69, 341, 342, 0, 16, 31, 22], ["-", 0, 57, 15, 339, 51, 69, 341, 342, 0, 22], ["+", 0, 57, 15, 339, 51, 69, 341, 342, 0, 22]]
1
466
#include <algorithm> #include <cstdio> #include <iostream> #include <queue> #include <string> #include <unordered_map> #include <vector> using namespace std; int k[10]; string s[30]; signed main() { int a; while (cin >> a, a) { for (int i = 0; i < a; i++) k[i] = i; int b, c, d; cin >> b >> c >> d; for (int e = 0; e < d; e++) { string f; cin >> f; s[e] = "0" + f + "0"; for (int g = 0; g < a - 1; g++) { if (f[g] == '1') { swap(k[g], k[g + 1]); } } } b--; c--; int atari = k[c]; if (b == atari) { puts("0"); continue; } for (int i = 0; i < a; i++) k[i] = i; for (int i = 0; i < d; i++) { for (int j = 1; j < a; j++) { if (s[i][j] == '1') { swap(k[j - 1], k[j]); } else { if (s[i][j - 1] == '0' && s[i][j + 1] == '0') { if ((k[j - 1] == atari && k[j] == b) || (k[j] == atari && k[j - 1] == b)) { cout << i + 1 << " " << j << endl; goto l; } } } } } puts("-1"); l:; } }
#include <algorithm> #include <cstdio> #include <iostream> #include <queue> #include <string> #include <unordered_map> #include <vector> using namespace std; int k[10]; string s[30]; signed main() { int a; while (cin >> a, a) { for (int i = 0; i < a; i++) k[i] = i; int b, c, d; cin >> b >> c >> d; for (int e = 0; e < d; e++) { string f; cin >> f; s[e] = "0" + f + "0"; for (int g = 0; g < a - 1; g++) { if (f[g] == '1') { swap(k[g], k[g + 1]); } } } b--; c--; int atari = k[c]; if (b == atari) { puts("0"); continue; } for (int i = 0; i < a; i++) k[i] = i; for (int i = 0; i < d; i++) { for (int j = 1; j < a; j++) { if (s[i][j] == '1') { swap(k[j - 1], k[j]); } else { if (s[i][j - 1] == '0' && s[i][j + 1] == '0') { if ((k[j - 1] == atari && k[j] == b) || (k[j] == atari && k[j - 1] == b)) { cout << i + 1 << " " << j << endl; goto l; } } } } } puts("1"); l:; } }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
386
#include <algorithm> #include <cmath> #include <iostream> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { int n, m, g, d; char b; END: while (cin >> n >> m >> g >> d, n) { int hl[d][n - 1], rtr[d + 1], tr[d]; m--, g--; rtr[d] = g; rep(i, d) rep(j, n - 1) cin >> b, hl[i][j] = b - '0'; rep(i, d) { rtr[d - i - 1] = rtr[d - i]; if (rtr[d - i - 1] < n - 1 && hl[d - i - 1][rtr[d - i - 1]]) rtr[d - i - 1]++; else if (rtr[d - i - 1] > 0 && hl[d - i - 1][rtr[d - i - 1] - 1]) rtr[d - i - 1]--; } if (rtr[0] == m) { cout << 1 << endl; continue; } rep(i, d) { tr[i] = i > 0 ? tr[i - 1] : m; if (tr[i] < n - 1 && hl[i][tr[i]]) tr[i]++; else if (tr[i] > 0 && hl[i][tr[i] - 1]) tr[i]--; else if (abs(tr[i] - rtr[i]) == 1 && (rtr[i] == n - 1 || !hl[i][rtr[i]]) && (rtr[i] == 0 || !hl[i][rtr[i] - 1])) { cout << i + 1 << " " << min(tr[i], rtr[i]) + 1 << endl; goto END; } } cout << 0 << endl; } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { int n, m, g, d; char b; END: while (cin >> n >> m >> g >> d, n) { int hl[d][n - 1], rtr[d + 1], tr[d]; m--, g--; rtr[d] = g; rep(i, d) rep(j, n - 1) cin >> b, hl[i][j] = b - '0'; rep(i, d) { rtr[d - i - 1] = rtr[d - i]; if (rtr[d - i - 1] < n - 1 && hl[d - i - 1][rtr[d - i - 1]]) rtr[d - i - 1]++; else if (rtr[d - i - 1] > 0 && hl[d - i - 1][rtr[d - i - 1] - 1]) rtr[d - i - 1]--; } if (rtr[0] == m) { cout << 0 << endl; continue; } rep(i, d) { tr[i] = i > 0 ? tr[i - 1] : m; if (tr[i] < n - 1 && hl[i][tr[i]]) tr[i]++; else if (tr[i] > 0 && hl[i][tr[i] - 1]) tr[i]--; else if (abs(tr[i] - rtr[i]) == 1 && (rtr[i] == n - 1 || !hl[i][rtr[i]]) && (rtr[i] == 0 || !hl[i][rtr[i] - 1])) { cout << i + 1 << " " << min(tr[i], rtr[i]) + 1 << endl; goto END; } } cout << 1 << endl; } return 0; }
[["-", 64, 9, 0, 1, 0, 16, 31, 16, 12, 13], ["+", 64, 9, 0, 1, 0, 16, 31, 16, 12, 13], ["-", 8, 9, 0, 1, 0, 16, 31, 16, 12, 13], ["+", 8, 9, 0, 1, 0, 16, 31, 16, 12, 13]]
1
444
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <fstream> #include <functional> #include <iostream> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long LL; typedef pair<int, int> P; static const double EPS = 1e-5; #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) int main(void) { int n, m, d, a; while (cin >> n) { if (n == 0) break; cin >> m >> a >> d; string sen[31]; int s[31]; int g[31]; s[0] = m; g[d] = a; REP(i, d) { cin >> sen[i]; } REP(i, d) { if (s[i] != 1 && sen[i][s[i] - 2] == '1') { s[i + 1] = s[i] - 1; } else if (s[i] != n && sen[i][s[i] - 1] == '1') { s[i + 1] = s[i] + 1; } else { s[i + 1] = s[i]; } if (g[d - i] != 1 && sen[d - i][g[d - i] - 2] == '1') { g[d - i - 1] = g[d - i] - 1; } else if (g[d - i] != n && sen[d - i][g[d - i] - 1] == '1') { g[d - i - 1] = g[d - i] + 1; } else { g[d - i - 1] = g[d - i]; } } bool f = true; int ans[2]; if (s[d] == g[d]) { cout << 0 << endl; } else { FOR(i, 1, d + 1) { if (g[i] - s[i] == 1) { if (sen[i - 1][s[i] - 1] == '0' && (s[i] == 1 || sen[i - 1][s[i] - 2] == '0') && (g[i] == n || sen[i - 1][s[i]] == '0')) { f = false; ans[0] = i; ans[1] = s[i]; break; } } else if (g[i] - s[i] == -1) { swap(s[i], g[i]); if (sen[i - 1][s[i] - 1] == '0' && (s[i] == 1 || sen[i - 1][s[i] - 2] == '0') && (g[i] == n || sen[i - 1][s[i]] == '0')) { f = false; ans[0] = i; ans[1] = s[i]; break; } } } if (f) cout << 1 << endl; else { printf("%d %d\n", ans[0], ans[1]); } } } return 0; }
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <fstream> #include <functional> #include <iostream> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long LL; typedef pair<int, int> P; static const double EPS = 1e-5; #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) int main(void) { int n, m, d, a; while (cin >> n) { if (n == 0) break; cin >> m >> a >> d; string sen[31]; int s[31]; int g[31]; s[0] = m; g[d] = a; REP(i, d) { cin >> sen[i]; } REP(i, d) { if (s[i] != 1 && sen[i][s[i] - 2] == '1') { s[i + 1] = s[i] - 1; } else if (s[i] != n && sen[i][s[i] - 1] == '1') { s[i + 1] = s[i] + 1; } else { s[i + 1] = s[i]; } if (g[d - i] != 1 && sen[d - i - 1][g[d - i] - 2] == '1') { g[d - i - 1] = g[d - i] - 1; } else if (g[d - i] != n && sen[d - i - 1][g[d - i] - 1] == '1') { g[d - i - 1] = g[d - i] + 1; } else { g[d - i - 1] = g[d - i]; } } bool f = true; int ans[2]; if (s[d] == g[d]) { cout << 0 << endl; } else { FOR(i, 1, d + 1) { if (g[i] - s[i] == 1) { if (sen[i - 1][s[i] - 1] == '0' && (s[i] == 1 || sen[i - 1][s[i] - 2] == '0') && (g[i] == n || sen[i - 1][s[i]] == '0')) { f = false; ans[0] = i; ans[1] = s[i]; break; } } else if (g[i] - s[i] == -1) { swap(s[i], g[i]); if (sen[i - 1][s[i] - 1] == '0' && (s[i] == 1 || sen[i - 1][s[i] - 2] == '0') && (g[i] == n || sen[i - 1][s[i]] == '0')) { f = false; ans[0] = i; ans[1] = s[i]; break; } } } if (f) cout << 1 << endl; else { printf("%d %d\n", ans[0], ans[1]); } } } return 0; }
[["+", 31, 69, 28, 69, 341, 342, 0, 16, 17, 33], ["+", 31, 69, 28, 69, 341, 342, 0, 16, 12, 13]]
1
727
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main() { while (true) { int N; cin >> N; if (!N) break; int M, G, D; cin >> M >> G >> D; vector<string> lots(D); for (int i = 0; i < D; ++i) { cin >> lots[i]; lots[i] = '0' + lots[i] + '0'; } vector<vector<int>> history(D + 1, vector<int>(N + 2)); for (int i = 0; i <= N + 1; ++i) history[0][i] = i; // Simulation for (int turn = 1; turn < D + 1; ++turn) { history[turn] = history[turn - 1]; for (int i = 1; i < N; ++i) if (lots[turn - 1][i] == '1') swap(history[turn][i], history[turn][i + 1]); } int target = history[D][G]; if (target == M) cout << 0 << endl; else { for (int turn = 0; turn < D; ++turn) { for (int i = 1; i <= N; ++i) { if (history[turn][i] == M) { if (history[turn][i - 1] == target && lots[turn + 1].substr(i - 2, 3) == "000") { cout << turn + 1 << ' ' << i - 1 << endl; goto next; } else if (history[turn][i + 1] == target && lots[turn + 1].substr(i - 1, 3) == "000") { cout << turn + 1 << ' ' << i << endl; goto next; } } } } cout << 1 << endl; } next:; } return 0; }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main() { while (true) { int N; cin >> N; if (!N) break; int M, G, D; cin >> M >> G >> D; vector<string> lots(D); for (int i = 0; i < D; ++i) { cin >> lots[i]; lots[i] = '0' + lots[i] + '0'; } vector<vector<int>> history(D + 1, vector<int>(N + 2)); for (int i = 0; i <= N + 1; ++i) history[0][i] = i; // Simulation for (int turn = 1; turn < D + 1; ++turn) { history[turn] = history[turn - 1]; for (int i = 1; i < N; ++i) if (lots[turn - 1][i] == '1') swap(history[turn][i], history[turn][i + 1]); } int target = history[D][G]; if (target == M) cout << 0 << endl; else { for (int turn = 0; turn < D; ++turn) { for (int i = 1; i <= N; ++i) { if (history[turn][i] == M) { if (history[turn][i - 1] == target && lots[turn].substr(i - 2, 3) == "000") { cout << turn + 1 << ' ' << i - 1 << endl; goto next; } else if (history[turn][i + 1] == target && lots[turn].substr(i - 1, 3) == "000") { cout << turn + 1 << ' ' << i << endl; goto next; } } } } cout << 1 << endl; } next:; } return 0; }
[["-", 63, 118, 28, 69, 341, 342, 0, 16, 17, 72], ["-", 63, 118, 28, 69, 341, 342, 0, 16, 12, 13]]
1
428
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; bool solve(vector<string> &line, int a, int b) { int n = line[0].size() + 1; int m = line.size(); for (int i = 0; i < m; ++i) { if (a > 0 && line[i][a - 1] == '1') { --a; } else if (a < n - 1 && line[i][a] == '1') { ++a; } } return a == b; } int main() { for (;;) { int n, a, b, m; cin >> n >> a >> b >> m; if (n == 0) return 0; --a; --b; vector<string> line(m); for (int i = 0; i < m; ++i) cin >> line[i]; if (solve(line, a, b)) { cout << 0 << endl; continue; } bool ok = false; for (int i = 0; i < m; ++i) { for (int j = 0; j < n - 1; ++j) { if (!ok && (j == 0 || line[i][j - 1] == '0') && line[i][j] == '0' && (j == n - 2 && line[i][j - 1] == '0')) { line[i][j] = '1'; if (solve(line, a, b)) { cout << (i + 1) << ' ' << (j + 1) << endl; ok = true; } line[i][j] = '0'; } } } if (!ok) cout << 1 << endl; } }
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; bool solve(vector<string> &line, int a, int b) { int n = line[0].size() + 1; int m = line.size(); for (int i = 0; i < m; ++i) { if (a > 0 && line[i][a - 1] == '1') { --a; } else if (a < n - 1 && line[i][a] == '1') { ++a; } } return a == b; } int main() { for (;;) { int n, a, b, m; cin >> n >> a >> b >> m; if (n == 0) return 0; --a; --b; vector<string> line(m); for (int i = 0; i < m; ++i) cin >> line[i]; if (solve(line, a, b)) { cout << 0 << endl; continue; } bool ok = false; for (int i = 0; i < m; ++i) { for (int j = 0; j < n - 1; ++j) { if (!ok && (j == 0 || line[i][j - 1] == '0') && line[i][j] == '0' && (j == n - 2 || line[i][j + 1] == '0')) { line[i][j] = '1'; if (solve(line, a, b)) { cout << (i + 1) << ' ' << (j + 1) << endl; ok = true; } line[i][j] = '0'; } } } if (!ok) cout << 1 << endl; } }
[["-", 15, 339, 51, 16, 12, 23, 0, 16, 17, 98], ["+", 15, 339, 51, 16, 12, 23, 0, 16, 17, 106], ["-", 12, 16, 31, 69, 341, 342, 0, 16, 17, 33], ["+", 12, 16, 31, 69, 341, 342, 0, 16, 17, 72]]
1
419
#include <iostream> #include <vector> using namespace std; int n, m, goal, d; bool check(vector<int> line, vector<string> path) { int i, j; for (i = 0; i < d; i++) for (j = 0; j < n - 1; j++) if (path[i][j] == '1') swap(line[j], line[j + 1]); return line[goal - 1] == m; } int main() { int i, j; vector<int> line; vector<string> path; bool f; while (cin >> n, n) { cin >> m >> goal >> d; line.resize(n); path.resize(d); for (i = 0; i < n; i++) line[i] = i + 1; for (i = 0; i < d; i++) cin >> path[i]; f = check(line, path) * 2; for (i = 0; !f && i < d; i++) { for (j = 0; !f && j < n - 1; j++) { if (path[i][j] == '1') continue; if (0 < j && path[i][j - 1] == '1') continue; if (j < n - 1 && path[i][j + 1] == '1') continue; path[i][j] = '1'; f = check(line, path); path[i][j] = '0'; } } if (f == 2) puts("0"); else if (f == 0) puts("1"); else cout << i << " " << j << endl; } }
#include <iostream> #include <vector> using namespace std; int n, m, goal, d; int check(vector<int> line, vector<string> path) { int i, j; for (i = 0; i < d; i++) for (j = 0; j < n - 1; j++) if (path[i][j] == '1') swap(line[j], line[j + 1]); return line[goal - 1] == m; } int main() { int f, i, j; vector<int> line; vector<string> path; while (cin >> n, n) { cin >> m >> goal >> d; line.resize(n); path.resize(d); for (i = 0; i < n; i++) line[i] = i + 1; for (i = 0; i < d; i++) cin >> path[i]; f = check(line, path) * 2; for (i = 0; !f && i < d; i++) { for (j = 0; !f && j < n - 1; j++) { if (path[i][j] == '1') continue; if (0 < j && path[i][j - 1] == '1') continue; if (j < n - 1 && path[i][j + 1] == '1') continue; path[i][j] = '1'; f = check(line, path); path[i][j] = '0'; } } if (f == 2) puts("0"); else if (f == 0) puts("1"); else cout << i << " " << j << endl; } }
[["-", 36, 36, 36, 36, 0, 30, 0, 14, 39, 40], ["+", 36, 36, 36, 36, 0, 30, 0, 14, 39, 40], ["+", 0, 30, 0, 14, 8, 9, 0, 43, 49, 22], ["+", 0, 30, 0, 14, 8, 9, 0, 43, 0, 21], ["-", 0, 30, 0, 14, 8, 9, 0, 43, 0, 35], ["-", 0, 30, 0, 14, 8, 9, 0, 43, 39, 40], ["-", 0, 30, 0, 14, 8, 9, 0, 43, 49, 22]]
1
390
#include <cstring> #include <iostream> using namespace std; const int MAX = 12; bool cross[MAX][MAX]; int n, start, goal, d; void init() { memset(cross, false, sizeof(cross)); } bool move() { int pos = start; for (int i = 0; i < d; i++) { if (cross[i][pos]) pos++; else if (cross[i][pos - 1]) pos--; } return pos == goal; } bool addline() { for (int i = 1; i <= d; i++) for (int j = 1; j < n; j++) if (!cross[i][j] && !cross[i][j - 1] && !cross[i][j + 1]) { cross[i][j] = true; if (move()) { cout << i << " " << j + 1 << endl; return true; } cross[i][j] = false; } return false; } void solve() { if (move()) cout << "0" << endl; else if (addline()) return; else cout << "1" << endl; } int main() { while (cin >> n && n) { init(); cin >> start >> goal >> d; for (int i = 1; i <= d; i++) for (int j = 1; j < n; j++) { char c; cin >> c; if (c == '1') cross[i][j] = true; else cross[i][j] = false; } solve(); } return 0; }
#include <cstring> #include <iostream> using namespace std; const int MAX = 12; bool cross[MAX][MAX]; int n, start, goal, d; void init() { memset(cross, false, sizeof(cross)); } bool move() { int pos = start; for (int i = 1; i <= d; i++) { if (cross[i][pos]) pos++; else if (cross[i][pos - 1]) pos--; } return pos == goal; } bool addline() { for (int i = 1; i <= d; i++) for (int j = 1; j < n; j++) if (!cross[i][j] && !cross[i][j - 1] && !cross[i][j + 1]) { cross[i][j] = true; if (move()) { cout << i << " " << j << endl; return true; } cross[i][j] = false; } return false; } void solve() { if (move()) cout << "0" << endl; else if (addline()) return; else cout << "1" << endl; } int main() { while (cin >> n && n) { init(); cin >> start >> goal >> d; for (int i = 1; i <= d; i++) for (int j = 1; j < n; j++) { char c; cin >> c; if (c == '1') cross[i][j] = true; else cross[i][j] = false; } solve(); } 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, 15, 16, 17, 18], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 17, 72], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13]]
1
367
#include <iostream> #include <string> #include <vector> using namespace std; int n, m, x, d; vector<string> field; bool solve() { int num[n]; for (int i = 0; i < n; i++) num[i] = i + 1; for (int i = 0; i < d; i++) { for (int j = 0; j < n - 1; j++) { if (field[i][j] == '1') { swap(num[j], num[j + 1]); } } } return (num[x] == m); } int main() { while (cin >> n, n) { cin >> m >> x >> d; field.clear(); for (int i = 0; i < d; i++) { string line; cin >> line; field.push_back(line); } if (solve()) { cout << 0 << endl; continue; } for (int i = 0; i < d; i++) { for (int j = 0; j < n - 1; j++) { if ((j == 0 || field[i][j - 1] == '0') && field[i][j] == '0' && (j + 1 == n - 1 || field[i][j + 1] == '0')) { field[i][j] = '1'; if (solve()) { cout << i + 1 << ' ' << j + 2 << endl; goto NEXT; } field[i][j] = '0'; } } } cout << 1 << endl; NEXT: continue; } }
#include <iostream> #include <string> #include <vector> using namespace std; int n, m, x, d; vector<string> field; bool solve() { int num[n]; for (int i = 0; i < n; i++) num[i] = i + 1; for (int i = 0; i < d; i++) { for (int j = 0; j < n - 1; j++) { if (field[i][j] == '1') { swap(num[j], num[j + 1]); } } } return (num[x - 1] == m); } int main() { while (cin >> n, n) { cin >> m >> x >> d; field.clear(); for (int i = 0; i < d; i++) { string line; cin >> line; field.push_back(line); } if (solve()) { cout << 0 << endl; continue; } for (int i = 0; i < d; i++) { for (int j = 0; j < n - 1; j++) { if ((j == 0 || field[i][j - 1] == '0') && field[i][j] == '0' && (j + 1 == n - 1 || field[i][j + 1] == '0')) { field[i][j] = '1'; if (solve()) { cout << i + 1 << ' ' << j + 1 << endl; goto NEXT; } field[i][j] = '0'; } } } cout << 1 << endl; NEXT: continue; } }
[["+", 0, 16, 31, 69, 341, 342, 0, 16, 17, 33], ["+", 0, 16, 31, 69, 341, 342, 0, 16, 12, 13], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13], ["+", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13]]
1
366
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <ctype.h> #include <fstream> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define REP(i, x, n) for (int i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) #define lengthof(x) (sizeof(x) / sizeof(*(x))) #define FILL(ptr, value) FILL_((ptr), sizeof(ptr) / sizeof(value), (value)) template <typename T> void FILL_(void *ptr, size_t size, T value) { std::fill((T *)ptr, (T *)ptr + size, value); } // 4方向ベクトル→↑←↓ int dx[] = {1, 0, -1, 0}; int dy[] = {0, -1, 0, 1}; int n, m, hoshi, d; char maze[32][16]; bool judge() { int point = m - 1; for (int i = 0; i < d; i++) { if (point == 0) { if (maze[i][point] == '1') { point += 1; } } else if (point == n - 1) { if (maze[i][point - 1] == '1') { point -= 1; } } else { if (maze[i][point - 1] == '1') point -= 1; else if (maze[i][point] == '1') point += 1; } } if (point == hoshi) return true; return false; } int main() { while (cin >> n, n) { scanf("%d%d%d", &m, &hoshi, &d); hoshi -= 1; for (int i = 0; i < d; i++) { cin >> maze[i]; } if (judge()) cout << "0" << endl; else { int x, y; bool f = false; for (int i = 0; i < d && !f; i++) { for (int j = 0; j < n - 1 && !f; j++) { if (maze[i][j] == '0' && (maze[i][j - 1] != '1' || maze[i][j + 1] != '1')) { maze[i][j] = '1'; if (judge()) { f = true; x = j; y = i; break; } maze[i][j] = '0'; } } } if (f) cout << y + 1 << " " << x + 1 << endl; else cout << "1" << endl; } } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <ctype.h> #include <fstream> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define REP(i, x, n) for (int i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) #define lengthof(x) (sizeof(x) / sizeof(*(x))) #define FILL(ptr, value) FILL_((ptr), sizeof(ptr) / sizeof(value), (value)) template <typename T> void FILL_(void *ptr, size_t size, T value) { std::fill((T *)ptr, (T *)ptr + size, value); } // 4方向ベクトル→↑←↓ int dx[] = {1, 0, -1, 0}; int dy[] = {0, -1, 0, 1}; int n, m, hoshi, d; char maze[32][16]; bool judge() { int point = m - 1; for (int i = 0; i < d; i++) { if (point == 0) { if (maze[i][point] == '1') { point += 1; } } else if (point == n - 1) { if (maze[i][point - 1] == '1') { point -= 1; } } else { if (maze[i][point - 1] == '1') point -= 1; else if (maze[i][point] == '1') point += 1; } } if (point == hoshi) return true; return false; } int main() { while (cin >> n, n) { scanf("%d%d%d", &m, &hoshi, &d); hoshi -= 1; for (int i = 0; i < d; i++) { cin >> maze[i]; } if (judge()) cout << "0" << endl; else { int x, y; bool f = false; for (int i = 0; i < d && !f; i++) { for (int j = 0; j < n - 1 && !f; j++) { if (maze[i][j] == '0' && (maze[i][j - 1] != '1' && maze[i][j + 1] != '1')) { maze[i][j] = '1'; if (judge()) { f = true; x = j; y = i; break; } maze[i][j] = '0'; } } } if (f) cout << y + 1 << " " << x + 1 << endl; else cout << "1" << endl; } } return 0; }
[["-", 15, 339, 51, 16, 12, 23, 0, 16, 17, 106], ["+", 15, 339, 51, 16, 12, 23, 0, 16, 17, 98]]
1
570
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; #define pu push #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 2000000000 #define sz(x) ((int)(x).size()) #define fi first #define sec second #define SORT(x) sort((x).begin(), (x).end()) #define all(x) (x).begin(), (x).end() #define EQ(a, b) (abs((a) - (b)) < EPS) bool bar[35][15]; int v[15][35]; int n, m, p, d; int main() { while (1) { for (int i = 0; i < 35; i++) { for (int j = 0; j < 15; j++) { bar[i][j] = false; v[j][i] = 0; } } cin >> n; if (n == 0) break; cin >> m; cin >> p; cin >> d; for (int i = 0; i < d; i++) { string s; cin >> s; for (int j = 0; j < s.size(); j++) { if (s[j] == '1') bar[i + 1][j + 1] = true; } } for (int i = 1; i <= n; i++) { v[i][0] = i; } for (int i = 1; i <= d; i++) { for (int j = 1; j <= n; j++) { v[j][i] = v[j][i - 1]; } for (int j = 1; j < n; j++) { if (bar[i][j]) swap(v[j][i], v[j + 1][i]); } } if (v[p][d] == m) { cout << 0 << endl; continue; } for (int i = 1; i <= d; i++) { for (int j = 1; j < n; j++) { if (!bar[i][j - 1] && !bar[i][j] && !bar[i + 1][j] && ((v[j][i] == v[p][d] && v[j + 1][i] == m) || (v[j][i] == m && v[j + 1][i] == v[p][d]))) { cout << i << ' ' << j << endl; goto end; } } } cout << 1 << endl; end: continue; } return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; #define pu push #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 2000000000 #define sz(x) ((int)(x).size()) #define fi first #define sec second #define SORT(x) sort((x).begin(), (x).end()) #define all(x) (x).begin(), (x).end() #define EQ(a, b) (abs((a) - (b)) < EPS) bool bar[35][15]; int v[15][35]; int n, m, p, d; int main() { while (1) { for (int i = 0; i < 35; i++) { for (int j = 0; j < 15; j++) { bar[i][j] = false; v[j][i] = 0; } } cin >> n; if (n == 0) break; cin >> m; cin >> p; cin >> d; for (int i = 0; i < d; i++) { string s; cin >> s; for (int j = 0; j < s.size(); j++) { if (s[j] == '1') bar[i + 1][j + 1] = true; } } for (int i = 1; i <= n; i++) { v[i][0] = i; } for (int i = 1; i <= d; i++) { for (int j = 1; j <= n; j++) { v[j][i] = v[j][i - 1]; } for (int j = 1; j < n; j++) { if (bar[i][j]) swap(v[j][i], v[j + 1][i]); } } if (v[p][d] == m) { cout << 0 << endl; continue; } for (int i = 1; i <= d; i++) { for (int j = 1; j < n; j++) { if (!bar[i][j - 1] && !bar[i][j] && !bar[i][j + 1] && ((v[j][i] == v[p][d] && v[j + 1][i] == m) || (v[j][i] == m && v[j + 1][i] == v[p][d]))) { cout << i << ' ' << j << endl; goto end; } } } cout << 1 << endl; end: continue; } return 0; }
[["-", 28, 69, 28, 69, 341, 342, 0, 16, 17, 72], ["-", 28, 69, 28, 69, 341, 342, 0, 16, 12, 13], ["+", 12, 91, 28, 69, 341, 342, 0, 16, 17, 72], ["+", 12, 91, 28, 69, 341, 342, 0, 16, 12, 13]]
1
594
def amida(n,m,s,l) a=Array.new(n) {|i| i+1} l.each do |i| i.each_with_index do |j,id| a[id],a[id+1]=a[id+1],a[id] if j==1 end end return a[s-1]==m ? true : false end loop do break if gets.chomp=="0" n,m,s,d,l=$_.to_i,gets.to_i,gets.to_i,gets.to_i,[] d.times do l<<gets.chomp.split("").map(&:to_i) end if amida(n,m,s,l) then puts 0 else bf=false ll= Marshal.load(Marshal.dump(l)) l.each_with_index do |i, idi| i.each_with_index do |j, idj| if j==1 then ll[idi][[0,idj-1].max]=1 ll[idi][idj]=1 ll[idi][[n-2,idj+1].min]=1 end end end ll.each_with_index do |i, idi| i.each_with_index do |j, idj| if j==0 then l3= Marshal.load(Marshal.dump(l)) l3[idi][idj]=1 puts "#{idi+1} #{idj+1}" if bf=amida(n,m,s,l3) end end break if bf end puts 1 unless bf end end
def amida(n,m,s,l) a=Array.new(n) {|i| i+1} l.each do |i| i.each_with_index do |j,id| a[id],a[id+1]=a[id+1],a[id] if j==1 end end return a[s-1]==m ? true : false end loop do break if gets.chomp=="0" n,m,s,d,l=$_.to_i,gets.to_i,gets.to_i,gets.to_i,[] d.times do l<<gets.chomp.split("").map(&:to_i) end if amida(n,m,s,l) then puts 0 else bf=false ll= Marshal.load(Marshal.dump(l)) l.each_with_index do |i, idi| i.each_with_index do |j, idj| if j==1 then ll[idi][[0,idj-1].max]=1 ll[idi][idj]=1 ll[idi][[n-2,idj+1].min]=1 end end end ll.each_with_index do |i, idi| i.each_with_index do |j, idj| if j==0 then l3= Marshal.load(Marshal.dump(l)) l3[idi][idj]=1 puts "#{idi+1} #{idj+1}" if bf=amida(n,m,s,l3) end break if bf end break if bf end puts 1 unless bf end end
[["-", 36, 36, 36, 36, 0, 42, 0, 768, 0, 769], ["+", 36, 36, 36, 36, 0, 42, 0, 768, 0, 769]]
4
159
def goal?(amida,m,s) s == 1 + (amida.inject(m-1) do |s,i| if s>1 && i[s-1]=="1" s-1 elsif i[s]=="1" s+1 else s end end) end loop do n = gets.to_i break if n==0 m,s,d = 3.times.map{ gets.to_i } amida = d.times.map{ gets.chomp } if goal?(amida,m,s) puts 0 next end catch :find do amida.each_with_index do |a,i| n.times do |j| if "0#{a}0"[j,3]=="000" added = amida.dup b = a.dup b[j] = "1" added[i] = b if goal?(added,m,s) puts "#{i+1} #{j+1}" throw :find end end end end puts 1 end end
def goal?(amida,m,s) s == 1 + (amida.inject(m-1) do |s,i| if s>=1 && i[s-1]=="1" s-1 elsif i[s]=="1" s+1 else s end end) end loop do n = gets.to_i break if n==0 m,s,d = 3.times.map{ gets.to_i } amida = d.times.map{ gets.chomp } if goal?(amida,m,s) puts 0 next end catch :find do amida.each_with_index do |a,i| n.times do |j| if "0#{a}0"[j,3]=="000" added = amida.dup b = a.dup b[j] = "1" added[i] = b if goal?(added,m,s) puts "#{i+1} #{j+1}" throw :find end end end end puts 1 end end
[["-", 8, 736, 0, 121, 15, 738, 31, 738, 17, 47], ["+", 8, 736, 0, 121, 15, 738, 31, 738, 17, 20]]
4
210
def check(n,m,star,d,f): ns=[i for i in range(n)] for i in range(d): for j in range(n-1): if f[i][j]: c=ns[j+1] ns[j+1]=ns[j] ns[j]=c return ns[star-1]==m-1 while True: n=int(input()) if(n==0):break m=int(input()) star=int(input()) d=int(input()) f=[[False for i in range(n-1)]for j in range(d)] for i in range(d): st=input() for j in range(n-1): f[i][j]=st[j]=='1' if check(n,m,star,d,f): print(1) else: ans=False for i in range(d): for j in range(n-1): if ans:break ok=not f[i][j] if(j>0 and f[i][j-1]) or (j<n-2 and f[i][j+1]):ok=False if ok: f[i][j]=True if check(n,m,star,d,f): print(i+1,j+1) ans=True f[i][j]=False if not ans: print(0)
def check(n,m,star,d,f): ns=[i for i in range(n)] for i in range(d): for j in range(n-1): if f[i][j]: c=ns[j+1] ns[j+1]=ns[j] ns[j]=c return ns[star-1]==m-1 while True: n=int(input()) if(n==0):break m=int(input()) star=int(input()) d=int(input()) f=[[False for i in range(n-1)]for j in range(d)] for i in range(d): st=input() for j in range(n-1): f[i][j]=st[j]=='1' if check(n,m,star,d,f): print(0) else: ans=False for i in range(d): for j in range(n-1): if ans:break ok=not f[i][j] if(j>0 and f[i][j-1]) or (j<n-2 and f[i][j+1]):ok=False if ok: f[i][j]=True if check(n,m,star,d,f): print(i+1,j+1) ans=True f[i][j]=False if not ans: print(1)
[["-", 64, 196, 0, 1, 0, 652, 3, 4, 0, 612], ["+", 64, 196, 0, 1, 0, 652, 3, 4, 0, 612]]
5
343
import static java.lang.Math.*; import java.io.*; import java.util.*; class Main { public static void main(final String[] args) { final Scanner stdin = new Scanner(System.in); while (stdin.hasNextInt()) { final int n = stdin.nextInt(); final int s = stdin.nextInt(); final boolean[] visited = new boolean[10]; final int ans = dfs(n, s, visited, 0, 0); System.out.println(ans); } } static int dfs(final int n, final int s, final boolean[] visited, final int sum, final int depth) { if (sum == s && depth == n) { return 1; } else if (sum > s || depth > n) { return 0; } int limSum = sum; int limNum = 9; for (int i = 0; i < n - depth; i++) { while (limNum > 0 && visited[limNum]) { limNum--; } limSum += limNum; } if (limSum < s) { return 0; } int comb = 0; for (int i = 0; i <= 9; i++) { if (!visited[i]) { visited[i] = true; comb += dfs(n, s, visited, sum + (depth + 1) * i, depth + 1); visited[i] = false; } } return comb; } }
import static java.lang.Math.*; import java.io.*; import java.util.*; class Main { public static void main(final String[] args) { final Scanner stdin = new Scanner(System.in); while (stdin.hasNextInt()) { final int n = stdin.nextInt(); final int s = stdin.nextInt(); final boolean[] visited = new boolean[10]; final int ans = dfs(n, s, visited, 0, 0); System.out.println(ans); } } static int dfs(final int n, final int s, final boolean[] visited, final int sum, final int depth) { if (sum == s && depth == n) { return 1; } else if (sum > s || depth > n) { return 0; } int limSum = sum; int limNum = 9; for (int i = 0; i < n - depth; i++) { while (limNum > 0 && visited[limNum]) { limNum--; } limSum += limNum * (n - i); } if (limSum < s) { return 0; } int comb = 0; for (int i = 0; i <= 9; i++) { if (!visited[i]) { visited[i] = true; comb += dfs(n, s, visited, sum + (depth + 1) * i, depth + 1); visited[i] = false; } } return comb; } }
[["+", 8, 196, 0, 1, 0, 11, 12, 16, 17, 48], ["+", 0, 1, 0, 11, 12, 16, 12, 23, 0, 24], ["+", 0, 11, 12, 16, 12, 23, 0, 16, 31, 22], ["+", 0, 11, 12, 16, 12, 23, 0, 16, 17, 33], ["+", 0, 11, 12, 16, 12, 23, 0, 16, 12, 22], ["+", 0, 1, 0, 11, 12, 16, 12, 23, 0, 25]]
3
312
#include <stdio.h> int data[10]; int ans[15][500]; void sub(int n, int x); int check(int a, int x); int main(void) { int n, i, s; for (n = 0; n < 15; n++) { for (i = 0; i < 500; i++) { ans[n][i] = 0; } } for (n = 1; n <= 10; n++) { sub(n, 0); } while (scanf("%d %d", &n, &s) != EOF) { if (s >= 400) { printf("0\n"); } else { printf("%d", ans[n][s]); } } return 0; } void sub(int n, int x) { int i, sum; if (x == n) { sum = 0; for (i = 0; i < n; i++) { sum = sum + data[i] * (i + 1); } ans[n][sum]++; return; } for (i = 0; i < 10; i++) { data[x] = i; if (check(i, x) == 0) { sub(n, x + 1); } } } int check(int a, int x) { int i; for (i = 0; i < x; i++) { if (data[i] == a) { return 1; } } return 0; }
#include <stdio.h> int data[10]; int ans[15][500]; void sub(int n, int x); int check(int a, int x); int main(void) { int n, i, s; for (n = 0; n < 15; n++) { for (i = 0; i < 500; i++) { ans[n][i] = 0; } } for (n = 1; n <= 10; n++) { sub(n, 0); } while (scanf("%d %d", &n, &s) != EOF) { if (s >= 400) { printf("0\n"); } else { printf("%d\n", ans[n][s]); } } return 0; } void sub(int n, int x) { int i, sum; if (x == n) { sum = 0; for (i = 0; i < n; i++) { sum = sum + data[i] * (i + 1); } ans[n][sum]++; return; } for (i = 0; i < 10; i++) { data[x] = i; if (check(i, x) == 0) { sub(n, x + 1); } } } int check(int a, int x) { int i; for (i = 0; i < x; i++) { if (data[i] == a) { return 1; } } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
0
328
#include <stdio.h> int memo[10][10][0x400][331]; int tansaku(int flags, int now, int max, int target) { int i; int result = 0; if (now >= max) return (target == 0) ? 1 : 0; if (target < 0 || target > 330) return 0; if (memo[max][now][flags][target] > 0) { return memo[max][now][flags][target] - 1; } for (i = 0; i <= 9; i++) { if (flags & (1 << i)) { result += tansaku(flags & ~(1 << i), now + 1, max, target - i * (now + 1)); } } memo[max][now][flags][target] = result + 1; return result; } int main(void) { int n, s; while (scanf("%d%d", &n, &s) == 2) { printf("%d\n", tansaku(0x3FF, 0, n, s)); } return 0; }
#include <stdio.h> int memo[10][10][0x400][331]; int tansaku(int flags, int now, int max, int target) { int i; int result = 0; if (now >= max) return (target == 0) ? 1 : 0; if (target < 0 || target > 330) return 0; if (memo[max - 1][now][flags][target] > 0) { return memo[max - 1][now][flags][target] - 1; } for (i = 0; i <= 9; i++) { if (flags & (1 << i)) { result += tansaku(flags & ~(1 << i), now + 1, max, target - i * (now + 1)); } } memo[max - 1][now][flags][target] = result + 1; return result; } int main(void) { int n, s; while (scanf("%d%d", &n, &s) == 2) { printf("%d\n", tansaku(0x3FF, 0, n, s)); } return 0; }
[["+", 28, 69, 28, 69, 28, 69, 71, 16, 17, 33], ["+", 28, 69, 28, 69, 28, 69, 71, 16, 12, 13]]
0
240
#include <stdio.h> #include <string.h> int u[10] = {0}, dp[10][331] = {{0}}, j, k; void f(int p, int r) { int i; dp[p][r]++; for (i = 0; i < 10; i++) if (!u[i]) { u[i] = 1; f(p + 1, r + i * (p + 1)); u[i] = 0; } } int main() { f(0, 0); for (; ~scanf("%d%d", &j, &k);) printf("%d\n", (j > 10 || k > 330) ? 0 : dp[j][k]); return 0; }
#include <stdio.h> #include <string.h> int u[10] = {0}, dp[11][333] = {{0}}, j, k; void f(int p, int r) { int i; dp[p][r]++; for (i = 0; i < 10; i++) if (!u[i]) { u[i] = 1; f(p + 1, r + i * (p + 1)); u[i] = 0; } } int main() { f(0, 0); for (; ~scanf("%d%d", &j, &k);) printf("%d\n", (j > 10 || k > 330) ? 0 : dp[j][k]); return 0; }
[["-", 0, 43, 49, 50, 49, 80, 49, 80, 81, 13], ["+", 0, 43, 49, 50, 49, 80, 49, 80, 81, 13], ["-", 0, 30, 0, 43, 49, 50, 49, 80, 81, 13], ["+", 0, 30, 0, 43, 49, 50, 49, 80, 81, 13]]
0
171
used[9]; dfs(int step, int s) { int i, res; if (!step) return (s == 0); if (s < 0) return (0); if (s > (step * (step + 1)) * 4) return (0); res = 0; for (i = 0; i <= 9; ++i) { if (!used[i]) { used[i] = 1; res += dfs(step - 1, s - (i * step)); used[i] = 0; } } return (res); } main(n, v) { while (~scanf("%d %d", &n, &v)) { printf("%d\n", dfs(n, v)); } return (0); }
used[9]; dfs(int step, int s) { int i, res; if (!step) return (s == 0); if (s < 0) return (0); if (s > (step * (step + 1)) * 5) return (0); res = 0; for (i = 0; i <= 9; ++i) { if (!used[i]) { used[i] = 1; res += dfs(step - 1, s - (i * step)); used[i] = 0; } } return (res); } main(n, v) { while (~scanf("%d %d", &n, &v)) { printf("%d\n", dfs(n, v)); } return (0); }
[["-", 0, 57, 15, 23, 0, 16, 12, 16, 12, 13], ["+", 0, 57, 15, 23, 0, 16, 12, 16, 12, 13]]
0
177
a[99][999], c; f(i, d, s, b) { for (++a[d++][s]; i--;) b >> i & 1 || f(10, d, s + i * d, b | 1 << i); } main() { for (f(10L, 0L); scanf("%d%d", a, &c) / 2;) printf("%d\n", c < 400 ? a[**a][c] : 0); }
a[99][999], c; f(i, d, s, b) { for (++a[d++][s]; i--;) b >> i & 1 || f(10, d, s + i * d, b | 1 << i); } main() { for (f(10, 0, 0, 0); scanf("%d%d", a, &c) / 2;) printf("%d\n", c < 400 ? a[**a][c] : 0); }
[["-", 0, 9, 0, 7, 10, 2, 3, 4, 0, 13], ["+", 0, 9, 0, 7, 10, 2, 3, 4, 0, 13], ["+", 0, 9, 0, 7, 10, 2, 3, 4, 0, 21]]
0
118
#include <stdio.h> int main(void) { float x1, y1, x2, y2, xq, yq, a, b, x, y, xp, yp; while (scanf("%f,%f,%f,%f,%f,%f", &x1, &y1, &x2, &y2, &xq, &yq) != EOF) { if (x1 == x2) { xp = 2 * x1 - xq; yp = yq; } else if (y1 == y2) { xp = xq; yp = 2 * y1 - yq; } else { a = (y2 - y1) / (x2 - x1); if (x1 == 0) { b = y2 - ((y2 - y1) / (x2 - x1)) * x2; } else { b = y1 - ((y2 - y1) / (x2 - x1)) * x1; } x = (xq + (yq - b) * a) / (a * a + 1); y = (xq + (yq - b) * a) / (a * a + 1) + b; xp = 2 * x - xq; yp = 2 * y - yq; } printf("%.6f %.6f\n", xp, yp); } return 0; }
#include <stdio.h> int main(void) { float x1, y1, x2, y2, xq, yq, a, b, x, y, xp, yp; while (scanf("%f,%f,%f,%f,%f,%f", &x1, &y1, &x2, &y2, &xq, &yq) != EOF) { if (x1 == x2) { xp = 2 * x1 - xq; yp = yq; } else if (y1 == y2) { xp = xq; yp = 2 * y1 - yq; } else { a = (y2 - y1) / (x2 - x1); if (x1 == 0) { b = y2 - ((y2 - y1) / (x2 - x1)) * x2; } else { b = y1 - ((y2 - y1) / (x2 - x1)) * x1; } x = (xq + (yq - b) * a) / (a * a + 1); y = ((xq + (yq - b) * a) / (a * a + 1)) * a + b; xp = 2 * x - xq; yp = 2 * y - yq; } printf("%.6f %.6f\n", xp, yp); } return 0; }
[["+", 31, 16, 31, 23, 0, 16, 31, 23, 0, 24], ["+", 31, 16, 31, 23, 0, 16, 12, 23, 0, 25], ["+", 0, 1, 0, 11, 12, 16, 31, 16, 17, 48], ["+", 0, 1, 0, 11, 12, 16, 31, 16, 12, 22]]
0
251
#include <stdio.h> int main(int argc, char *argv[]) { double x1, y1, x2, y2, xq, yq; while (scanf("%lf,%lf,%lf,%lf,%lf,%lf", &x1, &y1, &x2, &y2, &xq, &yq) != EOF) { double a, b, qa, qb, tx, ty; if (x1 == x2) { if (x1 < xq) { printf("%lf %lf\n", x1 - (xq - x1), yq); } else { printf("%lf %lf\n", x1 + (x1 - xq), yq); } continue; } a = (y1 - y2) / (x1 - x2); if (a == 0) { if (y1 < yq) { printf("%lf %lf\n", xq, y1 - (yq - y1)); } else { printf("%lf %lf\n", xq, y1 + (y1 - yq)); } continue; } b = y1 - a * x1; qa = -1 / a; qb = yq - qa * xq; tx = -(b + qb) / (a * qa); ty = tx * a + b; printf("%lf %lf\n", tx + (tx - xq), ty + (ty - yq)); } return 0; }
#include <stdio.h> int main(int argc, char *argv[]) { double x1, y1, x2, y2, xq, yq; while (scanf("%lf,%lf,%lf,%lf,%lf,%lf", &x1, &y1, &x2, &y2, &xq, &yq) != EOF) { double a, b, qa, qb, tx, ty; if (x1 == x2) { if (x1 < xq) { printf("%lf %lf\n", x1 - (xq - x1), yq); } else { printf("%lf %lf\n", x1 + (x1 - xq), yq); } continue; } a = (y1 - y2) / (x1 - x2); if (a == 0) { if (y1 < yq) { printf("%lf %lf\n", xq, y1 - (yq - y1)); } else { printf("%lf %lf\n", xq, y1 + (y1 - yq)); } continue; } b = y1 - a * x1; qa = -1 / a; qb = yq - qa * xq; tx = -(b - qb) / (a - qa); ty = tx * a + b; printf("%lf %lf\n", tx + (tx - xq), ty + (ty - yq)); } return 0; }
[["-", 12, 16, 31, 91, 28, 23, 0, 16, 17, 72], ["+", 12, 16, 31, 91, 28, 23, 0, 16, 17, 33], ["-", 0, 11, 12, 16, 12, 23, 0, 16, 17, 48], ["+", 0, 11, 12, 16, 12, 23, 0, 16, 17, 33]]
0
273
#include <algorithm> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; typedef complex<double> P; #define REP(i, j) for (int i = 0; i < (int)(j); ++i) #define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i) #define SORT(v) sort((v).begin(), (v).end()) #define REVERSE(v) reverse((v).begin(), (v).end()) const double EPS = 1e-8; const double INF = 1e12; struct L : public vector<P> { L(const P &a, const P &b) { push_back(a); push_back(b); } }; double dot(const P &a, const P &b) { return real(conj(a) * b); } P projection(const L &l, const P &p) { double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]); return l[0] + t * (l[0] - l[1]); } P reflection(const L &l, const P &p) { return p + P(2, 0) * (projection(l, p) - p); } int main() { double x0, y0, x1, y1, x2, y2; while (scanf("%lf,%lf,%lf,%lf,%lf,%lf", &x0, &y0, &x1, &y1, &x2, &y2) != EOF) { P P1(x0, y0), P2(x1, y1), Q(x2, y2); P ans = reflection(L(P1, P2), Q); printf("%.8lf, %.8lf\n", ans.real(), ans.imag()); } return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; typedef complex<double> P; #define REP(i, j) for (int i = 0; i < (int)(j); ++i) #define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i) #define SORT(v) sort((v).begin(), (v).end()) #define REVERSE(v) reverse((v).begin(), (v).end()) const double EPS = 1e-8; const double INF = 1e12; struct L : public vector<P> { L(const P &a, const P &b) { push_back(a); push_back(b); } }; double dot(const P &a, const P &b) { return real(conj(a) * b); } P projection(const L &l, const P &p) { double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]); return l[0] + t * (l[0] - l[1]); } P reflection(const L &l, const P &p) { return p + P(2, 0) * (projection(l, p) - p); } int main() { double x0, y0, x1, y1, x2, y2; while (scanf("%lf,%lf,%lf,%lf,%lf,%lf", &x0, &y0, &x1, &y1, &x2, &y2) != EOF) { P P1(x0, y0), P2(x1, y1), Q(x2, y2); P ans = reflection(L(P1, P2), Q); printf("%.8lf %.8lf\n", ans.real(), ans.imag()); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
369
#include <iostream> #include <math.h> #include <stdio.h> #include <vector> using namespace std; #define EPS (1e-20) 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); } 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; } }; double norm(Point p) { return p.x * p.x + p.y * p.y; } double abs(Point p) { return sqrt(norm(p)); } typedef Point Vector; double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } struct Segment { Point p1, p2; }; Point project(Segment s, Point p) { Vector base = s.p2 - s.p1; double r = dot(p - s.p1, base) / norm(base); return s.p1 + base * r; } Point reflect(Segment s, Point p) { return p + (project(s, p) - p) * 2.0; } int main() { double x1, y1, x2, y2, xq, yq; char c; while (cin >> x1 >> c >> y1 >> c >> x2 >> c >> y2 >> c >> xq >> c >> yq) { Point P1(x1, y1); Point P2(x2, y2); Point q(xq, yq); Segment s; s.p1 = P1, s.p2 = P2; Point p = project(s, q); printf("%.6f %.6f\n", p.x, p.y); } }
#include <iostream> #include <math.h> #include <stdio.h> #include <vector> using namespace std; #define EPS (1e-20) 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); } 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; } }; double norm(Point p) { return p.x * p.x + p.y * p.y; } double abs(Point p) { return sqrt(norm(p)); } typedef Point Vector; double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } struct Segment { Point p1, p2; }; Point project(Segment s, Point p) { Vector base = s.p2 - s.p1; double r = dot(p - s.p1, base) / norm(base); return s.p1 + base * r; } Point reflect(Segment s, Point p) { return p + (project(s, p) - p) * 2.0; } int main() { double x1, y1, x2, y2, xq, yq; char c; while (cin >> x1 >> c >> y1 >> c >> x2 >> c >> y2 >> c >> xq >> c >> yq) { Point P1(x1, y1); Point P2(x2, y2); Point q(xq, yq); Segment s; s.p1 = P1, s.p2 = P2; Point p = reflect(s, q); printf("%.6f %.6f\n", p.x, p.y); } }
[["-", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22], ["+", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22]]
1
485
#include <cmath> #include <cstdio> #include <iomanip> #include <iostream> using namespace std; #define EPS 1e-10 #define equals(a, b) (fabs((a) - (b)) < EPS) class Point { public: long double x, y; Point(long double x = 0, long 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); } long double abs() { return sqrt(norm()); } long 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 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; } }; struct Segment { Point p1, p2; }; long double norm(Point p) { return p.x * p.x + p.y * p.y; } long double abs(Point p) { return sqrt(norm(p)); } double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } Point projection(Segment a, Point b) { Point sa = a.p2 - a.p1; double hyp = dot(b - a.p1, sa); double r = hyp / norm(sa); return a.p1 + sa * r; } Point reflection(Segment a, Point b) { return b + ((projection(a, b) - b) * 2.0); } int main() { Point P1, P2, P3; Segment G; int q; while (scanf("%Lf,%Lf,%Lf,%Lf,%Lf,%Lf", &P2.x, P2.y, P3.x, P3.y, P1.x, P1.y) != EOF) { G.p1 = P2; G.p2 = P3; P2 = reflection(G, P1); cout << fixed << setprecision(15) << P2.x << ' ' << fixed << setprecision(15) << P2.y << endl; } return 0; }
#include <cmath> #include <cstdio> #include <iomanip> #include <iostream> using namespace std; #define EPS 1e-10 #define equals(a, b) (fabs((a) - (b)) < EPS) class Point { public: long double x, y; Point(long double x = 0, long 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); } long double abs() { return sqrt(norm()); } long 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 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; } }; struct Segment { Point p1, p2; }; long double norm(Point p) { return p.x * p.x + p.y * p.y; } long double abs(Point p) { return sqrt(norm(p)); } double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } Point projection(Segment a, Point b) { Point sa = a.p2 - a.p1; double hyp = dot(b - a.p1, sa); double r = hyp / norm(sa); return a.p1 + sa * r; } Point reflection(Segment a, Point b) { return b + ((projection(a, b) - b) * 2.0); } int main() { Point P1, P2, P3; Segment G; int q; while (scanf("%Lf,%Lf,%Lf,%Lf,%Lf,%Lf", &P2.x, &P2.y, &P3.x, &P3.y, &P1.x, &P1.y) != EOF) { G.p1 = P2; G.p2 = P3; P2 = reflection(G, P1); cout << fixed << setprecision(15) << P2.x << ' ' << fixed << setprecision(15) << P2.y << endl; } return 0; }
[["+", 51, 16, 31, 2, 3, 4, 0, 66, 17, 67]]
1
559
#include <cmath> #include <complex> #include <cstdio> #include <iomanip> #include <iostream> using namespace std; int main() { float x1, y1, x2, y2, xp, yp; double x0, y0, x, y, _x, _y, a, b; while (scanf("%f,%f,%f,%f,%f,%f", &x1, &y1, &x2, &y2, &xp, &yp) != EOF) { double angle = atan2(y2 - y1, x2 - x1); if (angle > M_PI / 2) angle -= M_PI; if (angle < -M_PI / 2) angle += M_PI; x0 = (y1 == y2 ? 0 : x1 - y1 * (x2 - x1) / (y2 - y1)); y0 = (x1 == x2 ? 0 : y1 - x1 * (y2 - y1) / (x2 - x1)); x = xp - x0; y = yp - y0; a = cos(angle) * cos(angle) - sin(angle) * sin(angle); b = 2 * sin(angle) * cos(angle); _x = a * x + b * y + x0; _y = b * x - a * y + y0; cout << fixed << setprecision(6) << _x << " " << _y << endl; } return 0; }
#include <cmath> #include <complex> #include <cstdio> #include <iomanip> #include <iostream> using namespace std; int main() { float x1, y1, x2, y2, xp, yp; double x0, y0, x, y, _x, _y, a, b; while (scanf("%f,%f,%f,%f,%f,%f", &x1, &y1, &x2, &y2, &xp, &yp) != EOF) { double angle = atan2(y2 - y1, x2 - x1); if (angle > M_PI / 2) angle -= M_PI; if (angle < -M_PI / 2) angle += M_PI; x0 = (y1 == y2 ? 0 : x1 - y1 * (x2 - x1) / (y2 - y1)); y0 = (x0 != 0 || x1 == x2 ? 0 : y1 - x1 * (y2 - y1) / (x2 - x1)); x = xp - x0; y = yp - y0; a = cos(angle) * cos(angle) - sin(angle) * sin(angle); b = 2 * sin(angle) * cos(angle); _x = a * x + b * y + x0; _y = b * x - a * y + y0; cout << fixed << setprecision(6) << _x << " " << _y << endl; } return 0; }
[["+", 12, 23, 0, 41, 15, 16, 31, 16, 31, 22], ["+", 12, 23, 0, 41, 15, 16, 31, 16, 17, 79], ["+", 12, 23, 0, 41, 15, 16, 31, 16, 12, 13], ["+", 0, 11, 12, 23, 0, 41, 15, 16, 17, 106]]
1
266
#include <cmath> #include <complex> #include <cstdio> using namespace std; typedef complex<double> xy_t; double dot_product(xy_t a, xy_t b) { return (conj(a) * b).real(); } int main() { double x[3], y[3]; xy_t p[3], a, b, c, d; while (~scanf("%lf,%lf,%lf,%lf,%lf,%lf", &x[0], &y[0], &x[1], &y[1], &x[2], &y[2])) { p[0] = xy_t(x[0], y[0]); p[1] = xy_t(x[1], y[1]); p[2] = xy_t(x[2], y[2]); a = p[1] - p[0]; b = p[2] - p[0]; c = (dot_product(a, b) / norm(a)) * a; d = c - b + c + p[0]; printf("%.6f %.6f", d.real(), d.imag()); } }
#include <cmath> #include <complex> #include <cstdio> using namespace std; typedef complex<double> xy_t; double dot_product(xy_t a, xy_t b) { return (conj(a) * b).real(); } int main() { double x[3], y[3]; xy_t p[3], a, b, c, d; while (~scanf("%lf,%lf,%lf,%lf,%lf,%lf", &x[0], &y[0], &x[1], &y[1], &x[2], &y[2])) { p[0] = xy_t(x[0], y[0]); p[1] = xy_t(x[1], y[1]); p[2] = xy_t(x[2], y[2]); a = p[1] - p[0]; b = p[2] - p[0]; c = (dot_product(a, b) / norm(a)) * a; d = c - b + c + p[0]; printf("%.4f %.4f\n", d.real(), d.imag()); } }
[["-", 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
249
#include <bits/stdc++.h> #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) using namespace std; static const int COUNTER_CLOCKWISE = 1; static const int CLOCKWISE = -1; static const int ONLINE_BACK = 2; static const int ONLINE_FRONT = -2; static const int ON_SEGMENT = 0; struct Point { double x, y; Point() {} Point(double x, double y) : 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 k) { return Point(x * k, y * k); } Point operator/(double k) { return Point(x / k, y / k); } double norm() { return x * x + y * y; } double abs() { return sqrt(norm()); } 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; typedef vector<Point> Polygon; struct Segment { Point p1, p2; Segment() {} Segment(Point p1, Point p2) : p1(p1), p2(p2) {} }; typedef Segment Line; struct Circle { Point c; double r; Circle(Point c, double r) : c(c), r(r) {} }; double norm(Vector a) { return a.x * a.x + a.y * a.y; } double abs(Vector a) { return sqrt(norm(a)); } 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; } bool isOrthogonal(Vector a, Vector b) { return equals(dot(a, b), 0.0); } bool isOrthogonal(Point a1, Point a2, Point b1, Point b2) { return isOrthogonal(a1 - a2, b1 - b2); } bool isOrthogonal(Segment s1, Segment s2) { return equals(dot(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0); } bool isParallel(Vector a, Vector b) { return equals(cross(a, b), 0.0); } bool isParallel(Point a1, Point a2, Point b1, Point b2) { return isParallel(a1 - a2, b1 - b2); } bool isParallel(Segment s1, Segment s2) { return equals(cross(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0); } Point project(Segment s, Point p) { Vector base = s.p2 - s.p1; double r = dot(p - s.p1, base) / norm(base); return s.p1 + base * r; } Point reflect(Segment s, Point p) { return p + (project(s, p) - p) * 2.0; } int ccw(Point p0, Point p1, Point p2) { Vector a = p1 - p0; Vector b = p2 - p0; if (cross(a, b) > EPS) return COUNTER_CLOCKWISE; if (cross(a, b) < -EPS) return CLOCKWISE; if (dot(a, b) < -EPS) return ONLINE_BACK; if (a.norm() < b.norm()) return ONLINE_FRONT; return ON_SEGMENT; } bool intersect(Point p1, Point p2, Point p3, Point p4) { return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0); } bool intersect(Segment s1, Segment s2) { return intersect(s1.p1, s1.p2, s2.p1, s2.p2); } double getDistanceLP(Line l, Point p) { return abs(cross(l.p2 - l.p1, p - l.p1) / abs(l.p2 - l.p1)); } double getDistanceSP(Segment s, Point p) { if (dot(s.p2 - s.p1, p - s.p1) < 0.0) return abs(p - s.p1); if (dot(s.p1 - s.p2, p - s.p2) < 0.0) return abs(p - s.p2); return getDistanceLP(s, p); } double getDistance(Segment s1, Segment s2) { if (intersect(s1, s2)) return 0.0; return min(min(getDistanceSP(s1, s2.p1), getDistanceSP(s1, s2.p2)), min(getDistanceSP(s2, s1.p1), getDistanceSP(s2, s1.p2))); } Point getCrossPoint(Segment s1, Segment s2) { Vector base = s2.p2 - s2.p1; double d1 = abs(cross(base, s1.p1 - s2.p1)); double d2 = abs(cross(base, s1.p2 - s2.p1)); double t = d1 / (d1 + d2); return s1.p1 + (s1.p2 - s1.p1) * t; } pair<Point, Point> getCrossPoints(Circle c, Line l) { Vector pr = project(l, c.c); Vector e = (l.p2 - l.p1) / abs(l.p2 - l.p1); double base = sqrt(c.r * c.r - norm(pr - c.c)); return make_pair(pr + e * base, pr - e * base); } double arg(Vector p) { return atan2(p.y, p.x); } Vector polar(double a, double r) { return Point(cos(r) * a, sin(r) * a); } pair<Point, Point> getCrossPoints(Circle c1, Circle c2) { double d = abs(c1.c - c2.c); double a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); double t = arg(c2.c - c1.c); return make_pair(c1.c + polar(c1.r, t + a), c1.c + polar(c1.r, t - a)); } int contains(Polygon g, Point p) { int n = g.size(); bool x = false; for (int i = 0; i < n; i++) { Point a = g[i] - p, b = g[(i + 1) % n] - p; if (fabs(cross(a, b)) < EPS && dot(a, b) < EPS) return 1; if (a.y > b.y) swap(a, b); if (a.y < EPS && EPS < b.y && cross(a, b) > EPS) x = !x; } return (x ? 2 : 0); } Polygon andrewScan(Polygon s) { Polygon u, l; if (s.size() < 3) return s; sort(s.begin(), s.end()); u.push_back(s[0]); u.push_back(s[1]); l.push_back(s[s.size() - 1]); l.push_back(s[s.size() - 2]); for (int i = 2; i < (int)s.size(); i++) { for (int n = u.size(); n >= 2 && ccw(u[n - 2], u[n - 1], s[i]) != CLOCKWISE; n--) { u.pop_back(); } u.push_back(s[i]); } for (int i = s.size() - 3; i >= 0; i--) { for (int n = l.size(); n >= 2 && ccw(l[n - 2], l[n - 1], s[i]) != CLOCKWISE; n--) { l.pop_back(); } l.push_back(s[i]); } reverse(l.begin(), l.end()); for (int i = u.size() - 2; i >= 1; i--) l.push_back(u[i]); return l; } double area(Polygon s) { double res = 0; for (int i = 0; i < (int)s.size(); i++) { res += cross(s[i], s[(i + 1) % s.size()]) / 2.0; } return abs(res); } Point getCrossPointLL(Line l1, Line l2) { double a = cross(l1.p2 - l1.p1, l2.p2 - l2.p1); double b = cross(l1.p2 - l1.p1, l1.p2 - l2.p1); if (abs(a) < EPS && abs(b) < EPS) return l2.p1; return l2.p1 + (l2.p2 - l2.p1) * (b / a); } Polygon convexCut(Polygon p, Line l) { Polygon q; for (int i = 0; i < (int)p.size(); i++) { Point a = p[i], b = p[(i + 1) % p.size()]; if (ccw(l.p1, l.p2, a) != -1) q.push_back(a); if (ccw(l.p1, l.p2, a) * ccw(l.p1, l.p2, b) < 0) q.push_back(getCrossPointLL(Line(a, b), l)); } return q; } Line bisector(Point p1, Point p2) { Circle c1 = Circle(p1, abs(p1 - p2)), c2 = Circle(p2, abs(p1 - p2)); pair<Point, Point> p = getCrossPoints(c1, c2); if (cross(p2 - p1, p.first - p1) > 0) swap(p.first, p.second); return Line(p.first, p.second); } int main() { Segment s; Point p; while (scanf("%lf,%lf,%f,%lf,%lf,%lf", &(s.p1.x), &(s.p1.y), &(s.p2.x), &(s.p2.y), &(p.x), &(p.y)) == 6) { Point ans = reflect(s, p); printf("%.8lf %.8lf\n", ans.x, ans.y); } return 0; }
#include <bits/stdc++.h> #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) using namespace std; static const int COUNTER_CLOCKWISE = 1; static const int CLOCKWISE = -1; static const int ONLINE_BACK = 2; static const int ONLINE_FRONT = -2; static const int ON_SEGMENT = 0; struct Point { double x, y; Point() {} Point(double x, double y) : 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 k) { return Point(x * k, y * k); } Point operator/(double k) { return Point(x / k, y / k); } double norm() { return x * x + y * y; } double abs() { return sqrt(norm()); } 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; typedef vector<Point> Polygon; struct Segment { Point p1, p2; Segment() {} Segment(Point p1, Point p2) : p1(p1), p2(p2) {} }; typedef Segment Line; struct Circle { Point c; double r; Circle(Point c, double r) : c(c), r(r) {} }; double norm(Vector a) { return a.x * a.x + a.y * a.y; } double abs(Vector a) { return sqrt(norm(a)); } 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; } bool isOrthogonal(Vector a, Vector b) { return equals(dot(a, b), 0.0); } bool isOrthogonal(Point a1, Point a2, Point b1, Point b2) { return isOrthogonal(a1 - a2, b1 - b2); } bool isOrthogonal(Segment s1, Segment s2) { return equals(dot(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0); } bool isParallel(Vector a, Vector b) { return equals(cross(a, b), 0.0); } bool isParallel(Point a1, Point a2, Point b1, Point b2) { return isParallel(a1 - a2, b1 - b2); } bool isParallel(Segment s1, Segment s2) { return equals(cross(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0); } Point project(Segment s, Point p) { Vector base = s.p2 - s.p1; double r = dot(p - s.p1, base) / norm(base); return s.p1 + base * r; } Point reflect(Segment s, Point p) { return p + (project(s, p) - p) * 2.0; } int ccw(Point p0, Point p1, Point p2) { Vector a = p1 - p0; Vector b = p2 - p0; if (cross(a, b) > EPS) return COUNTER_CLOCKWISE; if (cross(a, b) < -EPS) return CLOCKWISE; if (dot(a, b) < -EPS) return ONLINE_BACK; if (a.norm() < b.norm()) return ONLINE_FRONT; return ON_SEGMENT; } bool intersect(Point p1, Point p2, Point p3, Point p4) { return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0); } bool intersect(Segment s1, Segment s2) { return intersect(s1.p1, s1.p2, s2.p1, s2.p2); } double getDistanceLP(Line l, Point p) { return abs(cross(l.p2 - l.p1, p - l.p1) / abs(l.p2 - l.p1)); } double getDistanceSP(Segment s, Point p) { if (dot(s.p2 - s.p1, p - s.p1) < 0.0) return abs(p - s.p1); if (dot(s.p1 - s.p2, p - s.p2) < 0.0) return abs(p - s.p2); return getDistanceLP(s, p); } double getDistance(Segment s1, Segment s2) { if (intersect(s1, s2)) return 0.0; return min(min(getDistanceSP(s1, s2.p1), getDistanceSP(s1, s2.p2)), min(getDistanceSP(s2, s1.p1), getDistanceSP(s2, s1.p2))); } Point getCrossPoint(Segment s1, Segment s2) { Vector base = s2.p2 - s2.p1; double d1 = abs(cross(base, s1.p1 - s2.p1)); double d2 = abs(cross(base, s1.p2 - s2.p1)); double t = d1 / (d1 + d2); return s1.p1 + (s1.p2 - s1.p1) * t; } pair<Point, Point> getCrossPoints(Circle c, Line l) { Vector pr = project(l, c.c); Vector e = (l.p2 - l.p1) / abs(l.p2 - l.p1); double base = sqrt(c.r * c.r - norm(pr - c.c)); return make_pair(pr + e * base, pr - e * base); } double arg(Vector p) { return atan2(p.y, p.x); } Vector polar(double a, double r) { return Point(cos(r) * a, sin(r) * a); } pair<Point, Point> getCrossPoints(Circle c1, Circle c2) { double d = abs(c1.c - c2.c); double a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); double t = arg(c2.c - c1.c); return make_pair(c1.c + polar(c1.r, t + a), c1.c + polar(c1.r, t - a)); } int contains(Polygon g, Point p) { int n = g.size(); bool x = false; for (int i = 0; i < n; i++) { Point a = g[i] - p, b = g[(i + 1) % n] - p; if (fabs(cross(a, b)) < EPS && dot(a, b) < EPS) return 1; if (a.y > b.y) swap(a, b); if (a.y < EPS && EPS < b.y && cross(a, b) > EPS) x = !x; } return (x ? 2 : 0); } Polygon andrewScan(Polygon s) { Polygon u, l; if (s.size() < 3) return s; sort(s.begin(), s.end()); u.push_back(s[0]); u.push_back(s[1]); l.push_back(s[s.size() - 1]); l.push_back(s[s.size() - 2]); for (int i = 2; i < (int)s.size(); i++) { for (int n = u.size(); n >= 2 && ccw(u[n - 2], u[n - 1], s[i]) != CLOCKWISE; n--) { u.pop_back(); } u.push_back(s[i]); } for (int i = s.size() - 3; i >= 0; i--) { for (int n = l.size(); n >= 2 && ccw(l[n - 2], l[n - 1], s[i]) != CLOCKWISE; n--) { l.pop_back(); } l.push_back(s[i]); } reverse(l.begin(), l.end()); for (int i = u.size() - 2; i >= 1; i--) l.push_back(u[i]); return l; } double area(Polygon s) { double res = 0; for (int i = 0; i < (int)s.size(); i++) { res += cross(s[i], s[(i + 1) % s.size()]) / 2.0; } return abs(res); } Point getCrossPointLL(Line l1, Line l2) { double a = cross(l1.p2 - l1.p1, l2.p2 - l2.p1); double b = cross(l1.p2 - l1.p1, l1.p2 - l2.p1); if (abs(a) < EPS && abs(b) < EPS) return l2.p1; return l2.p1 + (l2.p2 - l2.p1) * (b / a); } Polygon convexCut(Polygon p, Line l) { Polygon q; for (int i = 0; i < (int)p.size(); i++) { Point a = p[i], b = p[(i + 1) % p.size()]; if (ccw(l.p1, l.p2, a) != -1) q.push_back(a); if (ccw(l.p1, l.p2, a) * ccw(l.p1, l.p2, b) < 0) q.push_back(getCrossPointLL(Line(a, b), l)); } return q; } Line bisector(Point p1, Point p2) { Circle c1 = Circle(p1, abs(p1 - p2)), c2 = Circle(p2, abs(p1 - p2)); pair<Point, Point> p = getCrossPoints(c1, c2); if (cross(p2 - p1, p.first - p1) > 0) swap(p.first, p.second); return Line(p.first, p.second); } int main() { Segment s; Point p; while (scanf("%lf,%lf,%lf,%lf,%lf,%lf", &(s.p1.x), &(s.p1.y), &(s.p2.x), &(s.p2.y), &(p.x), &(p.y)) == 6) { Point ans = reflect(s, p); printf("%.8lf %.8lf\n", ans.x, ans.y); } return 0; }
[["-", 51, 16, 31, 2, 3, 4, 0, 5, 0, 6], ["+", 51, 16, 31, 2, 3, 4, 0, 5, 0, 6]]
1
2,381
#include <stdio.h> #include <string.h> int main() { double x1, y1, x2, y2, xq, yq, x, y; while (0 <= scanf("%lf,%lf,%lf,%lf,%lf,%lf", &x1, &y1, &x2, &y2, &xq, &yq)) { x = ((x2 - x1) * (x2 - x1) * xq + (x2 - x1) * (y2 - y1) * (yq - y1) + (y2 - y1) * (y2 - y1) * x1) / ((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1)); y = (y2 - y1) * ((x2 - x1) * xq + (y2 - y1) * (yq - yq) - (x2 - x1) * x1) / ((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1)); printf("%lf %lf\n", 2 * x - xq, 2 * y - yq); } return 0; }
#include <stdio.h> #include <string.h> int main() { double x1, y1, x2, y2, xq, yq, x, y; while (0 <= scanf("%lf,%lf,%lf,%lf,%lf,%lf", &x1, &y1, &x2, &y2, &xq, &yq)) { x = ((x2 - x1) * (x2 - x1) * xq + (x2 - x1) * (y2 - y1) * (yq - y1) + (y2 - y1) * (y2 - y1) * x1) / ((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1)); y = (y2 - y1) * ((x2 - x1) * xq + (y2 - y1) * (yq - y1) - (x2 - x1) * x1) / ((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1)) + y1; printf("%lf %lf\n", 2 * x - xq, 2 * y - yq); } return 0; }
[["-", 31, 16, 12, 16, 12, 23, 0, 16, 12, 22], ["+", 31, 16, 12, 16, 12, 23, 0, 16, 12, 22], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 17, 72], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 12, 22]]
1
221
#include <cmath> #include <cstdio> #include <iostream> using namespace std; #define EPS 1e-6 int main(void) { double x1, y1, x2, y2, xq, yq; double a, b, c, d; double X, Y; double retx, rety; while (cin >> x1) { cin.ignore(); cin >> y1; cin.ignore(); cin >> x2; cin.ignore(); cin >> y2; cin.ignore(); cin >> xq; cin.ignore(); cin >> yq; // calc ax+by+c=0 X = x2 - x1; Y = y2 - y1; if (X == 0) { printf("%.6f %.6f\n", 2 * x1 - xq, y1); } else if (Y == 0) { printf("%.6f %.6f\n", x1, 2 * y1 - yq); } else { a = Y / X; b = y1 - (Y / X) * x1; double theta; theta = atan(a); retx = xq * cos(2 * theta) + yq * sin(2 * theta) - b * sin(2 * theta); rety = xq * sin(2 * theta) - yq * cos(2 * theta) + b * cos(2 * theta) + b; if (fabs(retx) < EPS) retx = 0; if (fabs(rety) < EPS) rety = 0; printf("%.6f %.6f\n", retx, rety); } } }
#include <cmath> #include <cstdio> #include <iostream> using namespace std; #define EPS 1e-6 int main(void) { double x1, y1, x2, y2, xq, yq; double a, b, c, d; double X, Y; double retx, rety; while (cin >> x1) { cin.ignore(); cin >> y1; cin.ignore(); cin >> x2; cin.ignore(); cin >> y2; cin.ignore(); cin >> xq; cin.ignore(); cin >> yq; // calc ax+by+c=0 X = x2 - x1; Y = y2 - y1; if (X == 0) { printf("%.6f %.6f\n", 2 * x1 - xq, yq); } else if (Y == 0) { printf("%.6f %.6f\n", xq, 2 * y1 - yq); } else { a = Y / X; b = y1 - (Y / X) * x1; double theta; theta = atan(a); retx = xq * cos(2 * theta) + yq * sin(2 * theta) - b * sin(2 * theta); rety = xq * sin(2 * theta) - yq * cos(2 * theta) + b * cos(2 * theta) + b; if (fabs(retx) < EPS) retx = 0; if (fabs(rety) < EPS) rety = 0; printf("%.6f %.6f\n", retx, rety); } } }
[["-", 64, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 64, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
1
301
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define mp make_pair #define pb push_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define repi(i, a, b) for (int i = (a); i < (b); i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef pair<int, int> pii; const int INF = 1 << 29; const double EPS = 1e-9; const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1}; int main() { double x1, y1, x2, y2, xq, yq; double m, x, y, c; while (~scanf("%lf,%lf,%lf,%lf,%lf,%lf", &x1, &y1, &x2, &y2, &xq, &yq)) { if (x2 == x1) { xq *= -1; printf("%.6f, %.6f\n", xq, yq); } else { m = (y2 - y1) / (x2 - x1); c = y2 - m * x2; x = (2 * m * yq - xq * (m * m - 1) - 2 * m * c) / (m * m + 1); y = (2 * m * xq + yq * (m * m - 1) + 2 * c) / (m * m + 1); printf("%.6f %.6f\n", x, y); } } return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define mp make_pair #define pb push_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define repi(i, a, b) for (int i = (a); i < (b); i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef pair<int, int> pii; const int INF = 1 << 29; const double EPS = 1e-9; const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1}; int main() { double x1, y1, x2, y2, xq, yq; double m, x, y, c; while (~scanf("%lf,%lf,%lf,%lf,%lf,%lf", &x1, &y1, &x2, &y2, &xq, &yq)) { if (x2 == x1) { xq *= -1; printf("%.6f %.6f\n", xq, yq); } else { m = (y2 - y1) / (x2 - x1); c = y2 - m * x2; x = (2 * m * yq - xq * (m * m - 1) - 2 * m * c) / (m * m + 1); y = (2 * m * xq + yq * (m * m - 1) + 2 * c) / (m * m + 1); printf("%.6f %.6f\n", x, y); } } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
348
#include <cstdio> #include <iostream> int main() { double x0, y0, x1, y1, x2, y2, xq, yq, x, y, m, a; char c; while (std::cin >> x1 >> c >> y1 >> c >> x2 >> c >> y2 >> c >> xq >> c >> yq) { if (x1 == x2) { y = y1; x = x1; } else { m = (y2 - y1) / (x2 - x1); a = m * m + 1; x0 = m * x1 - y1; y0 = xq + m * yq; x = (m * x0 + y0) / a; y = (-x0 + m * y0) / a; } printf("%.5f %.5f\n", 2 * x - xq, 2 * y - yq); } }
#include <cstdio> #include <iostream> int main() { double x0, y0, x1, y1, x2, y2, xq, yq, x, y, m, a; char c; while (std::cin >> x1 >> c >> y1 >> c >> x2 >> c >> y2 >> c >> xq >> c >> yq) { if (x1 == x2) { y = yq; x = x1; } else { m = (y2 - y1) / (x2 - x1); a = m * m + 1; x0 = m * x1 - y1; y0 = xq + m * yq; x = (m * x0 + y0) / a; y = (-x0 + m * y0) / a; } printf("%.5f %.5f\n", 2 * x - xq, 2 * y - yq); } }
[["-", 0, 57, 64, 9, 0, 1, 0, 11, 12, 22], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 12, 22]]
1
170
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <istream> #include <map> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define vci vector<int> #define vcs vector<string> #define pb push_back #define sz size() #define mapii map<int, int> #define mapci map<char, int> #define mapsi map<string, int> #define all(x) x.begin(), x.end() #define minit(a, i) memset(a, i, sizeof(a)); #define for_(i, a, b) for (int i = (int)a; i < (int)b; i++) #define for_d(i, a, b) for (int i = (int)a - 1; i >= b; i--) #define for_r(i, a, b, c) for (int i = (int)a; i < (int)b; i += c) #define for_dr(i, a, b, c) for (int i = (int)a - 1; i >= b; i -= c) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; } typedef long long ll; typedef double D; const int iINF = 2147483647; const ll lINF = 9223372036854775807; template <class T> inline void dbg(T t) { cout << t << endl; } // define eps double EPS = 1e-10; // Point2D & Vector2D struct Point2D { double x, y; Point2D(double a = 0, double b = 0) : x(a), y(b) {} }; typedef Point2D Vector2D; // Point2D's and Vector2D's operator Point2D operator+(Point2D a, Point2D b) { return Point2D(a.x + b.x, a.y + b.y); } Point2D operator-(Point2D a, Point2D b) { return Point2D(a.x - b.x, a.y - b.y); } Point2D operator*(Point2D a, double d) { return Point2D(a.x * d, a.y * d); } Point2D operator/(Point2D a, double d) { return Point2D(a.x / d, a.y / d); } bool operator<(const Point2D &a, const Point2D &b) { return a.x != b.x ? a.x < b.x : a.y < b.y; } bool operator>(const Point2D &a, const Point2D &b) { return b.x != a.x ? b.x < a.x : b.y < a.y; } bool operator==(const Point2D &a, const Point2D &b) { return fabs(a.x - b.x) < EPS && fabs(a.y - b.y) < EPS; } // basic function double norm(Point2D a) { return a.x * a.x + a.y * a.y; } double dot2D(const Vector2D &a, const Vector2D &b) { return a.x * b.x + a.y * b.y; } double cross2D(const Vector2D &a, const Vector2D &b) { return a.x * b.y - a.y * b.x; } double dist2D(const Point2D &a, const Point2D &b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } bool is_vertical(const Vector2D &a, const Vector2D &b) { return fabs(dot2D(a, b)) < EPS; } bool is_parrallel(const Vector2D &a, const Vector2D &b) { return fabs(cross2D(a, b)) < EPS; } // Line struct Line { Point2D s, e; // start, end #define sx s.x #define sy s.y #define ex e.x #define ey e.y Line(double _x = 0, double _y = 0, double x_ = 0, double y_ = 0) { s.x = _x; s.y = _y; e.x = x_; e.y = y_; } }; bool is_cross(const Line &a, const Line &b) { return (cross2D(a.e - a.s, b.s - a.s) * cross2D(a.e - a.s, b.e - a.s) < EPS) && (cross2D(b.e - b.s, a.s - b.s) * cross2D(b.e - b.s, a.e - b.s) < EPS); } bool is_parrallel(const Line &a, const Line &b) { return is_parrallel(a.s - a.e, b.s - b.e); } // 線分の交点 Point2D crossPoint(const Line &l1, const Line &l2) { Point2D b = l2.e - l2.s; double d1 = fabs(cross2D(b, l1.s - l2.s)); double d2 = fabs(cross2D(b, l1.e - l2.s)); double t = d1 / (d1 + d2); return l1.s + (l1.e - l1.s) * t; } // 点と線の距離 double point_line_dist(const Point2D &A, const Line &l) { double a = (l.s.y - l.e.y) / (l.s.x - l.e.x); double c = (-1) * a * l.s.x - l.s.y; return fabs(a * A.x + A.y + c) / sqrt(a * a + 1.0); } // 点Aの線分lにおける線対称 Point2D line_symmetry(const Point2D &A, const Line &l) { complex<D> p1(l.sx, l.sy), p2(l.ex, l.ey), q(A.x, A.y); complex<D> r = p1 + polar(abs(q - p1), 2 * arg(p2 - p1) - arg(q - p1)); Point2D ret(r.real(), r.imag()); return ret; } int main() { Point2D A; Line l; char c; while (cin >> l.sx >> c >> l.sy >> c >> l.ex >> c >> l.ey >> c >> A.x >> c >> A.y) { Point2D ans = line_symmetry(A, l); printf("%lf %lf", ans.x, ans.y); } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <istream> #include <map> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define vci vector<int> #define vcs vector<string> #define pb push_back #define sz size() #define mapii map<int, int> #define mapci map<char, int> #define mapsi map<string, int> #define all(x) x.begin(), x.end() #define minit(a, i) memset(a, i, sizeof(a)); #define for_(i, a, b) for (int i = (int)a; i < (int)b; i++) #define for_d(i, a, b) for (int i = (int)a - 1; i >= b; i--) #define for_r(i, a, b, c) for (int i = (int)a; i < (int)b; i += c) #define for_dr(i, a, b, c) for (int i = (int)a - 1; i >= b; i -= c) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; } typedef long long ll; typedef double D; const int iINF = 2147483647; const ll lINF = 9223372036854775807; template <class T> inline void dbg(T t) { cout << t << endl; } // define eps double EPS = 1e-10; // Point2D & Vector2D struct Point2D { double x, y; Point2D(double a = 0, double b = 0) : x(a), y(b) {} }; typedef Point2D Vector2D; // Point2D's and Vector2D's operator Point2D operator+(Point2D a, Point2D b) { return Point2D(a.x + b.x, a.y + b.y); } Point2D operator-(Point2D a, Point2D b) { return Point2D(a.x - b.x, a.y - b.y); } Point2D operator*(Point2D a, double d) { return Point2D(a.x * d, a.y * d); } Point2D operator/(Point2D a, double d) { return Point2D(a.x / d, a.y / d); } bool operator<(const Point2D &a, const Point2D &b) { return a.x != b.x ? a.x < b.x : a.y < b.y; } bool operator>(const Point2D &a, const Point2D &b) { return b.x != a.x ? b.x < a.x : b.y < a.y; } bool operator==(const Point2D &a, const Point2D &b) { return fabs(a.x - b.x) < EPS && fabs(a.y - b.y) < EPS; } // basic function double norm(Point2D a) { return a.x * a.x + a.y * a.y; } double dot2D(const Vector2D &a, const Vector2D &b) { return a.x * b.x + a.y * b.y; } double cross2D(const Vector2D &a, const Vector2D &b) { return a.x * b.y - a.y * b.x; } double dist2D(const Point2D &a, const Point2D &b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } bool is_vertical(const Vector2D &a, const Vector2D &b) { return fabs(dot2D(a, b)) < EPS; } bool is_parrallel(const Vector2D &a, const Vector2D &b) { return fabs(cross2D(a, b)) < EPS; } // Line struct Line { Point2D s, e; // start, end #define sx s.x #define sy s.y #define ex e.x #define ey e.y Line(double _x = 0, double _y = 0, double x_ = 0, double y_ = 0) { s.x = _x; s.y = _y; e.x = x_; e.y = y_; } }; bool is_cross(const Line &a, const Line &b) { return (cross2D(a.e - a.s, b.s - a.s) * cross2D(a.e - a.s, b.e - a.s) < EPS) && (cross2D(b.e - b.s, a.s - b.s) * cross2D(b.e - b.s, a.e - b.s) < EPS); } bool is_parrallel(const Line &a, const Line &b) { return is_parrallel(a.s - a.e, b.s - b.e); } // 線分の交点 Point2D crossPoint(const Line &l1, const Line &l2) { Point2D b = l2.e - l2.s; double d1 = fabs(cross2D(b, l1.s - l2.s)); double d2 = fabs(cross2D(b, l1.e - l2.s)); double t = d1 / (d1 + d2); return l1.s + (l1.e - l1.s) * t; } // 点と線の距離 double point_line_dist(const Point2D &A, const Line &l) { double a = (l.s.y - l.e.y) / (l.s.x - l.e.x); double c = (-1) * a * l.s.x - l.s.y; return fabs(a * A.x + A.y + c) / sqrt(a * a + 1.0); } // 点Aの線分lにおける線対称 Point2D line_symmetry(const Point2D &A, const Line &l) { complex<D> p1(l.sx, l.sy), p2(l.ex, l.ey), q(A.x, A.y); complex<D> r = p1 + polar(abs(q - p1), 2 * arg(p2 - p1) - arg(q - p1)); Point2D ret(r.real(), r.imag()); return ret; } int main() { Point2D A; Line l; char c; while (cin >> l.sx >> c >> l.sy >> c >> l.ex >> c >> l.ey >> c >> A.x >> c >> A.y) { Point2D ans = line_symmetry(A, l); printf("%lf %lf\n", ans.x, ans.y); } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
1
1,276
import sys from math import* for e in sys.stdin: a,b,c,d,x,y=map(float,e.split(',')) g=-2*(atan2(x-b,y-a)-atan2(d-b,c-a));s,t=sin(g),cos(g);x-=a;y-=b print(f'{t*x-s*y+a:.6f} {s*x+t*y+b:.6f}')
import sys from math import* for e in sys.stdin: a,b,c,d,x,y=map(float,e.split(',')) g=-2*(atan2(y-b,x-a)-atan2(d-b,c-a));s,t=sin(g),cos(g);x-=a;y-=b print(f'{t*x-s*y+a:.6f} {s*x+t*y+b:.6f}')
[["-", 0, 657, 31, 652, 3, 4, 0, 657, 31, 22], ["+", 0, 657, 31, 652, 3, 4, 0, 657, 31, 22]]
5
120
import sys for line in sys.stdin.readlines(): x1,y1,x2,y2,xq,yq=list(map(float,line.strip().split(","))) if x1==x2: x,y=-xq+2*x1,yq elif y1==y2: x,y=xq,-yq+2*y1 else: m=(y1-y2)/(x1-x2) n=-m*x1+y1 x,y= (2*yq*m-2*m*n+xq-m*m*xq)/(m*m+1),(xq-x)/m+yq; print("{:.6f} {:.6f}".format(x+0,y+0))
import sys for line in sys.stdin.readlines(): x1,y1,x2,y2,xq,yq=list(map(float,line.strip().split(","))) if x1==x2: x,y=-xq+2*x1,yq elif y1==y2: x,y=xq,-yq+2*y1 else: m=(y1-y2)/(x1-x2) n=-m*x1+y1 x=(2*yq*m-2*m*n+xq-m*m*xq)/(m*m+1) y=(xq-x)/m+yq print("{:.6f} {:.6f}".format(x+0,y+0))
[["-", 8, 196, 0, 1, 0, 662, 31, 684, 0, 21], ["-", 8, 196, 0, 1, 0, 662, 31, 684, 0, 22], ["-", 8, 196, 0, 1, 0, 662, 12, 432, 0, 21], ["+", 75, 76, 8, 196, 0, 1, 0, 662, 31, 22], ["+", 75, 76, 8, 196, 0, 1, 0, 662, 0, 32], ["-", 8, 196, 0, 57, 75, 76, 8, 196, 0, 35]]
5
163
import java.util.*; class Main { Scanner sc = new Scanner(System.in); public void run() { int[] merry = new int[] {1, 4, 1, 4, 1, 2, 1, 2}; while (sc.hasNext()) { int index = 0, max = -1, val = 0; int[] people = new int[] {sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt()}; for (int i = 0; i < 8; i++) { int sum = 0; for (int j = 0; j < 8; j++) { sum += Math.min(merry[(i + j) % 8], people[j]); } if (sum > max) { index = i; max = sum; int v = 0; for (int j = 0; j < 8; j++) { v = merry[(i + j) % 8] + v * 10; } val = v; } else if (sum == max) { int v = 0; for (int j = 0; j < 8; j++) { v = merry[(i + j) % 8] + v * 10; } if (v < val) { index = i; max = sum; val = v; } } } for (int i = 0; i < 8; i++) { pr((i == 0 ? "" : " ") + merry[(i + index) % 8]); } } } public static void main(String[] args) { new Main().run(); } public static void pr(Object o) { System.out.print(o); } public static void ln(Object o) { System.out.println(o); } public static void ln() { System.out.println(); } } class P { double x; double y; P(double _x, double _y) { x = _x; y = _y; } public static final P Xunit = new P(1, 0); public static final P Yunit = new P(0, 1); public static double dot(P a, P b) { return a.x * b.x + a.y * b.y; } public static double cross(P a, P b) { return a.x * b.y - a.y * b.x; } public static boolean isParallel(P a1, P a2, P b1, P b2) { return Math.abs(cross(a2.s(a1), b2.s(b1))) < Double.MIN_NORMAL; } public static boolean isVertical(P a1, P a2, P b1, P b2) { return Math.abs(dot(a2.s(a1), b2.s(b1))) < Double.MIN_NORMAL; } public P a(P a) { return new P(x + a.x, y + a.y); } public P s(P a) { return new P(x - a.x, y - a.y); } public double norm() { return Math.sqrt(x * x + y * y); } public double arg() { double s = Math.acos(x / norm()); return y > 0 ? s : 2 * Math.PI - s; } public P rotate(double t) { return new P(Math.cos(t) * this.x - Math.sin(t) * this.y, Math.sin(t) * this.x + Math.cos(t) * this.y); } public static int ccw(P a, P b, P c) { if (P.cross(b.s(a), c.s(a)) > Double.MIN_NORMAL) { return 1; } if (cross(b.s(a), c.s(a)) < -Double.MIN_NORMAL) { return -1; } if (dot(b, c) < -Double.MIN_NORMAL) return 2; if (b.norm() < c.norm() - Double.MIN_NORMAL) return -2; return 0; } @Override public boolean equals(Object obj) { if (obj instanceof P) { P p = (P)obj; return x == p.x && y == p.y; } return false; } public String toString() { return "(" + x + "," + y + ")"; } }
import java.util.*; class Main { Scanner sc = new Scanner(System.in); public void run() { int[] merry = new int[] {1, 4, 1, 4, 1, 2, 1, 2}; while (sc.hasNext()) { int index = 0, max = -1, val = 0; int[] people = new int[] {sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt()}; for (int i = 0; i < 8; i++) { int sum = 0; for (int j = 0; j < 8; j++) { sum += Math.min(merry[(i + j) % 8], people[j]); } if (sum > max) { index = i; max = sum; int v = 0; for (int j = 0; j < 8; j++) { v = merry[(i + j) % 8] + v * 10; } val = v; } else if (sum == max) { int v = 0; for (int j = 0; j < 8; j++) { v = merry[(i + j) % 8] + v * 10; } if (v < val) { index = i; max = sum; val = v; } } } for (int i = 0; i < 8; i++) { pr((i == 0 ? "" : " ") + merry[(i + index) % 8]); } ln(); } } public static void main(String[] args) { new Main().run(); } public static void pr(Object o) { System.out.print(o); } public static void ln(Object o) { System.out.println(o); } public static void ln() { System.out.println(); } } class P { double x; double y; P(double _x, double _y) { x = _x; y = _y; } public static final P Xunit = new P(1, 0); public static final P Yunit = new P(0, 1); public static double dot(P a, P b) { return a.x * b.x + a.y * b.y; } public static double cross(P a, P b) { return a.x * b.y - a.y * b.x; } public static boolean isParallel(P a1, P a2, P b1, P b2) { return Math.abs(cross(a2.s(a1), b2.s(b1))) < Double.MIN_NORMAL; } public static boolean isVertical(P a1, P a2, P b1, P b2) { return Math.abs(dot(a2.s(a1), b2.s(b1))) < Double.MIN_NORMAL; } public P a(P a) { return new P(x + a.x, y + a.y); } public P s(P a) { return new P(x - a.x, y - a.y); } public double norm() { return Math.sqrt(x * x + y * y); } public double arg() { double s = Math.acos(x / norm()); return y > 0 ? s : 2 * Math.PI - s; } public P rotate(double t) { return new P(Math.cos(t) * this.x - Math.sin(t) * this.y, Math.sin(t) * this.x + Math.cos(t) * this.y); } public static int ccw(P a, P b, P c) { if (P.cross(b.s(a), c.s(a)) > Double.MIN_NORMAL) { return 1; } if (cross(b.s(a), c.s(a)) < -Double.MIN_NORMAL) { return -1; } if (dot(b, c) < -Double.MIN_NORMAL) return 2; if (b.norm() < c.norm() - Double.MIN_NORMAL) return -2; return 0; } @Override public boolean equals(Object obj) { if (obj instanceof P) { P p = (P)obj; return x == p.x && y == p.y; } return false; } public String toString() { return "(" + x + "," + y + ")"; } }
[["+", 0, 52, 8, 196, 0, 1, 0, 492, 141, 22], ["+", 8, 196, 0, 1, 0, 492, 3, 4, 0, 24], ["+", 8, 196, 0, 1, 0, 492, 3, 4, 0, 25], ["+", 8, 196, 0, 52, 8, 196, 0, 1, 0, 35]]
3
1,002
#include <stdio.h> int main(void) { int go_round[8] = {1, 2, 1, 2, 1, 4, 1, 4}; int client[8]; int minimum[8], min, temp; int seisuu; int i, j; int temp_s; while (scanf("%d%d%d%d%d%d%d%d", &client[0], &client[1], &client[2], &client[3], &client[4], &client[5], &client[6], &client[7]) != EOF) { for (i = 0; i < 8; i++) { temp = temp_s = 0; for (j = 0; j < 8; j++) { temp += (client[j] - go_round[(i + j) % 8] > 0) ? (client[j] - go_round[(i + j) % 8]) : 0; temp_s = temp_s * 10 + go_round[(i + j) % 8]; } if (temp <= min || i == 0) { if (temp == min && i != 0) { if (temp_s < seisuu) { min = temp; seisuu = temp_s; } for (j = 0; j < 8; j++) { minimum[j] = go_round[(i + j) % 8]; } } else { min = temp; seisuu = temp_s; for (j = 0; j < 8; j++) { minimum[j] = go_round[(i + j) % 8]; } } } } for (i = 0; i < 8; i++) { if (i != 0) { printf(" "); } printf("%d", minimum[i]); } printf("\n"); } return (0); }
#include <stdio.h> int main(void) { int go_round[8] = {1, 2, 1, 2, 1, 4, 1, 4}; int client[8]; int minimum[8], min, temp; int seisuu; int i, j; int temp_s; while (scanf("%d%d%d%d%d%d%d%d", &client[0], &client[1], &client[2], &client[3], &client[4], &client[5], &client[6], &client[7]) != EOF) { for (i = 0; i < 8; i++) { temp = temp_s = 0; for (j = 0; j < 8; j++) { temp += (client[j] - go_round[(i + j) % 8] > 0) ? (client[j] - go_round[(i + j) % 8]) : 0; temp_s = temp_s * 10 + go_round[(i + j) % 8]; } if (temp <= min || i == 0) { if (temp == min && i != 0) { if (temp_s < seisuu) { min = temp; seisuu = temp_s; for (j = 0; j < 8; j++) { minimum[j] = go_round[(i + j) % 8]; } } } else { min = temp; seisuu = temp_s; for (j = 0; j < 8; j++) { minimum[j] = go_round[(i + j) % 8]; } } } } for (i = 0; i < 8; i++) { if (i != 0) { printf(" "); } printf("%d", minimum[i]); } printf("\n"); } return (0); }
[["-", 0, 57, 64, 9, 0, 57, 64, 9, 0, 46], ["+", 0, 57, 64, 9, 0, 7, 8, 9, 0, 46]]
0
383
#include <stdio.h> #define N 8 int main(void) { int i, j; int d[N] = {1, 2, 1, 2, 1, 4, 1, 4}; int num[N]; int dt[N][N]; int p[N]; /* 配列から値へ */ for (i = 0; i < N; i++) { num[i] = 0; for (j = 0; j < N; j++) { num[i] *= 10; num[i] += d[(i + j) % N]; } } /* 値のソート */ for (i = 0; i < N - 1; i++) { int w = num[i + 1]; for (j = i + 1; j > 0 && num[j - 1] > w; j--) num[j] = num[j - 1]; num[j] = w; } /* 値から配列へ */ for (i = 0; i < N; i++) { for (j = N - 1; j >= 0; j--) { dt[i][j] = num[i] % 10; num[i] /= 10; } } /* 実際の処理 */ while (scanf("%d%d%d%d%d%d%d%d", &p[0], &p[1], &p[2], &p[3], &p[4], &p[5], &p[6], &p[7]) == 8) { int dsum, msumind, msum; // 合計値,合計最大値インデックス /* 0番目を最小として始める */ for (j = 0; j < N; j++) if (p[j] > dt[0][j]) dsum += p[j] - dt[0][j]; msum = dsum; msumind = 0; /* 1番目以降を見ていく */ for (i = 1; i < N; i++) { dsum = 0; for (j = 0; j < N; j++) if (p[j] > dt[i][j]) dsum += p[j] - dt[i][j]; if (msum > dsum) { msumind = i; msum = dsum; } } /* 出力 */ for (j = 0; j < N; j++) { printf("%d", dt[msumind][j]); if (j < N - 1) printf(" "); } printf("\n"); } return (0); }
#include <stdio.h> #define N 8 int main(void) { int i, j; int d[N] = {1, 2, 1, 2, 1, 4, 1, 4}; int num[N]; int dt[N][N]; int p[N]; /* 配列から値へ */ for (i = 0; i < N; i++) { num[i] = 0; for (j = 0; j < N; j++) { num[i] *= 10; num[i] += d[(i + j) % N]; } } /* 値のソート */ for (i = 0; i < N - 1; i++) { int w = num[i + 1]; for (j = i + 1; j > 0 && num[j - 1] > w; j--) num[j] = num[j - 1]; num[j] = w; } /* 値から配列へ */ for (i = 0; i < N; i++) { for (j = N - 1; j >= 0; j--) { dt[i][j] = num[i] % 10; num[i] /= 10; } } /* 実際の処理 */ while (scanf("%d%d%d%d%d%d%d%d", &p[0], &p[1], &p[2], &p[3], &p[4], &p[5], &p[6], &p[7]) == 8) { int dsum, msumind, msum; // 合計値,合計最大値インデックス /* 0番目を最小として始める */ dsum = 0; for (j = 0; j < N; j++) if (p[j] > dt[0][j]) dsum += p[j] - dt[0][j]; msum = dsum; msumind = 0; /* 1番目以降を見ていく */ for (i = 1; i < N; i++) { dsum = 0; for (j = 0; j < N; j++) if (p[j] > dt[i][j]) dsum += p[j] - dt[i][j]; if (msum > dsum) { msumind = i; msum = dsum; } } /* 出力 */ for (j = 0; j < N; j++) { printf("%d", dt[msumind][j]); if (j < N - 1) printf(" "); } printf("\n"); } 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]]
0
508
#include <algorithm> #include <climits> #include <cstdio> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; const int latte[8] = {4, 1, 4, 1, 2, 1, 2, 1}, pri[8] = {0, 5, 1, 7, 3, 6, 2, 4}; int main() { int human[8]; while (cin >> human[0]) { int ansv = 1000000000, anspri = 10, ans; for (int i = 1; i < 8; i++) { cin >> human[i]; } for (int i = 0; i < 8; i++) { int v = 0; for (int j = 0; j < 8; j++) { v += max(0, human[j] - latte[(i + j) % 8]); } if (ansv > v || (ansv == v && anspri > pri[i])) { ansv = v; anspri = pri[i]; ans = i; } } printf("%d", latte[ans]); for (int i = 1; i < 8; i++) { printf(" %d", latte[(ans + i) % 8]); } puts(""); } return 0; }
#include <algorithm> #include <climits> #include <cstdio> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; const int latte[8] = {4, 1, 4, 1, 2, 1, 2, 1}, pri[8] = {0, 5, 1, 7, 3, 6, 2, 4}; int main() { int human[8]; while (cin >> human[0]) { int ansv = 1000000000, anspri = 10, ans; for (int i = 1; i < 8; i++) { cin >> human[i]; } for (int i = 0; i < 8; i++) { int v = 0; for (int j = 0; j < 8; j++) { v += max(0, human[j] - latte[(i + j) % 8]); } if (ansv > v || (ansv == v && anspri < pri[i])) { ansv = v; anspri = pri[i]; ans = i; } } printf("%d", latte[ans]); for (int i = 1; i < 8; i++) { printf(" %d", latte[(ans + i) % 8]); } puts(""); } return 0; }
[["-", 51, 16, 12, 23, 0, 16, 12, 16, 17, 47], ["+", 51, 16, 12, 23, 0, 16, 12, 16, 17, 18]]
1
284
import re import sys for s in sys.stdin: print(re.sub(r"@(\d)(.)", lambda m: m.group(2)*int(m.group(1)), s))
import re import sys for s in sys.stdin: print(re.sub(r"@(\d)(.)", lambda m: m.group(2)*int(m.group(1)), s.rstrip()))
[["+", 0, 652, 3, 4, 0, 652, 63, 319, 0, 131], ["+", 0, 652, 3, 4, 0, 652, 63, 319, 319, 22], ["+", 0, 652, 3, 4, 0, 652, 3, 4, 0, 24], ["+", 0, 652, 3, 4, 0, 652, 3, 4, 0, 25]]
5
44
import sys for line in sys.stdin: line = line.strip() i = 0 ans = "" while i < len(line): if line[i] == "@": m = int(line[i+1]) c = line[i+2] i += 2 ans += m*c else: ans += line[i] i += 1 print(ans)
import sys for line in sys.stdin: line = line.strip() i = 0 ans = "" while i < len(line): if line[i] == "@": m = int(line[i+1]) c = line[i+2] i += 3 ans += m*c else: ans += line[i] i += 1 print(ans)
[["-", 0, 57, 64, 196, 0, 1, 0, 677, 12, 612], ["+", 0, 57, 64, 196, 0, 1, 0, 677, 12, 612]]
5
83
import sys for e in sys.stdin: a='';b=n=0 for c in e: if'@'==c:b=1 elif b&1:n=int(c);b=2 elif b&2:a+=c*n;b=0 else:a+=c print(a)
import sys for e in sys.stdin: a='';b=n=0 for c in e[:-1]: if'@'==c:b=1 elif b&1:n=int(c);b=2 elif b&2:a+=c*n;b=0 else:a+=c print(a)
[["+", 0, 7, 8, 196, 0, 7, 12, 206, 0, 70], ["+", 0, 7, 12, 206, 206, 663, 0, 664, 17, 33], ["+", 0, 7, 12, 206, 206, 663, 0, 664, 28, 612], ["+", 0, 7, 8, 196, 0, 7, 12, 206, 0, 73], ["+", 0, 656, 0, 7, 8, 196, 0, 7, 0, 102]]
5
72
while True: try: inp = input() i, msg = 0, "" while i < len(inp)-1: if inp[i] == "@": msg += inp[i+2]*int(inp[i+1]) i += 3 else: msg += inp[i] i += 1 print(msg) except: break
while True: try: inp = input() i, msg = 0, "" while i < len(inp): if inp[i] == "@": msg += inp[i+2]*int(inp[i+1]) i += 3 else: msg += inp[i] i += 1 print(msg) except: break
[["-", 8, 196, 0, 52, 15, 666, 0, 657, 17, 33], ["-", 8, 196, 0, 52, 15, 666, 0, 657, 12, 612]]
5
77
while True: try: char = input() ans = "" while "@" in char: ans += char[0:char.index("@")] char = char[char.index("@"):] char = char[2] * int(char[1]) + char[3:] ans += char except: break
while True: try: char = input() ans = "" while "@" in char: ans += char[0:char.index("@")] char = char[char.index("@"):] char = char[2] * int(char[1]) + char[3:] ans += char print(ans) except: break
[["+", 0, 246, 8, 196, 0, 1, 0, 652, 63, 22], ["+", 8, 196, 0, 1, 0, 652, 3, 4, 0, 24], ["+", 8, 196, 0, 1, 0, 652, 3, 4, 0, 22], ["+", 8, 196, 0, 1, 0, 652, 3, 4, 0, 25]]
5
76
<?php while (($line = trim(fgets(STDIN))) !== '0') { $n = (int)$line; //0埋め $arr = array_fill(0, $n, array_fill(0, $n, 0)); $x = (int)($n / 2); $y = $x + 1; //最初の入力 $arr[$y][$x] = 1; $m = pow($n, 2); $cnt = 2; $right = true; //全ての入力が終わるまで while ($cnt <= $m) { //右下モード if ($right) { $x = $x + 1 < $n ? $x + 1 : 0; $y = $y + 1 < $n ? $y + 1 : 0; //既に入力済み if ($arr[$y][$x]) { //左下モード $right = false; } else { $arr[$y][$x] = $cnt; //右下モードにして次の数字 $right = true; $cnt++; } //左下モード } else { $x = $x - 1 < 0 ? $n - 1 : $x - 1; $y = $y + 1 < $n ? $y + 1 : 0; //既に入力済み if ($arr[$y][$x]) { continue; } $arr[$y][$x] = $cnt; //右下モードにして次の数字 $right = true; $cnt++; } } for ($y = 0; $y < $n; $y++) { for ($x = 0; $x < $n; $x++) { printf('%4s', $arr[$y][$x]); } echo PHP_EOL; } echo PHP_EOL; }
<?php while (($line = trim(fgets(STDIN))) !== '0') { $n = (int)$line; //0埋め $arr = array_fill(0, $n, array_fill(0, $n, 0)); $x = (int)($n / 2); $y = $x + 1; //最初の入力 $arr[$y][$x] = 1; $m = pow($n, 2); $cnt = 2; $right = true; //全ての入力が終わるまで while ($cnt <= $m) { //右下モード if ($right) { $x = $x + 1 < $n ? $x + 1 : 0; $y = $y + 1 < $n ? $y + 1 : 0; //既に入力済み if ($arr[$y][$x]) { //左下モード $right = false; } else { $arr[$y][$x] = $cnt; //右下モードにして次の数字 $right = true; $cnt++; } //左下モード } else { $x = $x - 1 < 0 ? $n - 1 : $x - 1; $y = $y + 1 < $n ? $y + 1 : 0; //既に入力済み if ($arr[$y][$x]) { continue; } $arr[$y][$x] = $cnt; //右下モードにして次の数字 $right = true; $cnt++; } } for ($y = 0; $y < $n; $y++) { for ($x = 0; $x < $n; $x++) { printf('%4s', $arr[$y][$x]); } echo PHP_EOL; } }
[["-", 0, 493, 0, 52, 8, 9, 0, 608, 0, 621], ["-", 0, 493, 0, 52, 8, 9, 0, 608, 0, 141], ["-", 0, 493, 0, 52, 8, 9, 0, 608, 0, 35]]
6
359
import java.lang.*; import java.math.*; import java.util.*; public class Main { Scanner sc = new Scanner(System.in); void run() { for (;;) { int n = sc.nextInt(); if (n == 0) break; int[][] ms = new int[n][n]; int posy = n / 2 + 1; int posx = n / 2; for (int i = 1; i < n * n + 1; i++) { int ny = posy + 1; int nx = posx + 1; if (ny >= n) { ny = 0; } if (nx >= n) { nx = 0; } for (; ms[ny][nx] != 0;) { ny += 1; nx -= 1; if (ny >= n) { ny = 0; } if (nx < 0) { nx = n - 1; } } ms[ny][nx] = i; posx = nx; posy = ny; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.printf("%4d", ms[i][j]); } System.out.println(); } } } public static void main(String[] args) { Main m = new Main(); m.run(); } }
import java.lang.*; import java.math.*; import java.util.*; public class Main { Scanner sc = new Scanner(System.in); void run() { for (;;) { int n = sc.nextInt(); if (n == 0) break; int[][] ms = new int[n][n]; int posy = n / 2; int posx = n / 2 - 1; for (int i = 1; i < n * n + 1; i++) { int ny = posy + 1; int nx = posx + 1; if (ny >= n) { ny = 0; } if (nx >= n) { nx = 0; } for (; ms[ny][nx] != 0;) { ny += 1; nx -= 1; if (ny >= n) { ny = 0; } if (nx < 0) { nx = n - 1; } } ms[ny][nx] = i; posx = nx; posy = ny; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.printf("%4d", ms[i][j]); } System.out.println(); } } } public static void main(String[] args) { Main m = new Main(); m.run(); } }
[["-", 8, 196, 0, 503, 49, 200, 51, 16, 17, 72], ["-", 8, 196, 0, 503, 49, 200, 51, 16, 12, 499], ["+", 8, 196, 0, 503, 49, 200, 51, 16, 17, 33], ["+", 8, 196, 0, 503, 49, 200, 51, 16, 12, 499]]
3
310
#include <stdio.h> #define MAX 15 int main(void) { int n, i, j, nx, ny, sum; int magic[MAX][MAX]; while (1) { for (i = 0; i < MAX; i++) { for (j = 0; j < MAX; j++) { magic[i][j] = 0; } } scanf("%d", &n); if (n == 0) { break; } magic[n / 2 + 1][n / 2] = 1; nx = n / 2 + 1; ny = n / 2; for (i = 2; i <= n * n; i++) { nx++; ny++; while (1) { nx = (nx + n) % n; ny = (ny + n) % n; if (magic[nx][ny] == 0) { magic[nx][ny] = i; break; } nx++; ny--; } } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%3d", magic[i][j]); } printf("\n"); } } return 0; }
#include <stdio.h> #define MAX 15 int main(void) { int n, i, j, nx, ny, sum; int magic[MAX][MAX]; while (1) { for (i = 0; i < MAX; i++) { for (j = 0; j < MAX; j++) { magic[i][j] = 0; } } scanf("%d", &n); if (n == 0) { break; } magic[n / 2 + 1][n / 2] = 1; nx = n / 2 + 1; ny = n / 2; for (i = 2; i <= n * n; i++) { nx++; ny++; while (1) { nx = (nx + n) % n; ny = (ny + n) % n; if (magic[nx][ny] == 0) { magic[nx][ny] = i; break; } nx++; ny--; } } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%4d", magic[i][j]); } printf("\n"); } } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
0
266
#include <stdio.h> int b[15][15]; void reset() { int i, j; for (i = 0; i < 15; i++) { for (j = 0; j < 15; j++) { b[i][j] = 0; } } } void magic_square(int x, int y, int n, int c) { if (c == n * n) return; if (b[x][y] == 0) { b[x][y] = ++c; if (++x == n) x = 0; if (++y == n) y = 0; magic_square(x, y, n, c); } else { if (++x == n) x = 0; if (--y == -1) y = n - 1; magic_square(x, y, n, c); } } int main() { int n, i, j; while (scanf("%d", &n), n) { reset(); magic_square(n / 2 + 1, n / 2, n, 0); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (j < n - 1) printf("%4d \b", b[i][j]); else printf("%4d\n", b[i][j]); } } } return 0; }
#include <stdio.h> int b[15][15]; void reset() { int i, j; for (i = 0; i < 15; i++) { for (j = 0; j < 15; j++) { b[i][j] = 0; } } } void magic_square(int x, int y, int n, int c) { if (c == n * n) return; if (b[x][y] == 0) { b[x][y] = ++c; if (++x == n) x = 0; if (++y == n) y = 0; magic_square(x, y, n, c); } else { if (++x == n) x = 0; if (--y == -1) y = n - 1; magic_square(x, y, n, c); } } int main() { int n, i, j; while (scanf("%d", &n), n) { reset(); magic_square(n / 2 + 1, n / 2, n, 0); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (j < n - 1) printf("%4d", b[i][j]); else printf("%4d\n", b[i][j]); } } } return 0; }
[["-", 64, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["-", 64, 1, 0, 2, 3, 4, 0, 5, 0, 44], ["+", 64, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
0
309
#include <stdio.h> int main() { int n, i, an[15][15], bn[15][15], a, b, n1, c = 2, d = 0, j; while (1) { for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { an[i][j] = bn[i][j] = 0; } } c = 2; d = 0; scanf("%d", &n); if (n == 0) break; an[n / 2 + 1][n / 2] = 1; bn[n / 2 + 1][n / 2] = 1; a = n / 2 + 1; b = n / 2; n1 = n - 1; while (1) { a++; b++; if (a > n1) a = 0; if (b > n1) b = 0; if (bn[a][b] != 1) { an[a][b] = c; bn[a][b] = 1; c++; } else if (bn[a][b] == 1) { a++; b--; if (a > n1) a = 0; if (b < 0) b = n1; an[a][b] = c; bn[a][b] = 1; c++; } if (c == n * n + 1) break; } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%4d", an[i][j]); } printf("\n"); } } return 0; }
#include <stdio.h> int main() { int n, i, an[15][15], bn[15][15], a, b, n1, c = 2, d = 0, j; while (1) { for (i = 0; i < 15; i++) { for (j = 0; j < 15; j++) { an[i][j] = bn[i][j] = 0; } } c = 2; d = 0; scanf("%d", &n); if (n == 0) break; an[n / 2 + 1][n / 2] = 1; bn[n / 2 + 1][n / 2] = 1; a = n / 2 + 1; b = n / 2; n1 = n - 1; while (1) { a++; b++; if (a > n1) a = 0; if (b > n1) b = 0; if (bn[a][b] != 1) { an[a][b] = c; bn[a][b] = 1; c++; } else if (bn[a][b] == 1) { a++; b--; if (a > n1) a = 0; if (b < 0) b = n1; an[a][b] = c; bn[a][b] = 1; c++; } if (c == n * n + 1) break; } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%4d", an[i][j]); } printf("\n"); } } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13]]
0
377
#include <stdio.h> int main(void) { int m[20][20], i, l, n, x, y; while (scanf("%d", &n) != 0) { if (n == 0) break; for (i = 0; i < 20; i++) { for (l = 0; l < 20; l++) { m[i][l] = 0; } } x = (n - 1) / 2; y = x + 1; m[x][y] = 1; for (i = 2; i <= n * n; i++) { x += 1; y += 1; while (1) { if (x >= n) { x = 0; continue; } if (x < 0) { x = n - 1; continue; } if (y >= n) { y = 0; continue; } if (y < 0) { y = n - 1; continue; } if (m[x][y] > 0) { x -= 1; y += 1; continue; } break; } m[x][y] = i; } for (i = 0; i < n; i++) { for (l = 0; l < n; l++) { printf("%3d", m[l][i]); } printf("\n"); } } return 0; }
#include <stdio.h> int main(void) { int m[20][20], i, l, n, x, y; while (scanf("%d", &n) != 0) { if (n == 0) break; for (i = 0; i < 20; i++) { for (l = 0; l < 20; l++) { m[i][l] = 0; } } x = (n - 1) / 2; y = x + 1; m[x][y] = 1; for (i = 2; i <= n * n; i++) { x += 1; y += 1; while (1) { if (x >= n) { x = 0; continue; } if (x < 0) { x = n - 1; continue; } if (y >= n) { y = 0; continue; } if (y < 0) { y = n - 1; continue; } if (m[x][y] > 0) { x -= 1; y += 1; continue; } break; } m[x][y] = i; } for (i = 0; i < n; i++) { for (l = 0; l < n; l++) { printf("%4d", m[l][i]); } printf("\n"); } } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
0
300
#include <iostream> #include <string.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { int field[16][16]; int n; while (cin >> n && n) { memset(field, 0, sizeof(field)); int x = n / 2; int y = n / 2 + 1; field[y][x] = 1; int count = 2; while (true) { if (count > n * n) { break; } x += 1; y += 1; if (x >= n) { x = 0; } if (x < 0) { x = n - 1; } if (y >= n) { y = 0; } if (y < 0) { y = n - 1; } if (field[y][x] == 0) { field[y][x] = count; count++; } else { y -= 2; } } rep(i, n) { rep(j, n) { cout.width(4); cout << field[i][j]; } cout << endl; } } return 0; }
#include <iostream> #include <string.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int main() { int field[16][16]; int n; while (cin >> n && n) { memset(field, 0, sizeof(field)); int x = n / 2; int y = n / 2 + 1; field[y][x] = 1; int count = 2; while (true) { if (count > n * n) { break; } x += 1; y += 1; if (x >= n) { x = 0; } if (x < 0) { x = n - 1; } if (y >= n) { y = 0; } if (y < 0) { y = n - 1; } if (field[y][x] == 0) { field[y][x] = count; count++; } else { x -= 2; } } rep(i, n) { rep(j, n) { cout.width(4); cout << field[i][j]; } cout << endl; } } return 0; }
[["-", 75, 76, 0, 9, 0, 1, 0, 11, 31, 22], ["+", 75, 76, 0, 9, 0, 1, 0, 11, 31, 22]]
1
239
#include <cstdio> #include <iomanip> #include <iostream> #include <vector> using namespace std; int main() { int n; while (cin >> n, n) { vector<vector<int>> d(n, vector<int>(n, 0)); int x = n / 2 + 1, y = n / 2; d[x][y] = 1; int m = 1; while (m < n * n) { m++; int dx = (x + 1) % n, dy = (y + 1) % n; while (d[dx][dy] != 0) { dx = (dx + 1) % n; dy = (dy + n - 1) % n; } d[dx][dy] = m; x = dx; y = dy; // cout << dx << " " << dy << " " << m << endl; } for (auto i : d) { for (auto &j : i) { cout << setw(3) << j; } cout << endl; } } return 0; }
#include <cstdio> #include <iomanip> #include <iostream> #include <vector> using namespace std; int main() { int n; while (cin >> n, n) { vector<vector<int>> d(n, vector<int>(n, 0)); int x = n / 2 + 1, y = n / 2; d[x][y] = 1; int m = 1; while (m < n * n) { m++; int dx = (x + 1) % n, dy = (y + 1) % n; while (d[dx][dy] != 0) { dx = (dx + 1) % n; dy = (dy + n - 1) % n; } d[dx][dy] = m; x = dx; y = dy; } for (auto i : d) { for (auto &j : i) { cout << setw(4) << j; } cout << endl; } } return 0; }
[["-", 0, 16, 31, 16, 12, 2, 3, 4, 0, 13], ["+", 0, 16, 31, 16, 12, 2, 3, 4, 0, 13]]
1
207
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n, n) { int x = n / 2, y = n / 2 + 1, fld[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { fld[i][j] = -1; } } fld[x][y] = 1; for (int i = 2; i <= n * n; i++) { x++; y++; while (1) { if (x < 0) x = n - 1; if (x >= n) x = 0; if (y < 0) y = n - 1; if (y >= n) y = 0; if (fld[x][y] == -1) { fld[x][y] = i; break; } if (fld[x][y] != -1) { x--; y++; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%3d", fld[i][j]); } cout << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n, n) { int x = n / 2, y = n / 2 + 1, fld[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { fld[i][j] = -1; } } fld[x][y] = 1; for (int i = 2; i <= n * n; i++) { x++; y++; while (1) { if (x < 0) x = n - 1; if (x >= n) x = 0; if (y < 0) y = n - 1; if (y >= n) y = 0; if (fld[x][y] == -1) { fld[x][y] = i; break; } if (fld[x][y] != -1) { x--; y++; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%4d", fld[j][i]); } cout << endl; } } }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["-", 3, 4, 0, 69, 28, 69, 341, 342, 0, 22], ["+", 3, 4, 0, 69, 28, 69, 341, 342, 0, 22], ["-", 0, 2, 3, 4, 0, 69, 341, 342, 0, 22], ["+", 0, 2, 3, 4, 0, 69, 341, 342, 0, 22]]
1
271
#include <iostream> using namespace std; int main() { // your code goes here int n; int a[20][20]; cin >> n; while (n != 0) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i][j] = 0; } } int k = 1 + n / 2; int l = n / 2; int m = 1; a[k][l] = 1; for (int u = 0; u < n; u++) { for (int v = 1; v < n; v++) { k = (k + 1) % n; l = (l + 1) % n; m = m + 1; a[k][l] = m; } k = (k + 2) % n; m = m + 1; a[k][l] = m; } a[k][l] = a[k][l] - n * n; for (int q = 0; q < n; q++) { for (int w = 0; w < n; w++) { if (10 > a[q][w]) { cout << " " << a[q][w]; } else if (100 > a[q][w]) { cout << " " << a[q][w]; } else if (100 < a[q][w]) { cout << " " << a[q][w]; } } cout << endl; } cin >> n; } return 0; }
#include <iostream> using namespace std; int main() { // your code goes here int n; int a[20][20]; cin >> n; while (n != 0) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i][j] = 0; } } int k = 1 + n / 2; int l = n / 2; int m = 1; a[k][l] = 1; for (int u = 0; u < n; u++) { for (int v = 1; v < n; v++) { k = (k + 1) % n; l = (l + 1) % n; m = m + 1; a[k][l] = m; } k = (k + 2) % n; m = m + 1; a[k][l] = m; } a[k][l] = a[k][l] - n * n; for (int q = 0; q < n; q++) { for (int w = 0; w < n; w++) { if (10 > a[q][w]) { cout << " " << a[q][w]; } else if (100 > a[q][w]) { cout << " " << a[q][w]; } else if (99 < a[q][w]) { cout << " " << a[q][w]; } } cout << endl; } cin >> n; } return 0; }
[["-", 75, 76, 0, 57, 15, 339, 51, 16, 31, 13], ["+", 75, 76, 0, 57, 15, 339, 51, 16, 31, 13]]
1
353
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = n - 1; i >= 0; i--) #define PB push_back #define INF (1 << 29) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define CLR(a) memset(a, 0, sizeof(a)) const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0}; typedef long long int ll; using namespace std; int main() { while (true) { int n; cin >> n; if (n == 0) break; vector<vector<int>> m(n, vector<int>(n, -1)); int x, y, c; x = n / 2; y = n / 2; c = 1; y += 1; m[y][x] = c; while (c < n * n) { c++; if (m[y][x] > 0) { x = (x + 1) % n; y = (y + 1) % n; if (m[y][x] > 0) { x = (x - 1 + n) % n; y = (y + 1) % n; } } m[y][x] = c; } REP(i, n) { REP(j, n - 1) { printf("%4d ", m[i][j]); } printf("%4d\n", m[i][3]); } } return 0; }
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = n - 1; i >= 0; i--) #define PB push_back #define INF (1 << 29) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define CLR(a) memset(a, 0, sizeof(a)) const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0}; typedef long long int ll; using namespace std; int main() { while (true) { int n; cin >> n; if (n == 0) break; vector<vector<int>> m(n, vector<int>(n, -1)); int x, y, c; x = n / 2; y = n / 2; c = 1; y += 1; m[y][x] = c; while (c < n * n) { c++; if (m[y][x] > 0) { x = (x + 1) % n; y = (y + 1) % n; if (m[y][x] > 0) { x = (x - 1 + n) % n; y = (y + 1) % n; } } m[y][x] = c; } REP(i, n) { REP(j, n - 1) { printf("%4d", m[i][j]); } printf("%4d\n", m[i][n - 1]); } } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["-", 0, 2, 3, 4, 0, 69, 341, 342, 0, 13], ["+", 3, 4, 0, 69, 341, 342, 0, 16, 31, 22], ["+", 3, 4, 0, 69, 341, 342, 0, 16, 17, 33], ["+", 3, 4, 0, 69, 341, 342, 0, 16, 12, 13]]
1
334
#include <cstdio> #include <iostream> using namespace std; int main() { int a[15][15]; int n, x, y, i = 1; cin >> n; while (n != 0) { for (int j = 0; j < n * n; j++) a[j / n][j % n] = -1; x = n / 2; y = n / 2 + 1; i = 1; while (i <= n * n) { if (x >= n) { x = 0; continue; } else if (x < 0) { x = n - 1; continue; } else if (y >= n) { y = 0; continue; } else if (y < 0) { y = n - 1; continue; } // cout << i<<"("<<x<<","<<y<<")"; if (a[y][x] == -1) { // cout <<"-"; a[y][x] = i; x++; y++; i++; } else { // cout <<"*("<<a[y][x]<<")**"; x--; y++; } } cout << endl; for (y = 0; y < n; y++) { for (x = 0; x < n; x++) printf("%4d", a[y][x]); cout << endl; } cin >> n; } return 0; }
#include <cstdio> #include <iostream> using namespace std; int main() { int a[15][15]; int n, x, y, i = 1; cin >> n; while (n != 0) { for (int j = 0; j < n * n; j++) a[j / n][j % n] = -1; x = n / 2; y = n / 2 + 1; i = 1; while (i <= n * n) { if (x >= n) { x = 0; continue; } else if (x < 0) { x = n - 1; continue; } else if (y >= n) { y = 0; continue; } else if (y < 0) { y = n - 1; continue; } // cout << i<<"("<<x<<","<<y<<")"; if (a[y][x] == -1) { // cout <<"-"; a[y][x] = i; x++; y++; i++; } else { // cout <<"*("<<a[y][x]<<")**"; x--; y++; } } for (y = 0; y < n; y++) { for (x = 0; x < n; x++) printf("%4d", a[y][x]); cout << endl; } cin >> n; } return 0; }
[["-", 0, 52, 8, 9, 0, 1, 0, 16, 31, 22], ["-", 0, 52, 8, 9, 0, 1, 0, 16, 17, 151], ["-", 0, 52, 8, 9, 0, 1, 0, 16, 12, 22], ["-", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
270
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> using namespace std; typedef long long Int; typedef vector<int> vint; typedef pair<int, int> pint; #define mp make_pair template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; } template <class T> void chmin(T &t, T f) { if (t > f) t = f; } template <class T> void chmax(T &t, T f) { if (t < f) t = f; } int in() { int x; scanf("%d", &x); return x; } int maho[15][15]; int main() { int n; for (; n = in();) { int x = n / 2, y = n / 2 + 1; int i; memset(maho, 0, sizeof(maho)); for (i = 1; i <= n * n; i++) { while (maho[y][x]) { x--; y++; if (x == -1) x = n - 1; if (y == n) y = 0; } maho[y][x] = i; x++; y++; if (x == n) x = 0; if (y == n) y = 0; } int xx, yy; for (yy = 0; yy < n; yy++) { for (xx = 0; xx < n; xx++) { printf("%3d", maho[yy][xx]); } cout << endl; } } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> using namespace std; typedef long long Int; typedef vector<int> vint; typedef pair<int, int> pint; #define mp make_pair template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; } template <class T> void chmin(T &t, T f) { if (t > f) t = f; } template <class T> void chmax(T &t, T f) { if (t < f) t = f; } int in() { int x; scanf("%d", &x); return x; } int maho[15][15]; int main() { int n; for (; n = in();) { int x = n / 2, y = n / 2 + 1; int i; memset(maho, 0, sizeof(maho)); for (i = 1; i <= n * n; i++) { while (maho[y][x]) { x--; y++; if (x == -1) x = n - 1; if (y == n) y = 0; } maho[y][x] = i; x++; y++; if (x == n) x = 0; if (y == n) y = 0; } int xx, yy; for (yy = 0; yy < n; yy++) { for (xx = 0; xx < n; xx++) { printf("%4d", maho[yy][xx]); } cout << endl; } } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
391
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { int i, j, k; int n; int a[15][15]; while (cin >> n && n != 0) { k = 0; for (i = 0; i < 15; i++) { for (j = 0; j < 15; j++) { a[i][j] = 0; } } for (i = -n / 2; i <= n / 2; i++) for (j = 0; j < n; j++) a[(j - i + n) % n][(j + i + n) % n] = ++k; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) printf("%4d", a[i][j]); puts(""); } } return 0; }
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { int i, j, k; int n; int a[15][15]; while (cin >> n && n != 0) { k = 0; for (i = 0; i < 15; i++) { for (j = 0; j < 15; j++) { a[i][j] = 0; } } for (i = -n / 2; i <= n / 2; i++) for (j = 0; j < n; j++) a[(j + i + n) % n][(j - i + n) % n] = ++k; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) printf("%4d", a[i][j]); puts(""); } } return 0; }
[["-", 0, 16, 31, 23, 0, 16, 31, 16, 17, 33], ["+", 0, 16, 31, 23, 0, 16, 31, 16, 17, 72], ["-", 0, 16, 31, 23, 0, 16, 31, 16, 17, 72], ["+", 0, 16, 31, 23, 0, 16, 31, 16, 17, 33]]
1
206
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> #include <vector> using namespace ::std; int main() { int number; int i, j; int center = 0; int h = 0; int w = 0; cin >> number; while (number != 0) { int magic[number][number]; for (i = 0; i < number; i++) { for (j = 0; j < number; j++) { magic[i][j] = -1; } } center = number / 2; h = center + 1; w = center; magic[h][w] = 1; for (i = 2; i <= number * number; i++) { h = (h + 1) % number; w = (w + 1) % number; if (magic[h][w] != -1) { h = (h + 1) % number; w = w - 1; if (w == -1) { w = w + number; } } magic[h][w] = i; } for (i = 0; i < number; i++) { for (j = 0; j < number; j++) { printf("%3d", magic[i][j]); } printf("\n"); } cin >> number; } return 0; }
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> #include <vector> using namespace ::std; int main() { int number; int i, j; int center = 0; int h = 0; int w = 0; cin >> number; while (number != 0) { int magic[number][number]; for (i = 0; i < number; i++) { for (j = 0; j < number; j++) { magic[i][j] = -1; } } center = number / 2; h = center + 1; w = center; magic[h][w] = 1; for (i = 2; i <= number * number; i++) { h = (h + 1) % number; w = (w + 1) % number; if (magic[h][w] != -1) { h = (h + 1) % number; w = w - 1; if (w == -1) { w = w + number; } } magic[h][w] = i; } for (i = 0; i < number; i++) { for (j = 0; j < number; j++) { printf("%4d", magic[i][j]); } printf("\n"); } cin >> number; } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
281
#include <iomanip> #include <iostream> using namespace std; int n; int m[15][15]; void func(int x, int y, int l) { int nx, ny; if (!m[x][y]) { m[x][y] = l; if (l == n * n) return; if (x < n - 1) nx = x + 1; else nx = 0; if (y < n - 1) ny = y + 1; else ny = 0; func(nx, ny, l + 1); } else { if (x < n - 1) nx = x + 1; else nx = 0; if (y) ny = y - 1; else ny = n - 1; func(nx, ny, l); } } int main() { int c; while (1) { cin >> n; if (!n) break; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { m[i][j] = 0; } } c = 0; func(n / 2 + 1, n / 2, 1); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cout << setw(3) << m[i][j]; } cout << endl; } } return 0; }
#include <iomanip> #include <iostream> using namespace std; int n; int m[15][15]; void func(int x, int y, int l) { int nx, ny; if (!m[x][y]) { m[x][y] = l; if (l == n * n) return; if (x < n - 1) nx = x + 1; else nx = 0; if (y < n - 1) ny = y + 1; else ny = 0; func(nx, ny, l + 1); } else { if (x < n - 1) nx = x + 1; else nx = 0; if (y) ny = y - 1; else ny = n - 1; func(nx, ny, l); } } int main() { int c; while (1) { cin >> n; if (!n) break; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { m[i][j] = 0; } } c = 0; func(n / 2 + 1, n / 2, 1); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cout << setw(4) << m[i][j]; } cout << endl; } } return 0; }
[["-", 0, 16, 31, 16, 12, 2, 3, 4, 0, 13], ["+", 0, 16, 31, 16, 12, 2, 3, 4, 0, 13]]
1
310
#include <iomanip> #include <iostream> using namespace std; int main() { int n; int table[20][20]; while (cin >> n, n) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) table[i][j] = 0; int y = n / 2 + 1, x = n / 2; for (int i = 1; i <= n * n; i++) { table[y][x] = i; y++; x++; while (true) { if (i == n * n) break; if (y >= n) y = 0; if (x < 0) x = n - 1; if (x >= n) x = 0; if (table[y][x]) { y++; x--; } else { break; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << setw(3) << right << table[i][j]; } cout << endl; } } }
#include <iomanip> #include <iostream> using namespace std; int main() { int n; int table[20][20]; while (cin >> n, n) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) table[i][j] = 0; int y = n / 2 + 1, x = n / 2; for (int i = 1; i <= n * n; i++) { table[y][x] = i; y++; x++; while (true) { if (i == n * n) break; if (y >= n) y = 0; if (x < 0) x = n - 1; if (x >= n) x = 0; if (table[y][x]) { y++; x--; } else { break; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << setw(4) << right << table[i][j]; } cout << endl; } } }
[["-", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13], ["+", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13]]
1
247
arr, ans = gets.split.map{ |i| i.delete(",.") }, [] arr.each do |str| p str.length ans << str if (3..6).include?(str.length) end puts ans.join(" ")
arr, ans = gets.split.map{ |i| i.delete(",.") }, [] arr.each do |str| ans << str if (3..6).include?(str.length) end puts ans.join(" ")
[["-", 0, 652, 196, 737, 8, 736, 0, 652, 735, 22], ["-", 8, 736, 0, 652, 3, 4, 0, 652, 486, 22], ["-", 8, 736, 0, 652, 3, 4, 0, 652, 17, 131], ["-", 8, 736, 0, 652, 3, 4, 0, 652, 735, 22]]
4
62
a =gets a.gsub!(","," ") a.gsub!("."," ") a = a.split(" ") r = a.map do |el| if el.length >= 3 && el.length <= 6 el end end puts r.compact!
a =gets a.gsub!(","," ") a.gsub!("."," ") a = a.split(" ") r = a.map do |el| if el.length >= 3 && el.length <= 6 el end end puts r.compact!.join(' ')
[["+", 0, 493, 0, 652, 3, 4, 0, 652, 17, 131], ["+", 0, 493, 0, 652, 3, 4, 0, 652, 735, 22], ["+", 0, 652, 3, 4, 0, 652, 3, 4, 0, 24], ["+", 3, 4, 0, 652, 3, 4, 0, 557, 0, 62], ["+", 3, 4, 0, 652, 3, 4, 0, 557, 0, 6], ["+", 0, 652, 3, 4, 0, 652, 3, 4, 0, 25]]
4
65
import re s = re.split("[ .,]", input()) sl = len(s) ans = list() for i in range(sl) : if(2 < len(s[i]) and len(s[i]) < 7) : ans.append(s[i]) else : pass ansl = len(ans) for i in range(ansl - 1) : if(i == ansl) : print(ans[i]) else : print(ans[i], end = (" "))
import re s = re.split("[ .,]", input()) sl = len(s) ans = list() for i in range(sl) : if(2 < len(s[i]) and len(s[i]) < 7) : ans.append(s[i]) else : pass ansl = len(ans) for i in range(ansl) : if(i == ansl - 1) : print(ans[i]) else : print(ans[i], end = (" "))
[["-", 0, 7, 12, 652, 3, 4, 0, 657, 17, 33], ["-", 0, 7, 12, 652, 3, 4, 0, 657, 12, 612], ["+", 0, 57, 15, 23, 0, 666, 0, 657, 17, 33], ["+", 0, 57, 15, 23, 0, 666, 0, 657, 12, 612]]
5
117
l=list(map(str,input().split())) for i in l: tmp=i.replace(',','') tmp=i.replace('.','') if 2<len(tmp)<7: print(tmp, end=' ')
l=list(map(str,input().split())) for i in l: i=i.replace(',','') i=i.replace('.','') if 2<len(i)<7: print(i, end=' ')
[["-", 0, 7, 8, 196, 0, 1, 0, 662, 31, 22], ["+", 0, 7, 8, 196, 0, 1, 0, 662, 31, 22], ["-", 0, 57, 15, 666, 0, 652, 3, 4, 0, 22], ["+", 0, 57, 15, 666, 0, 652, 3, 4, 0, 22], ["-", 64, 196, 0, 1, 0, 652, 3, 4, 0, 22], ["+", 64, 196, 0, 1, 0, 652, 3, 4, 0, 22]]
5
68
print(' '.join([w for w in input().replace(',', '').replace('.', '').split(', ') if len(w) > 2 and len(w) < 7]))
print(' '.join([w for w in input().replace(',', '').replace('.', '').split(' ') if len(w) > 2 and len(w) < 7]))
[["-", 0, 659, 12, 652, 3, 4, 0, 557, 0, 6], ["+", 0, 659, 12, 652, 3, 4, 0, 557, 0, 6]]
5
60
import re print(*[x for x in re.split(r'\s|"|,|\.',input())if 2<len(s)<7])
import re print(*[x for x in re.split(r'\s|"|,|\.',input())if 2<len(x)<7])
[["-", 0, 678, 0, 666, 0, 652, 3, 4, 0, 22], ["+", 0, 678, 0, 666, 0, 652, 3, 4, 0, 22]]
5
33
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { ArrayList<Integer> list = new ArrayList<Integer>(); int n = sc.nextInt(); int m = sc.nextInt(); if (n == 0 && m == 0) break; for (int i = 1; i <= n; i++) list.add(i); int x = -1; while (list.size() != 1) { x = (x + n) % list.size(); list.remove(x); x = x - 1; } System.out.println(list.get(0)); } } }
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { ArrayList<Integer> list = new ArrayList<Integer>(); int n = sc.nextInt(); int m = sc.nextInt(); if (n == 0 && m == 0) break; for (int i = 1; i <= n; i++) list.add(i); int x = -1; while (list.size() > 1) { x = (x + m) % list.size(); list.remove(x); x = x - 1; } System.out.println(list.get(0)); } } }
[["-", 8, 196, 0, 52, 15, 15, 0, 16, 17, 79], ["+", 8, 196, 0, 52, 15, 15, 0, 16, 17, 47], ["-", 0, 11, 12, 16, 31, 23, 0, 16, 12, 22], ["+", 0, 11, 12, 16, 31, 23, 0, 16, 12, 22]]
3
176
import java.util.*; public class Main { public static void main(String[] args) { new Main(); } public Main() { Scanner cin = new Scanner(System.in); while (true) { int n = cin.nextInt(); int m = cin.nextInt(); if (n == 0) break; boolean check[] = new boolean[1000]; int i, j; int now = n - 1; for (i = 0; i < n - 1; i++) { int nokori = n - i; int need = m % nokori; if (need == 0) need = nokori; for (j = 0; j < need; j++) { now = (now + 1) % n; if (check[now]) j--; } check[now] = true; } for (i = 0; i < n - 1; i++) { if (!check[i]) System.out.println(i + 1); } } } }
import java.util.*; public class Main { public static void main(String[] args) { new Main(); } public Main() { Scanner cin = new Scanner(System.in); while (true) { int n = cin.nextInt(); int m = cin.nextInt(); if (n == 0) break; boolean check[] = new boolean[1000]; int i, j; int now = n - 1; for (i = 0; i < n - 1; i++) { int nokori = n - i; int need = m % nokori; if (need == 0) need = nokori; for (j = 0; j < need; j++) { now = (now + 1) % n; if (check[now]) j--; } check[now] = true; } for (i = 0; i < n; i++) { if (!check[i]) System.out.println(i + 1); } } } }
[["-", 8, 196, 0, 7, 15, 16, 12, 16, 17, 33], ["-", 8, 196, 0, 7, 15, 16, 12, 16, 12, 499]]
3
220
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.Deque; /** * Joseph's Potato */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = ""; while (!(line = br.readLine()).equals("0 0")) { int n = Integer.parseInt(line.substring(0, line.indexOf(' '))); int m = Integer.parseInt(line.substring(line.indexOf(' ') + 1)); Deque<Integer> q = new ArrayDeque<Integer>(); for (int i = 1; i <= n; i++) { q.offer(i); } while (q.size() != 1) { for (int i = 0; i < m; i++) { q.offer(q.poll()); } q.poll(); } System.out.println(q.poll()); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.Deque; /** * Joseph's Potato */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = ""; while (!(line = br.readLine()).equals("0 0")) { int n = Integer.parseInt(line.substring(0, line.indexOf(' '))); int m = Integer.parseInt(line.substring(line.indexOf(' ') + 1)); Deque<Integer> q = new ArrayDeque<Integer>(n); for (int i = 1; i <= n; i++) { q.offer(i); } while (q.size() != 1) { for (int i = 0; i < m - 1; i++) { q.offer(q.poll()); } q.poll(); } System.out.println(q.poll()); } } }
[["+", 0, 503, 49, 200, 51, 230, 3, 4, 0, 22], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 12, 499]]
3
237
int joseph(int n, int diff, int initial) { int r = 0, i = 1; for (; i < n; i++) r = (r + diff) % i; return (r + 1 + initial) % n + 1; } int main() { int n, k; for (; scanf("%d%d", &n, &k), n;) printf("%d\n", joseph(n, k, k)); return 0; }
int joseph(int n, int diff, int initial) { int r = 0, i = 1; for (; i < n; i++) r = (r + diff) % i; return (r + 1 + initial) % n + 1; } int main() { int n, k; for (; scanf("%d%d", &n, &k), n;) printf("%d\n", joseph(n, k, k - 1)); return 0; }
[["+", 3, 4, 0, 2, 3, 4, 0, 16, 17, 33], ["+", 3, 4, 0, 2, 3, 4, 0, 16, 12, 13]]
0
106
/* AOJ 0085 * * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0085 * */ #include <stdio.h> #define DEBUG 0 int next[1001]; int main() { int m, n; int count; int i; int p, prev; while (scanf("%d %d", &n, &m) == 2) { if (m == 0 && n == 0) { break; } for (i = 1; i < n; i++) { next[i] = i + 1; } next[n] = 1; count = n; p = 1; while (count > 1) { for (i = 0; i < m - 1; i++) { prev = p; p = next[p]; } if (DEBUG) printf("removing %d\n", p); next[prev] = next[p]; p = next[p]; count--; } printf("%d\n", p); } return 0; }
/* AOJ 0085 * * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0085 * */ #include <stdio.h> #define DEBUG 0 int next[1001]; int main() { int m, n; int count; int i; int p, prev; while (scanf("%d %d", &n, &m) == 2) { if (m == 0 && n == 0) { break; } for (i = 1; i < n; i++) { next[i] = i + 1; } next[n] = 1; count = n; prev = 1; p = 1; while (count > 1) { for (i = 0; i < m - 1; i++) { prev = p; p = next[p]; } if (DEBUG) printf("removing %d\n", p); next[prev] = next[p]; p = next[p]; count--; } printf("%d\n", p); } 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]]
0
189
#include <iostream> #include <queue> using namespace std; main() { int n, m; queue<int> poteto; while (1) { cin >> n >> m; if (n == 0 && m == 0) break; for (int i = 1; i <= n; i++) { poteto.push(i); } while (poteto.size() > 1) { for (int j = 0; j < m - 1; j++) { poteto.push(poteto.front()); poteto.pop(); } poteto.pop(); } cout << endl; cout << poteto.front() << endl; poteto.pop(); } }
#include <iostream> #include <queue> using namespace std; main() { int n, m; queue<int> poteto; while (1) { cin >> n >> m; if (n == 0 && m == 0) break; for (int i = 1; i <= n; i++) { poteto.push(i); } while (poteto.size() > 1) { for (int j = 0; j < m - 1; j++) { poteto.push(poteto.front()); poteto.pop(); } poteto.pop(); } cout << poteto.front() << endl; poteto.pop(); } }
[["-", 0, 52, 8, 9, 0, 1, 0, 16, 12, 22], ["-", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35], ["-", 8, 9, 0, 1, 0, 16, 31, 16, 31, 22], ["-", 8, 9, 0, 1, 0, 16, 31, 16, 17, 151]]
1
144
#define scanf_s scanf //#define gets_s gets #include <iostream> #include <math.h> #include <stdio.h> #include <string> using namespace std; #define MAX 1000 #define MIN 2 int main(void) { int n, m; while (scanf_s("%d %d", &n, &m) != EOF) { if (n == 0 && m == 0) break; int h[MAX + 1] = {0}, ans = 0, cou = 0, k = 0; while (cou < n) { for (int i = ans + 1; k <= m; ++i) { if (i > n) { i = i - n; printf("\n"); } if (h[i] == false) { ++k; } if (k >= m) { k = 0; ++cou; h[i] = true; ans = i; break; } } } printf("%d\n", ans); } }
#define scanf_s scanf //#define gets_s gets #include <iostream> #include <math.h> #include <stdio.h> #include <string> using namespace std; #define MAX 1000 #define MIN 2 int main(void) { int n, m; while (scanf_s("%d %d", &n, &m) != EOF) { if (n == 0 && m == 0) break; int h[MAX + 1] = {0}, ans = 0, cou = 0, k = 0; while (cou < n) { for (int i = ans + 1; k <= m; ++i) { if (i > n) { i = i - n; } if (h[i] == false) { ++k; } if (k >= m) { k = 0; ++cou; h[i] = true; ans = i; break; } } } printf("%d\n", ans); } }
[["-", 0, 57, 64, 9, 0, 1, 0, 2, 63, 22], ["-", 64, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 62], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44], ["-", 64, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["-", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35]]
1
188
#include <iostream> #include <vector> using namespace std; int main() { int n, k, m, i; for (; cin >> n >> k, n;) { vector<int> vec(n); for (i = 0; i < n; i++) vec[i] = i + 1; i = (m - 1) % vec.size(); vec.erase(vec.begin() + i); for (; vec.size() > 1; vec.erase(vec.begin() + i)) if (i += (k - 1) % vec.size(), i >= vec.size()) i -= vec.size(); cout << vec[0] << endl; } }
#include <iostream> #include <vector> using namespace std; int main() { int n, k, i; for (; cin >> n >> k, n;) { vector<int> vec(n); for (i = 0; i < n; i++) vec[i] = i + 1; i = (k - 1) % vec.size(); vec.erase(vec.begin() + i); for (; vec.size() > 1; vec.erase(vec.begin() + i)) if (i += (k - 1) % vec.size(), i >= vec.size()) i -= vec.size(); cout << vec[0] << endl; } }
[["-", 0, 30, 0, 14, 8, 9, 0, 43, 49, 22], ["-", 0, 30, 0, 14, 8, 9, 0, 43, 0, 21], ["-", 0, 11, 12, 16, 31, 23, 0, 16, 31, 22], ["+", 0, 11, 12, 16, 31, 23, 0, 16, 31, 22]]
1
160
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; const double EPS = 1e-9; const double PI = 3.14159265359; typedef complex<double> P; int main() { int m, n; while (cin >> n >> m, m + n) { vector<int> v(n + 1); int pos = 30; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < m; j++) { pos = pos % n + 1; while (v[pos] != 0) { pos = pos % n + 1; } } v[pos] = 1; // cout << "out:" << pos << endl; } for (int i = 1; i < n + 1; i++) { if (!v[i]) { cout << i << endl; } } } return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; const double EPS = 1e-9; const double PI = 3.14159265359; typedef complex<double> P; int main() { int m, n; while (cin >> n >> m, m + n) { vector<int> v(n + 1); int pos = 0; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < m; j++) { pos = pos % n + 1; while (v[pos] != 0) { pos = pos % n + 1; } } v[pos] = 1; } for (int i = 1; i < n + 1; i++) { if (!v[i]) { cout << i << endl; } } } return 0; }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
1
200
#include <iostream> using namespace std; int potato(int x, int y) { int i; for (i = y - 1; i < (y - 1) * x; i = (i * y) / (y - 1) + 1) return (x * y - i); } int main() { int x, y; while (cin >> x >> y, x || y) { cout << potato(x, y) << endl; } }
#include <iostream> using namespace std; int potato(int x, int y) { int i; for (i = y - 1; i < (y - 1) * x; i = (y * i) / (y - 1) + 1) ; return (x * y - i); } int main() { int x, y; while (cin >> x >> y, x || y) { cout << potato(x, y) << endl; } }
[["-", 12, 16, 31, 16, 31, 23, 0, 16, 31, 22], ["-", 12, 16, 31, 16, 31, 23, 0, 16, 17, 48], ["+", 12, 16, 31, 16, 31, 23, 0, 16, 17, 48], ["+", 12, 16, 31, 16, 31, 23, 0, 16, 12, 22], ["+", 0, 14, 8, 9, 0, 7, 8, 1, 0, 35]]
1
99
#include <iostream> using namespace std; long long int func(int year, int month, int day) { return (year * 10000 + month * 100 + day); } int main() { long long int data[4]; data[0] = 1868 * 10000 + 9 * 100 + 8; data[1] = 1912 * 10000 + 7 * 100 + 29; data[2] = 1926 * 10000 + 12 * 100 + 24; data[3] = 1989 * 10000 + 1 * 100 + 7; long long int num; int year, month, day; while (cin >> year) { cin >> month; cin >> day; num = func(year, month, day); if (num < data[0]) { cout << "pre-meiji" << endl; } else if (data[0] <= num && num <= data[1]) { cout << "meiji" << year - 1868 + 1 << " " << month << " " << day << endl; } else if (data[1] <= num && num <= data[2]) { cout << "taisho" << year - 1912 + 1 << " " << month << " " << day << endl; } else if (data[2] <= num && num <= data[3]) { cout << "showa" << year - 1926 + 1 << " " << month << " " << day << endl; } else { cout << "heisei" << year - 1989 + 1 << " " << month << " " << day << endl; } } return 0; } /* Sample Input 2005 9 3 1868 12 2 1868 9 7 Output for the Sample Input heisei 17 9 3 meiji 1 12 2 pre-meiji */
#include <iostream> using namespace std; long long int func(int year, int month, int day) { return (year * 10000 + month * 100 + day); } int main() { long long int data[4]; data[0] = 1868 * 10000 + 9 * 100 + 8; data[1] = 1912 * 10000 + 7 * 100 + 29; data[2] = 1926 * 10000 + 12 * 100 + 24; data[3] = 1989 * 10000 + 1 * 100 + 7; long long int num; int year, month, day; while (cin >> year) { cin >> month; cin >> day; num = func(year, month, day); if (num < data[0]) { cout << "pre-meiji" << endl; } else if (data[0] <= num && num <= data[1]) { cout << "meiji " << year - 1868 + 1 << " " << month << " " << day << endl; } else if (data[1] <= num && num <= data[2]) { cout << "taisho " << year - 1912 + 1 << " " << month << " " << day << endl; } else if (data[2] <= num && num <= data[3]) { cout << "showa " << year - 1926 + 1 << " " << month << " " << day << endl; } else { cout << "heisei " << year - 1989 + 1 << " " << month << " " << day << endl; } } return 0; } /* Sample Input 2005 9 3 1868 12 2 1868 9 7 Output for the Sample Input heisei 17 9 3 meiji 1 12 2 pre-meiji */
[["-", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6], ["+", 31, 16, 31, 16, 31, 16, 12, 5, 0, 6]]
1
335
#include <cstdio> #include <iostream> using namespace std; string G[] = {"meiji", "taisho", "showa", "heisei"}; int Gnum[] = {1868, 1912, 1926, 1989}; int YEAR(int year) { if (19890108 <= year) { return 3; } else if (19891225 <= year) { return 2; } else if (19120730 <= year) { return 1; } else if (18680908 <= year) { return 0; } return 4; } int main() { long long int tosi, mon, day; while (cin >> tosi >> mon >> day) { int gen = YEAR(tosi * 10000 + mon * 100 + day); if (gen == 4) cout << "pre-meiji" << endl; else cout << G[gen] << ' ' << tosi - Gnum[gen] + 1 << ' ' << mon << ' ' << day << endl; } }
#include <cstdio> #include <iostream> using namespace std; string G[] = {"meiji", "taisho", "showa", "heisei"}; int Gnum[] = {1868, 1912, 1926, 1989}; int YEAR(int year) { if (19890108 <= year) { return 3; } else if (19261225 <= year) { return 2; } else if (19120730 <= year) { return 1; } else if (18680908 <= year) { return 0; } return 4; } int main() { long long int tosi, mon, day; while (cin >> tosi >> mon >> day) { long long int gen = YEAR(tosi * 10000 + mon * 100 + day); if (gen == 4) cout << "pre-meiji" << endl; else cout << G[gen] << ' ' << tosi - Gnum[gen] + 1 << ' ' << mon << ' ' << day << endl; } return 0; }
[["-", 75, 76, 0, 57, 15, 339, 51, 16, 31, 13], ["+", 75, 76, 0, 57, 15, 339, 51, 16, 31, 13], ["+", 0, 52, 8, 9, 0, 43, 39, 86, 0, 96], ["+", 0, 30, 0, 14, 8, 9, 0, 37, 0, 38], ["+", 0, 30, 0, 14, 8, 9, 0, 37, 0, 13], ["+", 0, 30, 0, 14, 8, 9, 0, 37, 0, 35]]
1
196
gengo = { "meiji" => 18680908..19120729, "taisho" => 19120730..19261224, "showa" => 19261225..19890107, "heisei" => 19890108..99999999, } while gets y, m, d = $_.split date = sprintf("%4d%02d%02d\n", y, m, d).to_i puts "pre-meiji" unless gengo.any? do |g, range| if range === date gy = (date - range.min) / 10000 + 1 puts "#{g} #{gy} #{m} #{d}" break true end end end
gengo = { "meiji" => 18680908..19120729, "taisho" => 19120730..19261224, "showa" => 19261225..19890107, "heisei" => 19890108..99999999, } while gets y, m, d = $_.split date = sprintf("%4d%02d%02d\n", y, m, d).to_i puts "pre-meiji" unless gengo.any? do |g, range| if range === date gy = date / 10000 - range.min / 10000 + 1 puts "#{g} #{gy} #{m} #{d}" break true end end end
[["-", 0, 662, 12, 738, 31, 738, 31, 739, 0, 24], ["+", 0, 662, 12, 738, 31, 738, 31, 738, 17, 85], ["+", 0, 662, 12, 738, 31, 738, 31, 738, 12, 612], ["-", 0, 662, 12, 738, 31, 738, 31, 739, 0, 25]]
4
118
def solve(y,m,d): if y<1868 or (y==1868 and m<9) or (y==1868 and m==9 and d<8): print("pre-meiji") elif 1868<y<1912 or (y==1868 and 7<m) or (y==1868 and m==9 and 8<=d) or (y==1912 and m<7) or(y==1912 and m==7 and d<=29): print("meiji %d %d %d"%(y-1868+1,m,d)) elif 1912<y<1926 or (y==1912 and 7<m) or (y==1912 and m==7 and 25<=d) or (y==1926 and m<12) or (y==1926 and m==12 and d<=24): print("taisho %d %d %d"%(y-1912+1,m,d)) elif 1926<y<1989 or (y==1926 and 12<m) or (y==1926 and m==12 and 25<=d) or (y==1989 and m<1) or (y==1989 and m==1 and d<=7): print("showa %d %d %d"%(y-1026+1,m,d)) else: print("heisei %d %d %d"%(y-1989+1,m,d)) while True: try: y,m,d=map(int,input().split()) solve(y,m,d) except EOFError: break
def solve(y,m,d): if y<1868 or (y==1868 and m<9) or (y==1868 and m==9 and d<8): print("pre-meiji") elif 1868<y<1912 or (y==1868 and 9<m) or (y==1868 and m==9 and 8<=d) or (y==1912 and m<7) or(y==1912 and m==7 and d<=29): print("meiji %d %d %d"%(y-1868+1,m,d)) elif 1912<y<1926 or (y==1912 and 7<m) or (y==1912 and m==7 and 30<=d) or (y==1926 and m<12) or (y==1926 and m==12 and d<=24): print("taisho %d %d %d"%(y-1912+1,m,d)) elif 1926<y<1989 or (y==1926 and 12<m) or (y==1926 and m==12 and 25<=d) or (y==1989 and m<1) or (y==1989 and m==1 and d<=7): print("showa %d %d %d"%(y-1926+1,m,d)) else: print("heisei %d %d %d"%(y-1989+1,m,d)) while True: try: y,m,d=map(int,input().split()) solve(y,m,d) except EOFError: break
[["-", 31, 679, 12, 23, 0, 679, 12, 666, 0, 612], ["+", 31, 679, 12, 23, 0, 679, 12, 666, 0, 612], ["-", 0, 657, 12, 660, 0, 657, 31, 657, 12, 612], ["+", 0, 657, 12, 660, 0, 657, 31, 657, 12, 612]]
5
319
# Aizu Problem 0083: Era Transformation # import sys, math, os # read input: PYDEV = os.environ.get('PYDEV') if PYDEV=="True": sys.stdin = open("sample-input.txt", "rt") def era_transformation(y, m, d): if int(m) < 10: m = '0' + m if int(d) < 10: d = '0' + d date = int(y + m + d) y = int(y) m = int(m) d = int(d) if date < 18680908: print("pre-meiji") return elif date <= 19120729: print("meiji", y - 1868 + 1, m, d) elif date <= 19261224: print("taisho", y - 1926 + 1, m, d) elif date <= 19890107: print("showa", y - 1989 + 1, m, d) else: print("heisei", y - 1989 + 1, m, d) for line in sys.stdin: y, m, d = line.split() era_transformation(y, m, d)
# Aizu Problem 0083: Era Transformation # import sys, math, os # read input: PYDEV = os.environ.get('PYDEV') if PYDEV=="True": sys.stdin = open("sample-input.txt", "rt") def era_transformation(y, m, d): if int(m) < 10: m = '0' + m if int(d) < 10: d = '0' + d date = int(y + m + d) y = int(y) m = int(m) d = int(d) if date < 18680908: print("pre-meiji") return elif date <= 19120729: print("meiji", y - 1868 + 1, m, d) elif date <= 19261224: print("taisho", y - 1912 + 1, m, d) elif date <= 19890107: print("showa", y - 1926 + 1, m, d) else: print("heisei", y - 1989 + 1, m, d) for line in sys.stdin: y, m, d = line.split() era_transformation(y, m, d)
[["-", 0, 652, 3, 4, 0, 657, 31, 657, 12, 612], ["+", 0, 652, 3, 4, 0, 657, 31, 657, 12, 612]]
5
229
ans=[] while True: try: year,month,day=list(map(int,input().split())) except EOFError: break except ValueError: break wareki='' if (year < 1868) or (year == 1868 and month < 9) or (year == 1868 and month == 9 and day <= 7): ans.append('pre-meiji') continue elif (year < 1912) or (year == 1912 and month < 7) or (year == 1912 and month == 7 and day <= 29): wareki+='meiji '+str(year-1867) elif (year < 1926) or (year == 1926 and month < 12) or (year == 1926 and month == 12 and day <= 24): wareki+='taisho '+str(year-1911) elif (year < 1989) or (year == 1989 and month < 1) or (year == 1989 and month == 1 and day <= 7): wareki+='taisho '+str(year-1925) else: wareki+='heisei '+str(year-1988) ans.append(wareki+' '+str(month)+' '+str(day)) for i in ans: print(i)
ans=[] while True: try: year,month,day=list(map(int,input().split())) except EOFError: break except ValueError: break wareki='' if (year < 1868) or (year == 1868 and month < 9) or (year == 1868 and month == 9 and day <= 7): ans.append('pre-meiji') continue elif (year < 1912) or (year == 1912 and month < 7) or (year == 1912 and month == 7 and day <= 29): wareki+='meiji '+str(year-1867) elif (year < 1926) or (year == 1926 and month < 12) or (year == 1926 and month == 12 and day <= 24): wareki+='taisho '+str(year-1911) elif (year < 1989) or (year == 1989 and month < 1) or (year == 1989 and month == 1 and day <= 7): wareki+='showa '+str(year-1925) else: wareki+='heisei '+str(year-1988) ans.append(wareki+' '+str(month)+' '+str(day)) for i in ans: print(i)
[["-", 0, 1, 0, 677, 12, 657, 31, 557, 0, 6], ["+", 0, 1, 0, 677, 12, 657, 31, 557, 0, 6]]
5
258
while True: try: y, m, d = list(map(int, input().split())) date = int("{}{}{}".format('%04d'%y,'%02d'%m,'%02d'%d)) if date < 18680908: print("pre-meiji") elif date < 19210730: y = y - 1868 + 1 print("meiji {} {} {}".format(y,m,d)) elif date < 19261225: y = y - 1912 + 1 print("taisho {} {} {}".format(y,m,d)) elif date < 19890108: y = y - 1926 + 1 print("showa {} {} {}".format(y,m,d)) else: y = y - 1989 + 1 print("heisei {} {} {}".format(y,m,d)) except: break
while True: try: y, m, d = list(map(int, input().split())) date = int("{}{}{}".format('%04d'%y,'%02d'%m,'%02d'%d)) if date < 18680908: print("pre-meiji") elif date < 19120730: y = y - 1868 + 1 print("meiji {} {} {}".format(y,m,d)) elif date < 19261225: y = y - 1912 + 1 print("taisho {} {} {}".format(y,m,d)) elif date < 19890108: y = y - 1926 + 1 print("showa {} {} {}".format(y,m,d)) else: y = y - 1989 + 1 print("heisei {} {} {}".format(y,m,d)) except: break
[["-", 8, 196, 0, 57, 75, 665, 15, 666, 0, 612], ["+", 8, 196, 0, 57, 75, 665, 15, 666, 0, 612]]
5
174
import sys s=0 p=[list(map(float,e.split(',')))for e in sys.stdin] x,y=p[0] for i in range(len(p)-2): a,b=p[i+1];c,d=p[i+2] s-=((a-x)*(d-y)-(b-y)*(c-x))/2 print(s)
import sys s=0 p=[list(map(float,e.split(',')))for e in sys.stdin] x,y=p[0] for i in range(len(p)-2): a,b=p[i+1];c,d=p[i+2] s+=((a-x)*(d-y)-(b-y)*(c-x))/2 print(abs(s))
[["-", 0, 7, 8, 196, 0, 1, 0, 677, 17, 110], ["+", 0, 7, 8, 196, 0, 1, 0, 677, 17, 107], ["+", 0, 1, 0, 652, 3, 4, 0, 652, 63, 22], ["+", 0, 652, 3, 4, 0, 652, 3, 4, 0, 24], ["+", 0, 652, 3, 4, 0, 652, 3, 4, 0, 25]]
5
106
import sys s=0 p=[list(map(float,e.split(',')))for e in sys.stdin];n=len(p) for i in range(n):a,b=p[i];c,d=p[-~i%n];s+=a*d-b*c print(-s/2)
import sys s=0 p=[list(map(float,e.split(',')))for e in sys.stdin] n=len(p) for i in range(n):a,b=p[i];c,d=p[-~i%n];s+=a*d-b*c print(abs(s)/2)
[["-", 36, 36, 36, 36, 36, 36, 0, 656, 0, 35], ["-", 0, 652, 3, 4, 0, 657, 31, 664, 17, 33], ["+", 0, 652, 3, 4, 0, 657, 31, 652, 63, 22], ["+", 3, 4, 0, 657, 31, 652, 3, 4, 0, 24], ["+", 3, 4, 0, 657, 31, 652, 3, 4, 0, 25]]
5
84
import sys x=[list(map(float,s[:-1].split(","))) for s in sys.stdin] s=[x[i][0]*x[i+1][1]-x[i][1]*x[i+1][0] for i in range(len(x)-1)] print(abs(sum(s))/2)
import sys x=[list(map(float,s[:-1].split(","))) for s in sys.stdin] s=[x[i][0]*x[i-1][1]-x[i][1]*x[i-1][0] for i in range(len(x))] print(abs(sum(s))/2)
[["-", 31, 657, 12, 206, 51, 206, 206, 657, 17, 72], ["+", 31, 657, 12, 206, 51, 206, 206, 657, 17, 33], ["-", 12, 657, 12, 206, 51, 206, 206, 657, 17, 72], ["+", 12, 657, 12, 206, 51, 206, 206, 657, 17, 33], ["-", 0, 659, 12, 652, 3, 4, 0, 657, 17, 33], ["-", 0, 659, 12, 652, 3, 4, 0, 657, 12, 612]]
5
96
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n; Double n1, n2; n = sc.nextInt(); while (n != -1) { n1 = n * 1.0 / 2; n2 = n1 - (n1 * n1 * n1 - n) / (3 * n1 * n1); while (0.00001 * n <= Math.abs(n2 * n2 * n2 - n)) { n1 = n2; n2 = n1 - (n1 * n1 * n1 - n) / (3 * n1 * n1); } System.out.printf("%.6f", n2); n = sc.nextInt(); } } }
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n; Double n1, n2; n = sc.nextInt(); while (n != -1) { n1 = n * 1.0 / 2; n2 = n1 - (n1 * n1 * n1 - n) / (3 * n1 * n1); while (0.000001 * n <= Math.abs(n2 * n2 * n2 - n)) { n1 = n2; n2 = n1 - (n1 * n1 * n1 - n) / (3 * n1 * n1); } System.out.printf("%.6f\n", n2); n = sc.nextInt(); } } }
[["-", 0, 52, 15, 15, 0, 16, 31, 16, 31, 515], ["+", 0, 52, 15, 15, 0, 16, 31, 16, 31, 515], ["+", 0, 1, 0, 492, 3, 4, 0, 5, 0, 44]]
3
158
import java.util.*; class Main { void run() { Scanner sc = new Scanner(System.in); while (true) { int z = sc.nextInt(); if (z == -1) break; double q = (double)z; double x = q / 2; while (true) { if (Math.abs(x * x * x - q) < 0.00001 * q) break; x = x - (x * x * x - q) / (3 * x * x); // System.out.println(x); } System.out.printf("%.6f", x); } } public static void main(String[] args) { new Main().run(); } }
import java.util.*; class Main { void run() { Scanner sc = new Scanner(System.in); while (true) { int z = sc.nextInt(); if (z == -1) break; double q = (double)z; double x = q / 2; while (true) { if (Math.abs(x * x * x - q) < 0.00001 * q) break; x = x - (x * x * x - q) / (3 * x * x); // System.out.println(x); } System.out.printf("%.6f\n", x); } } public static void main(String[] args) { new Main().run(); } }
[["+", 0, 1, 0, 492, 3, 4, 0, 5, 0, 44]]
3
151
#include <math.h> #include <stdio.h> int main(void) { int q; double x; while (1) { scanf("%d", &q); if (q == -1) { break; } x = q / 2.0; while (fabs(x * x * x - q) >= 1e-5 * q) { x = x - (x * x * x - q) / (x * x); } printf("%f\n", x); } return (0); }
#include <math.h> #include <stdio.h> int main(void) { int q; double x; while (1) { scanf("%d", &q); if (q == -1) { break; } x = q / 2.0; while (fabs(x * x * x - q) >= 1e-5 * q) { x = x - (x * x * x - q) / (3 * x * x); } printf("%f\n", x); } return (0); }
[["+", 12, 16, 12, 23, 0, 16, 31, 16, 31, 13], ["+", 12, 16, 12, 23, 0, 16, 31, 16, 17, 48]]
0
103
#include <math.h> #include <stdio.h> int main(void) { int q; double x; while (1) { scanf("%d", &q); if (q == -1) break; x = q / 2; while (fabs(x * x * x - q) >= 0.00001 * q) x = x - (x * x * x - q) / (3 * x * x); printf("%f\n", x); } return 0; }
#include <math.h> #include <stdio.h> int main(void) { int q; double x; while (1) { scanf("%d", &q); if (q == -1) break; x = (double)q / 2; while (fabs(x * x * x - q) >= 0.00001 * q) x = x - (x * x * x - q) / (3 * x * x); printf("%f\n", x); } return 0; }
[["+", 0, 1, 0, 11, 12, 16, 31, 74, 0, 24], ["+", 0, 11, 12, 16, 31, 74, 39, 77, 39, 40], ["+", 0, 1, 0, 11, 12, 16, 31, 74, 0, 25]]
0
99