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
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) // repetition //------------------------------------------ #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) // constant //-------------------------------------------- const double eps = 1e-10; const double PI = acos(-1.0); const double INF = 1e9; struct P { double x; double y; P() { x = INF, y = INF; } P(const double &s, const double &e) { x = s; y = e; } P operator-(const P &t) const { return P(x - t.x, y - t.y); } P operator+(const P &t) const { return P(x + t.x, y + t.y); } P operator*(const double &d) const { return P(x * d, y * d); } }; double dis(P t) { return sqrt(t.x * t.x + t.y * t.y); } P unit_vector(P t) { double u = fabs(dis(t)); return P(t.x / u, t.y / u); } P rotate(P t, P p, double r) { // double r=radians(angle); double ta = cos(r) * (t.x - p.x) - sin(r) * (t.y - p.y) + p.x; double tb = sin(r) * (t.x - p.x) + cos(r) * (t.y - p.y) + p.y; return P(ta, tb); } void interpoint_cc(P a, double ra, P b, double rb, P ans[]) { double di = fabs(dis(a - b)); if (di > ra + rb || di < fabs(ra - rb)) return; double t = (ra * ra - rb * rb + di * di) / (di + di); double rd = acos(t / ra); P dv = unit_vector(b - a); P g1 = rotate(dv, P(0, 0), rd); P g2 = rotate(dv, P(0, 0), -rd); ans[0] = a + g1 * ra; ans[1] = a + g2 * ra; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); int N; while (cin >> N, N) { vector<P> p(N); vector<double> ls(N); REP(i, N) cin >> p[i].x >> p[i].y >> ls[i]; double lb = 0, ub = 300; REP(loop, 100) { double h = (lb + ub) / 2.; bool ok = true; REP(i, N) if (ls[i] < h) ok = false; if (ok) { ok = false; vector<double> ri(N); REP(i, N) ri[i] = sqrt(ls[i] * ls[i] - h * h); vector<P> chk; REP(i, N) { REP(j, N) { if (dis(p[i] - p[j]) > ri[i] + ri[j] || dis(p[i] - p[j]) < fabs(ri[i] - ri[j])) continue; P icp[2]; interpoint_cc(p[i], ri[i], p[j], ri[j], icp); chk.PB(icp[0]); chk.PB(icp[1]); } chk.PB(p[i]); } for (auto &pp : chk) { int cnt = 0; REP(k, N) { if (dis(p[k] - pp) <= ri[k]) ++cnt; } if (cnt == N) ok = true; } } if (ok) lb = h; else ub = h; } cout << fixed << setprecision(9) << lb << endl; } return 0; }
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) // repetition //------------------------------------------ #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) // constant //-------------------------------------------- const double eps = 1e-10; const double PI = acos(-1.0); const double INF = 1e9; struct P { double x; double y; P() { x = INF, y = INF; } P(const double &s, const double &e) { x = s; y = e; } P operator-(const P &t) const { return P(x - t.x, y - t.y); } P operator+(const P &t) const { return P(x + t.x, y + t.y); } P operator*(const double &d) const { return P(x * d, y * d); } }; double dis(P t) { return sqrt(t.x * t.x + t.y * t.y); } P unit_vector(P t) { double u = fabs(dis(t)); return P(t.x / u, t.y / u); } P rotate(P t, P p, double r) { // double r=radians(angle); double ta = cos(r) * (t.x - p.x) - sin(r) * (t.y - p.y) + p.x; double tb = sin(r) * (t.x - p.x) + cos(r) * (t.y - p.y) + p.y; return P(ta, tb); } void interpoint_cc(P a, double ra, P b, double rb, P ans[]) { double di = fabs(dis(a - b)); if (di > ra + rb || di < fabs(ra - rb)) return; double t = (ra * ra - rb * rb + di * di) / (di + di); double rd = acos(t / ra); P dv = unit_vector(b - a); P g1 = rotate(dv, P(0, 0), rd); P g2 = rotate(dv, P(0, 0), -rd); ans[0] = a + g1 * ra; ans[1] = a + g2 * ra; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); int N; while (cin >> N, N) { vector<P> p(N); vector<double> ls(N); REP(i, N) cin >> p[i].x >> p[i].y >> ls[i]; double lb = 0, ub = 300; REP(loop, 100) { double h = (lb + ub) / 2.; bool ok = true; REP(i, N) if (ls[i] < h) ok = false; if (ok) { ok = false; vector<double> ri(N); REP(i, N) ri[i] = sqrt(ls[i] * ls[i] - h * h); vector<P> chk; REP(i, N) { REP(j, N) { if (dis(p[i] - p[j]) > ri[i] + ri[j] || dis(p[i] - p[j]) < fabs(ri[i] - ri[j])) continue; P icp[2]; interpoint_cc(p[i], ri[i], p[j], ri[j], icp); chk.PB(icp[0]); chk.PB(icp[1]); } chk.PB(p[i]); } for (auto &pp : chk) { int cnt = 0; REP(k, N) { if (dis(p[k] - pp) <= ri[k] + eps) ++cnt; } if (cnt == N) ok = true; } } if (ok) lb = h; else ub = h; } cout << fixed << setprecision(9) << lb << endl; } return 0; }
[["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 72], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 12, 22]]
1
1,019
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> //#include<cctype> #include <climits> #include <iostream> #include <map> #include <string> #include <vector> //#include<list> #include <algorithm> #include <deque> #include <queue> //#include<numeric> #include <complex> #include <utility> //#include<memory> #include <cassert> #include <functional> const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int, int> pii; typedef complex<double> C; const double EPS = 1e-6; double x[11], y[11], l[11]; double square(double X) { return X * X; } double calc(int n, double X, double Y) { double ret = 300; for (int i = 0; i < n; i++) { ret = min(ret, /*sqrt*/ (square(l[i]) - square(x[i] - X) - square(y[i] - Y))); } return ret; } double searchY(int n, double x) { double left = -100, right = 100; for (int t = 0; t < 200; t++) { double m1 = (2 * left + right) / 3; double m2 = (left + 2 * right) / 3; double M1 = calc(n, x, m1); double M2 = calc(n, x, m2); if (M1 > M2) { right = m2; } else { left = m1; } } return calc(n, x, (right + left) / 2); } double search(int n) { double left = -100, right = 100; for (int t = 0; t < 200; t++) { double m1 = (2 * left + right) / 3; double m2 = (left + 2 * right) / 3; double M1 = searchY(n, m1); double M2 = searchY(n, m2); if (M1 > M2) { right = m2; } else { left = m1; } } return searchY(n, (left + right) / 2); } void solve(int n) { printf("%.15lf\n", sqrt(search(n))); } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; while (cin >> n) { if (n == 0) break; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> l[i]; } solve(n); } return 0; }
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> //#include<cctype> #include <climits> #include <iostream> #include <map> #include <string> #include <vector> //#include<list> #include <algorithm> #include <deque> #include <queue> //#include<numeric> #include <complex> #include <utility> //#include<memory> #include <cassert> #include <functional> const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int, int> pii; typedef complex<double> C; const double EPS = 1e-6; double x[11], y[11], l[11]; double square(double X) { return X * X; } double calc(int n, double X, double Y) { double ret = 1e9; for (int i = 0; i < n; i++) { ret = min(ret, /*sqrt*/ (square(l[i]) - square(x[i] - X) - square(y[i] - Y))); } return ret; } double searchY(int n, double x) { double left = -100, right = 100; for (int t = 0; t < 200; t++) { double m1 = (2 * left + right) / 3; double m2 = (left + 2 * right) / 3; double M1 = calc(n, x, m1); double M2 = calc(n, x, m2); if (M1 > M2) { right = m2; } else { left = m1; } } return calc(n, x, (right + left) / 2); } double search(int n) { double left = -100, right = 100; for (int t = 0; t < 200; t++) { double m1 = (2 * left + right) / 3; double m2 = (left + 2 * right) / 3; double M1 = searchY(n, m1); double M2 = searchY(n, m2); if (M1 > M2) { right = m2; } else { left = m1; } } return searchY(n, (left + right) / 2); } void solve(int n) { printf("%.15lf\n", sqrt(search(n))); } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; while (cin >> n) { if (n == 0) break; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> l[i]; } solve(n); } return 0; }
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13]]
1
555
#include <algorithm> #include <array> #include <chrono> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> // cin.sync_with_stdio(false); // streambuf using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; using vi = vector<int>; using vll = vector<ll>; using vpii = vector<pii>; template <class T, int s> using va = vector<array<T, s>>; template <class T, class T2> using umap = unordered_map<T, T2>; template <class T> using uset = unordered_set<T>; template <class T, class S> void cmin(T &a, const S &b) { if (a > b) a = b; } template <class T, class S> void cmax(T &a, const S &b) { if (a < b) a = b; } #define ALL(a) a.begin(), a.end() #define rep(i, a) for (int i = 0; i < a; i++) #define rep1(i, a) for (int i = 1; i <= a; i++) #define rrep(i, a) for (int i = a - 1; i >= 0; i--) #define rrep1(i, a) for (int i = a; i; i--) const ll mod = 1000000007; #ifndef INT_MAX const int INT_MAX = numeric_limits<signed>().max(); #endif template <class T> using heap = priority_queue<T, vector<T>, greater<T>>; template <class T> using pque = priority_queue<T, vector<T>, function<T(T, T)>>; template <class T> inline void hash_combine(size_t &seed, const T &v) { hash<T> hasher; seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } namespace std { template <typename S, typename T> struct hash<pair<S, T>> { inline size_t operator()(const pair<S, T> &v) const { size_t seed = 0; hash_combine(seed, v.first); hash_combine(seed, v.second); return seed; } }; // Recursive template code derived from Matthieu M. template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1> struct HashValueImpl { static void apply(size_t &seed, Tuple const &tuple) { HashValueImpl<Tuple, Index - 1>::apply(seed, tuple); hash_combine(seed, std::get<Index>(tuple)); } }; template <class Tuple> struct HashValueImpl<Tuple, 0> { static void apply(size_t &seed, Tuple const &tuple) { hash_combine(seed, std::get<0>(tuple)); } }; template <typename... TT> struct hash<std::tuple<TT...>> { size_t operator()(std::tuple<TT...> const &tt) const { size_t seed = 0; HashValueImpl<std::tuple<TT...>>::apply(seed, tt); return seed; } }; } // namespace std ll pow(ll base, ll i, ll mod) { ll a = 1; while (i) { if (i & 1) { a *= base; a %= mod; } base *= base; base %= mod; i /= 2; } return a; } class unionfind { vector<int> par, rank, size_; //????????§??????????????¢???????????????????????????rank???????????????size????????? public: unionfind(int n) : par(n), rank(n), size_(n, 1) { iota(ALL(par), 0); } int find(int x) { if (par[x] == x) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x), y = find(y); if (x == y) return; if (rank[x] < rank[y]) swap(x, y); par[y] = x; size_[x] += size_[y]; if (rank[x] == rank[y]) rank[x]++; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return size_[find(x)]; } }; ll gcd(ll a, ll b) { while (b) { ll c = a % b; a = b; b = c; } return a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } int popcnt(unsigned long long a) { a = (a & 0x5555555555555555) + (a >> 1 & 0x5555555555555555); a = (a & 0x3333333333333333) + (a >> 2 & 0x3333333333333333); a = (a & 0x0f0f0f0f0f0f0f0f) + (a >> 4 & 0x0f0f0f0f0f0f0f0f); a = (a & 0x00ff00ff00ff00ff) + (a >> 8 & 0x00ff00ff00ff00ff); a = (a & 0x0000ffff0000ffff) + (a >> 16 & 0x0000ffff0000ffff); return (a & 0xffffffff) + (a >> 32); } template <class T, class Func = function<const T(const T, const T)>> class segtree { vector<T> obj; int offset; Func updater; T e; int bufsize(int num) { int i = 1; for (; num > i; i <<= 1) ; offset = i - 1; return (i << 1) - 1; } T query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return e; if (a <= l && r <= b) return obj[k]; else return updater(query(a, b, k * 2 + 1, l, (l + r) / 2), query(a, b, k * 2 + 2, (l + r) / 2, r)); } public: T query(int a, int b) { //[a,b) return query(a, b, 0, 0, offset + 1); } void updateall(int l = 0, int r = -1) { if (r < 0) r = offset + 1; l += offset, r += offset; do { l = l - 1 >> 1, r = r - 1 >> 1; for (int i = l; i < r; i++) obj[i] = updater(obj[i * 2 + 1], obj[i * 2 + 2]); } while (l); } void update(int k, T &a) { k += offset; obj[k] = a; while (k) { k = k - 1 >> 1; obj[k] = updater(obj[k * 2 + 1], obj[k * 2 + 2]); } } segtree(int n, T e, const Func &updater_ = Func()) : obj(bufsize(n), e), e(e), updater(updater_) {} segtree(vector<T> &vec, T e, const Func &updater = Func()) : obj(bufsize(vec.size()), e), e(e), updater(updater) { copy(vec.begin(), vec.end(), obj.begin() + offset); updateall(); } typename vector<T>::reference operator[](int n) { return obj[n + offset]; } }; template <class T> class matrix { vector<vector<T>> obj; pair<int, int> s; public: matrix(pair<int, int> size, T e = 0) : matrix(size.first, size.second, e) {} matrix(int n, int m = -1, T e = 0) : obj(n, vector<T>(m == -1 ? n : m, e)), s(n, m == -1 ? n : m) {} static matrix e(int n) { matrix a = (n); for (int i = 0; i < n; i++) a[i][i] = 1; return a; } matrix &operator+=(const matrix &p) { if (s != p.s) throw runtime_error("matrix error"); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) obj[i][j] += p.obj[i][j]; return *this; } matrix operator+(const matrix &p) { matrix res(*this); return res += p; } matrix &operator-=(const matrix &p) { if (s != p.s) throw runtime_error("matrix error"); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) obj[i][j] -= p.obj[i][j]; return *this; } matrix operator-(const matrix &p) { matrix res(*this); return res -= p; } matrix &operator*=(T p) { for (auto &a : obj) for (auto &b : a) b *= p; return *this; } matrix operator*(T p) { matrix res(*this); return res *= p; } matrix operator*(const matrix &p) { if (s.second != p.s.first) throw runtime_error("matrix error"); matrix ret(s.first, p.s.second); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) for (int k = 0; k < p.s.second; k++) ret[i][k] += obj[i][j] * p.obj[j][k]; return ret; } matrix &operator*=(const matrix &p) { return *this = *this * p; } pair<int, int> size() const { return s; } matrix &mod(T m) { for (auto &a : obj) for (auto &b : a) b %= m; return *this; } typename vector<vector<T>>::reference operator[](int t) { return obj[t]; } }; template <class T> inline matrix<T> pow(matrix<T> &base, unsigned exp) { auto base_(base); if (base_.size().first != base_.size().second) throw runtime_error("matrix error"); matrix<T> res = matrix<T>::e(base_.size().first); for (;;) { if (exp & 1) res *= base_; if (!(exp /= 2)) break; base_ *= base_; } return res; } template <class T> inline matrix<T> modpow(matrix<T> &base, unsigned exp, T m) { auto base_(base); if (base.size().first != base_.size().second) throw runtime_error("matrix error"); matrix<T> res = matrix<T>::e(base_.size().first); for (;;) { if (exp & 1) (res *= base_).mod(m); if (!(exp /= 2)) break; (base_ *= base_).mod(m); } return res; } template <class T> int id(vector<T> &a, T b) { return lower_bound(ALL(a), b) - a.begin(); } class Flow { int V; struct edge { int to, cap, rev, cost; }; vector<vector<edge>> G; vector<int> level, iter, h, dist, prevv, preve; public: Flow(int size) : G(size + 1), V(size + 1) {} void add_edge(int from, int to, int cap, int cost = 0) { G[from].push_back(edge{to, cap, (int)G[to].size(), cost}); G[to].push_back(edge{from, 0, (int)G[from].size() - 1, -cost}); } void bfs(int s) { fill(level.begin(), level.end(), -1); queue<int> que; level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v, int t, int f) { if (v == t) return f; for (int &i = iter[v]; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { level.resize(V); iter.resize(V); int flow = 0; for (;;) { bfs(s); if (level[t] < 0) return flow; fill(iter.begin(), iter.end(), 0); int f; while ((f = dfs(s, t, numeric_limits<int>::max())) > 0) { flow += f; } } } typedef pair<int, int> P; int min_cost_flow(int s, int t, int f) { int res = 0; h.resize(V); dist.resize(V); prevv.resize(V); preve.resize(V); fill(h.begin(), h.end(), 0); while (f > 0) { priority_queue<P, vector<P>, greater<P>> que; fill(dist.begin(), dist.end(), numeric_limits<int>::max()); dist[s] = 0; que.push({0, s}); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (dist[v] < p.first) continue; for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) { dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v; preve[e.to] = i; que.push({dist[e.to], e.to}); } } } if (dist[t] == numeric_limits<int>::max()) { return -1; } for (int v = 0; v < V; v++) h[v] += dist[v]; int d = f; for (int v = t; v != s; v = prevv[v]) { d = min(d, G[prevv[v]][preve[v]].cap); } f -= d; res += d * h[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } return res; } }; const ld eps = 1e-11, pi = acos(-1.0); typedef complex<ld> P; typedef vector<P> VP; ld dot(P a, P b) { return real(conj(a) * b); } ld cross(P a, P b) { return imag(conj(a) * b); } namespace std { bool operator<(const P &a, const P &b) { return abs(a.real() - b.real()) < eps ? a.imag() < b.imag() : a.real() < b.real(); } } // namespace std struct L { P a, b; }; // line->l,segment->s struct C { P p; ld r; }; int ccw(P a, P b, P c) { b -= a; c -= a; if (cross(b, c) > eps) return 1; // counter clockwise if (cross(b, c) < -eps) return -1; // clockwise if (dot(b, c) < 0) return 2; // c--a--b on line if (norm(b) < norm(c)) return -2; // a--b--c on line return 0; // a--c--b on line } bool isis_ll(L l, L m) { // is intersect return abs(cross(l.b - l.a, m.b - m.a)) > eps; } bool isis_ls(L l, L s) { ld a = cross(l.b - l.a, s.a - l.a); ld b = cross(l.b - l.a, s.b - l.a); return (a * b < eps); } bool isis_lp(L l, P p) { return abs(cross(l.b - p, l.a - p)) < eps; } bool isis_ss(L s, L t) { return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0; } P is_ll(L s, L t) { // intersect P sv = s.b - s.a, tv = t.b - t.a; return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv); } bool isis_sp(L s, P p) { return abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps; } P proj(L l, P p) { ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + t * (l.a - l.b); } ld dist_lp(L l, P p) { return abs(p - proj(l, p)); } ld dist_ll(L l, L m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); } ld dist_ls(L l, L s) { if (isis_ls(l, s)) return 0; return min(dist_lp(l, s.a), dist_lp(l, s.b)); } ld dist_sp(L s, P p) { P r = proj(s, p); if (isis_sp(s, r)) return abs(r - p); return min(abs(s.a - p), abs(s.b - p)); } ld dist_ss(L s, L t) { if (isis_ss(s, t)) return 0; ld a = min(dist_sp(s, t.a), dist_sp(t, s.a)); ld b = min(dist_sp(s, t.b), dist_sp(t, s.b)); return min(a, b); } VP is_cc(C c1, C c2) { VP res; ld d = abs(c1.p - c2.p); ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d); ld dfr = c1.r * c1.r - rc * rc; if (abs(dfr) < eps) dfr = 0.0; else if (dfr < 0.0) return res; // no intersection ld rs = sqrt(dfr); P diff = (c2.p - c1.p) / d; res.push_back(c1.p + diff * P(rc, rs)); if (dfr != 0.0) res.push_back(c1.p + diff * P(rc, -rs)); return res; } bool isis_vc(vector<C> vc) { VP crs; int n = vc.size(); rep(i, n) rep(j, i) for (P p : is_cc(vc[i], vc[j])) crs.push_back(p); rep(i, n) crs.push_back(vc[i].p); for (P p : crs) { bool valid = true; rep(i, n) if (abs(p - vc[i].p) > vc[i].r + eps) valid = false; if (valid) return true; } return false; } VP is_lc(C c, L l) { VP res; ld d = dist_lp(l, c.p); if (d < c.r + eps) { ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); // safety; P nor = (l.a - l.b) / abs(l.a - l.b); res.push_back(proj(l, c.p) + len * nor); res.push_back(proj(l, c.p) - len * nor); } return res; } VP is_sc(C c, L l) { VP v = is_lc(c, l), res; for (P p : v) if (isis_sp(l, p)) res.push_back(p); return res; } vector<L> tangent_cp(C c, P p) { //????????\???? vector<L> ret; P v = c.p - p; ld d = abs(v); ld l = sqrt(norm(v) - c.r * c.r); if (isnan(l)) { return ret; } P v1 = v * P(l / d, c.r / d); P v2 = v * P(l / d, -c.r / d); ret.push_back(L{p, p + v1}); if (l < eps) return ret; ret.push_back(L{p, p + v2}); return ret; } vector<L> tangent_cc(C c1, C c2) { vector<L> ret; if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) { P center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r); ret = tangent_cp(c1, center); } if (abs(c1.r - c2.r) > eps) { P out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r); vector<L> nret = tangent_cp(c1, out); ret.insert(ret.end(), ALL(nret)); } else { P v = c2.p - c1.p; v /= abs(v); P q1 = c1.p + v * P(0, 1) * c1.r; P q2 = c1.p + v * P(0, -1) * c1.r; ret.push_back(L{q1, q1 + v}); ret.push_back(L{q2, q2 + v}); } return ret; } ld area(const VP &p) { //??¢????? ld res = 0; int n = p.size(); rep(j, n) res += cross(p[j], p[(j + 1) % n]); return res / 2; } bool is_polygon(L l, VP &g) { int n = g.size(); for (int i = 0; i < n; i++) { P a = g[i]; P b = g[(i + 1) % n]; if (isis_ss(l, L{a, b})) return true; } return false; } int is_in_Polygon(const VP &g, P p) { bool in = false; int n = g.size(); for (int i = 0; i < n; i++) { P a = g[i] - p, b = g[(i + 1) % n] - p; if (imag(a) > imag(b)) swap(a, b); if (imag(a) <= 0 && 0 < imag(b)) if (cross(a, b) < 0) in = !in; if (abs(cross(a, b)) < eps && dot(a, b) < eps) return 0; // on } if (in) return 1; // in return -1; // out } VP ConvexHull(VP ps) { int n = ps.size(); int k = 0; sort(ps.begin(), ps.end()); VP ch(2 * n); for (int i = 0; i < n; ch[k++] = ps[i++]) while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; ch.resize(k - 1); return ch; } VP ConvexCut(const VP &ps, L l) { VP Q; for (int i = 0; i < (int)ps.size(); i++) { P A = ps[i], B = ps[(i + 1) % ps.size()]; if (ccw(l.a, l.b, A) != -1) Q.push_back(A); if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0) Q.push_back(is_ll(L{A, B}, l)); } return Q; } double dist2(P a) { return real(a) * real(a) + imag(a) * imag(a); } // Suffix Array ?????????O(|S|log^2|S|), ????´¢O(|T|log|S|), // ?????????????§????O(|S|) class StringSearch { const int n; string S; public: vector<int> sa, rank; StringSearch(const string &S_) : n(S_.size()), S(S_), sa(n + 1), rank(n + 1) { for (int i = 0; i <= n; i++) { sa[i] = i; rank[i] = i < n ? S[i] : -1; } vector<int> tmp(n + 1); for (int k = 1; k <= n; k *= 2) { auto Compare_SA = [=](int i, int j) { if (this->rank[i] != this->rank[j]) return this->rank[i] < this->rank[j]; int ri = i + k <= n ? this->rank[i + k] : -1; int rj = j + k <= n ? this->rank[j + k] : -1; return ri < rj; }; sort(sa.begin(), sa.end(), Compare_SA); tmp[sa[0]] = 0; for (int i = 1; i <= n; i++) { tmp[sa[i]] = tmp[sa[i - 1]] + (Compare_SA(sa[i - 1], sa[i]) ? 1 : 0); } for (int i = 0; i <= n; i++) { this->rank[i] = tmp[i]; } } } bool Contain(const string &T) { int a = 0, b = n; while (b - a > 1) { int c = (a + b) / 2; if (S.compare(sa[c], T.length(), T) < 0) a = c; else b = c; } return S.compare(sa[b], T.length(), T) == 0; } vector<int> LCPArray() { for (int i = 0; i <= n; i++) rank[sa[i]] = i; int h = 0; vector<int> lcp(n + 1); for (int i = 0; i < n; i++) { int j = sa[rank[i] - 1]; if (h > 0) h--; for (; j + h < n && i + h < n; h++) { if (S[j + h] != S[i + h]) break; } lcp[rank[i] - 1] = h; } return lcp; } }; int main() { int n; while (cin >> n, n) { va<int, 3> bl(n); rep(i, n) rep(j, 3) cin >> bl[i][j]; va<double, 6> h; rep(i, n) rep(j, i) rep(k, j) { double x[2][3] = { {(double)bl[i][0] - bl[j][0], (double)bl[i][1] - bl[j][1], -pow(bl[i][2], 2) + pow(bl[j][2], 2) + pow(bl[i][0], 2) + pow(bl[i][1], 2) - pow(bl[j][0], 2) - pow(bl[j][1], 2)}, {(double)bl[i][0] - bl[k][0], (double)bl[i][1] - bl[k][1], -pow(bl[i][2], 2) + pow(bl[k][2], 2) + pow(bl[i][0], 2) + pow(bl[i][1], 2) - pow(bl[k][0], 2) - pow(bl[k][1], 2)}}; double a, b, c; if (x[1][1] == 0) rep(i, 3) swap(x[0][i], x[1][i]); if (x[1][1] == 0) continue; if (x[0][0] - (x[0][1] / x[1][1]) * x[1][0] == 0) continue; a = (x[0][2] - x[0][1] / x[1][1] * x[1][2]) / (x[0][0] - (x[0][1] / x[1][1]) * x[1][0]) / 2; b = (x[1][2] - 2 * a * x[1][0]) / 2 / x[1][1]; c = pow(bl[i][2], 2) - pow(a - bl[i][0], 2) - pow(b - bl[i][1], 2); if (c < 0) continue; c = sqrt(c); h.push_back({a, b, c, (double)k, (double)j, (double)i}); } rep(i, n) rep(j, i) { double l = hypot(bl[i][0] - bl[j][0], bl[i][1] - bl[j][1]); double a = (pow(bl[j][2], 2) - pow(bl[i][2], 2) + pow(l, 2)) / 2 / l; double x = (bl[i][0] * a + bl[j][0] * (l - a)) / l, y = (bl[i][1] * a + bl[j][1] * (l - a)) / l, z = pow(bl[j][2], 2) - pow(a, 2); if (z < 0) continue; z = sqrt(z); h.push_back( {(double)x, (double)y, (double)z, (double)j, (double)i, (double)i}); } rep(i, n) h.push_back({(double)bl[i][0], (double)bl[i][1], (double)bl[i][2], (double)i, (double)i, (double)i}); // sort(ALL(h), [](const array<double, 6> &a, const array<double, 6> &b) // {return a[2] != b[2] ? a[2] > b[2]:a > b; }); double ans = 0; for (auto &&x : h) { int pos = 3; bool f = 1; rep(i, n) { if (i == x[pos]) { if (pos != 5) pos++; continue; } if (pow(x[0] - bl[i][0], 2) + pow(x[1] - bl[i][1], 2) + pow(x[2], 2) > pow(bl[i][2], 2) + 1e-7) { f = 0; break; } } if (f) { cmax(ans, x[2]); break; } } cout << fixed << setprecision(10) << ans << endl; } }
#include <algorithm> #include <array> #include <chrono> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> // cin.sync_with_stdio(false); // streambuf using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; using vi = vector<int>; using vll = vector<ll>; using vpii = vector<pii>; template <class T, int s> using va = vector<array<T, s>>; template <class T, class T2> using umap = unordered_map<T, T2>; template <class T> using uset = unordered_set<T>; template <class T, class S> void cmin(T &a, const S &b) { if (a > b) a = b; } template <class T, class S> void cmax(T &a, const S &b) { if (a < b) a = b; } #define ALL(a) a.begin(), a.end() #define rep(i, a) for (int i = 0; i < a; i++) #define rep1(i, a) for (int i = 1; i <= a; i++) #define rrep(i, a) for (int i = a - 1; i >= 0; i--) #define rrep1(i, a) for (int i = a; i; i--) const ll mod = 1000000007; #ifndef INT_MAX const int INT_MAX = numeric_limits<signed>().max(); #endif template <class T> using heap = priority_queue<T, vector<T>, greater<T>>; template <class T> using pque = priority_queue<T, vector<T>, function<T(T, T)>>; template <class T> inline void hash_combine(size_t &seed, const T &v) { hash<T> hasher; seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } namespace std { template <typename S, typename T> struct hash<pair<S, T>> { inline size_t operator()(const pair<S, T> &v) const { size_t seed = 0; hash_combine(seed, v.first); hash_combine(seed, v.second); return seed; } }; // Recursive template code derived from Matthieu M. template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1> struct HashValueImpl { static void apply(size_t &seed, Tuple const &tuple) { HashValueImpl<Tuple, Index - 1>::apply(seed, tuple); hash_combine(seed, std::get<Index>(tuple)); } }; template <class Tuple> struct HashValueImpl<Tuple, 0> { static void apply(size_t &seed, Tuple const &tuple) { hash_combine(seed, std::get<0>(tuple)); } }; template <typename... TT> struct hash<std::tuple<TT...>> { size_t operator()(std::tuple<TT...> const &tt) const { size_t seed = 0; HashValueImpl<std::tuple<TT...>>::apply(seed, tt); return seed; } }; } // namespace std ll pow(ll base, ll i, ll mod) { ll a = 1; while (i) { if (i & 1) { a *= base; a %= mod; } base *= base; base %= mod; i /= 2; } return a; } class unionfind { vector<int> par, rank, size_; //????????§??????????????¢???????????????????????????rank???????????????size????????? public: unionfind(int n) : par(n), rank(n), size_(n, 1) { iota(ALL(par), 0); } int find(int x) { if (par[x] == x) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x), y = find(y); if (x == y) return; if (rank[x] < rank[y]) swap(x, y); par[y] = x; size_[x] += size_[y]; if (rank[x] == rank[y]) rank[x]++; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return size_[find(x)]; } }; ll gcd(ll a, ll b) { while (b) { ll c = a % b; a = b; b = c; } return a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } int popcnt(unsigned long long a) { a = (a & 0x5555555555555555) + (a >> 1 & 0x5555555555555555); a = (a & 0x3333333333333333) + (a >> 2 & 0x3333333333333333); a = (a & 0x0f0f0f0f0f0f0f0f) + (a >> 4 & 0x0f0f0f0f0f0f0f0f); a = (a & 0x00ff00ff00ff00ff) + (a >> 8 & 0x00ff00ff00ff00ff); a = (a & 0x0000ffff0000ffff) + (a >> 16 & 0x0000ffff0000ffff); return (a & 0xffffffff) + (a >> 32); } template <class T, class Func = function<const T(const T, const T)>> class segtree { vector<T> obj; int offset; Func updater; T e; int bufsize(int num) { int i = 1; for (; num > i; i <<= 1) ; offset = i - 1; return (i << 1) - 1; } T query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return e; if (a <= l && r <= b) return obj[k]; else return updater(query(a, b, k * 2 + 1, l, (l + r) / 2), query(a, b, k * 2 + 2, (l + r) / 2, r)); } public: T query(int a, int b) { //[a,b) return query(a, b, 0, 0, offset + 1); } void updateall(int l = 0, int r = -1) { if (r < 0) r = offset + 1; l += offset, r += offset; do { l = l - 1 >> 1, r = r - 1 >> 1; for (int i = l; i < r; i++) obj[i] = updater(obj[i * 2 + 1], obj[i * 2 + 2]); } while (l); } void update(int k, T &a) { k += offset; obj[k] = a; while (k) { k = k - 1 >> 1; obj[k] = updater(obj[k * 2 + 1], obj[k * 2 + 2]); } } segtree(int n, T e, const Func &updater_ = Func()) : obj(bufsize(n), e), e(e), updater(updater_) {} segtree(vector<T> &vec, T e, const Func &updater = Func()) : obj(bufsize(vec.size()), e), e(e), updater(updater) { copy(vec.begin(), vec.end(), obj.begin() + offset); updateall(); } typename vector<T>::reference operator[](int n) { return obj[n + offset]; } }; template <class T> class matrix { vector<vector<T>> obj; pair<int, int> s; public: matrix(pair<int, int> size, T e = 0) : matrix(size.first, size.second, e) {} matrix(int n, int m = -1, T e = 0) : obj(n, vector<T>(m == -1 ? n : m, e)), s(n, m == -1 ? n : m) {} static matrix e(int n) { matrix a = (n); for (int i = 0; i < n; i++) a[i][i] = 1; return a; } matrix &operator+=(const matrix &p) { if (s != p.s) throw runtime_error("matrix error"); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) obj[i][j] += p.obj[i][j]; return *this; } matrix operator+(const matrix &p) { matrix res(*this); return res += p; } matrix &operator-=(const matrix &p) { if (s != p.s) throw runtime_error("matrix error"); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) obj[i][j] -= p.obj[i][j]; return *this; } matrix operator-(const matrix &p) { matrix res(*this); return res -= p; } matrix &operator*=(T p) { for (auto &a : obj) for (auto &b : a) b *= p; return *this; } matrix operator*(T p) { matrix res(*this); return res *= p; } matrix operator*(const matrix &p) { if (s.second != p.s.first) throw runtime_error("matrix error"); matrix ret(s.first, p.s.second); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) for (int k = 0; k < p.s.second; k++) ret[i][k] += obj[i][j] * p.obj[j][k]; return ret; } matrix &operator*=(const matrix &p) { return *this = *this * p; } pair<int, int> size() const { return s; } matrix &mod(T m) { for (auto &a : obj) for (auto &b : a) b %= m; return *this; } typename vector<vector<T>>::reference operator[](int t) { return obj[t]; } }; template <class T> inline matrix<T> pow(matrix<T> &base, unsigned exp) { auto base_(base); if (base_.size().first != base_.size().second) throw runtime_error("matrix error"); matrix<T> res = matrix<T>::e(base_.size().first); for (;;) { if (exp & 1) res *= base_; if (!(exp /= 2)) break; base_ *= base_; } return res; } template <class T> inline matrix<T> modpow(matrix<T> &base, unsigned exp, T m) { auto base_(base); if (base.size().first != base_.size().second) throw runtime_error("matrix error"); matrix<T> res = matrix<T>::e(base_.size().first); for (;;) { if (exp & 1) (res *= base_).mod(m); if (!(exp /= 2)) break; (base_ *= base_).mod(m); } return res; } template <class T> int id(vector<T> &a, T b) { return lower_bound(ALL(a), b) - a.begin(); } class Flow { int V; struct edge { int to, cap, rev, cost; }; vector<vector<edge>> G; vector<int> level, iter, h, dist, prevv, preve; public: Flow(int size) : G(size + 1), V(size + 1) {} void add_edge(int from, int to, int cap, int cost = 0) { G[from].push_back(edge{to, cap, (int)G[to].size(), cost}); G[to].push_back(edge{from, 0, (int)G[from].size() - 1, -cost}); } void bfs(int s) { fill(level.begin(), level.end(), -1); queue<int> que; level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v, int t, int f) { if (v == t) return f; for (int &i = iter[v]; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { level.resize(V); iter.resize(V); int flow = 0; for (;;) { bfs(s); if (level[t] < 0) return flow; fill(iter.begin(), iter.end(), 0); int f; while ((f = dfs(s, t, numeric_limits<int>::max())) > 0) { flow += f; } } } typedef pair<int, int> P; int min_cost_flow(int s, int t, int f) { int res = 0; h.resize(V); dist.resize(V); prevv.resize(V); preve.resize(V); fill(h.begin(), h.end(), 0); while (f > 0) { priority_queue<P, vector<P>, greater<P>> que; fill(dist.begin(), dist.end(), numeric_limits<int>::max()); dist[s] = 0; que.push({0, s}); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (dist[v] < p.first) continue; for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) { dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v; preve[e.to] = i; que.push({dist[e.to], e.to}); } } } if (dist[t] == numeric_limits<int>::max()) { return -1; } for (int v = 0; v < V; v++) h[v] += dist[v]; int d = f; for (int v = t; v != s; v = prevv[v]) { d = min(d, G[prevv[v]][preve[v]].cap); } f -= d; res += d * h[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } return res; } }; const ld eps = 1e-11, pi = acos(-1.0); typedef complex<ld> P; typedef vector<P> VP; ld dot(P a, P b) { return real(conj(a) * b); } ld cross(P a, P b) { return imag(conj(a) * b); } namespace std { bool operator<(const P &a, const P &b) { return abs(a.real() - b.real()) < eps ? a.imag() < b.imag() : a.real() < b.real(); } } // namespace std struct L { P a, b; }; // line->l,segment->s struct C { P p; ld r; }; int ccw(P a, P b, P c) { b -= a; c -= a; if (cross(b, c) > eps) return 1; // counter clockwise if (cross(b, c) < -eps) return -1; // clockwise if (dot(b, c) < 0) return 2; // c--a--b on line if (norm(b) < norm(c)) return -2; // a--b--c on line return 0; // a--c--b on line } bool isis_ll(L l, L m) { // is intersect return abs(cross(l.b - l.a, m.b - m.a)) > eps; } bool isis_ls(L l, L s) { ld a = cross(l.b - l.a, s.a - l.a); ld b = cross(l.b - l.a, s.b - l.a); return (a * b < eps); } bool isis_lp(L l, P p) { return abs(cross(l.b - p, l.a - p)) < eps; } bool isis_ss(L s, L t) { return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0; } P is_ll(L s, L t) { // intersect P sv = s.b - s.a, tv = t.b - t.a; return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv); } bool isis_sp(L s, P p) { return abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps; } P proj(L l, P p) { ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + t * (l.a - l.b); } ld dist_lp(L l, P p) { return abs(p - proj(l, p)); } ld dist_ll(L l, L m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); } ld dist_ls(L l, L s) { if (isis_ls(l, s)) return 0; return min(dist_lp(l, s.a), dist_lp(l, s.b)); } ld dist_sp(L s, P p) { P r = proj(s, p); if (isis_sp(s, r)) return abs(r - p); return min(abs(s.a - p), abs(s.b - p)); } ld dist_ss(L s, L t) { if (isis_ss(s, t)) return 0; ld a = min(dist_sp(s, t.a), dist_sp(t, s.a)); ld b = min(dist_sp(s, t.b), dist_sp(t, s.b)); return min(a, b); } VP is_cc(C c1, C c2) { VP res; ld d = abs(c1.p - c2.p); ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d); ld dfr = c1.r * c1.r - rc * rc; if (abs(dfr) < eps) dfr = 0.0; else if (dfr < 0.0) return res; // no intersection ld rs = sqrt(dfr); P diff = (c2.p - c1.p) / d; res.push_back(c1.p + diff * P(rc, rs)); if (dfr != 0.0) res.push_back(c1.p + diff * P(rc, -rs)); return res; } bool isis_vc(vector<C> vc) { VP crs; int n = vc.size(); rep(i, n) rep(j, i) for (P p : is_cc(vc[i], vc[j])) crs.push_back(p); rep(i, n) crs.push_back(vc[i].p); for (P p : crs) { bool valid = true; rep(i, n) if (abs(p - vc[i].p) > vc[i].r + eps) valid = false; if (valid) return true; } return false; } VP is_lc(C c, L l) { VP res; ld d = dist_lp(l, c.p); if (d < c.r + eps) { ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); // safety; P nor = (l.a - l.b) / abs(l.a - l.b); res.push_back(proj(l, c.p) + len * nor); res.push_back(proj(l, c.p) - len * nor); } return res; } VP is_sc(C c, L l) { VP v = is_lc(c, l), res; for (P p : v) if (isis_sp(l, p)) res.push_back(p); return res; } vector<L> tangent_cp(C c, P p) { //????????\???? vector<L> ret; P v = c.p - p; ld d = abs(v); ld l = sqrt(norm(v) - c.r * c.r); if (isnan(l)) { return ret; } P v1 = v * P(l / d, c.r / d); P v2 = v * P(l / d, -c.r / d); ret.push_back(L{p, p + v1}); if (l < eps) return ret; ret.push_back(L{p, p + v2}); return ret; } vector<L> tangent_cc(C c1, C c2) { vector<L> ret; if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) { P center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r); ret = tangent_cp(c1, center); } if (abs(c1.r - c2.r) > eps) { P out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r); vector<L> nret = tangent_cp(c1, out); ret.insert(ret.end(), ALL(nret)); } else { P v = c2.p - c1.p; v /= abs(v); P q1 = c1.p + v * P(0, 1) * c1.r; P q2 = c1.p + v * P(0, -1) * c1.r; ret.push_back(L{q1, q1 + v}); ret.push_back(L{q2, q2 + v}); } return ret; } ld area(const VP &p) { //??¢????? ld res = 0; int n = p.size(); rep(j, n) res += cross(p[j], p[(j + 1) % n]); return res / 2; } bool is_polygon(L l, VP &g) { int n = g.size(); for (int i = 0; i < n; i++) { P a = g[i]; P b = g[(i + 1) % n]; if (isis_ss(l, L{a, b})) return true; } return false; } int is_in_Polygon(const VP &g, P p) { bool in = false; int n = g.size(); for (int i = 0; i < n; i++) { P a = g[i] - p, b = g[(i + 1) % n] - p; if (imag(a) > imag(b)) swap(a, b); if (imag(a) <= 0 && 0 < imag(b)) if (cross(a, b) < 0) in = !in; if (abs(cross(a, b)) < eps && dot(a, b) < eps) return 0; // on } if (in) return 1; // in return -1; // out } VP ConvexHull(VP ps) { int n = ps.size(); int k = 0; sort(ps.begin(), ps.end()); VP ch(2 * n); for (int i = 0; i < n; ch[k++] = ps[i++]) while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; ch.resize(k - 1); return ch; } VP ConvexCut(const VP &ps, L l) { VP Q; for (int i = 0; i < (int)ps.size(); i++) { P A = ps[i], B = ps[(i + 1) % ps.size()]; if (ccw(l.a, l.b, A) != -1) Q.push_back(A); if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0) Q.push_back(is_ll(L{A, B}, l)); } return Q; } double dist2(P a) { return real(a) * real(a) + imag(a) * imag(a); } // Suffix Array ?????????O(|S|log^2|S|), ????´¢O(|T|log|S|), // ?????????????§????O(|S|) class StringSearch { const int n; string S; public: vector<int> sa, rank; StringSearch(const string &S_) : n(S_.size()), S(S_), sa(n + 1), rank(n + 1) { for (int i = 0; i <= n; i++) { sa[i] = i; rank[i] = i < n ? S[i] : -1; } vector<int> tmp(n + 1); for (int k = 1; k <= n; k *= 2) { auto Compare_SA = [=](int i, int j) { if (this->rank[i] != this->rank[j]) return this->rank[i] < this->rank[j]; int ri = i + k <= n ? this->rank[i + k] : -1; int rj = j + k <= n ? this->rank[j + k] : -1; return ri < rj; }; sort(sa.begin(), sa.end(), Compare_SA); tmp[sa[0]] = 0; for (int i = 1; i <= n; i++) { tmp[sa[i]] = tmp[sa[i - 1]] + (Compare_SA(sa[i - 1], sa[i]) ? 1 : 0); } for (int i = 0; i <= n; i++) { this->rank[i] = tmp[i]; } } } bool Contain(const string &T) { int a = 0, b = n; while (b - a > 1) { int c = (a + b) / 2; if (S.compare(sa[c], T.length(), T) < 0) a = c; else b = c; } return S.compare(sa[b], T.length(), T) == 0; } vector<int> LCPArray() { for (int i = 0; i <= n; i++) rank[sa[i]] = i; int h = 0; vector<int> lcp(n + 1); for (int i = 0; i < n; i++) { int j = sa[rank[i] - 1]; if (h > 0) h--; for (; j + h < n && i + h < n; h++) { if (S[j + h] != S[i + h]) break; } lcp[rank[i] - 1] = h; } return lcp; } }; int main() { int n; while (cin >> n, n) { va<int, 3> bl(n); rep(i, n) rep(j, 3) cin >> bl[i][j]; va<double, 6> h; rep(i, n) rep(j, i) rep(k, j) { double x[2][3] = { {(double)bl[i][0] - bl[j][0], (double)bl[i][1] - bl[j][1], -pow(bl[i][2], 2) + pow(bl[j][2], 2) + pow(bl[i][0], 2) + pow(bl[i][1], 2) - pow(bl[j][0], 2) - pow(bl[j][1], 2)}, {(double)bl[i][0] - bl[k][0], (double)bl[i][1] - bl[k][1], -pow(bl[i][2], 2) + pow(bl[k][2], 2) + pow(bl[i][0], 2) + pow(bl[i][1], 2) - pow(bl[k][0], 2) - pow(bl[k][1], 2)}}; double a, b, c; if (x[1][1] == 0) rep(i, 3) swap(x[0][i], x[1][i]); if (x[1][1] == 0) continue; if (x[0][0] - (x[0][1] / x[1][1]) * x[1][0] == 0) continue; a = (x[0][2] - x[0][1] / x[1][1] * x[1][2]) / (x[0][0] - (x[0][1] / x[1][1]) * x[1][0]) / 2; b = (x[1][2] - 2 * a * x[1][0]) / 2 / x[1][1]; c = pow(bl[i][2], 2) - pow(a - bl[i][0], 2) - pow(b - bl[i][1], 2); if (c < 0) continue; c = sqrt(c); h.push_back({a, b, c, (double)k, (double)j, (double)i}); } rep(i, n) rep(j, i) { double l = hypot(bl[i][0] - bl[j][0], bl[i][1] - bl[j][1]); double a = (pow(bl[j][2], 2) - pow(bl[i][2], 2) + pow(l, 2)) / 2 / l; double x = (bl[i][0] * a + bl[j][0] * (l - a)) / l, y = (bl[i][1] * a + bl[j][1] * (l - a)) / l, z = pow(bl[j][2], 2) - pow(a, 2); if (z < 0) continue; z = sqrt(z); h.push_back( {(double)x, (double)y, (double)z, (double)j, (double)i, (double)i}); } rep(i, n) h.push_back({(double)bl[i][0], (double)bl[i][1], (double)bl[i][2], (double)i, (double)i, (double)i}); // sort(ALL(h), [](const array<double, 6> &a, const array<double, 6> &b) // {return a[2] != b[2] ? a[2] > b[2]:a > b; }); double ans = 0; for (auto &&x : h) { int pos = 3; bool f = 1; rep(i, n) { if (i == x[pos]) { if (pos != 5) pos++; continue; } if (pow(x[0] - bl[i][0], 2) + pow(x[1] - bl[i][1], 2) + pow(x[2], 2) > pow(bl[i][2], 2) + 1e-7) { f = 0; break; } } if (f) { cmax(ans, x[2]); } } cout << fixed << setprecision(10) << ans << endl; } }
[["-", 8, 9, 0, 57, 64, 9, 0, 93, 0, 94], ["-", 8, 9, 0, 57, 64, 9, 0, 93, 0, 35]]
1
7,536
#ifndef _WIN32 #include <iostream> #endif #include <algorithm> #include <cmath> #include <complex> #include <functional> #include <iomanip> #include <map> #include <queue> #include <string> #include <tuple> #include <vector> using namespace std; typedef long long LL; #define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++) #define REP(i, n) FOR(i, 0, n) template <typename T> bool chmin(T &l, T r) { bool res = l > r; if (res) { l = r; } return res; } #define IL inline #define X real() #define Y imag() typedef double D; typedef complex<double> P; typedef pair<P, P> LS; typedef pair<P, D> C; const D EPS = 1e-8; IL D dot(P a, P b) { return a.X * b.X + a.Y * b.Y; } IL D cross(P a, P b) { return a.X * b.Y - a.Y * b.X; } struct cww { cww() { cout << fixed; cout << setprecision(10); } } star; IL D signed_distance_P_L(P p, LS l) { P a, b, c = p; tie(a, b) = l; return cross(b - a, c - a) / abs(b - a); } IL D distance_P_LS(P p, LS ls) { P a, b, c = p; tie(a, b) = ls; if (dot(b - a, c - a) < EPS) return abs(c - a); if (dot(a - b, c - b) < EPS) return abs(c - b); return abs(cross(b - a, c - a)) / abs(b - a); } IL bool isintersected_LS_LS(LS p, LS q) { P a, b, c, d; tie(a, b) = p; tie(c, d) = q; if (abs(cross(b - a, d - c)) < EPS) { if (abs(abs(b - c) + abs(a - c) - abs(a - b)) < EPS) return true; if (abs(abs(b - d) + abs(a - d) - abs(a - b)) < EPS) return true; if (abs(abs(a - c) + abs(a - d) - abs(c - d)) < EPS) return true; if (abs(abs(b - c) + abs(b - d) - abs(c - d)) < EPS) return true; return false; } return cross(b - a, c - a) * cross(b - a, d - a) < EPS && cross(d - c, a - c) * cross(d - c, b - c) < EPS; } IL D distance_LS_LS(LS p, LS q) { if (isintersected_LS_LS(p, q)) return 0; P a, b, c, d; tie(a, b) = p; tie(c, d) = q; double e, f, g, h; e = distance_P_LS(a, q); f = distance_P_LS(b, q); g = distance_P_LS(c, p); h = distance_P_LS(d, p); return min({e, f, g, h}); } P rot(P p, double r) { return P(p.X * cos(r) - p.Y * sin(r), p.X * sin(r) + p.Y * cos(r)); } IL bool isintersected_C_C(C p, C q) { P cp, cq; D rp, rq; tie(cp, rp) = p; tie(cq, rq) = q; D d = abs(cp - cq); return abs(rp - rq) < d + EPS && d < rp + rq + EPS; } IL LS cross_C_C(C p, C q) { P cp, cq; D rp, rq; tie(cp, rp) = p; tie(cq, rq) = q; D d = abs(cp - cq); D rc = (d * d + rp * rp - rq * rq) / (2 * d); D rs = sqrt(rp * rp - rc * rc); P diff = (cq - cp) / d; return LS(cp + P(rc, rs) * diff, cp + P(rc, -rs) * diff); } const D PI = acos(-1); typedef pair<double, int> pp; typedef priority_queue<pp, vector<pp>, greater<pp>> QUE; int main() { int N; while (cin >> N, N) { vector<P> xy(N); vector<D> len(N); REP(i, N) { double x, y; cin >> x >> y >> len[i]; xy[i] = P(x, y); } double lb = 0; double ub = 400; REP(loop, 100) { const D mid = (lb + ub) / 2; vector<C> cir(N); bool flag = true; REP(i, N) { double l = len[i] * len[i] - mid * mid; if (l < 0) { flag = false; break; } cir[i] = C(xy[i], sqrt(l)); } if (flag == false) { ub = mid; continue; } vector<P> nobu; REP(i, N) nobu.push_back(xy[i]); REP(i, N) { REP(j, N) { if (i != j) { if (!isintersected_C_C(cir[i], cir[j])) continue; auto hiro = cross_C_C(cir[i], cir[j]); nobu.push_back(hiro.first); nobu.push_back(hiro.second); } } } flag = false; for (auto &p : nobu) { bool btk = true; REP(i, N) { auto d = abs(p - xy[i]); if (d * d + mid * mid > len[i] * len[i]) btk = false; } if (btk) flag = true; } if (flag) lb = mid; else ub = mid; } cout << lb << endl; } return 0; }
#ifndef _WIN32 #include <iostream> #endif #include <algorithm> #include <cmath> #include <complex> #include <functional> #include <iomanip> #include <map> #include <queue> #include <string> #include <tuple> #include <vector> using namespace std; typedef long long LL; #define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++) #define REP(i, n) FOR(i, 0, n) template <typename T> bool chmin(T &l, T r) { bool res = l > r; if (res) { l = r; } return res; } #define IL inline #define X real() #define Y imag() typedef double D; typedef complex<double> P; typedef pair<P, P> LS; typedef pair<P, D> C; const D EPS = 1e-8; IL D dot(P a, P b) { return a.X * b.X + a.Y * b.Y; } IL D cross(P a, P b) { return a.X * b.Y - a.Y * b.X; } struct cww { cww() { cout << fixed; cout << setprecision(10); } } star; IL D signed_distance_P_L(P p, LS l) { P a, b, c = p; tie(a, b) = l; return cross(b - a, c - a) / abs(b - a); } IL D distance_P_LS(P p, LS ls) { P a, b, c = p; tie(a, b) = ls; if (dot(b - a, c - a) < EPS) return abs(c - a); if (dot(a - b, c - b) < EPS) return abs(c - b); return abs(cross(b - a, c - a)) / abs(b - a); } IL bool isintersected_LS_LS(LS p, LS q) { P a, b, c, d; tie(a, b) = p; tie(c, d) = q; if (abs(cross(b - a, d - c)) < EPS) { if (abs(abs(b - c) + abs(a - c) - abs(a - b)) < EPS) return true; if (abs(abs(b - d) + abs(a - d) - abs(a - b)) < EPS) return true; if (abs(abs(a - c) + abs(a - d) - abs(c - d)) < EPS) return true; if (abs(abs(b - c) + abs(b - d) - abs(c - d)) < EPS) return true; return false; } return cross(b - a, c - a) * cross(b - a, d - a) < EPS && cross(d - c, a - c) * cross(d - c, b - c) < EPS; } IL D distance_LS_LS(LS p, LS q) { if (isintersected_LS_LS(p, q)) return 0; P a, b, c, d; tie(a, b) = p; tie(c, d) = q; double e, f, g, h; e = distance_P_LS(a, q); f = distance_P_LS(b, q); g = distance_P_LS(c, p); h = distance_P_LS(d, p); return min({e, f, g, h}); } P rot(P p, double r) { return P(p.X * cos(r) - p.Y * sin(r), p.X * sin(r) + p.Y * cos(r)); } IL bool isintersected_C_C(C p, C q) { P cp, cq; D rp, rq; tie(cp, rp) = p; tie(cq, rq) = q; D d = abs(cp - cq); return abs(rp - rq) < d + EPS && d < rp + rq + EPS; } IL LS cross_C_C(C p, C q) { P cp, cq; D rp, rq; tie(cp, rp) = p; tie(cq, rq) = q; D d = abs(cp - cq); D rc = (d * d + rp * rp - rq * rq) / (2 * d); D rs = sqrt(rp * rp - rc * rc); P diff = (cq - cp) / d; return LS(cp + P(rc, rs) * diff, cp + P(rc, -rs) * diff); } const D PI = acos(-1); typedef pair<double, int> pp; typedef priority_queue<pp, vector<pp>, greater<pp>> QUE; int main() { int N; while (cin >> N, N) { vector<P> xy(N); vector<D> len(N); REP(i, N) { double x, y; cin >> x >> y >> len[i]; xy[i] = P(x, y); } double lb = 0; double ub = 400; REP(loop, 100) { const D mid = (lb + ub) / 2; vector<C> cir(N); bool flag = true; REP(i, N) { double l = len[i] * len[i] - mid * mid; if (l < 0) { flag = false; break; } cir[i] = C(xy[i], sqrt(l)); } if (flag == false) { ub = mid; continue; } vector<P> nobu; REP(i, N) nobu.push_back(xy[i]); REP(i, N) { REP(j, N) { if (i != j) { if (!isintersected_C_C(cir[i], cir[j])) continue; auto hiro = cross_C_C(cir[i], cir[j]); nobu.push_back(hiro.first); nobu.push_back(hiro.second); } } } flag = false; for (auto &p : nobu) { bool btk = true; REP(i, N) { auto d = abs(p - xy[i]); if (d * d + mid * mid > EPS + len[i] * len[i]) btk = false; } if (btk) flag = true; } if (flag) lb = mid; else ub = mid; } cout << lb << endl; } return 0; }
[["+", 0, 57, 15, 339, 51, 16, 12, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 72]]
1
1,368
#include <bits/stdc++.h> #define N 800 using namespace std; typedef pair<int, int> P; int n, p[N], d, a, b, c, maxc; bool used[N]; vector<P> e[N]; void delnode(int x, int sum) { used[x] = true; for (int i = 0; i < e[x].size(); i++) { int nx = e[x][i].first; if (used[nx]) continue; if (e[nx].size() == 1) { maxc = max(maxc, sum); return; } delnode(nx, sum + e[x][i].second); } } int main() { while (1) { cin >> n; if (!n) break; for (int i = 1; i < n; i++) cin >> p[i]; a = b = c = 0; for (int i = 1; i < n; i++) { cin >> d; a += d; e[i].push_back(P(p[i] - 1, d)); e[p[i] - 1].push_back(P(i, d)); } for (int i = 0; i < n; i++) if (e[i].size() == 1) b += e[i][0].second; for (int i = 0; i < n; i++) if (e[i].size() != 1) { memset(used, 0, sizeof(used)); maxc = 0; delnode(i, 0); c = max(c, maxc); } cout << 3 * a - 2 * b - c << endl; for (int i = 0; i < n; i++) e[i].clear(); } return 0; }
#include <bits/stdc++.h> #define N 800 using namespace std; typedef pair<int, int> P; int n, p[N], d, a, b, c, maxc; bool used[N]; vector<P> e[N]; void delnode(int x, int sum) { used[x] = true; for (int i = 0; i < e[x].size(); i++) { int nx = e[x][i].first; if (used[nx]) continue; if (e[nx].size() == 1) { maxc = max(maxc, sum); continue; } delnode(nx, sum + e[x][i].second); } } int main() { while (1) { cin >> n; if (!n) break; for (int i = 1; i < n; i++) cin >> p[i]; a = b = c = 0; for (int i = 1; i < n; i++) { cin >> d; a += d; e[i].push_back(P(p[i] - 1, d)); e[p[i] - 1].push_back(P(i, d)); } for (int i = 0; i < n; i++) if (e[i].size() == 1) b += e[i][0].second; for (int i = 0; i < n; i++) if (e[i].size() != 1) { memset(used, 0, sizeof(used)); maxc = 0; delnode(i, 0); c = max(c, maxc); } cout << 3 * a - 2 * b - c << endl; for (int i = 0; i < n; i++) e[i].clear(); } return 0; }
[["-", 8, 9, 0, 57, 64, 9, 0, 37, 0, 38], ["+", 8, 9, 0, 57, 64, 9, 0, 116, 0, 117]]
1
413
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, ll>; vector<P> G[800]; int used[800]; P dfs(int n) { used[n] = 1; int id = n; ll ma = 0; for (auto to : G[n]) { if (G[to.first].size() == 1 || used[to.first]) continue; auto p = dfs(to.first); if (p.second + to.second > ma) { ma = p.second + to.second; id = to.first; } } used[n] = 0; return P(id, ma); } int main() { int n; while (cin >> n, n) { vector<ll> a(n - 1), b(n - 1), d(n - 1); for (int i = 1, p; i < n; i++) { cin >> p; p--; a[i - 1] = i; b[i - 1] = p; } for (int i = 0; i < n - 1; i++) { cin >> d[i]; G[a[i]].push_back(P(b[i], d[i])); G[b[i]].push_back(P(a[i], d[i])); } ll res = 0; for (int i = 0; i < n - 1; i++) { res += d[i]; if ((int)G[a[i]].size() > 1 && (int)G[b[i]].size() > 1) { res += d[i] * 2; } } int s = 0; while (G[s].size() == 1) s++; auto p = dfs(s); p = dfs(p.first); cout << res - p.second << endl; for (int i = 0; i < n; i++) G[i].clear(); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, ll>; vector<P> G[800]; int used[800]; P dfs(int n) { used[n] = 1; int id = n; ll ma = 0; for (auto to : G[n]) { if (G[to.first].size() == 1 || used[to.first]) continue; auto p = dfs(to.first); if (p.second + to.second > ma) { ma = p.second + to.second; id = p.first; } } used[n] = 0; return P(id, ma); } int main() { int n; while (cin >> n, n) { vector<ll> a(n - 1), b(n - 1), d(n - 1); for (int i = 1, p; i < n; i++) { cin >> p; p--; a[i - 1] = i; b[i - 1] = p; } for (int i = 0; i < n - 1; i++) { cin >> d[i]; G[a[i]].push_back(P(b[i], d[i])); G[b[i]].push_back(P(a[i], d[i])); } ll res = 0; for (int i = 0; i < n - 1; i++) { res += d[i]; if ((int)G[a[i]].size() > 1 && (int)G[b[i]].size() > 1) { res += d[i] * 2; } } int s = 0; while (G[s].size() == 1) s++; auto p = dfs(s); p = dfs(p.first); cout << res - p.second << endl; for (int i = 0; i < n; i++) G[i].clear(); } return 0; }
[["-", 64, 9, 0, 1, 0, 11, 12, 118, 28, 22], ["+", 64, 9, 0, 1, 0, 11, 12, 118, 28, 22]]
1
464
require 'set' require 'matrix' include Math class Circle def initialize(*args) @id, @x, @y, @r = args end def cross?(other) (@x - other.x) ** 2 + (@y - other.y) ** 2 < (@r + other.r) ** 2 end def include?(x, y) (@x - x) ** 2 + (@y - y) **2 < @r ** 2 end def inspect @id end attr_reader :id, :x, :y, :r end EPS = 1e-10 class Circuit def initialize(circles) @circles = circles end def include?(x, y) x += EPS return false if @circles.any?{|c| c.include?(x, y)} cross_count = 0 @circles.each_cons(2) do |c1, c2| next if c1.x == c2.x c1, c2 = c2, c1 if c2.x < c1.x next if x < c1.x || c2.x < x cy = c1.y + (c2.y - c1.y) * (x - c1.x) / (c2.x - c1.x).to_f cross_count += 1 if cy > y end return cross_count.odd? end end loop do n, m = gets.split.map(&:to_i) break if n == 0 graph = {} circles = (1..n).map do |i| c = Circle.new(i, *gets.split.map(&:to_i)) graph[c] = [] c end edges = Set.new circles.combination(2) do |c1, c2| if c1.cross?(c2) graph[c1] << c2 graph[c2] << c1 edges << Set[c1, c2] end end #delete branches loop do c = graph.keys.min_by{|c| graph[c].size} break if !c if graph[c].size <= 1 graph.delete(c) graph.each do |u, neighbour| neighbour.delete(c) end edges.delete_if {|pair| pair.include?(c)} else break end end #find faces circuits = [] scanned = {} loop do found = false if e0 = edges.find {|e| !scanned[[e, :left]]} found = true scanned[[e0, :left]] = 0 u0, v0 = e0.to_a.sort_by(&:id) path = [u0, v0] u = u0 v = v0 until v == u0 t = atan2(v.y - u.y, v.x - u.x) rot = Matrix[ [cos(-t), -sin(-t)], [sin(-t), cos(-t)] ] w = (graph[v] - [u]).max_by do |w| vec = rot * Vector[w.x - v.x, w.y - v.y] x, y = vec.to_a atan2(y, x) end e = Set[v, w] if v.id < w.id scanned[[e, :left]] = 0 else scanned[[e, :right]] = 0 end path << w u, v = v, w end circuits << Circuit.new(path) end if e0 = edges.find {|e| !scanned[[e, :right]]} found = true scanned[[e0, :right]] = 0 u0, v0 = e0.to_a.sort_by(&:id).reverse path = [u0, v0] u = u0 v = v0 until v == u0 t = atan2(v.y - u.y, v.x - u.x) rot = Matrix[ [cos(-t), -sin(-t)], [sin(-t), cos(-t)] ] w = (graph[v] - [u]).min_by do |w| vec = rot * Vector[w.x - v.x, w.y - v.y] x, y = vec.to_a atan2(y, x) end e = Set[v, w] if v.id < w.id scanned[[e, :left]] = 0 else scanned[[e, :right]] = 0 end path << w u, v = v, w end circuits << Circuit.new(path) end break if ! found end ans = [] m.times do x1, y1, x2, y2 = gets.split.map(&:to_i) if circles.any? {|c| c.include?(x1, y1) ^ c.include?(x2, y2)} ans << 'NO' elsif circuits.any? {|c| c.include?(x1, y1) ^ c.include?(x2, y2)} ans << 'NO' else ans << 'YES' end end puts ans * ' ' end
require 'set' require 'matrix' include Math class Circle def initialize(*args) @id, @x, @y, @r = args end def cross?(other) (@x - other.x) ** 2 + (@y - other.y) ** 2 < (@r + other.r) ** 2 end def include?(x, y) (@x - x) ** 2 + (@y - y) **2 < @r ** 2 end def inspect @id end attr_reader :id, :x, :y, :r end EPS = 1e-10 class Circuit def initialize(circles) @circles = circles end def include?(x, y) x += EPS return false if @circles.any?{|c| c.include?(x, y)} cross_count = 0 @circles.each_cons(2) do |c1, c2| next if c1.x == c2.x c1, c2 = c2, c1 if c2.x < c1.x next if x < c1.x || c2.x < x cy = c1.y + (c2.y - c1.y) * (x - c1.x) / (c2.x - c1.x).to_f cross_count += 1 if cy > y end return cross_count.odd? end end loop do n, m = gets.split.map(&:to_i) break if n == 0 graph = {} circles = (1..n).map do |i| c = Circle.new(i, *gets.split.map(&:to_i)) graph[c] = [] c end edges = Set.new circles.combination(2) do |c1, c2| if c1.cross?(c2) graph[c1] << c2 graph[c2] << c1 edges << Set[c1, c2] end end #delete branches loop do c = graph.keys.min_by{|c| graph[c].size} break if !c if graph[c].size <= 1 graph.delete(c) graph.each do |u, neighbour| neighbour.delete(c) end edges.delete_if {|pair| pair.include?(c)} else break end end #find faces circuits = [] scanned = {} loop do found = false if e0 = edges.find {|e| !scanned[[e, :left]]} found = true scanned[[e0, :left]] = 0 u0, v0 = e0.to_a.sort_by(&:id) path = [u0, v0] u = u0 v = v0 until v == u0 t = atan2(v.y - u.y, v.x - u.x) rot = Matrix[ [cos(-t), -sin(-t)], [sin(-t), cos(-t)] ] w = (graph[v] - [u]).max_by do |w| vec = rot * Vector[w.x - v.x, w.y - v.y] x, y = vec.to_a atan2(y, x) end e = Set[v, w] if v.id < w.id scanned[[e, :left]] = 0 else scanned[[e, :right]] = 0 end path << w u, v = v, w end circuits << Circuit.new(path) end if e0 = edges.find {|e| !scanned[[e, :right]]} found = true scanned[[e0, :right]] = 0 u0, v0 = e0.to_a.sort_by(&:id).reverse path = [u0, v0] u = u0 v = v0 until v == u0 t = atan2(v.y - u.y, v.x - u.x) rot = Matrix[ [cos(-t), -sin(-t)], [sin(-t), cos(-t)] ] w = (graph[v] - [u]).min_by do |w| vec = rot * Vector[w.x - v.x, w.y - v.y] x, y = vec.to_a atan2(y, x) end e = Set[v, w] if v.id < w.id scanned[[e, :right]] = 0 else scanned[[e, :left]] = 0 end path << w u, v = v, w end circuits << Circuit.new(path) end break if ! found end ans = [] m.times do x1, y1, x2, y2 = gets.split.map(&:to_i) if circles.any? {|c| c.include?(x1, y1) ^ c.include?(x2, y2)} ans << 'NO' elsif circuits.any? {|c| c.include?(x1, y1) ^ c.include?(x2, y2)} ans << 'NO' else ans << 'YES' end end puts ans * ' ' end
[["-", 64, 749, 0, 662, 31, 742, 0, 516, 0, 753], ["+", 64, 749, 0, 662, 31, 742, 0, 516, 0, 753], ["-", 75, 95, 0, 662, 31, 742, 0, 516, 0, 753], ["+", 75, 95, 0, 662, 31, 742, 0, 516, 0, 753]]
4
1,011
// 50000 for aizu0056 // 1000000 for aizu0185 #define M 32769 p[M + 1]; int main(n, i, j) { p[1] = 1; for (; i <= M; i++) if (!p[i]) for (j = i * 2; j <= M; j += i) p[j] = 1; for (; scanf("%d", &n), n;) { int i = 2, s = 0; for (; i <= n / 2; i++) { if (p[i]) continue; if (!p[n - i]) { s++; } } printf("%d\n", s); } return 0; }
// 50000 for aizu0056 // 1000000 for aizu0185 #define M 32769 p[M + 1]; int main(i, j, n) { p[1] = 1; for (; i <= M; i++) if (!p[i]) for (j = i * 2; j <= M; j += i) p[j] = 1; for (; scanf("%d", &n), n;) { int i = 2, s = 0; for (; i <= n / 2; i++) { if (p[i]) continue; if (!p[n - i]) { s++; } } printf("%d\n", s); } return 0; }
[["-", 0, 30, 0, 14, 49, 53, 54, 55, 0, 22], ["-", 0, 30, 0, 14, 49, 53, 54, 55, 0, 21], ["+", 0, 30, 0, 14, 49, 53, 54, 55, 0, 21], ["+", 0, 30, 0, 14, 49, 53, 54, 55, 0, 22]]
0
149
// 50000 for aizu0056 // 1000000 for aizu0185 #define M 32769 p[M + 1]; main(n, i, j) { p[1] = 1; for (; i <= M; i++) if (!p[i]) for (j = i * 2; j <= M; j += i) p[j] = 1; for (; scanf("%d", &n), n;) { for (i = 2, j = 0; i <= n / 2; i++) { if (!p[i]) continue; if (!p[n - i]) { j++; } } printf("%d\n", j); } return 0; }
// 50000 for aizu0056 // 1000000 for aizu0185 #define M 32769 p[M + 1]; int main(i, j, n) { p[1] = 1; for (; i <= M; i++) if (!p[i]) for (j = i * 2; j <= M; j += i) p[j] = 1; for (; scanf("%d", &n), n;) { for (i = 2, j = 0; i <= n / 2; i++) { if (p[i]) continue; if (!p[n - i]) { j++; } } printf("%d\n", j); } return 0; }
[["+", 36, 36, 36, 36, 0, 30, 0, 14, 39, 40], ["-", 0, 30, 0, 1, 0, 2, 3, 4, 0, 22], ["-", 0, 30, 0, 1, 0, 2, 3, 4, 0, 21], ["+", 0, 30, 0, 14, 49, 53, 54, 55, 0, 21], ["+", 0, 30, 0, 14, 49, 53, 54, 55, 0, 22], ["-", 0, 1, 0, 2, 3, 4, 0, 25, 0, 35], ["-", 8, 9, 0, 57, 15, 23, 0, 91, 17, 111]]
0
148
// 50000 for aizu0056 // 1000000 for aizu0185 #define M 32769 p[M + 1]; main(n, i, j) { p[1] = 1; for (; i <= M; i++) if (!p[i]) for (j = i * 2; j <= M; j += i) p[j] = 1; for (; scanf("%d", &n), n; printf("%d\n", j)) for (i = 2, j = 0; i <= n / 2; i++) if (!p[i] && !p[n - i]) j++; return 0; }
// 50000 for aizu0056 // 1000000 for aizu0185 #define M 32768 p[M + 1]; main(i, j, n) { p[1] = 1; for (; i <= M; i++) if (!p[i]) for (j = i * 2; j <= M; j += i) p[j] = 1; for (; scanf("%d", &n), n; printf("%d\n", j)) for (i = 2, j = 0; i <= n / 2; i++) if (!p[i] && !p[n - i]) j++; return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["-", 0, 30, 0, 1, 0, 2, 3, 4, 0, 22], ["-", 0, 30, 0, 1, 0, 2, 3, 4, 0, 21], ["+", 0, 30, 0, 1, 0, 2, 3, 4, 0, 21], ["+", 0, 30, 0, 1, 0, 2, 3, 4, 0, 22]]
0
137
#include <iostream> #include <string.h> using namespace std; int main() { int pre[33000]; memset(pre, 0, sizeof(pre)); for (int i = 2; i < 33000; i++) { for (int j = 2; j * i < 33000; j++) { pre[i * j] = 1; } } int n; while (cin >> n) { int c = 0; for (int i = 2; i <= n / 2; i++) { if (pre[i] == 0 && pre[n - i] == 0) { c++; } } cout << c << endl; } }
#include <iostream> #include <string.h> using namespace std; int main() { int pre[33000]; memset(pre, 0, sizeof(pre)); for (int i = 2; i < 33000; i++) { for (int j = 2; j * i < 33000; j++) { pre[i * j] = 1; } } int n; while (cin >> n, n) { int c = 0; for (int i = 2; i <= n / 2; i++) { if (pre[i] == 0 && pre[n - i] == 0) { c++; } } cout << c << endl; } }
[["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 12, 22]]
1
138
#include <set> #include <stdio.h> #include <vector> #define MYSCAN scanf int primejudg(int num); std::vector<int> prime_v; std::set<int> prime_s; int prime_size; int main(void) { int max = 0; int i, n, count; prime_s.insert(2); prime_s.insert(3); prime_v.push_back(2); prime_v.push_back(3); prime_size = 2; while (true) { MYSCAN("%d", &n); if (n == 0) { break; } count = 0; // DPテ」ツ?ョティツカツウテ」ツつ甘」ツ?ェテ」ツ??」ツ?ィテ」ツ?禿」ツつ催」ツつ津ィツ」ツ愿・ツョツ? for (i = prime_v[prime_size - 1] + 2; i <= n; i += 2) { if (primejudg(i) == 0) { prime_s.insert(i); prime_v.push_back(i); prime_size++; } } // setテ」ツ?ョテ、ツクツュテ」ツ?ォテァツ崢クテヲツ鳴ケテ」ツ?古」ツ?づ」ツ?」テ」ツ?淌」ツつ嘉ヲツ閉ーテ」ツつ津ヲツ閉ーテ」ツ?暗」ツつ? for (i = 0; i <= prime_size / 2 && prime_v[i] <= n / 2; i++) { // printf("%d, %d\n", i, prime_size); if (prime_s.count(n - prime_v[i]) != 0) { count++; // printf("----- %d, %d -----\n", prime_v[i], n - prime_v[i]); } } printf("%d\n", count); } return 0; } // テァツエツ?ヲツ閉ーテ」ツ?凝」ツ?ゥテ」ツ??」ツ?凝・ツ按、テ・ツョツ? int primejudg(int num) { // テ、ツサツ甘」ツ?セテ」ツ?ァテ」ツ?ョテ・ツ?ィテァツエツ?ヲツ閉ーテ」ツ?ァテ・ツ可イテ」ツつ? for (int i = 0; i < prime_size; i++) { if (num % prime_v[i] == 0) { return -1; } } return 0; }
#include <set> #include <stdio.h> #include <vector> #define MYSCAN scanf int primejudg(int num); std::vector<int> prime_v; std::set<int> prime_s; int prime_size; int main(void) { int max = 0; int i, n, count; prime_s.insert(2); prime_s.insert(3); prime_v.push_back(2); prime_v.push_back(3); prime_size = 2; while (true) { MYSCAN("%d", &n); if (n == 0) { break; } count = 0; // DPテ」ツ?ョティツカツウテ」ツつ甘」ツ?ェテ」ツ??」ツ?ィテ」ツ?禿」ツつ催」ツつ津ィツ」ツ愿・ツョツ? for (i = prime_v[prime_size - 1] + 2; i <= n; i += 2) { if (primejudg(i) == 0) { prime_s.insert(i); prime_v.push_back(i); prime_size++; } } // setテ」ツ?ョテ、ツクツュテ」ツ?ォテァツ崢クテヲツ鳴ケテ」ツ?古」ツ?づ」ツ?」テ」ツ?淌」ツつ嘉ヲツ閉ーテ」ツつ津ヲツ閉ーテ」ツ?暗」ツつ? for (i = 0; i < prime_size && prime_v[i] <= n / 2; i++) { // printf("%d, %d\n", prime_v[i], n - prime_v[i]); if (prime_s.count(n - prime_v[i]) != 0) { count++; // printf("----- %d, %d -----\n", prime_v[i], n - prime_v[i]); } } printf("%d\n", count); } return 0; } // テァツエツ?ヲツ閉ーテ」ツ?凝」ツ?ゥテ」ツ??」ツ?凝・ツ按、テ・ツョツ? int primejudg(int num) { // テ、ツサツ甘」ツ?セテ」ツ?ァテ」ツ?ョテ・ツ?ィテァツエツ?ヲツ閉ーテ」ツ?ァテ・ツ可イテ」ツつ? for (int i = 0; i < prime_size; i++) { if (num % prime_v[i] == 0) { return -1; } } return 0; }
[["-", 8, 9, 0, 7, 15, 16, 31, 16, 17, 19], ["+", 8, 9, 0, 7, 15, 16, 31, 16, 17, 18], ["-", 0, 7, 15, 16, 31, 16, 12, 16, 17, 85], ["-", 0, 7, 15, 16, 31, 16, 12, 16, 12, 13]]
1
276
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++) #define RREP(i, n) for (int(i) = (int)(n)-1; (i) >= 0; (i)--) #define REMOVE(Itr, n) \ (Itr).erase(remove((Itr).begin(), (Itr).end(), n), (Itr).end()) #define PB_VEC(Itr1, Itr2) \ (Itr1).insert((Itr1).end(), (Itr2).begin(), (Itr2).end()) #define UNIQUE(Itr) \ sort((Itr).begin(), (Itr).end()); \ (Itr).erase(unique((Itr).begin(), (Itr).end()), (Itr).end()) typedef long long ll; bool isPrime(int n) { if (n == 1) return false; if (n == 2) return true; for (int i = 2; i < sqrt(n) + 1; i++) { if ((n % i) == 0) return false; } return true; } int main() { while (true) { int n; cin >> n; if (n == 0) { break; } int ans = 0; for (int i = 2; i < n / 2; i++) { if (isPrime(i) && isPrime(n - i)) ans++; } cout << ans << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++) #define RREP(i, n) for (int(i) = (int)(n)-1; (i) >= 0; (i)--) #define REMOVE(Itr, n) \ (Itr).erase(remove((Itr).begin(), (Itr).end(), n), (Itr).end()) #define PB_VEC(Itr1, Itr2) \ (Itr1).insert((Itr1).end(), (Itr2).begin(), (Itr2).end()) #define UNIQUE(Itr) \ sort((Itr).begin(), (Itr).end()); \ (Itr).erase(unique((Itr).begin(), (Itr).end()), (Itr).end()) typedef long long ll; bool isPrime(int n) { if (n == 1) return false; if (n == 2) return true; for (int i = 2; i < sqrt(n) + 1; i++) { if ((n % i) == 0) return false; } return true; } int main() { while (true) { int n; cin >> n; if (n == 0) { break; } int ans = 0; for (int i = 2; i <= n / 2; i++) { if (isPrime(i) && isPrime(n - i)) ans++; } cout << ans << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19]]
1
215
#include <algorithm> #include <iostream> #include <map> using namespace std; bool b[33000]; int main() { fill(b, b + 33000, true); b[0] = false; b[1] = false; for (int i = 2; i < 33000; i++) { if (b[i]) { int j = 2; while (i * j < 33000) { b[i * j] = false; j++; } } } int n; while (cin >> n && n != 0) { int sum = 0; for (int i = 0; i < n / 2; i++) { if (b[i] && b[n - i]) { sum++; } } cout << sum << endl; } }
#include <algorithm> #include <iostream> #include <map> using namespace std; bool b[33000]; int main() { fill(b, b + 33000, true); b[0] = false; b[1] = false; for (int i = 2; i < 33000; i++) { if (b[i]) { int j = 2; while (i * j < 33000) { b[i * j] = false; j++; } } } int n; while (cin >> n && n != 0) { int sum = 0; for (int i = 0; i <= n / 2; i++) { if (b[i] && b[n - i]) { sum++; } } cout << sum << endl; } }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19]]
1
162
#include <algorithm> #include <iostream> using namespace std; bool p[1000100]; void erato() { fill(p, p + 1000100, true); p[0] = p[1] = false; for (int i = 0; i < 1000100; i++) { if (!p[i]) continue; for (int j = i + i; j < 1000100; j += i) { p[j] = false; } } } int main() { erato(); int n; while (cin >> n) { int count = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { // if(i == j) continue; if (p[i] && p[j] && i + j == n) count++; } } cout << count << endl; } }
#include <algorithm> #include <iostream> using namespace std; bool p[1000100]; void erato() { fill(p, p + 1000100, true); p[0] = p[1] = false; for (int i = 0; i < 1000100; i++) { if (!p[i]) continue; for (int j = i + i; j < 1000100; j += i) { p[j] = false; } } } int main() { erato(); int n; while (cin >> n, n != 0) { int count = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { // if(i == j) continue; if (p[i] && p[j] && i + j == n) count++; } } cout << count << endl; } }
[["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 0, 52, 15, 339, 51, 34, 12, 16, 31, 22], ["+", 0, 52, 15, 339, 51, 34, 12, 16, 17, 79], ["+", 0, 52, 15, 339, 51, 34, 12, 16, 12, 13]]
1
181
#include <algorithm> #include <cmath> #include <cstdio> using namespace std; const double EPS = 1e-10; int r, n, xl[20], xr[20], h[20]; int right[20], left[20]; // right:(i, i+1) left:(-i-1, -i) bool ok(double height) { for (int i = 0; i < r; i++) { double temp = sqrt(r * r - i * i) + height; if (temp - (double)right[i] > EPS) return false; if (temp - (double)left[i] > EPS) return false; } return true; } double search() { double lb = (double)-r, ub = (double)r; while (ub > lb + EPS) { double md = (lb + ub) / 2; if (ok(md)) lb = md; else ub = md; } return lb; } void inputright(int l, int r, int height) { for (int i = l; i < r; i++) right[i] = max(right[i], height); } void inputleft(int l, int r, int height) { for (int i = r; i > l; i--) left[-i] = max(left[-i], height); } int main() { while (2) { scanf("%d%d", &r, &n); if (r == 0) break; for (int i = 0; i < 21; i++) right[i] = left[i] = 0; for (int i = 0; i < n; i++) { scanf("%d%d%d", &xl[i], &xr[i], &h[i]); if (xl[i] >= 0) inputright(xl[i], xr[i], h[i]); else if (xr[i] <= 0) inputleft(xl[i], xr[i], h[i]); else inputright(0, xr[i], h[i]), inputleft(xl[i], 0, h[i]); } printf("%f\n", (double)r + search()); } }
#include <algorithm> #include <cmath> #include <cstdio> using namespace std; const double EPS = 1e-10; int r, n, xl[20], xr[20], h[20]; int right[20], left[20]; // right:(i, i+1) left:(-i-1, -i) bool ok(double height) { for (int i = 0; i < r; i++) { double temp = sqrt(r * r - i * i) + height; if (temp - (double)right[i] > EPS) return false; if (temp - (double)left[i] > EPS) return false; } return true; } double search() { double lb = (double)-r, ub = 20.0; while (ub > lb + EPS) { double md = (lb + ub) / 2; if (ok(md)) lb = md; else ub = md; } return lb; } void inputright(int l, int r, int height) { for (int i = l; i < r; i++) right[i] = max(right[i], height); } void inputleft(int l, int r, int height) { for (int i = r; i > l; i--) left[-i] = max(left[-i], height); } int main() { while (2) { scanf("%d%d", &r, &n); if (r == 0) break; for (int i = 0; i < 21; i++) right[i] = left[i] = 0; for (int i = 0; i < n; i++) { scanf("%d%d%d", &xl[i], &xr[i], &h[i]); if (xl[i] >= 0) inputright(xl[i], xr[i], h[i]); else if (xr[i] <= 0) inputleft(xl[i], xr[i], h[i]); else inputright(0, xr[i], h[i]), inputleft(xl[i], 0, h[i]); } printf("%f\n", (double)r + search()); } }
[["-", 8, 9, 0, 43, 49, 50, 51, 74, 0, 24], ["-", 0, 43, 49, 50, 51, 74, 39, 77, 39, 40], ["-", 8, 9, 0, 43, 49, 50, 51, 74, 0, 25], ["-", 8, 9, 0, 43, 49, 50, 51, 74, 51, 22], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13]]
1
478
#include <algorithm> #include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; typedef long long ll; const int inf = 1e9 + 7; using pii = pair<int, int>; #define rep(i, n) for (int i = 0; i < n; ++i) #define rrep(i, st, n) for (int i = st; i < n; ++i) #define chmax(a, b) a = max(a, b) #define chmin(a, b) a = min(a, b) int dx[] = {0, 0, -1, 1}; int dy[] = {1, -1, 0, 0}; const int mod = 1e9 + 7; ll power(ll a, ll b) { return b ? power(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod : 1; } int main() { cin.tie(0); ios::sync_with_stdio(false); char c[20]; while (scanf("%s", c)) { if (c[0] == '#') break; string s = string(c); vector<string> v; rep(i, (1 << s.size())) { bool invalid = false; string t = s; rep(j, s.size()) { if (i & (1 << j)) { if (t[j] == 'z') { invalid = true; break; } t[j]++; } } if (invalid) continue; string u = t; int table[26] = {0}; rep(i, u.size()) { if (u[i] == 'a') continue; if (table[u[i] - 'a']) continue; table[u[i] - 'a'] = 1; u[i]--; } if (u != s) continue; v.push_back(t); } printf("%d\n", (int)v.size()); sort(v.begin(), v.end()); if (v.size() <= 10) { rep(i, v.size()) printf("%s\n", v[i].c_str()); } else { rep(i, 5) printf("%s\n", v[i].c_str()); rep(i, 5) printf("%s\n", v[v.size() - 1 - i].c_str()); } } }
#include <algorithm> #include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; typedef long long ll; const int inf = 1e9 + 7; using pii = pair<int, int>; #define rep(i, n) for (int i = 0; i < n; ++i) #define rrep(i, st, n) for (int i = st; i < n; ++i) #define chmax(a, b) a = max(a, b) #define chmin(a, b) a = min(a, b) int dx[] = {0, 0, -1, 1}; int dy[] = {1, -1, 0, 0}; const int mod = 1e9 + 7; ll power(ll a, ll b) { return b ? power(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod : 1; } int main() { cin.tie(0); ios::sync_with_stdio(false); char c[20]; while (scanf("%s", c)) { if (c[0] == '#') break; string s = string(c); vector<string> v; rep(i, (1 << s.size())) { bool invalid = false; string t = s; rep(j, s.size()) { if (i & (1 << j)) { if (t[j] == 'z') { invalid = true; break; } t[j]++; } } if (invalid) continue; string u = t; int table[26] = {0}; rep(i, u.size()) { if (u[i] == 'a') continue; if (table[u[i] - 'a']) continue; table[u[i] - 'a'] = 1; u[i]--; } if (u != s) continue; v.push_back(t); } printf("%d\n", (int)v.size()); sort(v.begin(), v.end()); if (v.size() <= 10) { rep(i, v.size()) printf("%s\n", v[i].c_str()); } else { rep(i, 5) printf("%s\n", v[i].c_str()); rep(i, 5) printf("%s\n", v[v.size() - 5 + i].c_str()); } } }
[["-", 28, 69, 341, 342, 0, 16, 31, 16, 12, 13], ["-", 63, 118, 28, 69, 341, 342, 0, 16, 17, 33], ["+", 28, 69, 341, 342, 0, 16, 31, 16, 12, 13], ["+", 63, 118, 28, 69, 341, 342, 0, 16, 17, 72]]
1
520
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; vector<string> res; void Rec(string s, char c) { if (c == 'a') { res.push_back(s); return; } const int n = s.size(); for (int i = 0; i < n; ++i) { if (s[i] == c) return; if (s[i] == c - 1) { string tmp(s); tmp[i] = c; Rec(tmp, c - 1); } } Rec(s, c - 1); } int main() { string s; while (cin >> s) { if (s == "#") break; res.clear(); Rec(s, 'z'); sort(res.begin(), res.end()); res.erase(unique(res.begin(), res.end()), res.end()); const int n = res.size(); cout << n << '\n'; if (n <= 10) { for (int i = 0; i < n; ++i) cout << res[i] << '\n'; } else { for (int i = 0; i < 10; ++i) cout << res[i] << '\n'; for (int i = n - 10; i < n; ++i) cout << res[i] << '\n'; } } return 0; }
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; vector<string> res; void Rec(string s, char c) { if (c == 'a') { res.push_back(s); return; } const int n = s.size(); for (int i = 0; i < n; ++i) { if (s[i] == c) return; if (s[i] == c - 1) { string tmp(s); tmp[i] = c; Rec(tmp, c - 1); } } Rec(s, c - 1); } int main() { string s; while (cin >> s) { if (s == "#") break; res.clear(); Rec(s, 'z'); sort(res.begin(), res.end()); res.erase(unique(res.begin(), res.end()), res.end()); const int n = res.size(); cout << n << '\n'; if (n <= 10) { for (int i = 0; i < n; ++i) cout << res[i] << '\n'; } else { for (int i = 0; i < 5; ++i) cout << res[i] << '\n'; for (int i = n - 5; i < n; ++i) cout << res[i] << '\n'; } } return 0; }
[["-", 75, 76, 0, 9, 0, 7, 15, 16, 12, 13], ["+", 75, 76, 0, 9, 0, 7, 15, 16, 12, 13], ["-", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
364
#include <algorithm> #include <bitset> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> //#include <utility> //#include <memory> //#include <functional> //#include <deque> //#include <cctype> //#include <ctime> //#include <numeric> //#include <list> //#include <iomanip> //#if __cplusplus >= 201103L //#include <array> //#include <tuple> //#include <initializer_list> //#include <forward_list> // //#define cauto const auto& //#else //#endif using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vint; typedef vector<vector<int>> vvint; typedef vector<long long> vll, vLL; typedef vector<vector<long long>> vvll, vvLL; #define VV(T) vector<vector<T>> template <class T> void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) { v.assign(a, vector<T>(b, t)); } template <class F, class T> void convert(const F &f, T &t) { stringstream ss; ss << f; ss >> t; } #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define reep(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) reep((i), 0, (n)) #define ALL(v) (v).begin(), (v).end() #define PB push_back #define F first #define S second #define mkp make_pair #define RALL(v) (v).rbegin(), (v).rend() #define DEBUG #ifdef DEBUG #define dump(x) cout << #x << " = " << (x) << endl; #define debug(x) \ cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; #else #define dump(x) #define debug(x) #endif #define LDcout(x, n) fixed << setprecision(n) << x #define MOD 1000000007LL #define EPS 1e-8 #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL #define maxs(x, y) x = max(x, y) #define mins(x, y) x = min(x, y) string s; vector<string> ans; bool check(string a) { rep(i, 25) { rep(j, a.size()) { if (a[j] == 'b' + i) { a[j]--; break; } } } if (a == s.substr(0, a.size())) return true; return false; } void foo(string t) { if (t.size() == s.size()) { if (check(t)) ans.PB(t); return; } int pos = t.size(); string tmp = t + s[pos]; if (check(tmp)) foo(tmp); tmp.back()++; if (check(tmp)) foo(tmp); } void mainmain() { while (cin >> s) { if (s == "#") return; ans = vector<string>(0); foo(""); sort(ALL(ans)); ans.erase(unique(ALL(ans)), ans.end()); if (ans.size() < 10) { cout << ans.size() << endl; rep(i, ans.size()) { cout << ans[i] << endl; } } else { cout << 10 << endl; rep(i, 5) cout << ans[i] << endl; reep(i, ans.size() - 5, ans.size()) cout << ans[i] << endl; } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); mainmain(); }
#include <algorithm> #include <bitset> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> //#include <utility> //#include <memory> //#include <functional> //#include <deque> //#include <cctype> //#include <ctime> //#include <numeric> //#include <list> //#include <iomanip> //#if __cplusplus >= 201103L //#include <array> //#include <tuple> //#include <initializer_list> //#include <forward_list> // //#define cauto const auto& //#else //#endif using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vint; typedef vector<vector<int>> vvint; typedef vector<long long> vll, vLL; typedef vector<vector<long long>> vvll, vvLL; #define VV(T) vector<vector<T>> template <class T> void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) { v.assign(a, vector<T>(b, t)); } template <class F, class T> void convert(const F &f, T &t) { stringstream ss; ss << f; ss >> t; } #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define reep(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) reep((i), 0, (n)) #define ALL(v) (v).begin(), (v).end() #define PB push_back #define F first #define S second #define mkp make_pair #define RALL(v) (v).rbegin(), (v).rend() #define DEBUG #ifdef DEBUG #define dump(x) cout << #x << " = " << (x) << endl; #define debug(x) \ cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; #else #define dump(x) #define debug(x) #endif #define LDcout(x, n) fixed << setprecision(n) << x #define MOD 1000000007LL #define EPS 1e-8 #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL #define maxs(x, y) x = max(x, y) #define mins(x, y) x = min(x, y) string s; vector<string> ans; bool check(string a) { rep(i, 25) { rep(j, a.size()) { if (a[j] == 'b' + i) { a[j]--; break; } } } if (a == s.substr(0, a.size())) return true; return false; } void foo(string t) { if (t.size() == s.size()) { if (check(t)) ans.PB(t); return; } int pos = t.size(); string tmp = t + s[pos]; if (check(tmp)) foo(tmp); tmp.back()++; if (check(tmp)) foo(tmp); } void mainmain() { while (cin >> s) { if (s == "#") return; ans = vector<string>(0); foo(""); sort(ALL(ans)); ans.erase(unique(ALL(ans)), ans.end()); if (ans.size() < 10) { cout << ans.size() << endl; rep(i, ans.size()) { cout << ans[i] << endl; } } else { cout << ans.size() << endl; rep(i, 5) cout << ans[i] << endl; reep(i, ans.size() - 5, ans.size()) cout << ans[i] << endl; } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); mainmain(); }
[["-", 0, 9, 0, 1, 0, 16, 31, 16, 12, 13], ["+", 0, 16, 31, 16, 12, 2, 63, 118, 28, 22], ["+", 0, 16, 31, 16, 12, 2, 63, 118, 17, 131], ["+", 0, 16, 31, 16, 12, 2, 63, 118, 119, 120], ["+", 0, 16, 31, 16, 12, 2, 3, 4, 0, 24], ["+", 0, 16, 31, 16, 12, 2, 3, 4, 0, 25]]
1
707
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; const static int tx[] = {0, 1, 0, -1}; const static int ty[] = {-1, 0, 1, 0}; const static double EPS = 1e-8; vector<string> candidates; int dfs(int pos, int used, const string &encrypted, string current) { if (pos == encrypted.size()) { candidates.push_back(current); return 1; } int res = 0; for (int offset = 0; offset <= 1; offset++) { if (encrypted[pos] == 'z' && offset == 1) continue; if (offset == 0) { if (encrypted[pos] != 'a' && !(used & (1 << ((encrypted[pos] + offset) - 'a')))) continue; res += dfs(pos + 1, used, encrypted, current + (char)(encrypted[pos] + offset)); } else if (offset == 1) { if (used & (1 << ((encrypted[pos] + offset) - 'a'))) continue; res += dfs(pos + 1, used | (1 << ((encrypted[pos] + offset) - 'a')), encrypted, current + (char)(encrypted[pos] + offset)); } } return res; } int main() { string str; while (cin >> str) { if (str == "#") break; candidates.clear(); printf("%d\n", dfs(0, 0, str, "")); sort(candidates.begin(), candidates.end()); if (candidates.size() > 10) { for (int i = 0; i < 5; i++) { cout << candidates[i] << endl; } for (int i = candidates.size() - 6; i < candidates.size(); i++) { cout << candidates[i] << endl; } } else { for (int i = 0; i < candidates.size(); i++) { cout << candidates[i] << endl; } } } }
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; const static int tx[] = {0, 1, 0, -1}; const static int ty[] = {-1, 0, 1, 0}; const static double EPS = 1e-8; vector<string> candidates; int dfs(int pos, int used, const string &encrypted, string current) { if (pos == encrypted.size()) { candidates.push_back(current); return 1; } int res = 0; for (int offset = 0; offset <= 1; offset++) { if (encrypted[pos] == 'z' && offset == 1) continue; if (offset == 0) { if (encrypted[pos] != 'a' && !(used & (1 << ((encrypted[pos] + offset) - 'a')))) continue; res += dfs(pos + 1, used, encrypted, current + (char)(encrypted[pos] + offset)); } else if (offset == 1) { if (used & (1 << ((encrypted[pos] + offset) - 'a'))) continue; res += dfs(pos + 1, used | (1 << ((encrypted[pos] + offset) - 'a')), encrypted, current + (char)(encrypted[pos] + offset)); } } return res; } int main() { string str; while (cin >> str) { if (str == "#") break; candidates.clear(); printf("%d\n", dfs(0, 0, str, "")); sort(candidates.begin(), candidates.end()); if (candidates.size() > 10) { for (int i = 0; i < 5; i++) { cout << candidates[i] << endl; } for (int i = candidates.size() - 5; i < candidates.size(); i++) { cout << candidates[i] << endl; } } else { for (int i = 0; i < candidates.size(); i++) { cout << candidates[i] << endl; } } } }
[["-", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
521
#include <algorithm> #include <climits> #include <cstdlib> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <vector> #define ALL(v) (v).begin(), (v).end() #define REP(i, p, n) for (int i = p; i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) #define dump(a) (cerr << #a << "=" << (a) << endl) #define DUMP(list) \ cout << "{"; \ for (auto nth : list) { \ cout << nth << " "; \ } \ cout << "}" << endl; using namespace std; template <class T> T MIN(const T &a, const T &b) { return a < b ? a : b; } template <class T> T MAX(const T &a, const T &b) { return a > b ? a : b; } template <class T> void MIN_UPDATE(T &a, const T &b) { if (a > b) a = b; } template <class T> void MAX_UPDATE(T &a, const T &b) { if (a < b) a = b; } string str; set<string> ans; //?????????????????£???????????£???????????? void change(string tmp, char rule) { if (rule == 'a') { ans.insert(tmp); return; } rep(i, tmp.size()) { if (tmp[i] == rule) return; if (tmp[i] == rule - 1) { string ctmp = tmp; ctmp[i]++; change(ctmp, rule - 1); } } change(tmp, rule - 1); } int main() { while (1) { cin >> str; ans.clear(); if (str == "#") { break; } change(str, 'z'); cout << ans.size() << endl; if (ans.size() > 5) { vector<string> vec(ans.begin(), ans.end()); for (int i = 0; i < 5; i++) { cout << vec[i] << endl; } for (int i = ans.size() - 5; i < ans.size(); i++) { cout << vec[i] << endl; } } else if (ans.size() > 0) { vector<string> vec(ans.begin(), ans.end()); rep(i, ans.size()) { cout << vec[i] << endl; } } } }
#include <algorithm> #include <climits> #include <cstdlib> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <vector> #define ALL(v) (v).begin(), (v).end() #define REP(i, p, n) for (int i = p; i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) #define dump(a) (cerr << #a << "=" << (a) << endl) #define DUMP(list) \ cout << "{"; \ for (auto nth : list) { \ cout << nth << " "; \ } \ cout << "}" << endl; using namespace std; template <class T> T MIN(const T &a, const T &b) { return a < b ? a : b; } template <class T> T MAX(const T &a, const T &b) { return a > b ? a : b; } template <class T> void MIN_UPDATE(T &a, const T &b) { if (a > b) a = b; } template <class T> void MAX_UPDATE(T &a, const T &b) { if (a < b) a = b; } string str; set<string> ans; //?????????????????£???????????£???????????? void change(string tmp, char rule) { if (rule == 'a') { ans.insert(tmp); return; } rep(i, tmp.size()) { if (tmp[i] == rule) return; if (tmp[i] == rule - 1) { string ctmp = tmp; ctmp[i]++; change(ctmp, rule - 1); } } change(tmp, rule - 1); } int main() { while (1) { cin >> str; ans.clear(); if (str == "#") { break; } change(str, 'z'); cout << ans.size() << endl; if (ans.size() > 10) { vector<string> vec(ans.begin(), ans.end()); for (int i = 0; i < 5; i++) { cout << vec[i] << endl; } for (int i = ans.size() - 5; i < ans.size(); i++) { cout << vec[i] << endl; } } else if (ans.size() > 0) { vector<string> vec(ans.begin(), ans.end()); rep(i, ans.size()) { cout << vec[i] << endl; } } } }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
481
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main() { string s; while (cin >> s, s[0] != '#') { vector<string> res; for (int i = 0; i < (1 << s.size()); i++) { string p = s; bool flg = true; bool found[26] = {0}; found[0] = true; for (int j = 0; j < s.size(); j++) { if (i & (1 << j)) { if (p[j] == 'z') { flg = false; break; } p[j]++; if (found[p[j] - 'a']) { flg = false; break; } found[p[j] - 'a'] = true; } else { if (!found[p[j] - 'a']) { flg = false; break; } found[p[j] - 'a'] = true; } } if (flg) { res.push_back(p); } } cout << res.size() << endl; sort(res.begin(), res.end()); if (res.size() > 10) { for (int i = 0; i < 5; i++) cout << res[i] << endl; for (int i = 0; i < 5; i++) cout << res[res.size() - 6 + i] << endl; } else { for (int i = 0; i < res.size(); i++) cout << res[i] << endl; } } }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main() { string s; while (cin >> s, s[0] != '#') { vector<string> res; for (int i = 0; i < (1 << s.size()); i++) { string p = s; bool flg = true; bool found[26] = {0}; found[0] = true; for (int j = 0; j < s.size(); j++) { if (i & (1 << j)) { if (p[j] == 'z') { flg = false; break; } else { p[j]++; if (found[p[j] - 'a']) { flg = false; break; } found[p[j] - 'a'] = true; } } else { if (!found[p[j] - 'a']) { flg = false; break; } found[p[j] - 'a'] = true; } } if (flg) { res.push_back(p); } } cout << res.size() << endl; sort(res.begin(), res.end()); if (res.size() > 10) { for (int i = 0; i < 5; i++) cout << res[i] << endl; for (int i = 0; i < 5; i++) cout << res[res.size() - 5 + i] << endl; } else { for (int i = 0; i < res.size(); i++) cout << res[i] << endl; } } }
[["+", 0, 57, 64, 9, 0, 57, 75, 76, 0, 95], ["+", 64, 9, 0, 57, 75, 76, 0, 9, 0, 45], ["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 46], ["-", 12, 69, 341, 342, 0, 16, 31, 16, 12, 13], ["+", 12, 69, 341, 342, 0, 16, 31, 16, 12, 13]]
1
362
#include <bits/stdc++.h> using namespace std; vector<string> ans; string Str, str; bool check() { bool alph[300] = {}; alph['a'] = 1; for (int i = 0; i < str.size(); i++) { int flg = 0; if (!alph[str[i]]) flg = alph[str[i]] = 1; if (str[i] - flg != Str[i]) return 0; } return 1; } bool used['z']; void saiki(int idx) { while (used[str[idx]] || str[idx] == 'z') idx++; if (idx == str.size()) { if (check()) ans.push_back(str); return; } saiki(idx + 1); str[idx]++; if (!used[str[idx]]) used[str[idx] - 1] = 1, saiki(idx + 1), used[str[idx] - 1] = 0; str[idx]--; } int main() { while (1) { cin >> Str; str = Str; if (Str == "#") break; ans.clear(); saiki(0); sort(ans.begin(), ans.end()); cout << ans.size() << endl; if (ans.size() <= 10) for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl; else { for (int i = 0; i < 5; i++) cout << ans[i] << endl; for (int i = 0; i < 5; i++) cout << ans[ans.size() - 5 + i] << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; vector<string> ans; string Str, str; bool check() { bool alph[300] = {}; alph['a'] = 1; for (int i = 0; i < str.size(); i++) { int flg = 0; if (!alph[str[i]]) flg = alph[str[i]] = 1; if (str[i] - flg != Str[i]) return 0; } return 1; } bool used['z']; void saiki(int idx) { while (used[str[idx]] || str[idx] == 'z') idx++; if (idx == str.size()) { if (check()) ans.push_back(str); return; } saiki(idx + 1); str[idx]++; if (!used[str[idx] - 1]) used[str[idx] - 1] = 1, saiki(idx + 1), used[str[idx] - 1] = 0; str[idx]--; } int main() { while (1) { cin >> Str; str = Str; if (Str == "#") break; ans.clear(); saiki(0); sort(ans.begin(), ans.end()); cout << ans.size() << endl; if (ans.size() <= 10) for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl; else { for (int i = 0; i < 5; i++) cout << ans[i] << endl; for (int i = 0; i < 5; i++) cout << ans[ans.size() - 5 + i] << endl; } } return 0; }
[["+", 51, 91, 28, 69, 341, 342, 0, 16, 17, 33], ["+", 51, 91, 28, 69, 341, 342, 0, 16, 12, 13]]
1
398
#include <bits/stdc++.h> using namespace std; #define int long long // <-----!!!!!!!!!!!!!!!!!!! #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define rrep2(i, a, b) for (int i = (a)-1; i >= b; i--) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define printV(v) \ for (auto x : v) { \ cout << x << " "; \ } \ cout << endl #define printVS(vs) \ for (auto x : vs) { \ cout << x << endl; \ } #define printVV(vv) \ for (auto v : vv) { \ for (auto &&x : v) { \ cout << x << " "; \ } \ cout << endl; \ } #define printP(p) cout << p.first << " " << p.second << endl #define printVP(vp) \ for (auto p : vp) \ printP(p); typedef long long ll; typedef pair<int, int> Pii; typedef tuple<int, int, int> TUPLE; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<Pii> vp; typedef vector<vector<int>> Graph; const int inf = 1e9; const int mod = 1e9 + 7; const int MAX = 1 << 20; bool ok[MAX]; string s; int n; void dfs(int i, int state, int used) { // cout << i << " " << state << " " << used << endl; if (i == n) { ok[state] = true; return; } if (s[i] == 'a' || ((used >> (s[i] - 'a')) & 1)) { dfs(i + 1, state, used | (1 << (s[i] - 'a'))); } if (s[i] != 'z' && !((used >> (s[i] - 'a' + 1)) & 1)) { dfs(i + 1, state | (1 << i), used | (1 << (s[i] - 'a' + 1))); } } string f(int state) { string t; rep(i, n) { t += s[i] + ((state >> i) & 1); } return t; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); while (cin >> s, s != "#") { n = s.size(); rep(i, 1 << n) { ok[i] = false; } dfs(0, 0, 0); int cnt = 0; vector<int> v; rep(i, 1 << n) { if (ok[i]) { cnt++; v.emplace_back(i); } } cout << cnt << endl; vector<string> vs; rep(i, v.size()) { vs.emplace_back(f(v[i])); } sort(all(vs)); if (cnt < 5) { rep(i, vs.size()) { cout << vs[i] << endl; } } else { rep(i, 5) { cout << vs[i] << endl; } rep2(i, (int)vs.size() - 5, vs.size()) { cout << vs[i] << endl; } } } }
#include <bits/stdc++.h> using namespace std; #define int long long // <-----!!!!!!!!!!!!!!!!!!! #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define rrep2(i, a, b) for (int i = (a)-1; i >= b; i--) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define printV(v) \ for (auto x : v) { \ cout << x << " "; \ } \ cout << endl #define printVS(vs) \ for (auto x : vs) { \ cout << x << endl; \ } #define printVV(vv) \ for (auto v : vv) { \ for (auto &&x : v) { \ cout << x << " "; \ } \ cout << endl; \ } #define printP(p) cout << p.first << " " << p.second << endl #define printVP(vp) \ for (auto p : vp) \ printP(p); typedef long long ll; typedef pair<int, int> Pii; typedef tuple<int, int, int> TUPLE; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<Pii> vp; typedef vector<vector<int>> Graph; const int inf = 1e9; const int mod = 1e9 + 7; const int MAX = 1 << 20; bool ok[MAX]; string s; int n; void dfs(int i, int state, int used) { if (i == n) { ok[state] = true; return; } if (s[i] == 'a' || ((used >> (s[i] - 'a')) & 1)) { dfs(i + 1, state, used | (1 << (s[i] - 'a'))); } if (s[i] != 'z' && !((used >> (s[i] - 'a' + 1)) & 1)) { dfs(i + 1, state | (1 << i), used | (1 << (s[i] - 'a' + 1))); } } string f(int state) { string t; rep(i, n) { t += s[i] + ((state >> i) & 1); } return t; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); while (cin >> s, s != "#") { n = s.size(); rep(i, 1 << n) { ok[i] = false; } dfs(0, 0, 0); int cnt = 0; vector<int> v; rep(i, 1 << n) { if (ok[i]) { cnt++; v.emplace_back(i); } } cout << cnt << endl; vector<string> vs; rep(i, v.size()) { vs.emplace_back(f(v[i])); } sort(all(vs)); if (cnt <= 10) { rep(i, vs.size()) { cout << vs[i] << endl; } } else { rep(i, 5) { cout << vs[i] << endl; } rep2(i, (int)vs.size() - 5, vs.size()) { cout << vs[i] << endl; } } } }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
628
#include <stdio.h> #include <stdlib.h> #define tree_valtype int typedef struct tree_edge_sub tree_edge; typedef struct { int num; int nearnum; tree_edge *near; } tree_vertex_sub; struct tree_edge_sub { tree_vertex_sub *v; int w; tree_edge *next; }; typedef struct tree_v_sub tree_vertex; struct tree_v_sub { int num; tree_valtype val; tree_vertex *parent; int pareweight; int chilnum; tree_vertex **children; int *chilweight; }; typedef struct { int N; int root; tree_vertex_sub **v_s; tree_vertex **v; tree_vertex **sorted_v; } tree; //頂点数N, 根の番号root, 各頂点の初期値ini_valの木を作る tree *make_tree(int N, int root, tree_valtype ini_val) { int i; tree *t = (tree *)malloc(sizeof(tree)); t->N = N; t->root = root; t->v_s = (tree_vertex_sub **)malloc(sizeof(tree_vertex_sub *) * N); t->v = (tree_vertex **)malloc(sizeof(tree_vertex *) * N); t->sorted_v = (tree_vertex **)malloc(sizeof(tree_vertex *) * N); tree_vertex *parent_in_law = (tree_vertex *)malloc(sizeof(tree_vertex)); parent_in_law->num = -1; parent_in_law->val = ini_val; parent_in_law->parent = NULL; parent_in_law->pareweight = -1; parent_in_law->chilnum = 0; parent_in_law->children = NULL; parent_in_law->chilweight = NULL; for (i = 0; i < N; i++) { (t->v_s)[i] = (tree_vertex_sub *)malloc(sizeof(tree_vertex_sub)); (t->v_s)[i]->num = i; (t->v_s)[i]->nearnum = 0; (t->v_s)[i]->near = NULL; (t->v)[i] = (tree_vertex *)malloc(sizeof(tree_vertex)); (t->v)[i]->num = i; (t->v)[i]->val = ini_val; (t->v)[i]->parent = parent_in_law; (t->v)[i]->pareweight = -1; (t->v)[i]->chilnum = 0; (t->v)[i]->children = NULL; (t->v)[i]->chilweight = NULL; (t->sorted_v)[i] = NULL; } return t; } //木tの頂点aと頂点bの間に重みwの無向辺を張る (0 <= a, b <= N - 1) void set_edge_tree(int a, int b, int w, tree *t) { tree_edge *new1 = (tree_edge *)malloc(sizeof(tree_edge)); new1->v = (t->v_s)[b]; new1->w = w; new1->next = (t->v_s)[a]->near; (t->v_s)[a]->near = new1; (t->v_s)[a]->nearnum++; tree_edge *new2 = (tree_edge *)malloc(sizeof(tree_edge)); new2->v = (t->v_s)[a]; new2->w = w; new2->next = (t->v_s)[b]->near; (t->v_s)[b]->near = new2; (t->v_s)[b]->nearnum++; } // set_edge_tree後に呼び出す void build_tree(tree *t) { int i, j; tree_vertex_sub **v_s = t->v_s; tree_vertex **v = t->v; tree_vertex **sorted_v = t->sorted_v; sorted_v[0] = v[t->root]; tree_vertex *nowv; tree_edge *nowe; for (i = 0, j = 1; j - i > 0; i++) { nowv = sorted_v[i]; if (i == 0) { v_s[nowv->num]->nearnum++; } nowv->children = (tree_vertex **)malloc(sizeof(tree_vertex *) * (v_s[nowv->num]->nearnum - 1)); nowv->chilweight = (int *)malloc(sizeof(int) * (v_s[nowv->num]->nearnum - 1)); if (i == 0) { v_s[nowv->num]->nearnum--; } for (nowe = v_s[nowv->num]->near; nowe != NULL; nowe = nowe->next) { if (nowe->v->num != nowv->parent->num) { (nowv->children)[nowv->chilnum] = v[nowe->v->num]; (nowv->chilweight)[nowv->chilnum] = nowe->w; nowv->chilnum++; v[nowe->v->num]->parent = nowv; v[nowe->v->num]->pareweight = nowe->w; sorted_v[j] = v[nowe->v->num]; j++; } } } sorted_v[0]->parent = NULL; } int main() { int N, i; while (1) { scanf("%d", &N); if (N == 0) { return 0; } tree *t = make_tree(N, 0, 0); int *p = (int *)malloc(sizeof(int) * N); int *d = (int *)malloc(sizeof(int) * N); for (i = 1; i < N; i++) { scanf("%d", &p[i]); } int dsum = 0; for (i = 1; i < N; i++) { scanf("%d", &d[i]); dsum += d[i]; } for (i = 1; i < N; i++) { set_edge_tree(i, p[i] - 1, d[i], t); } for (i = 0; t->v_s[i]->nearnum == 1; i++) { } t->root = i; // printf("t->root = %d\n", t->root); build_tree(t); tree_vertex *nowv; int l = t->v[0]->num; int dinsum = 0; for (i = 1; i < N; i++) { // printf("i = %d\n", i); nowv = t->sorted_v[i]; nowv->val = nowv->parent->val + nowv->pareweight; if (nowv->chilnum != 0) { dinsum += nowv->pareweight; if (t->v[l]->val < nowv->val) { l = nowv->num; } } } tree *t2 = make_tree(N, l, 0); t2->v_s = t->v_s; build_tree(t2); l = t2->v[0]->num; // printf("t2->root = %d\n", t2->root); for (i = 1; i < N; i++) { nowv = t2->sorted_v[i]; nowv->val = nowv->parent->val + nowv->pareweight; if (nowv->chilnum != 0) { if (t2->v[l]->val < nowv->val) { l = nowv->num; } } } // printf("dsum = %d\n", dsum); // printf("dinsum = %d\n", dinsum); printf("%d\n", dsum + 2 * dinsum - t2->v[l]->val); } }
#include <stdio.h> #include <stdlib.h> #define tree_valtype int typedef struct tree_edge_sub tree_edge; typedef struct { int num; int nearnum; tree_edge *near; } tree_vertex_sub; struct tree_edge_sub { tree_vertex_sub *v; int w; tree_edge *next; }; typedef struct tree_v_sub tree_vertex; struct tree_v_sub { int num; tree_valtype val; tree_vertex *parent; int pareweight; int chilnum; tree_vertex **children; int *chilweight; }; typedef struct { int N; int root; tree_vertex_sub **v_s; tree_vertex **v; tree_vertex **sorted_v; } tree; //頂点数N, 根の番号root, 各頂点の初期値ini_valの木を作る tree *make_tree(int N, int root, tree_valtype ini_val) { int i; tree *t = (tree *)malloc(sizeof(tree)); t->N = N; t->root = root; t->v_s = (tree_vertex_sub **)malloc(sizeof(tree_vertex_sub *) * N); t->v = (tree_vertex **)malloc(sizeof(tree_vertex *) * N); t->sorted_v = (tree_vertex **)malloc(sizeof(tree_vertex *) * N); tree_vertex *parent_in_law = (tree_vertex *)malloc(sizeof(tree_vertex)); parent_in_law->num = -1; parent_in_law->val = ini_val; parent_in_law->parent = NULL; parent_in_law->pareweight = -1; parent_in_law->chilnum = 0; parent_in_law->children = NULL; parent_in_law->chilweight = NULL; for (i = 0; i < N; i++) { (t->v_s)[i] = (tree_vertex_sub *)malloc(sizeof(tree_vertex_sub)); (t->v_s)[i]->num = i; (t->v_s)[i]->nearnum = 0; (t->v_s)[i]->near = NULL; (t->v)[i] = (tree_vertex *)malloc(sizeof(tree_vertex)); (t->v)[i]->num = i; (t->v)[i]->val = ini_val; (t->v)[i]->parent = parent_in_law; (t->v)[i]->pareweight = -1; (t->v)[i]->chilnum = 0; (t->v)[i]->children = NULL; (t->v)[i]->chilweight = NULL; (t->sorted_v)[i] = NULL; } return t; } //木tの頂点aと頂点bの間に重みwの無向辺を張る (0 <= a, b <= N - 1) void set_edge_tree(int a, int b, int w, tree *t) { tree_edge *new1 = (tree_edge *)malloc(sizeof(tree_edge)); new1->v = (t->v_s)[b]; new1->w = w; new1->next = (t->v_s)[a]->near; (t->v_s)[a]->near = new1; (t->v_s)[a]->nearnum++; tree_edge *new2 = (tree_edge *)malloc(sizeof(tree_edge)); new2->v = (t->v_s)[a]; new2->w = w; new2->next = (t->v_s)[b]->near; (t->v_s)[b]->near = new2; (t->v_s)[b]->nearnum++; } // set_edge_tree後に呼び出す void build_tree(tree *t) { int i, j; tree_vertex_sub **v_s = t->v_s; tree_vertex **v = t->v; tree_vertex **sorted_v = t->sorted_v; sorted_v[0] = v[t->root]; tree_vertex *nowv; tree_edge *nowe; for (i = 0, j = 1; j - i > 0; i++) { nowv = sorted_v[i]; if (i == 0) { v_s[nowv->num]->nearnum++; } nowv->children = (tree_vertex **)malloc(sizeof(tree_vertex *) * (v_s[nowv->num]->nearnum - 1)); nowv->chilweight = (int *)malloc(sizeof(int) * (v_s[nowv->num]->nearnum - 1)); if (i == 0) { v_s[nowv->num]->nearnum--; } for (nowe = v_s[nowv->num]->near; nowe != NULL; nowe = nowe->next) { if (nowe->v->num != nowv->parent->num) { (nowv->children)[nowv->chilnum] = v[nowe->v->num]; (nowv->chilweight)[nowv->chilnum] = nowe->w; nowv->chilnum++; v[nowe->v->num]->parent = nowv; v[nowe->v->num]->pareweight = nowe->w; sorted_v[j] = v[nowe->v->num]; j++; } } } sorted_v[0]->parent = NULL; } int main() { int N, i; while (1) { scanf("%d", &N); if (N == 0) { return 0; } tree *t = make_tree(N, 0, 0); int *p = (int *)malloc(sizeof(int) * N); int *d = (int *)malloc(sizeof(int) * N); for (i = 1; i < N; i++) { scanf("%d", &p[i]); } int dsum = 0; for (i = 1; i < N; i++) { scanf("%d", &d[i]); dsum += d[i]; } for (i = 1; i < N; i++) { set_edge_tree(i, p[i] - 1, d[i], t); } for (i = 0; t->v_s[i]->nearnum == 1; i++) { } t->root = i; // printf("t->root = %d\n", t->root); build_tree(t); tree_vertex *nowv; int l = t->sorted_v[0]->num; int dinsum = 0; for (i = 1; i < N; i++) { // printf("i = %d\n", i); nowv = t->sorted_v[i]; nowv->val = nowv->parent->val + nowv->pareweight; if (nowv->chilnum != 0) { dinsum += nowv->pareweight; if (t->v[l]->val < nowv->val) { l = nowv->num; } } } tree *t2 = make_tree(N, l, 0); t2->v_s = t->v_s; build_tree(t2); l = t2->sorted_v[0]->num; // printf("t2->root = %d\n", t2->root); for (i = 1; i < N; i++) { nowv = t2->sorted_v[i]; nowv->val = nowv->parent->val + nowv->pareweight; if (nowv->chilnum != 0) { if (t2->v[l]->val < nowv->val) { l = nowv->num; } } } // printf("dsum = %d\n", dsum); // printf("dinsum = %d\n", dinsum); printf("%d\n", dsum + 2 * dinsum - t2->v[l]->val); } }
[["-", 49, 50, 51, 118, 28, 69, 28, 118, 119, 120], ["+", 49, 50, 51, 118, 28, 69, 28, 118, 119, 120], ["-", 0, 11, 12, 118, 28, 69, 28, 118, 119, 120], ["+", 0, 11, 12, 118, 28, 69, 28, 118, 119, 120]]
0
1,418
#include <algorithm> #include <functional> #include <iostream> #include <queue> #include <vector> #pragma warning(disable : 4996) #define INF (1 << 29) using namespace std; int main() { int n, sum; while (true) { scanf("%d", &n); sum = 0; if (n == 0) break; vector<int> p(n - 1); vector<int> d(n - 1); vector<vector<pair<int, int>>> T(n); for (int i = 0; i < n - 1; i++) { scanf("%d", &p[i]); } for (int i = 0; i < n - 1; i++) { scanf("%d", &d[i]); sum += d[i]; } for (int i = 0; i < n - 1; i++) { T[i + 1].push_back(make_pair(p[i] - 1, d[i])); T[p[i] - 1].push_back(make_pair(i + 1, d[i])); } vector<vector<int>> dists(n, vector<int>(n, INF)); for (int i = 0; i < n; i++) { priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que; que.push(make_pair(0, i)); dists[i][i] = 0; while (!que.empty()) { pair<int, int> p1 = que.top(); que.pop(); int node = p1.second; int dist = p1.first; for (int j = 0; j < T[node].size(); j++) { if (dists[i][T[node][j].first] == INF) { dists[i][T[node][j].first] = dist + T[node][j].second; que.push(make_pair(dists[i][T[node][j].first], T[node][j].first)); } } } } vector<int> nodes(n); for (int i = 0; i < n; i++) { nodes[i] = T[i].size(); } int sum2 = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < nodes[i]; j++) { if (nodes[i] >= 2 && nodes[T[i][j].first] >= 2) { sum2 += T[i][j].second; } } } int ret = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (nodes[i] > 1 && nodes[j] > 1) { ret = max(ret, dists[i][j]); } } } printf("%d\n", sum + sum2 * 2 - ret); } return 0; }
#include <algorithm> #include <functional> #include <iostream> #include <queue> #include <vector> #pragma warning(disable : 4996) #define INF (1 << 29) using namespace std; int main() { int n, sum; while (true) { scanf("%d", &n); sum = 0; if (n == 0) break; vector<int> p(n - 1); vector<int> d(n - 1); vector<vector<pair<int, int>>> T(n); for (int i = 0; i < n - 1; i++) { scanf("%d", &p[i]); } for (int i = 0; i < n - 1; i++) { scanf("%d", &d[i]); sum += d[i]; } for (int i = 0; i < n - 1; i++) { T[i + 1].push_back(make_pair(p[i] - 1, d[i])); T[p[i] - 1].push_back(make_pair(i + 1, d[i])); } vector<vector<int>> dists(n, vector<int>(n, INF)); for (int i = 0; i < n; i++) { priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que; que.push(make_pair(0, i)); dists[i][i] = 0; while (!que.empty()) { pair<int, int> p1 = que.top(); que.pop(); int node = p1.second; int dist = p1.first; for (int j = 0; j < T[node].size(); j++) { if (dists[i][T[node][j].first] == INF) { dists[i][T[node][j].first] = dist + T[node][j].second; que.push(make_pair(dists[i][T[node][j].first], T[node][j].first)); } } } } vector<int> nodes(n); for (int i = 0; i < n; i++) { nodes[i] = T[i].size(); } int sum2 = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < nodes[i]; j++) { if (nodes[i] >= 2 && nodes[T[i][j].first] >= 2) { sum2 += T[i][j].second; } } } int ret = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (nodes[i] > 1 && nodes[j] > 1) { ret = max(ret, dists[i][j]); } } } printf("%d\n", sum + sum2 - ret); } return 0; }
[["-", 3, 4, 0, 16, 31, 16, 12, 16, 17, 48], ["-", 3, 4, 0, 16, 31, 16, 12, 16, 12, 13]]
1
683
#include <bits/stdc++.h> using namespace std; #define for_(i, a, b) for (int i = (a); i < (b); ++i) struct Box { double x, y, r; }; const double EPS = 1e-9; void solve(int n) { vector<double> h, w; vector<Box> vb; for_(i, 0, n) { double x, y, r; cin >> x >> y >> r; vb.push_back(Box{x, y, r}); h.push_back(y - r); h.push_back(y + r); w.push_back(x - r); w.push_back(x + r); } sort(h.begin(), h.end()); sort(w.begin(), w.end()); h.erase(unique(h.begin(), h.end()), h.end()); w.erase(unique(w.begin(), w.end()), w.end()); int H = h.size(), W = w.size(); double ans = 0; for_(i, 0, H - 1) for_(j, 0, W - 1) { double lx = w[j], rx = w[j + 1]; double ly = h[i], ry = h[i + 1]; for_(k, 0, n) { Box b = vb[k]; if (b.x - b.r < lx + EPS && b.y - b.r < ly + EPS && b.x + b.r > rx - EPS && b.y + b.r > ry - EPS) { ans += (rx - lx) * (ry - ly); break; } } } cout << setprecision(9) << setiosflags(ios::fixed) << ans << endl; } int main() { int case_number = 0, n; while (cin >> n, n) { ++case_number; cout << case_number << " "; solve(n); } }
#include <bits/stdc++.h> using namespace std; #define for_(i, a, b) for (int i = (a); i < (b); ++i) struct Box { double x, y, r; }; const double EPS = 1e-9; void solve(int n) { vector<double> h, w; vector<Box> vb; for_(i, 0, n) { double x, y, r; cin >> x >> y >> r; vb.push_back(Box{x, y, r}); h.push_back(y - r); h.push_back(y + r); w.push_back(x - r); w.push_back(x + r); } sort(h.begin(), h.end()); sort(w.begin(), w.end()); h.erase(unique(h.begin(), h.end()), h.end()); w.erase(unique(w.begin(), w.end()), w.end()); int H = h.size(), W = w.size(); double ans = 0; for_(i, 0, H - 1) for_(j, 0, W - 1) { double lx = w[j], rx = w[j + 1]; double ly = h[i], ry = h[i + 1]; for_(k, 0, n) { Box b = vb[k]; if (b.x - b.r < lx + EPS && b.y - b.r < ly + EPS && b.x + b.r > rx - EPS && b.y + b.r > ry - EPS) { ans += (rx - lx) * (ry - ly); break; } } } cout << setprecision(2) << setiosflags(ios::fixed) << ans << endl; } int main() { int case_number = 0, n; while (cin >> n, n) { ++case_number; cout << case_number << " "; solve(n); } }
[["-", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13], ["+", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13]]
1
438
#include <bits/stdc++.h> using namespace std; int main() { int N, tc = 1; while (cin >> N, N) { vector<double> X1(N), Y1(N), X2(N), Y2(N); vector<double> xs, ys; for (int i = 0; i < N; i++) { double x, y, r; cin >> x >> y >> r; X1[i] = x - r; Y1[i] = y - r; X2[i] = x + r; Y2[i] = y + r; xs.push_back(X1[i]); xs.push_back(X2[i]); ys.push_back(Y1[i]); ys.push_back(Y2[i]); } sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(), xs.end()), xs.end()); sort(ys.begin(), ys.end()); ys.erase(unique(ys.begin(), ys.end()), ys.end()); double res = 0; for (int i = 0; i < (int)xs.size() - 1; i++) { for (int j = 0; j < (int)ys.size() - 1; j++) { int cnt = 0; for (int k = 0; k < N; k++) { if (X1[k] <= xs[i] && xs[i + 1] <= X2[k] && Y1[k] <= ys[j] && ys[j + 1] <= Y2[k]) { cnt++; } } if (cnt > 0) { res += (xs[i + 1] - xs[i]) * (ys[j + 1] - ys[j]); } } } printf("%d %.10f\n", tc++, res); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N, tc = 1; while (cin >> N, N) { vector<double> X1(N), Y1(N), X2(N), Y2(N); vector<double> xs, ys; for (int i = 0; i < N; i++) { double x, y, r; cin >> x >> y >> r; X1[i] = x - r; Y1[i] = y - r; X2[i] = x + r; Y2[i] = y + r; xs.push_back(X1[i]); xs.push_back(X2[i]); ys.push_back(Y1[i]); ys.push_back(Y2[i]); } sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(), xs.end()), xs.end()); sort(ys.begin(), ys.end()); ys.erase(unique(ys.begin(), ys.end()), ys.end()); double res = 0; for (int i = 0; i < (int)xs.size() - 1; i++) { for (int j = 0; j < (int)ys.size() - 1; j++) { int cnt = 0; for (int k = 0; k < N; k++) { if (X1[k] <= xs[i] && xs[i + 1] <= X2[k] && Y1[k] <= ys[j] && ys[j + 1] <= Y2[k]) { cnt++; } } if (cnt > 0) { res += (xs[i + 1] - xs[i]) * (ys[j + 1] - ys[j]); } } } printf("%d %.2f\n", tc++, res); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
431
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define mp make_pair #define all(in) in.begin(), in.end() #define shosu(x) fixed << setprecision(x) using namespace std; // kaewasuretyuui typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef pair<int, pii> pip; typedef vector<pip> vip; const double PI = acos(-1); const double EPS = 1e-8; const int inf = 1e8; int main() { int n; int co = 0; while (cin >> n, n) { co++; vector<double> x, y, x1, x2, y1, y2; vvi in(210, vi(210)); rep(i, n) { double a, b, c; cin >> a >> b >> c; x.pb(a - c); x.pb(a + c); y.pb(b - c); y.pb(b + c); x1.pb(a - c); x2.pb(a + c); y1.pb(b - c); y2.pb(b + c); } sort(all(x)); sort(all(y)); x.erase(unique(all(x)), x.end()); y.erase(unique(all(y)), y.end()); rep(i, n) { int a = find(all(x), x1[i]) - x.begin(); int b = find(all(x), x2[i]) - x.begin(); int c = find(all(y), y1[i]) - y.begin(); int d = find(all(y), y2[i]) - y.begin(); loop(j, a, b) loop(k, c, d) in[j][k] = true; } double sum = 0; rep(i, 210) rep(j, 210) if (in[i][j]) sum += (x[i + 1] - x[i]) * (y[j + 1] - y[j]); cout << co << " " << shosu(9) << sum << endl; } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define mp make_pair #define all(in) in.begin(), in.end() #define shosu(x) fixed << setprecision(x) using namespace std; // kaewasuretyuui typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef pair<int, pii> pip; typedef vector<pip> vip; const double PI = acos(-1); const double EPS = 1e-8; const int inf = 1e8; int main() { int n; int co = 0; while (cin >> n, n) { co++; vector<double> x, y, x1, x2, y1, y2; vvi in(210, vi(210)); rep(i, n) { double a, b, c; cin >> a >> b >> c; x.pb(a - c); x.pb(a + c); y.pb(b - c); y.pb(b + c); x1.pb(a - c); x2.pb(a + c); y1.pb(b - c); y2.pb(b + c); } sort(all(x)); sort(all(y)); x.erase(unique(all(x)), x.end()); y.erase(unique(all(y)), y.end()); rep(i, n) { int a = find(all(x), x1[i]) - x.begin(); int b = find(all(x), x2[i]) - x.begin(); int c = find(all(y), y1[i]) - y.begin(); int d = find(all(y), y2[i]) - y.begin(); loop(j, a, b) loop(k, c, d) in[j][k] = true; } double sum = 0; rep(i, 210) rep(j, 210) if (in[i][j]) sum += (x[i + 1] - x[i]) * (y[j + 1] - y[j]); cout << co << " " << shosu(2) << sum << endl; } }
[["-", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13], ["+", 31, 16, 31, 16, 12, 2, 3, 4, 0, 13]]
1
548
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; int main() { int n; int t = 0; while (cin >> n, n) { t++; double x[101], y[101], r[101]; vector<double> X, Y; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> r[i]; X.push_back(x[i] - r[i]); X.push_back(x[i] + r[i]); Y.push_back(y[i] - r[i]); Y.push_back(y[i] + r[i]); } sort(X.begin(), X.end()); sort(Y.begin(), Y.end()); X.erase(unique(X.begin(), X.end()), X.end()); Y.erase(unique(Y.begin(), Y.end()), Y.end()); vector<P> xx(n), yy(n); for (int i = 0; i < n; i++) { xx[i].first = find(X.begin(), X.end(), x[i] - r[i]) - X.begin(); xx[i].second = find(X.begin(), X.end(), x[i] + r[i]) - X.begin(); yy[i].first = find(Y.begin(), Y.end(), y[i] - r[i]) - Y.begin(); yy[i].second = find(Y.begin(), Y.end(), y[i] + r[i]) - Y.begin(); } bool mas[202][202] = {{false}}; for (int i = 0; i < n; i++) { for (int j = yy[i].first; j <= yy[i].second; j++) { for (int k = xx[i].first; k <= xx[i].second; k++) mas[j][k] = true; } } double ans = 0.0; for (int i = 0; i < (int)Y.size() - 1; i++) { for (int j = 0; j < (int)X.size() - 1; j++) { if (mas[i][j]) ans += (X[j + 1] - X[j]) * (Y[i + 1] - Y[i]); } } printf("%d %.2f\n", t, ans); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; int main() { int n; int t = 0; while (cin >> n, n) { t++; double x[101], y[101], r[101]; vector<double> X, Y; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> r[i]; X.push_back(x[i] - r[i]); X.push_back(x[i] + r[i]); Y.push_back(y[i] - r[i]); Y.push_back(y[i] + r[i]); } sort(X.begin(), X.end()); sort(Y.begin(), Y.end()); X.erase(unique(X.begin(), X.end()), X.end()); Y.erase(unique(Y.begin(), Y.end()), Y.end()); vector<P> xx(n), yy(n); for (int i = 0; i < n; i++) { xx[i].first = find(X.begin(), X.end(), x[i] - r[i]) - X.begin(); xx[i].second = find(X.begin(), X.end(), x[i] + r[i]) - X.begin(); yy[i].first = find(Y.begin(), Y.end(), y[i] - r[i]) - Y.begin(); yy[i].second = find(Y.begin(), Y.end(), y[i] + r[i]) - Y.begin(); } bool mas[202][202] = {{false}}; for (int i = 0; i < n; i++) { for (int j = yy[i].first; j < yy[i].second; j++) { for (int k = xx[i].first; k < xx[i].second; k++) mas[j][k] = true; } } double ans = 0.0; for (int i = 0; i < (int)Y.size() - 1; i++) { for (int j = 0; j < (int)X.size() - 1; j++) { if (mas[i][j]) ans += (X[j + 1] - X[j]) * (Y[i + 1] - Y[i]); } } printf("%d %.2f\n", t, ans); } return 0; }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18]]
1
624
#include <algorithm> #include <iomanip> #include <iostream> #include <map> #include <unordered_map> #include <vector> using namespace std; struct Point { double x; double y; Point(double x, double y) : x(x), y(y) {} Point() : x(0), y(0) {} }; struct Rectangle { Point topLeft; Point bottomRight; Rectangle(Point topLeft, Point bottomRight) : topLeft(topLeft), bottomRight(bottomRight) {} Rectangle() {} }; int compress(vector<double> &x, unordered_map<double, int> &zip, vector<int> &unzip) { sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end()); unzip.resize(x.size()); for (size_t i = 0; i < x.size(); i++) { zip[x[i]] = i; unzip[i] = x[i]; } return zip.size(); } double solve(const vector<Rectangle> rectangles) { vector<double> xs; unordered_map<double, int> xZip; vector<int> xUnzip; vector<double> ys; unordered_map<double, int> yZip; vector<int> yUnzip; for (auto &r : rectangles) { xs.push_back(r.topLeft.x); xs.push_back(r.bottomRight.x); ys.push_back(r.topLeft.y); ys.push_back(r.bottomRight.y); } vector<vector<bool>> checker(ys.size(), vector<bool>(xs.size(), false)); compress(xs, xZip, xUnzip); compress(ys, yZip, yUnzip); for (auto &r : rectangles) { for (int y = yZip[r.topLeft.y]; y < yZip[r.bottomRight.y]; ++y) { for (int x = xZip[r.topLeft.x]; x < xZip[r.bottomRight.x]; ++x) { checker[y][x] = true; } } } double res = 0.0; for (int y = 0; y < yUnzip.size() - 1; ++y) for (int x = 0; x < xUnzip.size() - 1; ++x) if (checker[y][x]) { res += (yUnzip[y + 1] - yUnzip[y]) * (xUnzip[x + 1] - xUnzip[x]); } return res; } int main(void) { int cnt = 0; while (1) { ++cnt; vector<Rectangle> rectangles; int n; cin >> n; if (n == 0) break; for (int i = 0; i < n; ++i) { double x, y, r; cin >> x >> y >> r; rectangles.push_back(Rectangle(Point(x - r, y - r), Point(x + r, y + r))); } cout << fixed << setprecision(2) << cnt << " " << solve(rectangles) << endl; } }
#include <algorithm> #include <iomanip> #include <iostream> #include <map> #include <unordered_map> #include <vector> using namespace std; struct Point { double x; double y; Point(double x, double y) : x(x), y(y) {} Point() : x(0), y(0) {} }; struct Rectangle { Point topLeft; Point bottomRight; Rectangle(Point topLeft, Point bottomRight) : topLeft(topLeft), bottomRight(bottomRight) {} Rectangle() {} }; int compress(vector<double> &x, unordered_map<double, int> &zip, vector<double> &unzip) { sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end()); unzip.resize(x.size()); for (size_t i = 0; i < x.size(); i++) { zip[x[i]] = i; unzip[i] = x[i]; } return zip.size(); } double solve(const vector<Rectangle> rectangles) { vector<double> xs; unordered_map<double, int> xZip; vector<double> xUnzip; vector<double> ys; unordered_map<double, int> yZip; vector<double> yUnzip; for (auto &r : rectangles) { xs.push_back(r.topLeft.x); xs.push_back(r.bottomRight.x); ys.push_back(r.topLeft.y); ys.push_back(r.bottomRight.y); } vector<vector<bool>> checker(ys.size(), vector<bool>(xs.size(), false)); compress(xs, xZip, xUnzip); compress(ys, yZip, yUnzip); for (auto &r : rectangles) { for (int y = yZip[r.topLeft.y]; y < yZip[r.bottomRight.y]; ++y) { for (int x = xZip[r.topLeft.x]; x < xZip[r.bottomRight.x]; ++x) { checker[y][x] = true; } } } double res = 0.0; for (int y = 0; y < yUnzip.size() - 1; ++y) for (int x = 0; x < xUnzip.size() - 1; ++x) if (checker[y][x]) { res += (yUnzip[y + 1] - yUnzip[y]) * (xUnzip[x + 1] - xUnzip[x]); } return res; } int main(void) { int cnt = 0; while (1) { ++cnt; vector<Rectangle> rectangles; int n; cin >> n; if (n == 0) break; for (int i = 0; i < n; ++i) { double x, y, r; cin >> x >> y >> r; rectangles.push_back(Rectangle(Point(x - r, y - r), Point(x + r, y + r))); } cout << fixed << setprecision(2) << cnt << " " << solve(rectangles) << endl; } }
[["-", 0, 56, 39, 344, 3, 347, 0, 77, 39, 40], ["+", 0, 56, 39, 344, 3, 347, 0, 77, 39, 40], ["-", 0, 43, 39, 344, 3, 347, 0, 77, 39, 40], ["+", 0, 43, 39, 344, 3, 347, 0, 77, 39, 40]]
1
674
import java.awt.geom.*; import java.util.*; public class Main { // 0033 cording start // 0047 cording end // 0118 sample matched private void doit() { Scanner sc = new Scanner(System.in); while (true) { int p = sc.nextInt(); int n = sc.nextInt(); if ((p | n) == 0) break; double sqrtP = Math.sqrt(p); double nearMax = Integer.MAX_VALUE; int maxA = -1, maxB = -1; double nearMin = 0.0; int minA = -1, minB = -1; for (int i = 1; i < n; i++) { double temp = i * sqrtP; double temp2 = Math.ceil(temp); double temp1 = Math.floor(temp); double temp1i = temp1 / i; // small double temp2i = temp2 / i; // big // System.out.println("sqrtP = " + sqrtP + " temp = " + temp); // System.out.println("temp1 = " + temp1 + " temp2= " + temp2); // System.out.println("temp1i = " + temp1i + " temp2i = " + temp2i); // System.out.println("1 = " + (sqrtP - temp1i) + " " + (temp2i - sqrtP) // ); if ((int)temp1 < n && temp1i > nearMin) { nearMin = temp1i; minA = (int)temp1; minB = i; } if ((int)temp2 < n && temp2i < nearMax) { nearMax = temp2i; maxA = (int)temp2; maxB = i; } } System.out.println(maxA + "/" + maxB + " " + minA + "/" + minB); } } public static void main(String[] args) { new Main().doit(); } }
import java.awt.geom.*; import java.util.*; public class Main { // 0033 cording start // 0047 cording end // 0118 sample matched // 0119 WA * 2 private void doit() { Scanner sc = new Scanner(System.in); while (true) { int p = sc.nextInt(); int n = sc.nextInt(); if ((p | n) == 0) break; double sqrtP = Math.sqrt(p); double nearMax = Integer.MAX_VALUE; int maxA = -1, maxB = -1; double nearMin = 0.0; int minA = -1, minB = -1; for (int i = 1; i <= n; i++) { double temp = i * sqrtP; double temp2 = Math.ceil(temp); double temp1 = Math.floor(temp); double temp1i = temp1 / i; // small double temp2i = temp2 / i; // big // System.out.println("sqrtP = " + sqrtP + " temp = " + temp); // System.out.println("temp1 = " + temp1 + " temp2= " + temp2); // System.out.println("temp1i = " + temp1i + " temp2i = " + temp2i); // System.out.println("1 = " + (sqrtP - temp1i) + " " + (temp2i - sqrtP) // ); if ((int)temp1 <= n && temp1i > nearMin) { nearMin = temp1i; minA = (int)temp1; minB = i; } if ((int)temp2 <= n && temp2i < nearMax) { nearMax = temp2i; maxA = (int)temp2; maxB = i; } } System.out.println(maxA + "/" + maxB + " " + minA + "/" + minB); } } public static void main(String[] args) { new Main().doit(); } }
[["-", 0, 52, 8, 196, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 196, 0, 7, 15, 16, 17, 19], ["-", 0, 57, 15, 15, 0, 16, 31, 16, 17, 18], ["+", 0, 57, 15, 15, 0, 16, 31, 16, 17, 19]]
3
294
#include <algorithm> #include <iostream> using namespace std; double mysqrt(double x) { double l = 0, r = x; for (int i = 0; i < 64; i++) { double m = (l + r) / 2.0; if (m * m < x) { l = m; } else { r = m; } } return l; } int main() { int p, n; while (cin >> p >> n, n) { double sp = mysqrt(p); int a1 = 0, b1 = 1, a2 = p, b2 = 1; for (int i = 1; i <= n; i++) { int l = 0, r = n; for (int x = 0; x < 50; x++) { int m = (l + r) / 2; if ((double)m / i < sp) { if (__gcd(m, i) == 1) { if (m * b1 > a1 * i) { a1 = m; b1 = i; } } l = m; } else { r = m - 1; } } l = 0, r = n; for (int x = 0; x < 50; x++) { int m = (l + r + 1) / 2; if ((double)m / i > sp) { if (__gcd(m, i) == 1) { if (m * b2 < a2 * i) { a2 = m; b2 = i; } } r = m; } else { l = m + 1; } } } cout << a2 << "/" << b2 << " " << a1 << "/" << b1 << endl; } }
#include <algorithm> #include <iostream> using namespace std; double mysqrt(double x) { double l = 0, r = x; for (int i = 0; i < 64; i++) { double m = (l + r) / 2.0; if (m * m < x) { l = m; } else { r = m; } } return l; } int main() { int p, n; while (cin >> p >> n, n) { double sp = mysqrt(p); int a1 = 0, b1 = 1, a2 = p, b2 = 1; for (int i = 1; i <= n; i++) { int l = 0, r = n; for (int x = 0; x < 50; x++) { int m = (l + r + 1) / 2; if ((double)m / i < sp) { if (__gcd(m, i) == 1) { if (m * b1 > a1 * i) { a1 = m; b1 = i; } } l = m; } else { r = m - 1; } } l = 0, r = n; for (int x = 0; x < 50; x++) { int m = (l + r) / 2; if ((double)m / i > sp) { if (__gcd(m, i) == 1) { if (m * b2 < a2 * i) { a2 = m; b2 = i; } } r = m; } else { l = m + 1; } } } cout << a2 << "/" << b2 << " " << a1 << "/" << b1 << endl; } }
[["+", 49, 50, 51, 16, 31, 23, 0, 16, 17, 72], ["+", 49, 50, 51, 16, 31, 23, 0, 16, 12, 13], ["-", 49, 50, 51, 16, 31, 23, 0, 16, 17, 72], ["-", 49, 50, 51, 16, 31, 23, 0, 16, 12, 13]]
1
355
#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 <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long Int; typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<string> vstring; typedef vector<pint> vpint; struct Edge { int to, from, cost; }; #ifdef DEBUGLOCAL #define debug cout #else stringstream __ss__; #define debug __ss__ #endif template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) debug << *i << " "; debug << 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; } #define rep(i, n) for (int i = 0; i < (n); ++i) #define repd(i, n) for (int i = (n)-1; i >= 0; i--) #define repn(i, m, n) for (int i = (m); i <= (n); ++i) #define repnd(i, m, n) for (int i = (n)-1; i >= (m); i--) #define rep0(i, n) for (i = 0; i < (n); ++i) #define all(n) n.begin(), n.end() #define sz(n) ((int)(n).size()) #define MP make_pair #define PB push_back #define SS stringstream #define X second #define Y first #define PUTLINE debug << "LINE:" << __LINE__ << endl; const int INF = 2147483647; const double EPS = 1e-10; const double PI = acos(-1.0); const int dx[] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1, 0}; int main() { pair<Int, Int> res0, res1; Int p, n; for (;;) { cin >> p >> n; if (p == 0 && n == 0) break; res0.first = 1; res0.second = n; res1.first = n; res1.second = 1; int a, b; repn(i, 1, n) { a = 1; b = n + 1; while (a < b - 1) { Int m = (a + b) / 2; if (m * m <= p * i * i) a = m; else b = m; } debug << a << endl; b = a + 1; if (res0.first * i < a * res0.second) { res0.first = a; res0.second = i; } if (b * b > p * i * i && res1.first * i > b * res1.second) { res1.first = b; res1.second = i; } } cout << res1.first << "/" << res1.second << " " << res0.first << "/" << res0.second << 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 <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long Int; typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<string> vstring; typedef vector<pint> vpint; struct Edge { int to, from, cost; }; #ifdef DEBUGLOCAL #define debug cout #else stringstream __ss__; #define debug __ss__ #endif template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) debug << *i << " "; debug << 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; } #define rep(i, n) for (int i = 0; i < (n); ++i) #define repd(i, n) for (int i = (n)-1; i >= 0; i--) #define repn(i, m, n) for (int i = (m); i <= (n); ++i) #define repnd(i, m, n) for (int i = (n)-1; i >= (m); i--) #define rep0(i, n) for (i = 0; i < (n); ++i) #define all(n) n.begin(), n.end() #define sz(n) ((int)(n).size()) #define MP make_pair #define PB push_back #define SS stringstream #define X second #define Y first #define PUTLINE debug << "LINE:" << __LINE__ << endl; const int INF = 2147483647; const double EPS = 1e-10; const double PI = acos(-1.0); const int dx[] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1, 0}; int main() { pair<Int, Int> res0, res1; Int p, n; for (;;) { cin >> p >> n; if (p == 0 && n == 0) break; res0.first = 1; res0.second = n; res1.first = n; res1.second = 1; int a, b; repn(i, 1, n) { a = 1; b = n + 1; while (a < b - 1) { Int m = (a + b) / 2; if (m * m <= p * i * i) a = m; else b = m; } debug << a << endl; b = a + 1; if (res0.first * i < a * res0.second) { res0.first = a; res0.second = i; } if (b <= n && b * b > p * i * i && res1.first * i > b * res1.second) { res1.first = b; res1.second = i; } } cout << res1.first << "/" << res1.second << " " << res0.first << "/" << res0.second << endl; } return 0; }
[["+", 15, 339, 51, 16, 31, 16, 31, 16, 17, 19], ["+", 15, 339, 51, 16, 31, 16, 31, 16, 12, 22], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 98], ["+", 51, 16, 31, 16, 12, 16, 31, 16, 31, 22]]
1
617
#include <cassert> #include <cmath> #include <iostream> #include <set> #include <vector> using namespace std; const double eps = 1e-11; bool eq(double a, double b) { return abs(b - a) < eps; } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } const double inf = 1e30; int main() { int p, n; while (cin >> p >> n) { if (p == 0 && n == 0) break; double rp = sqrt(p); double min_diff_lower = inf; double min_diff_upper = inf; int ans_lower_p, ans_lower_q, ans_upper_p, ans_upper_q; for (int i = 1; i <= n; ++i) { int r = n; int l = 1; int cnt = 0; int m; while (cnt++ < 20) { m = (r + l) / 2; if ((double)m / i > rp) { r = m; } else { l = m; } } ++m; if ((double)m / i <= rp) --m; assert(rp - (double)m / i > 0); if (min_diff_lower > rp - (double)m / i) { min_diff_lower = rp - (double)m / i; ans_lower_p = m; ans_lower_q = i; } for (int j = m + 1; j <= n; ++j) { if ((double)j / i > rp) { m = j; break; } } if (((double)m / i - rp) > 0 && min_diff_upper > ((double)m / i - rp)) { min_diff_upper = (double)m / i - rp; ans_upper_p = m; ans_upper_q = i; } } int lower_gcd = gcd(ans_lower_p, ans_lower_q); int upper_gcd = gcd(ans_upper_p, ans_upper_q); ans_lower_p /= lower_gcd; ans_lower_q /= lower_gcd; ans_upper_p /= upper_gcd; ans_upper_q /= upper_gcd; cout << ans_upper_p << "/" << ans_upper_q << " " << ans_lower_p << "/" << ans_lower_q << endl; } return 0; }
#include <cassert> #include <cmath> #include <iostream> #include <set> #include <vector> using namespace std; const double eps = 1e-11; bool eq(double a, double b) { return abs(b - a) < eps; } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } const double inf = 1e30; int main() { int p, n; while (cin >> p >> n) { if (p == 0 && n == 0) break; double rp = sqrt(p); double min_diff_lower = inf; double min_diff_upper = inf; int ans_lower_p, ans_lower_q, ans_upper_p, ans_upper_q; for (int i = 1; i <= n; ++i) { int r = n; int l = 1; int cnt = 0; int m; while (cnt++ < 20) { m = (r + l) / 2; if ((double)m / i > rp) { r = m; } else { l = m; } } ++m; if (rp - (double)m / i < 0) --m; assert(rp - (double)m / i > 0); if (min_diff_lower > rp - (double)m / i) { min_diff_lower = rp - (double)m / i; ans_lower_p = m; ans_lower_q = i; } for (int j = m + 1; j <= n; ++j) { if ((double)j / i > rp) { m = j; break; } } if (((double)m / i - rp) > 0 && min_diff_upper > ((double)m / i - rp)) { min_diff_upper = (double)m / i - rp; ans_upper_p = m; ans_upper_q = i; } } int lower_gcd = gcd(ans_lower_p, ans_lower_q); int upper_gcd = gcd(ans_upper_p, ans_upper_q); ans_lower_p /= lower_gcd; ans_lower_q /= lower_gcd; ans_upper_p /= upper_gcd; ans_upper_q /= upper_gcd; cout << ans_upper_p << "/" << ans_upper_q << " " << ans_lower_p << "/" << ans_lower_q << endl; } return 0; }
[["+", 0, 57, 15, 339, 51, 16, 31, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 33], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 22], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
425
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; #define kane 17 const int m[kane] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 111, 144, 169, 196, 225, 256, 289}; int cnt = 0; int search(int num) { int maxx; if (num == 0 || num == 1) { return 0; } for (maxx = 0; num - m[maxx + 1] >= 0; maxx++) ; return maxx; } void func(int num, int k) { // cout<<num<<":"<<m[k]<<endl; if (num <= 3 || k == 0) { cnt++; // cout<<"☆"<<endl; return; } for (; k >= 0; k--) func(num - m[k], min(k, search(num - m[k]))); } int main() { while (1) { int num; cnt = 0; cin >> num; if (num == 0) break; func(num, search(num)); cout << cnt << endl; // cout<<search(num)<<endl; } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; #define kane 17 const int m[kane] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289}; int cnt = 0; int search(int num) { int maxx; if (num == 0 || num == 1) { return 0; } for (maxx = 0; num - m[maxx + 1] >= 0; maxx++) ; return maxx; } void func(int num, int k) { // cout<<num<<":"<<m[k]<<endl; if (num <= 3 || k == 0) { cnt++; // cout<<"☆"<<endl; return; } for (; k >= 0; k--) func(num - m[k], min(k, search(num - m[k]))); } int main() { while (1) { int num; cnt = 0; cin >> num; if (num == 0) break; func(num, search(num)); cout << cnt << endl; // cout<<search(num)<<endl; } return 0; }
[["-", 0, 30, 0, 43, 49, 50, 51, 83, 0, 13], ["+", 0, 30, 0, 43, 49, 50, 51, 83, 0, 13]]
1
227
#include <stdio.h> int n, dp[301][18]; int main() { while (~scanf("%d", &n), n) { for (int i = 0; i <= n; i++) { for (int j = 0; j <= 17; j++) dp[i][j] = 0; } dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j * j <= n; j++) { for (int k = 0; k <= j; k++) dp[i][j] += dp[i - j * j][k]; } } long long ret = 0; for (int i = 0; i <= 17; i++) ret += dp[n][i]; printf("%lld\n", ret); } return 0; }
#include <stdio.h> int n; long long dp[301][18]; int main() { while (~scanf("%d", &n), n) { for (int i = 0; i <= n; i++) { for (int j = 0; j <= 17; j++) dp[i][j] = 0; } dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j * j <= i; j++) { for (int k = 0; k <= j; k++) dp[i][j] += dp[i - j * j][k]; } } long long ret = 0; for (int i = 0; i <= 17; i++) ret += dp[n][i]; printf("%lld\n", ret); } return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 43, 0, 21], ["+", 36, 36, 36, 36, 0, 30, 0, 43, 0, 35], ["+", 36, 36, 0, 30, 0, 43, 39, 86, 0, 96], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22]]
1
197
#include <bits/stdc++.h> using namespace std; #define pb(n) push_back(n) #define fi first #define se second #define all(r) (r).begin(), (r).end() #define vmax(ary) *max_element(all(ary)) #define vmin(ary) *min_element(all(ary)) #define debug(x) cout << #x << ": " << x << endl #define fcout(n) cout << fixed << setprecision((n)) #define scout(n) cout << setw(n) #define vary(type, name, size, init) vector<type> name(size, init) #define vvl(v, w, h, init) vector<vector<ll>> v(w, vector<ll>(h, init)) #define mp(a, b) make_pair(a, b) #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define REP(i, a, b) for (int i = (a); i < (int)(b); ++i) #define repi(it, array) \ for (auto it = array.begin(), end = array.end(); it != end; ++it) #define repa(n, array) for (auto &n : (array)) using ll = long long; using vi = vector<int>; using vl = vector<ll>; using dict = map<string, int>; using pii = pair<int, int>; using pll = pair<ll, ll>; const int mod = 1000000007; constexpr int inf = ((1 << 30) - 1) * 2 + 1; constexpr double PI = acos(-1.0); double eps = 1e-10; vector<vector<ll>> dp(1000, vector<ll>(20, 0)); vary(int, v, 0, 0); int tar; int DP(int i, int k) { if (k == tar) { return 1; } if (k > tar) return 0; int cnt = 0; dp[i][k] = 1; REP(j, i, 18) { cnt += DP(j, k + v[j]); } return cnt; } int main() { cin.tie(0); ios::sync_with_stdio(false); v.clear(); REP(i, 0, 18) { v.pb(i * i); } int n; while (cin >> n && n) { tar = n; dp = vector<vector<ll>>(1000, vector<ll>(40, 0)); cout << DP(1, 0) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define pb(n) push_back(n) #define fi first #define se second #define all(r) (r).begin(), (r).end() #define vmax(ary) *max_element(all(ary)) #define vmin(ary) *min_element(all(ary)) #define debug(x) cout << #x << ": " << x << endl #define fcout(n) cout << fixed << setprecision((n)) #define scout(n) cout << setw(n) #define vary(type, name, size, init) vector<type> name(size, init) #define vvl(v, w, h, init) vector<vector<ll>> v(w, vector<ll>(h, init)) #define mp(a, b) make_pair(a, b) #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define REP(i, a, b) for (int i = (a); i < (int)(b); ++i) #define repi(it, array) \ for (auto it = array.begin(), end = array.end(); it != end; ++it) #define repa(n, array) for (auto &n : (array)) using ll = long long; using vi = vector<int>; using vl = vector<ll>; using dict = map<string, int>; using pii = pair<int, int>; using pll = pair<ll, ll>; const int mod = 1000000007; constexpr int inf = ((1 << 30) - 1) * 2 + 1; constexpr double PI = acos(-1.0); double eps = 1e-10; vector<vector<ll>> dp(1000, vector<ll>(20, 0)); vary(int, v, 0, 0); int tar; int DP(int i, int k) { if (k == tar) { return 1; } if (k > tar) return 0; int cnt = 0; dp[i][k] = 1; REP(j, i, 18) { cnt += DP(j, k + v[j]); } return cnt; } int main() { cin.tie(0); ios::sync_with_stdio(false); v.clear(); REP(i, 0, 18) { v.pb(i * i); } int n; while (cin >> n && n) { tar = n; dp = vector<vector<ll>>(1000, vector<ll>(1000, 0)); cout << DP(1, 0) << endl; } return 0; }
[["-", 12, 2, 3, 4, 0, 2, 3, 4, 0, 13], ["+", 12, 2, 3, 4, 0, 2, 3, 4, 0, 13]]
1
422
#include <iostream> using namespace std; #define KMAX 17 #define VMAX 300 static const int K[17] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289}; int T[KMAX][VMAX + 1]; main() { for (int j = 0; j <= VMAX; j++) T[0][j] = 1; for (int i = 0; i < KMAX; i++) T[i][0] = 1; for (int i = 1; i < KMAX; i++) { for (int j = 1; j < K[i]; j++) T[i][j] = T[i - 1][j]; for (int j = K[i]; j <= VMAX; j++) { T[i][j] = T[i - 1][j] + T[i][j - K[i]]; } } int n; while (cin >> n && n) cout << T[KMAX - 1][n] << "hoge" << endl; }
#include <iostream> using namespace std; #define KMAX 17 #define VMAX 300 static const int K[17] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289}; int T[KMAX][VMAX + 1]; main() { for (int j = 0; j <= VMAX; j++) T[0][j] = 1; for (int i = 0; i < KMAX; i++) T[i][0] = 1; for (int i = 1; i < KMAX; i++) { for (int j = 1; j < K[i]; j++) T[i][j] = T[i - 1][j]; for (int j = K[i]; j <= VMAX; j++) { T[i][j] = T[i - 1][j] + T[i][j - K[i]]; } } int n; while (cin >> n && n) cout << T[KMAX - 1][n] << endl; }
[["-", 8, 1, 0, 16, 31, 16, 12, 5, 0, 62], ["-", 8, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["-", 8, 9, 0, 52, 8, 1, 0, 16, 17, 151]]
1
250
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; int dp[301][18]; int main() { int n; while (cin >> n, n) { for (int i = 0; i <= n; i++) dp[i][1] = dp[i][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 2; j <= 18; j++) { dp[i][j] = 0; int k; for (k = 0; i - k * j * j >= 0; k++) { dp[i][j] += dp[i - k * j * j][j - 1]; } } } cout << dp[n][18] << endl; } }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; int dp[301][18]; int main() { int n; while (cin >> n, n) { for (int i = 0; i <= n; i++) dp[i][1] = dp[i][0] = 1; for (int i = 0; i <= n; i++) { for (int j = 2; j <= 18; j++) { dp[i][j] = 0; int k; for (k = 0; i - k * j * j >= 0; k++) { dp[i][j] += dp[i - k * j * j][j - 1]; } } } cout << dp[n][18] << endl; } }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
180
#include <iostream> #include <vector> using namespace std; int dp[301]; void solve() { vector<int> square; for (int i = 1; i * i <= 300; ++i) { square.push_back(i * i); } dp[0] = 1; for (int i = 0; i < square.size(); ++i) { for (int ii = square[i]; ii <= 300; ++ii) { dp[ii] += dp[ii - square[i]]; } } int n; while (cin >> n) { cout << dp[n] << endl; } } int main() { solve(); return (0); }
#include <iostream> #include <vector> using namespace std; int dp[301]; void solve() { vector<int> square; for (int i = 1; i * i <= 300; ++i) { square.push_back(i * i); } dp[0] = 1; for (int i = 0; i < square.size(); ++i) { for (int ii = square[i]; ii <= 300; ++ii) { dp[ii] += dp[ii - square[i]]; } } int n; while (cin >> n, n) { cout << dp[n] << endl; } } int main() { solve(); return (0); }
[["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 12, 22]]
1
149
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #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; static const double EPS = 1e-10; typedef long long ll; typedef pair<int, int> PI; #define rep(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define mp(a, b) make_pair(a, b) #define pb(a) push_back(a) int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; typedef struct { int t, b, n, w, s, e; } die; die rot; main() { int n; while (cin >> n) { if (n == 0) break; // cout<<n<<endl; rot.t = 1; rot, n = 2; rot.w = 3; rot.b = 6; rot.s = 5; rot.e = 4; int k; rep(i, n) { string in; cin >> in; switch (in[0]) { case 'n': k = rot.t; rot.t = rot.s; rot.s = rot.b; rot.b = rot.n; rot.n = k; break; case 's': k = rot.t; rot.t = rot.n; rot.n = rot.b; rot.b = rot.s; rot.s = k; break; case 'w': k = rot.t; rot.t = rot.e; rot.e = rot.b; rot.b = rot.w; rot.w = k; break; case 'e': k = rot.t; rot.t = rot.w; rot.w = rot.b; rot.b = rot.e; rot.e = k; break; } // cout<<rot.t<<endl; } cout << rot.t << endl; } }
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #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; static const double EPS = 1e-10; typedef long long ll; typedef pair<int, int> PI; #define rep(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define mp(a, b) make_pair(a, b) #define pb(a) push_back(a) int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; typedef struct { int t, b, n, w, s, e; } die; die rot; main() { int n; while (cin >> n) { if (n == 0) break; rot.t = 1; rot.n = 2; rot.w = 3; rot.b = 6; rot.s = 5; rot.e = 4; int k; while (n--) { string in; cin >> in; switch (in[0]) { case 'n': k = rot.t; rot.t = rot.s; rot.s = rot.b; rot.b = rot.n; rot.n = k; break; case 's': k = rot.t; rot.t = rot.n; rot.n = rot.b; rot.b = rot.s; rot.s = k; break; case 'w': k = rot.t; rot.t = rot.e; rot.e = rot.b; rot.b = rot.w; rot.w = k; break; case 'e': k = rot.t; rot.t = rot.w; rot.w = rot.b; rot.b = rot.e; rot.e = k; break; } } cout << rot.t << endl; } }
[["-", 0, 52, 8, 9, 0, 1, 0, 34, 0, 21], ["+", 8, 9, 0, 1, 0, 11, 31, 118, 17, 131], ["-", 0, 52, 8, 9, 0, 14, 49, 53, 49, 22], ["+", 8, 9, 0, 52, 8, 9, 0, 52, 0, 89], ["-", 0, 14, 49, 53, 54, 55, 0, 56, 39, 78], ["-", 8, 9, 0, 14, 49, 53, 54, 55, 0, 21], ["+", 8, 9, 0, 52, 15, 339, 51, 27, 17, 68]]
1
422
#include <complex> #include <iostream> #include <vector> using namespace std; const double EPS = 1e-7; typedef complex<double> P; typedef vector<P> G; double cross(P a, P b) { return imag(conj(a) * b); } // 多角形gの面積を求める double polygonArea(G g) { double res = 0.0; for (int i = 0; i < g.size(); i++) res += cross(g[i], g[(i + 1) % g.size()]); return abs(0.5 * res); } // 凸多角形gを,2点p, qを結ぶ線分で切断する G convexCut(G g, P p, P q) { G res; int n = g.size(); for (int i = 0; i < n; i++) { P A(g[i]), B(g[(i + 1) % n]); double p1 = cross(q - p, A - p); double p2 = cross(q - p, B - p); if (p1 > -EPS) res.push_back(A); if (p1 * p2 < -EPS) res.push_back(A + cross(q - p, q - A) / cross(q - p, B - A) * (B - A)); } return res; } int main() { int x[3], y[3]; for (int test = 1;; test++) { for (int i = 0; i < 3; i++) cin >> x[i] >> y[i]; if (x[0]) break; G g; g.push_back(P(0, 0)); g.push_back(P(10000, 0)); g.push_back(P(10000, 10000)); g.push_back(P(0, 10000)); for (int i = 1; i < 3; i++) { P a = P(x[0], y[0]); P b = P(x[i], y[i]); P mid = 0.5 * (a + b); P dir = mid + P(0, 1) * (b - a); g = convexCut(g, mid, dir); } printf("%d %.10lf\n", test, 1e-8 * polygonArea(g)); } }
#include <complex> #include <iostream> #include <vector> using namespace std; const double EPS = 1e-7; typedef complex<double> P; typedef vector<P> G; double cross(P a, P b) { return imag(conj(a) * b); } // 多角形gの面積を求める double polygonArea(G g) { double res = 0.0; for (int i = 0; i < g.size(); i++) res += cross(g[i], g[(i + 1) % g.size()]); return abs(0.5 * res); } // 凸多角形gを,2点p, qを結ぶ線分で切断する G convexCut(G g, P p, P q) { G res; int n = g.size(); for (int i = 0; i < n; i++) { P A(g[i]), B(g[(i + 1) % n]); double p1 = cross(q - p, A - p); double p2 = cross(q - p, B - p); if (p1 > -EPS) res.push_back(A); if (p1 * p2 < -EPS) res.push_back(A + cross(q - p, q - A) / cross(q - p, B - A) * (B - A)); } return res; } int main() { int x[3], y[3]; for (int test = 1;; test++) { for (int i = 0; i < 3; i++) cin >> x[i] >> y[i]; if (x[0] == 0) break; G g; g.push_back(P(0, 0)); g.push_back(P(10000, 0)); g.push_back(P(10000, 10000)); g.push_back(P(0, 10000)); for (int i = 1; i < 3; i++) { P a = P(x[0], y[0]); P b = P(x[i], y[i]); P mid = 0.5 * (a + b); P dir = mid + P(0, 1) * (b - a); g = convexCut(g, mid, dir); } printf("%d %.10lf\n", test, 1e-8 * polygonArea(g)); } }
[["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
1
493
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; #define EPS (1e-10) #define EQ(a, b) (abs((a) - (b)) < EPS) #define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag())) typedef complex<double> P; typedef pair<P, P> Edge; typedef long long ll; const double PI = 4 * atan(1.0); const int MAX_SIZE = 10000; // 内積 double dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); } // 外積 double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); } // 点cが直線ab上にあるかないか int is_point_on_line(P a, P b, P c) { return EQ(cross(b - a, c - a), 0.0); } // 2直線の直行判定 int is_orthogonal(P a1, P a2, P b1, P b2) { return EQ(dot(a1 - a2, b1 - b2), 0.0); } // 2直線の平行判定 int is_parallel(P a1, P a2, P b1, P b2) { return EQ(cross(a1 - a2, b1 - b2), 0.0); } // 点a,bを通る直線と点cの間の距離 double distance_l_p(P a, P b, P c) { return abs(cross(b - a, c - a)) / abs(b - a); } // 点a,bを端点とする線分と点cとの距離 double distance_ls_p(P a, P b, P c) { if (dot(b - a, c - a) < EPS) return abs(c - a); if (dot(a - b, c - b) < EPS) return abs(c - b); return abs(cross(b - a, c - a)) / abs(b - a); } // 点が多角形の中に存在するかどうか bool isInPolygon(vector<P> &points, P s) { if (points.size() == 1) return false; vector<P> v; for (int i = 0; i < (int)points.size(); i++) v.push_back(points[i] - s); int sign = 0; for (int i = 1; i <= (int)v.size(); i++) { int prv = (i - 1 + v.size()) % v.size(); int cur = i % v.size(); double c = cross(v[prv], v[cur]); if (EQ(c, 0)) continue; else if (sign == 0) { if (c > 0) sign = 1; else sign = -1; } else { if (sign == -1 && c > 0) return false; else if (sign == 1 && c < 0) return false; } } return true; } struct Rec { vector<P> p; }; // a1,a2を端点とする線分とb1,b2を端点とする線分の交差判定 int is_intersected_ls(P a1, P a2, P b1, P b2) { // 線分が平行な場合は重なっていないことにする if (abs(cross(a2 - a1, b2 - b1)) < EPS) { return 0; } return (cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < EPS) && (cross(b2 - b1, a1 - b1) * cross(b2 - b1, a2 - b1) < EPS); } // aとbの矩形が接しているかどうか(どちらかがどちからを含んでいるものもふくむ bool isTouchRectangle(Rec &a, Rec &b) { // aの各頂点がbの中に存在しているかチェック for (int i = 0; i < 4; i++) if (isInPolygon(b.p, a.p[i])) return true; // 逆 for (int i = 0; i < 4; i++) if (isInPolygon(a.p, b.p[i])) return true; // 各辺が他方のどれかの辺と接しているか for (int i = 0; i < 4; i++) { int curi = i; int prvi = (i - 1 + 4) % 4; for (int j = 0; j < 4; j++) { int curj = j; int prvj = (j - 1 + 4) % 4; if (is_intersected_ls(a.p[curi], a.p[prvi], b.p[curj], b.p[prvj])) return true; } } return false; } // a1,a2を端点とする線分とb1,b2を端点とする線分の交点計算 P intersection_ls(P a1, P a2, P b1, P b2) { P b = b2 - b1; double d1 = abs(cross(b, a1 - b1)); double d2 = abs(cross(b, a2 - b1)); double t = d1 / (d1 + d2); return a1 + (a2 - a1) * t; } // a1,a2を通る直線とb1,b2を通る直線の交差判定 int is_intersected_l(P a1, P a2, P b1, P b2) { return !EQ(cross(a1 - a2, b1 - b2), 0.0); } // a1,a2を通る直線とb1,b2を通る直線の交点計算 P intersection_l(P a1, P a2, P b1, P b2) { P a = a2 - a1; P b = b2 - b1; return a1 + a * cross(b, b1 - a1) / cross(b, a); } // 単位ベクトルを求める P normalVector(P p) { return p / abs(p); } // 法線ベクトルを求める P unitVector(P a) { return P(-a.imag(), a.real()); } // 単位法線ベクトルを求める P unitNormalVector(P tmp) { P e = P(-tmp.imag(), tmp.real()); e /= abs(e); return e; } // 座標の回転(座標pにある点を,半時計回りにa(ラジアン)回転) P roundPoint(P p, double a) { return P(cos(a) * p.real() - sin(a) * p.imag(), sin(a) * p.real() + cos(a) * p.imag()); } /* 円周と線分の交差判定 */ bool isCircleCrossLine(P a, P b, P c, double r) { double d1 = abs(a - c); double d2 = abs(b - c); // 線分が中に含まれるとき、ここのコメントアウトをはずせばtrue(交差)となる // if(d1<r&&d2<r) // return true; double d = distance_ls_p(a, b, c); return (EQ(d, r) || d < r); } // 三角形の内部に点があるかどうか // 外積の正負がすべて同じなら内部に点あり bool isInTriangle(P p1, P p2, P p3, P s) { P a = p1 - s; P b = p2 - s; P c = p3 - s; return ((cross(a, b) > 0 && cross(b, c) > 0 && cross(c, a) > 0) || (cross(a, b) < 0 && cross(b, c) < 0 && cross(c, a) < 0)); } // 矩形の中に点が存在するかどうか bool isInRectangle(P p1, P p2, P p3, P p4, P s) { P a = p1 - s; P b = p2 - s; P c = p3 - s; P d = p4 - s; return ((cross(a, b) > 0 && cross(b, c) > 0 && cross(c, d) > 0 && cross(d, a) > 0) || (cross(a, b) < 0 && cross(b, c) < 0 && cross(c, d) < 0 && cross(d, a) < 0)); } // 三角形の面積を座標から計算 double calcAreaOfTriangle(P a, P b, P c) { return abs((b.real() - a.real()) * (c.imag() - a.imag()) - (c.real() - a.real()) * (b.imag() - a.imag())) / 2; } // 与えられた円の範囲内に点が存在するかどうか bool isContainingDot(P c, double r, P a) { return (((c.real() - a.real()) * (c.real() - a.real()) + (c.imag() - a.imag()) * (c.imag() - a.imag()) < r * r) || EQ((c.real() - a.real()) * (c.real() - a.real()) + (c.imag() - a.imag()) * (c.imag() - a.imag()), r * r)); } // 多角形の面積公式 double calcPolygonArea(vector<P> p) { double sum = 0; for (int i = 0; i < p.size(); i++) sum += cross(p[i], p[(i + 1) % (p.size())]); return sum / 2; } // 2ベクトル間の角度 // aからbへ左周りで何度か(0->2*PI) double diffAngle(P a, P b) { double angle = atan2(cross(a, b), dot(a, b)); if (angle < 0) return 2 * PI + angle; return angle; } // 2つのベクトルの重なっている部分の長さを返す // もし重なっていなければ0を返す double multipleLength(P a, P b, P c, P d) { Edge e1 = make_pair(a, b); Edge e2 = make_pair(c, d); // 平行であるかどうか if (!(is_parallel(e1.first, e2.first, e1.second, e2.second) && is_parallel(e1.first, e1.second, e2.first, e2.second))) return 0; double dist = 0; // 両方乗っている if (EQ(distance_ls_p(e1.first, e1.second, e2.first), 0) && EQ(distance_ls_p(e1.first, e1.second, e2.second), 0)) dist = abs(e2.first - e2.second); else if (EQ(distance_ls_p(e2.first, e2.second, e1.first), 0) && EQ(distance_ls_p(e2.first, e2.second, e1.second), 0)) dist = abs(e1.first - e1.second); else if (EQ(distance_ls_p(e1.first, e1.second, e2.first), 0)) { // どちらが線上にあるか if (EQ(distance_ls_p(e2.first, e2.second, e1.first), 0)) dist = abs(e1.first - e2.first); else dist = abs(e1.second - e2.first); } else if (EQ(distance_ls_p(e1.first, e1.second, e2.second), 0)) { if (EQ(distance_ls_p(e2.first, e2.second, e1.first), 0)) dist = abs(e1.first - e2.second); else dist = abs(e1.second - e2.second); } return dist; } // 2点を通る半径rの円の中点を求める pair<P, P> calcCircleCenterPoint(P dot1, P dot2, double r) { double v = abs(dot1 - dot2); double x = sqrt(r * r - (v / 2) * (v / 2)); // 二組の単位法線ベクトル P hose[2]; double x1 = -(dot1.imag() - dot2.imag()); double y1 = (dot1.real() - dot2.real()); hose[0] = P(x1, y1); double y2 = -y1; double x2 = -x1; hose[1] = P(x2, y2); double dd1 = abs(hose[0]); double dd2 = abs(hose[1]); hose[0] /= dd1; hose[0] *= x; hose[1] /= dd2; hose[1] *= x; P tmp = (dot1 + dot2); tmp /= 2; hose[0] += tmp; hose[1] += tmp; return make_pair(hose[0], hose[1]); } // 4線分が端点以外でクロスしているかどうかを判定 bool checkCross(const vector<Edge> &vec) { for (int i = 0; i < vec.size(); i++) { for (int j = i + 1; j < vec.size(); j++) { // 線分がクロスしているかどうか if (is_intersected_ls(vec[i].first, vec[i].second, vec[j].first, vec[j].second)) { // している場合、端点以外でクロスしているなら、false P p = intersection_ls(vec[i].first, vec[i].second, vec[j].first, vec[j].second); if (!(EQ(p, vec[i].first) || EQ(p, vec[i].second))) return false; } } } return true; } // 引き数で与えられた4つの点が矩形を作るどうかを判定 bool checkRec(const vector<P> &squ) { vector<int> v; for (int i = 0; i < squ.size(); i++) v.push_back(i); do { vector<Edge> ve; for (int i = 0; i < 4; i++) ve.push_back(make_pair(squ[v[i]], squ[v[(i + 1) % 4]])); // ここですべての線分がクロスするかどうかを計算 if (checkCross(ve)) { // 線分の長さを確認 if (EQ(abs(ve[0].first - ve[0].second), abs(ve[2].first - ve[2].second))) if (EQ(abs(ve[1].first - ve[1].second), abs(ve[3].first - ve[3].second))) // 直行 if (is_orthogonal(ve[0].first, ve[0].second, ve[1].first, ve[1].second)) if (is_orthogonal(ve[2].first, ve[2].second, ve[3].first, ve[3].second)) return true; } } while (next_permutation(v.begin(), v.end())); return false; } // 引き数で与えられた4つの点が正方形を作るかどうかを判定 bool checkSqu(const vector<P> &squ) { vector<int> v; for (int i = 0; i < squ.size(); i++) v.push_back(i); do { vector<Edge> ve; for (int i = 0; i < 4; i++) ve.push_back(make_pair(squ[v[i]], squ[v[(i + 1) % 4]])); // ここですべての線分がクロスするかどうかを計算 if (checkCross(ve)) { // 線分の長さを確認 double d = abs(ve[0].first - ve[0].second); for (int i = 1; i < 4; i++) { double d2 = abs(ve[i].first - ve[i].second); if (!EQ(d, d2)) return false; } // 直行確認 if (is_orthogonal(ve[0].first, ve[0].second, ve[1].first, ve[1].second)) if (is_orthogonal(ve[2].first, ve[2].second, ve[3].first, ve[3].second)) return true; } } while (next_permutation(v.begin(), v.end())); return false; } // 二円の共通接線を構成する4直線を求める(二円はお互い接したりどちらかをふくんだりしない) vector<pair<P, P>> calcCommonTangentialLine(P p1, double r1, P p2, double r2) { vector<pair<P, P>> res; if (r1 > r2) { swap(r1, r2); swap(p1, p2); } // 共通外接線 { double d = abs(p1 - p2); double a = abs(r1 - r2); double b = sqrt(d * d - a * a); double sita = acos(b / d); P e = normalVector(roundPoint((roundPoint((p2 - p1), sita)), PI / 2)); res.push_back(make_pair(e * r1 + p1, e * r2 + p2)); P e2 = normalVector(roundPoint((roundPoint((p2 - p1), -sita)), -PI / 2)); res.push_back(make_pair(e2 * r1 + p1, e2 * r2 + p2)); } // 共通内接線 { P cp = (r1 * p1 + r2 * p2) / (r1 + r2); double u = abs(cp - p1); double sita = acos(r1 / u); P e = normalVector(roundPoint(cp - p1, sita)); res.push_back(make_pair(e * r1 + p1, -e * r2 + p2)); P e2 = normalVector(roundPoint(cp - p1, -sita)); res.push_back(make_pair(e2 * r1 + p1, -e2 * r2 + p2)); } return res; } /* 与えられた3点a,b,c,をa->b->cと進むとき a->bで時計方向に折れて,b->c a->bで半時計方向に折れて,b->c a->bで逆を向いてaを通り越してb->c a->bでそのままb->c a->bで逆を向いてb->c のどれであるかを判定 */ int ccw(P a, P b, P c) { b -= a; c -= a; if (cross(b, c) > 0 && !EQ(cross(b, c), 0)) return 1; // counter clockwise if (cross(b, c) < 0 && !EQ(cross(b, c), 0)) return -1; // clockwise if (dot(b, c) < 0 && !EQ(dot(b, c), 0)) return 2; // c-a-b on line if (!EQ(abs(b), abs(c)) && abs(b) < abs(c)) return -2; // a-b-c on line return 0; } // 凸多角形の切断を行う // lineのfirst側から見て左の多角形が返る // 多角形は上が+y,右が+xの座標系で座標を時計周りに格納したvectorで表す vector<P> convex_cut(vector<P> polygon, pair<P, P> line) { vector<P> resPolygon; int n = polygon.size(); for (int i = 0; i < n; i++) { P A = polygon[i]; P B = polygon[(i + 1) % n]; if (ccw(line.first, line.second, A) != -1) resPolygon.push_back(A); if (ccw(line.first, line.second, A) * ccw(line.first, line.second, B) < 0) resPolygon.push_back(intersection_ls(A, B, line.first, line.second)); } return resPolygon; } P ps[3]; int xs[3]; int ys[3]; pair<P, P> makeDivisor(P a, P b) { P mid = (a + b); mid /= 2; P base = unitNormalVector(b - a); P p1 = base *= 1000000; p1 += mid; P p2 = base *= -1000000; p2 += mid; return make_pair(p1, p2); } int main() { int cnt = 1; while (cin >> xs[0] >> ys[0] >> xs[1] >> ys[1] >> xs[2] >> ys[2] && (xs[0] | xs[1] | xs[2] | ys[0] | ys[1] | ys[2])) { for (int i = 0; i < 3; i++) ps[i] = P(xs[i], ys[i]); vector<P> init; init.push_back(P(0, 0)); init.push_back(P(0, 10000)); init.push_back(P(10000, 10000)); init.push_back(P(10000, 0)); init = convex_cut(init, makeDivisor(ps[0], ps[1])); init = convex_cut(init, makeDivisor(ps[0], ps[2])); double S = abs(calcPolygonArea(init)); printf("%d %.10f\n", cnt, S / 100000000); cnt++; } return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; #define EPS (1e-10) #define EQ(a, b) (abs((a) - (b)) < EPS) #define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag())) typedef complex<double> P; typedef pair<P, P> Edge; typedef long long ll; const double PI = 4 * atan(1.0); const int MAX_SIZE = 10000; // 内積 double dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); } // 外積 double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); } // 点cが直線ab上にあるかないか int is_point_on_line(P a, P b, P c) { return EQ(cross(b - a, c - a), 0.0); } // 2直線の直行判定 int is_orthogonal(P a1, P a2, P b1, P b2) { return EQ(dot(a1 - a2, b1 - b2), 0.0); } // 2直線の平行判定 int is_parallel(P a1, P a2, P b1, P b2) { return EQ(cross(a1 - a2, b1 - b2), 0.0); } // 点a,bを通る直線と点cの間の距離 double distance_l_p(P a, P b, P c) { return abs(cross(b - a, c - a)) / abs(b - a); } // 点a,bを端点とする線分と点cとの距離 double distance_ls_p(P a, P b, P c) { if (dot(b - a, c - a) < EPS) return abs(c - a); if (dot(a - b, c - b) < EPS) return abs(c - b); return abs(cross(b - a, c - a)) / abs(b - a); } // 点が多角形の中に存在するかどうか bool isInPolygon(vector<P> &points, P s) { if (points.size() == 1) return false; vector<P> v; for (int i = 0; i < (int)points.size(); i++) v.push_back(points[i] - s); int sign = 0; for (int i = 1; i <= (int)v.size(); i++) { int prv = (i - 1 + v.size()) % v.size(); int cur = i % v.size(); double c = cross(v[prv], v[cur]); if (EQ(c, 0)) continue; else if (sign == 0) { if (c > 0) sign = 1; else sign = -1; } else { if (sign == -1 && c > 0) return false; else if (sign == 1 && c < 0) return false; } } return true; } struct Rec { vector<P> p; }; // a1,a2を端点とする線分とb1,b2を端点とする線分の交差判定 int is_intersected_ls(P a1, P a2, P b1, P b2) { // 線分が平行な場合は重なっていないことにする if (abs(cross(a2 - a1, b2 - b1)) < EPS) { return 0; } return (cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < EPS) && (cross(b2 - b1, a1 - b1) * cross(b2 - b1, a2 - b1) < EPS); } // aとbの矩形が接しているかどうか(どちらかがどちからを含んでいるものもふくむ bool isTouchRectangle(Rec &a, Rec &b) { // aの各頂点がbの中に存在しているかチェック for (int i = 0; i < 4; i++) if (isInPolygon(b.p, a.p[i])) return true; // 逆 for (int i = 0; i < 4; i++) if (isInPolygon(a.p, b.p[i])) return true; // 各辺が他方のどれかの辺と接しているか for (int i = 0; i < 4; i++) { int curi = i; int prvi = (i - 1 + 4) % 4; for (int j = 0; j < 4; j++) { int curj = j; int prvj = (j - 1 + 4) % 4; if (is_intersected_ls(a.p[curi], a.p[prvi], b.p[curj], b.p[prvj])) return true; } } return false; } // a1,a2を端点とする線分とb1,b2を端点とする線分の交点計算 P intersection_ls(P a1, P a2, P b1, P b2) { P b = b2 - b1; double d1 = abs(cross(b, a1 - b1)); double d2 = abs(cross(b, a2 - b1)); double t = d1 / (d1 + d2); return a1 + (a2 - a1) * t; } // a1,a2を通る直線とb1,b2を通る直線の交差判定 int is_intersected_l(P a1, P a2, P b1, P b2) { return !EQ(cross(a1 - a2, b1 - b2), 0.0); } // a1,a2を通る直線とb1,b2を通る直線の交点計算 P intersection_l(P a1, P a2, P b1, P b2) { P a = a2 - a1; P b = b2 - b1; return a1 + a * cross(b, b1 - a1) / cross(b, a); } // 単位ベクトルを求める P normalVector(P p) { return p / abs(p); } // 法線ベクトルを求める P unitVector(P a) { return P(-a.imag(), a.real()); } // 単位法線ベクトルを求める P unitNormalVector(P tmp) { P e = P(-tmp.imag(), tmp.real()); e /= abs(e); return e; } // 座標の回転(座標pにある点を,半時計回りにa(ラジアン)回転) P roundPoint(P p, double a) { return P(cos(a) * p.real() - sin(a) * p.imag(), sin(a) * p.real() + cos(a) * p.imag()); } /* 円周と線分の交差判定 */ bool isCircleCrossLine(P a, P b, P c, double r) { double d1 = abs(a - c); double d2 = abs(b - c); // 線分が中に含まれるとき、ここのコメントアウトをはずせばtrue(交差)となる // if(d1<r&&d2<r) // return true; double d = distance_ls_p(a, b, c); return (EQ(d, r) || d < r); } // 三角形の内部に点があるかどうか // 外積の正負がすべて同じなら内部に点あり bool isInTriangle(P p1, P p2, P p3, P s) { P a = p1 - s; P b = p2 - s; P c = p3 - s; return ((cross(a, b) > 0 && cross(b, c) > 0 && cross(c, a) > 0) || (cross(a, b) < 0 && cross(b, c) < 0 && cross(c, a) < 0)); } // 矩形の中に点が存在するかどうか bool isInRectangle(P p1, P p2, P p3, P p4, P s) { P a = p1 - s; P b = p2 - s; P c = p3 - s; P d = p4 - s; return ((cross(a, b) > 0 && cross(b, c) > 0 && cross(c, d) > 0 && cross(d, a) > 0) || (cross(a, b) < 0 && cross(b, c) < 0 && cross(c, d) < 0 && cross(d, a) < 0)); } // 三角形の面積を座標から計算 double calcAreaOfTriangle(P a, P b, P c) { return abs((b.real() - a.real()) * (c.imag() - a.imag()) - (c.real() - a.real()) * (b.imag() - a.imag())) / 2; } // 与えられた円の範囲内に点が存在するかどうか bool isContainingDot(P c, double r, P a) { return (((c.real() - a.real()) * (c.real() - a.real()) + (c.imag() - a.imag()) * (c.imag() - a.imag()) < r * r) || EQ((c.real() - a.real()) * (c.real() - a.real()) + (c.imag() - a.imag()) * (c.imag() - a.imag()), r * r)); } // 多角形の面積公式 double calcPolygonArea(vector<P> p) { double sum = 0; for (int i = 0; i < p.size(); i++) sum += cross(p[i], p[(i + 1) % (p.size())]); return sum / 2; } // 2ベクトル間の角度 // aからbへ左周りで何度か(0->2*PI) double diffAngle(P a, P b) { double angle = atan2(cross(a, b), dot(a, b)); if (angle < 0) return 2 * PI + angle; return angle; } // 2つのベクトルの重なっている部分の長さを返す // もし重なっていなければ0を返す double multipleLength(P a, P b, P c, P d) { Edge e1 = make_pair(a, b); Edge e2 = make_pair(c, d); // 平行であるかどうか if (!(is_parallel(e1.first, e2.first, e1.second, e2.second) && is_parallel(e1.first, e1.second, e2.first, e2.second))) return 0; double dist = 0; // 両方乗っている if (EQ(distance_ls_p(e1.first, e1.second, e2.first), 0) && EQ(distance_ls_p(e1.first, e1.second, e2.second), 0)) dist = abs(e2.first - e2.second); else if (EQ(distance_ls_p(e2.first, e2.second, e1.first), 0) && EQ(distance_ls_p(e2.first, e2.second, e1.second), 0)) dist = abs(e1.first - e1.second); else if (EQ(distance_ls_p(e1.first, e1.second, e2.first), 0)) { // どちらが線上にあるか if (EQ(distance_ls_p(e2.first, e2.second, e1.first), 0)) dist = abs(e1.first - e2.first); else dist = abs(e1.second - e2.first); } else if (EQ(distance_ls_p(e1.first, e1.second, e2.second), 0)) { if (EQ(distance_ls_p(e2.first, e2.second, e1.first), 0)) dist = abs(e1.first - e2.second); else dist = abs(e1.second - e2.second); } return dist; } // 2点を通る半径rの円の中点を求める pair<P, P> calcCircleCenterPoint(P dot1, P dot2, double r) { double v = abs(dot1 - dot2); double x = sqrt(r * r - (v / 2) * (v / 2)); // 二組の単位法線ベクトル P hose[2]; double x1 = -(dot1.imag() - dot2.imag()); double y1 = (dot1.real() - dot2.real()); hose[0] = P(x1, y1); double y2 = -y1; double x2 = -x1; hose[1] = P(x2, y2); double dd1 = abs(hose[0]); double dd2 = abs(hose[1]); hose[0] /= dd1; hose[0] *= x; hose[1] /= dd2; hose[1] *= x; P tmp = (dot1 + dot2); tmp /= 2; hose[0] += tmp; hose[1] += tmp; return make_pair(hose[0], hose[1]); } // 4線分が端点以外でクロスしているかどうかを判定 bool checkCross(const vector<Edge> &vec) { for (int i = 0; i < vec.size(); i++) { for (int j = i + 1; j < vec.size(); j++) { // 線分がクロスしているかどうか if (is_intersected_ls(vec[i].first, vec[i].second, vec[j].first, vec[j].second)) { // している場合、端点以外でクロスしているなら、false P p = intersection_ls(vec[i].first, vec[i].second, vec[j].first, vec[j].second); if (!(EQ(p, vec[i].first) || EQ(p, vec[i].second))) return false; } } } return true; } // 引き数で与えられた4つの点が矩形を作るどうかを判定 bool checkRec(const vector<P> &squ) { vector<int> v; for (int i = 0; i < squ.size(); i++) v.push_back(i); do { vector<Edge> ve; for (int i = 0; i < 4; i++) ve.push_back(make_pair(squ[v[i]], squ[v[(i + 1) % 4]])); // ここですべての線分がクロスするかどうかを計算 if (checkCross(ve)) { // 線分の長さを確認 if (EQ(abs(ve[0].first - ve[0].second), abs(ve[2].first - ve[2].second))) if (EQ(abs(ve[1].first - ve[1].second), abs(ve[3].first - ve[3].second))) // 直行 if (is_orthogonal(ve[0].first, ve[0].second, ve[1].first, ve[1].second)) if (is_orthogonal(ve[2].first, ve[2].second, ve[3].first, ve[3].second)) return true; } } while (next_permutation(v.begin(), v.end())); return false; } // 引き数で与えられた4つの点が正方形を作るかどうかを判定 bool checkSqu(const vector<P> &squ) { vector<int> v; for (int i = 0; i < squ.size(); i++) v.push_back(i); do { vector<Edge> ve; for (int i = 0; i < 4; i++) ve.push_back(make_pair(squ[v[i]], squ[v[(i + 1) % 4]])); // ここですべての線分がクロスするかどうかを計算 if (checkCross(ve)) { // 線分の長さを確認 double d = abs(ve[0].first - ve[0].second); for (int i = 1; i < 4; i++) { double d2 = abs(ve[i].first - ve[i].second); if (!EQ(d, d2)) return false; } // 直行確認 if (is_orthogonal(ve[0].first, ve[0].second, ve[1].first, ve[1].second)) if (is_orthogonal(ve[2].first, ve[2].second, ve[3].first, ve[3].second)) return true; } } while (next_permutation(v.begin(), v.end())); return false; } // 二円の共通接線を構成する4直線を求める(二円はお互い接したりどちらかをふくんだりしない) vector<pair<P, P>> calcCommonTangentialLine(P p1, double r1, P p2, double r2) { vector<pair<P, P>> res; if (r1 > r2) { swap(r1, r2); swap(p1, p2); } // 共通外接線 { double d = abs(p1 - p2); double a = abs(r1 - r2); double b = sqrt(d * d - a * a); double sita = acos(b / d); P e = normalVector(roundPoint((roundPoint((p2 - p1), sita)), PI / 2)); res.push_back(make_pair(e * r1 + p1, e * r2 + p2)); P e2 = normalVector(roundPoint((roundPoint((p2 - p1), -sita)), -PI / 2)); res.push_back(make_pair(e2 * r1 + p1, e2 * r2 + p2)); } // 共通内接線 { P cp = (r1 * p1 + r2 * p2) / (r1 + r2); double u = abs(cp - p1); double sita = acos(r1 / u); P e = normalVector(roundPoint(cp - p1, sita)); res.push_back(make_pair(e * r1 + p1, -e * r2 + p2)); P e2 = normalVector(roundPoint(cp - p1, -sita)); res.push_back(make_pair(e2 * r1 + p1, -e2 * r2 + p2)); } return res; } /* 与えられた3点a,b,c,をa->b->cと進むとき a->bで時計方向に折れて,b->c a->bで半時計方向に折れて,b->c a->bで逆を向いてaを通り越してb->c a->bでそのままb->c a->bで逆を向いてb->c のどれであるかを判定 */ int ccw(P a, P b, P c) { b -= a; c -= a; if (cross(b, c) > 0 && !EQ(cross(b, c), 0)) return 1; // counter clockwise if (cross(b, c) < 0 && !EQ(cross(b, c), 0)) return -1; // clockwise if (dot(b, c) < 0 && !EQ(dot(b, c), 0)) return 2; // c-a-b on line if (!EQ(abs(b), abs(c)) && abs(b) < abs(c)) return -2; // a-b-c on line return 0; } // 凸多角形の切断を行う // lineのfirst側から見て左の多角形が返る // 多角形は上が+y,右が+xの座標系で座標を時計周りに格納したvectorで表す vector<P> convex_cut(vector<P> polygon, pair<P, P> line) { vector<P> resPolygon; int n = polygon.size(); for (int i = 0; i < n; i++) { P A = polygon[i]; P B = polygon[(i + 1) % n]; if (ccw(line.first, line.second, A) != -1) resPolygon.push_back(A); if (ccw(line.first, line.second, A) * ccw(line.first, line.second, B) < 0) resPolygon.push_back(intersection_ls(A, B, line.first, line.second)); } return resPolygon; } P ps[3]; int xs[3]; int ys[3]; pair<P, P> makeDivisor(P a, P b) { P mid = (a + b); mid /= 2; P base = unitNormalVector(b - a); P p1 = base *= 1000000; p1 += mid; P p2 = base *= 1000000; p2 += mid; return make_pair(p1, p2); } int main() { int cnt = 1; while (cin >> xs[0] >> ys[0] >> xs[1] >> ys[1] >> xs[2] >> ys[2] && (xs[0] | xs[1] | xs[2] | ys[0] | ys[1] | ys[2])) { for (int i = 0; i < 3; i++) ps[i] = P(xs[i], ys[i]); vector<P> init; init.push_back(P(0, 0)); init.push_back(P(0, 10000)); init.push_back(P(10000, 10000)); init.push_back(P(10000, 0)); init = convex_cut(init, makeDivisor(ps[0], ps[1])); init = convex_cut(init, makeDivisor(ps[0], ps[2])); double S = abs(calcPolygonArea(init)); printf("%d %.10f\n", cnt, S / 100000000); cnt++; } return 0; }
[["-", 8, 9, 0, 43, 49, 50, 51, 11, 12, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 11, 12, 13]]
1
4,061
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <vector> #define _A_ true #define EPS 1e-10 #define COUNTER_CLOCKWISE 1 #define CLOCKWISE -1 #define ONLINE_BACK 2 #define ONLINE_FRONT -2 #define ON_SEGMENT 0 using namespace std; class Point { public: double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(x * a, y * a); } Point operator/(double a) { return Point(x / a, y / a); } //もし悪を使うなら要確認 double 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 Point; struct Segment { public: Point p1, p2; Segment(Point p = Point(), Point pp = Point()) : p1(p), p2(pp) {} }; typedef vector<Point> Polygon; typedef Segment Line; Point ACM, IC, PC; Segment IC_PC, PC_ACM, ACM_IC; double norm(Point p) { return p.x * p.x + p.y * p.y; } double abs(Point p) { return sqrt(norm(p)); } Point polar(double a, double rad) { return Point(a * cos(rad), a * sin(rad)); } double args(Point p) { return atan2(p.y, p.x); } double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } double cross(Point a, Point b) { return a.x * b.y - b.x * a.y; } Point project(Segment s, Point p) { Point base = s.p2 - s.p1; double t = dot(p - s.p1, base) / base.norm(); return s.p1 + base * t; } Point reflect(Segment s, Point p) { return p + (project(s, p) - p) * 2.0; } int ccw(Point p0, Point p1, Point p2) { Point a = p1 - p0; Point 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 isIntersect(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 isIntersect(Segment s1, Segment s2) { return isIntersect(s1.p1, s1.p2, s2.p1, s2.p2); } /* Point getCrossPoint(Segment s1,Segment s2){ assert(isIntersect(s1,s2)); P 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; } */ Point getCrossPointLines(Line s1, Line s2) { Point a = s1.p2 - s1.p1; // cout << "In getCrossPointLines--------" << endl; // cout << "a = " <<a.x << "," << a.y << endl; Point base = Point(s2.p2.x - s2.p1.x, s2.p2.y - s2.p1.y); // cout <<"base = "<< base.x << "," << base.y << endl; return s1.p1 + a * cross(base, s2.p1 - s1.p1) / cross(base, a); } Polygon cutPolygon(Polygon P, Line l) { Polygon u; for (int i = 0; i < P.size(); i++) { Point a = P[i], b = P[(i + 1) % P.size()]; if (ccw(l.p1, l.p2, a) != CLOCKWISE) u.push_back(a); if (ccw(l.p1, l.p2, a) * ccw(l.p1, l.p2, b) == -1) { u.push_back(getCrossPointLines(Segment(a, b), l)); } } return u; } Line CreateLine(Point p, Point pp) { Point mid = (p + pp) / 2; Point sl = pp - p; //原点に5 double rad = args(sl); Point ap = polar(abs(sl), rad + M_PI / 2) + mid; // ap.x = abs(sl)*cos(rad+M_PI/2)+mid.x; // ap.y = abs(sl)*sin(rad+M_PI/2)+mid.y; return Line(mid, ap); } double AreaCalc(Polygon p) { double area = 0; p.push_back(p[0]); for (int i = 0; i < p.size(); i++) { area += cross(p[i], p[i + 1]); } return abs(area / 2); } int main() { Polygon poly; Line IC_PC, IC_ACM; double area; int NARI = 1; while (_A_) { cin >> IC.x >> IC.y >> PC.x >> PC.y >> ACM.x >> ACM.y; if (IC.x + IC.y + PC.x + PC.y + ACM.x + ACM.y == 0) break; poly.clear(); IC_PC = CreateLine(IC, PC); IC_ACM = CreateLine(IC, ACM); poly.push_back(Point(0, 0)); poly.push_back(Point(10000, 0)); poly.push_back(Point(10000, 10000)); poly.push_back(Point(0, 10000)); poly = cutPolygon(poly, IC_PC); /* cout << "1st "; for(int i=0;i<poly.size();i++){ cout << poly[i].x << "," << poly[i].y << endl; } */ poly = cutPolygon(poly, IC_ACM); /* cout << "2nd "; for(int i=0;i<poly.size();i++){ cout << poly[i].x << "," << poly[i].y << endl; } */ area = AreaCalc(poly); // cout << area << endl; printf("%d %.5f\n", NARI++, area / 100000000); } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <vector> #define _A_ true #define EPS 1e-10 #define COUNTER_CLOCKWISE 1 #define CLOCKWISE -1 #define ONLINE_BACK 2 #define ONLINE_FRONT -2 #define ON_SEGMENT 0 using namespace std; class Point { public: double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(x * a, y * a); } Point operator/(double a) { return Point(x / a, y / a); } //もし悪を使うなら要確認 double 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 Point; struct Segment { public: Point p1, p2; Segment(Point p = Point(), Point pp = Point()) : p1(p), p2(pp) {} }; typedef vector<Point> Polygon; typedef Segment Line; Point ACM, IC, PC; Segment IC_PC, PC_ACM, ACM_IC; double norm(Point p) { return p.x * p.x + p.y * p.y; } double abs(Point p) { return sqrt(norm(p)); } Point polar(double a, double rad) { return Point(a * cos(rad), a * sin(rad)); } double args(Point p) { return atan2(p.y, p.x); } double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } double cross(Point a, Point b) { return a.x * b.y - b.x * a.y; } Point project(Segment s, Point p) { Point base = s.p2 - s.p1; double t = dot(p - s.p1, base) / base.norm(); return s.p1 + base * t; } Point reflect(Segment s, Point p) { return p + (project(s, p) - p) * 2.0; } int ccw(Point p0, Point p1, Point p2) { Point a = p1 - p0; Point 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 isIntersect(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 isIntersect(Segment s1, Segment s2) { return isIntersect(s1.p1, s1.p2, s2.p1, s2.p2); } /* Point getCrossPoint(Segment s1,Segment s2){ assert(isIntersect(s1,s2)); P 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; } */ Point getCrossPointLines(Line s1, Line s2) { Point a = s1.p2 - s1.p1; // cout << "In getCrossPointLines--------" << endl; // cout << "a = " <<a.x << "," << a.y << endl; Point base = Point(s2.p2.x - s2.p1.x, s2.p2.y - s2.p1.y); // cout <<"base = "<< base.x << "," << base.y << endl; return s1.p1 + a * cross(base, s2.p1 - s1.p1) / cross(base, a); } Polygon cutPolygon(Polygon P, Line l) { Polygon u; for (int i = 0; i < P.size(); i++) { Point a = P[i], b = P[(i + 1) % P.size()]; if (ccw(l.p1, l.p2, a) != CLOCKWISE) u.push_back(a); if (ccw(l.p1, l.p2, a) * ccw(l.p1, l.p2, b) == -1) { u.push_back(getCrossPointLines(Segment(a, b), l)); } } return u; } Line CreateLine(Point p, Point pp) { Point mid = (p + pp) / 2; Point sl = pp - p; //原点に5 double rad = args(sl); Point ap = polar(abs(sl), rad + M_PI / 2) + mid; // ap.x = abs(sl)*cos(rad+M_PI/2)+mid.x; // ap.y = abs(sl)*sin(rad+M_PI/2)+mid.y; return Line(mid, ap); } double AreaCalc(Polygon p) { double area = 0; p.push_back(p[0]); for (int i = 0; i < p.size() - 1; i++) { area += cross(p[i], p[i + 1]); } return abs(area / 2); } int main() { Polygon poly; Line IC_PC, IC_ACM; double area; int NARI = 1; while (_A_) { cin >> IC.x >> IC.y >> PC.x >> PC.y >> ACM.x >> ACM.y; if (IC.x + IC.y + PC.x + PC.y + ACM.x + ACM.y == 0) break; poly.clear(); IC_PC = CreateLine(IC, PC); IC_ACM = CreateLine(IC, ACM); poly.push_back(Point(0, 0)); poly.push_back(Point(10000, 0)); poly.push_back(Point(10000, 10000)); poly.push_back(Point(0, 10000)); poly = cutPolygon(poly, IC_PC); /* cout << "1st "; for(int i=0;i<poly.size();i++){ cout << poly[i].x << "," << poly[i].y << endl; } */ poly = cutPolygon(poly, IC_ACM); /* cout << "2nd "; for(int i=0;i<poly.size();i++){ cout << poly[i].x << "," << poly[i].y << endl; } */ area = AreaCalc(poly); // cout << area << endl; printf("%d %.5f\n", NARI++, area / 100000000); } return 0; }
[["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
1,297
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)n; ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cout << *i << " "; cout << endl; } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; int memo[8][8][7]; int d[8][8]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int w, h; int dfs(int x, int y, int hp) { int time = memo[y][x][hp]; if (hp == 0) return INF; if (d[y][x] == 3) return time; int res = INF; if (d[y][x] == 4) { int &nt = memo[y][x][6]; if (nt == -1 || nt > time + 1) { nt = time + 1; res = min(res, dfs(x, y, 6)); } } REP(r, 4) { int nx = x + dx[r]; int ny = y + dy[r]; if (0 <= nx && 0 <= ny && nx < w && ny < h && d[ny][nx] != 0) { int &nt = memo[ny][nx][hp - 1]; if (nt == -1 || nt > time + 1) { nt = time + 1; res = min(res, dfs(nx, ny, hp - 1)); } } } return res; } int main() { while (cin >> w >> h && w || h) { int ans; memset(memo, -1, sizeof(memo)); REP(y, h) REP(x, w) cin >> d[y][x]; REP(y, h) REP(x, w) if (d[y][x] == 2) { memo[y][x][6] = 0; ans = dfs(x, y, 6); } if (ans == INF) ans = -1; cout << ans << endl; } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)n; ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cout << *i << " "; cout << endl; } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; int memo[8][8][7]; int d[8][8]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int w, h; int dfs(int x, int y, int hp) { int time = memo[y][x][hp]; if (hp == 0) return INF; if (d[y][x] == 3) return time; int res = INF; if (d[y][x] == 4) { int &nt = memo[y][x][6]; if (nt == -1 || nt > time) { nt = time; res = min(res, dfs(x, y, 6)); } } REP(r, 4) { int nx = x + dx[r]; int ny = y + dy[r]; if (0 <= nx && 0 <= ny && nx < w && ny < h && d[ny][nx] != 0) { int &nt = memo[ny][nx][hp - 1]; if (nt == -1 || nt > time + 1) { nt = time + 1; res = min(res, dfs(nx, ny, hp - 1)); } } } return res; } int main() { while (cin >> w >> h && w || h) { int ans; memset(memo, -1, sizeof(memo)); REP(y, h) REP(x, w) cin >> d[y][x]; REP(y, h) REP(x, w) if (d[y][x] == 2) { memo[y][x][6] = 0; ans = dfs(x, y, 6); } if (ans == INF) ans = -1; cout << ans << endl; } return 0; }
[["-", 15, 339, 51, 16, 12, 16, 12, 16, 17, 72], ["-", 15, 339, 51, 16, 12, 16, 12, 16, 12, 13], ["-", 64, 9, 0, 1, 0, 11, 12, 16, 17, 72], ["-", 64, 9, 0, 1, 0, 11, 12, 16, 12, 13]]
1
552
#include <math.h> #include <stdio.h> int main(void) { int data_num, cur_data; double qr, rp, pq; int n, i; int p, q, r; int ap, aq, ar; double x[30]; double y[30]; double z[30]; double nqr, nrp, npq; scanf("%d", &data_num); for (cur_data = 0; cur_data < data_num; cur_data++) { scanf("%lf%lf%lf", &qr, &rp, &pq); scanf("%d", &n); for (i = 0; i < n; i++) scanf("%lf%lf%lf", &x[i], &y[i], &z[i]); ap = aq = ar = 0; for (p = 0; p < n; p++) { for (q = 0; q < n; q++) { if (p == q) continue; for (r = 0; r < n; r++) { if (p == r || q == r) continue; nqr = sqrt((x[q] - x[r]) * (x[q] - x[r]) + (y[q] - y[r]) * (y[q] - y[r]) + (z[q] - z[r]) * (z[q] - z[r])); nrp = sqrt((x[r] - x[p]) * (x[r] - x[p]) + (y[r] - y[p]) * (y[r] - y[p]) + (z[r] - z[p]) * (z[r] - z[p])); npq = sqrt((x[p] - x[q]) * (x[p] - x[q]) + (y[p] - y[q]) * (y[p] - y[q]) + (z[p] - z[q]) * (z[p] - z[q])); if (fabs(nrp / nqr - rp / qr) <= 0.05 && fabs(npq / nrp - pq / rp) <= 0.05 && fabs(nqr / npq - qr / pq) <= 0.05) { ap = p; aq = q; ar = r; } } } } printf("%d %d %d\n", ap + 1, aq + 1, ar + 1); } return 0; }
#include <math.h> #include <stdio.h> int main(void) { int data_num, cur_data; double qr, rp, pq; int n, i; int p, q, r; int ap, aq, ar; double x[30]; double y[30]; double z[30]; double nqr, nrp, npq; scanf("%d", &data_num); for (cur_data = 0; cur_data < data_num; cur_data++) { scanf("%lf%lf%lf", &qr, &rp, &pq); scanf("%d", &n); for (i = 0; i < n; i++) scanf("%lf%lf%lf", &x[i], &y[i], &z[i]); ap = aq = ar = 0; for (p = 0; p < n; p++) { for (q = 0; q < n; q++) { if (p == q) continue; for (r = 0; r < n; r++) { if (p == r || q == r) continue; nqr = sqrt((x[q] - x[r]) * (x[q] - x[r]) + (y[q] - y[r]) * (y[q] - y[r]) + (z[q] - z[r]) * (z[q] - z[r])); nrp = sqrt((x[r] - x[p]) * (x[r] - x[p]) + (y[r] - y[p]) * (y[r] - y[p]) + (z[r] - z[p]) * (z[r] - z[p])); npq = sqrt((x[p] - x[q]) * (x[p] - x[q]) + (y[p] - y[q]) * (y[p] - y[q]) + (z[p] - z[q]) * (z[p] - z[q])); if (fabs(nrp / nqr - rp / qr) <= 0.0001 && fabs(npq / nrp - pq / rp) <= 0.0001 && fabs(nqr / npq - qr / pq) <= 0.0001) { ap = p; aq = q; ar = r; } } } } printf("%d %d %d\n", ap + 1, aq + 1, ar + 1); } return 0; }
[["-", 15, 23, 0, 16, 31, 16, 31, 16, 12, 13], ["+", 15, 23, 0, 16, 31, 16, 31, 16, 12, 13], ["-", 15, 23, 0, 16, 31, 16, 12, 16, 12, 13], ["+", 15, 23, 0, 16, 31, 16, 12, 16, 12, 13], ["-", 0, 57, 15, 23, 0, 16, 12, 16, 12, 13], ["+", 0, 57, 15, 23, 0, 16, 12, 16, 12, 13]]
0
538
#include <bits/stdc++.h> using namespace std; struct P { double x, y, z; P() : x(0), y(0), z(0) {} P(double x, double y, double z) : x(x), y(y), z(z) {} P operator+(const P &b) const { P res; res.x = x + b.x; res.y = y + b.y; res.z = z + b.z; return res; } P operator-(const P &b) const { P res; res.x = x - b.x; res.y = y - b.y; res.z = z - b.z; return res; } bool operator<(const P &b) const { if (x == b.x) if (y == b.y) return z < b.z; else return y < b.y; else return x < b.x; } }; bool eq(double a, double b) { return abs(a) - abs(b) < 0.000001; } // Experimence int main(void) { int T; cin >> T; while (T--) { double QR, RP, PQ; cin >> QR >> RP >> PQ; double P_arg = acos((PQ * PQ + RP * RP - QR * QR) / (2 * PQ * RP)); double Q_arg = acos((PQ * PQ + QR * QR - RP * RP) / (2 * PQ * QR)); double R_arg = acos((QR * QR + RP * RP - PQ * PQ) / (2 * QR * RP)); int N; cin >> N; vector<P> points(N); for (int i = 0; i < N; i++) { cin >> points[i].x >> points[i].y >> points[i].z; } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { for (int k = 0; k < N; k++) { if (i == j || j == k || i == k) continue; vector<P> triangle(3); triangle[0] = points[i]; triangle[1] = points[j]; triangle[2] = points[k]; // do{ vector<double> edge(3); for (int edge_idx = 0; edge_idx < 3; edge_idx++) { P subtract = triangle[edge_idx % 3] - triangle[(edge_idx + 1) % 3]; edge[edge_idx] = sqrt(subtract.x * subtract.x + subtract.y * subtract.y + subtract.z * subtract.z); } double arg1, arg2, arg3; arg1 = acos((edge[0] * edge[0] + edge[2] * edge[2] - edge[1] * edge[1]) / (2 * edge[0] * edge[2])); arg2 = acos((edge[0] * edge[0] + edge[1] * edge[1] - edge[2] * edge[2]) / (2 * edge[0] * edge[1])); arg3 = acos((edge[1] * edge[1] + edge[2] * edge[2] - edge[0] * edge[0]) / (2 * edge[1] * edge[2])); // DEBUG if (eq(P_arg, arg1) && eq(Q_arg, arg2) && eq(R_arg, arg3)) { cout << i + 1 << " " << j + 1 << " " << k + 1 << endl; i = N; j = N; k = N; } //}while(next_permutation(triangle.begin(), triangle.end())); // cout << i << j << k << endl; } } } } }
#include <bits/stdc++.h> using namespace std; struct P { double x, y, z; P() : x(0), y(0), z(0) {} P(double x, double y, double z) : x(x), y(y), z(z) {} P operator+(const P &b) const { P res; res.x = x + b.x; res.y = y + b.y; res.z = z + b.z; return res; } P operator-(const P &b) const { P res; res.x = x - b.x; res.y = y - b.y; res.z = z - b.z; return res; } bool operator<(const P &b) const { if (x == b.x) if (y == b.y) return z < b.z; else return y < b.y; else return x < b.x; } }; bool eq(double a, double b) { return abs(a - b) < 0.0001; } // Experimence int main(void) { int T; cin >> T; while (T--) { double QR, RP, PQ; cin >> QR >> RP >> PQ; double P_arg = acos((PQ * PQ + RP * RP - QR * QR) / (2 * PQ * RP)); double Q_arg = acos((PQ * PQ + QR * QR - RP * RP) / (2 * PQ * QR)); double R_arg = acos((QR * QR + RP * RP - PQ * PQ) / (2 * QR * RP)); int N; cin >> N; vector<P> points(N); for (int i = 0; i < N; i++) { cin >> points[i].x >> points[i].y >> points[i].z; } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { for (int k = 0; k < N; k++) { if (i == j || j == k || i == k) continue; vector<P> triangle(3); triangle[0] = points[i]; triangle[1] = points[j]; triangle[2] = points[k]; // do{ vector<double> edge(3); for (int edge_idx = 0; edge_idx < 3; edge_idx++) { P subtract = triangle[edge_idx % 3] - triangle[(edge_idx + 1) % 3]; edge[edge_idx] = sqrt(subtract.x * subtract.x + subtract.y * subtract.y + subtract.z * subtract.z); } double arg1, arg2, arg3; arg1 = acos((edge[0] * edge[0] + edge[2] * edge[2] - edge[1] * edge[1]) / (2 * edge[0] * edge[2])); arg2 = acos((edge[0] * edge[0] + edge[1] * edge[1] - edge[2] * edge[2]) / (2 * edge[0] * edge[1])); arg3 = acos((edge[1] * edge[1] + edge[2] * edge[2] - edge[0] * edge[0]) / (2 * edge[1] * edge[2])); // DEBUG if (eq(P_arg, arg1) && eq(Q_arg, arg2) && eq(R_arg, arg3)) { cout << i + 1 << " " << j + 1 << " " << k + 1 << endl; i = N; j = N; k = N; } //}while(next_permutation(triangle.begin(), triangle.end())); // cout << i << j << k << endl; } } } } }
[["-", 0, 16, 31, 16, 31, 2, 3, 4, 0, 25], ["-", 0, 37, 0, 16, 31, 16, 12, 2, 63, 22], ["-", 0, 16, 31, 16, 12, 2, 3, 4, 0, 24], ["-", 0, 14, 8, 9, 0, 37, 0, 16, 12, 13], ["+", 0, 14, 8, 9, 0, 37, 0, 16, 12, 13]]
1
819
#include <bits/stdc++.h> using namespace std; double x[31], y[31], z[31]; double g(int i, int j) { double X = x[i] - x[j], Y = y[i] - y[j], Z = z[i] - z[j]; return sqrt(X * X + Y * Y + Z * Z); } int main() { int m, n, a1, a2, a3; cin >> m; while (m--) { double Q, R, P; cin >> Q >> R >> P; cin >> n; for (int i = 0; i < n; i++) cin >> x[i] >> y[i] >> z[i]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) { double p1 = g(i, j), p2 = g(j, k), p3 = g(k, i); if (abs(R / Q - p2 / p1) < 0.1 && abs(P / Q - p3 / p1) < 0.1) { a1 = i + 1; a2 = j + 1; a3 = k + 1; } } cout << a3 << ' ' << a2 << ' ' << a1 << endl; } }
#include <bits/stdc++.h> using namespace std; double x[31], y[31], z[31]; double g(int i, int j) { double X = x[i] - x[j], Y = y[i] - y[j], Z = z[i] - z[j]; return sqrt(X * X + Y * Y + Z * Z); } int main() { int m, n, a1, a2, a3; cin >> m; while (m--) { double Q, R, P; cin >> Q >> R >> P; cin >> n; for (int i = 0; i < n; i++) cin >> x[i] >> y[i] >> z[i]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) { double p1 = g(i, j), p2 = g(j, k), p3 = g(k, i); if (abs(R / Q - p2 / p1) < 0.001 && abs(P / Q - p3 / p1) < 0.001) { a1 = i + 1; a2 = j + 1; a3 = k + 1; } } L:; cout << a3 << ' ' << a1 << ' ' << a2 << endl; } }
[["-", 0, 57, 15, 339, 51, 16, 31, 16, 12, 13], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 12, 13], ["-", 0, 57, 15, 339, 51, 16, 12, 16, 12, 13], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 12, 13], ["+", 8, 9, 0, 52, 8, 9, 0, 97, 128, 129], ["+", 8, 9, 0, 52, 8, 9, 0, 97, 0, 102], ["+", 0, 52, 8, 9, 0, 97, 0, 1, 0, 35], ["-", 0, 16, 31, 16, 31, 16, 31, 16, 12, 22], ["+", 0, 16, 31, 16, 31, 16, 31, 16, 12, 22], ["-", 8, 9, 0, 1, 0, 16, 31, 16, 12, 22], ["+", 8, 9, 0, 1, 0, 16, 31, 16, 12, 22]]
1
302
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() const int INF = 1 << 29; struct P { int time; int id; int pcnt; P(int time, int id, int pcnt) : time(time), id(id), pcnt(pcnt) {} }; const bool operator<(const P &a, const P &b) { return a.time != b.time ? a.time > b.time : a.id > b.id; } int main() { int n, m; while (cin >> n >> m, n || m) { vector<int> consume[n], charge[n]; priority_queue<P> Q; REP(i, n) { int a; int cnt = 0; while (cin >> a, a) { if (cnt % 2) charge[i].push_back(a); else consume[i].push_back(a); cnt++; } Q.push(P(consume[i][0], i, 0)); } int pcnt[n]; memset(pcnt, 0, sizeof(pcnt)); int res = 0; int nowtime = 0; for (int now = 0;; ++now) { P p = Q.top(); int time = p.time, id = p.id, pcnt = p.pcnt; if (time >= m) break; if (now >= time) { // cout << time << " " << id << " " <<pcnt << endl; res += now - time; now += charge[id][pcnt]; Q.pop(); int next = (pcnt + 1) % charge[id].size(); // cout << now+consume[id][next] << endl; Q.push(P(now + consume[id][next], id, next)); now--; } } cout << res << endl; } }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() const int INF = 1 << 29; struct P { int time; int id; int pcnt; P(int time, int id, int pcnt) : time(time), id(id), pcnt(pcnt) {} }; const bool operator<(const P &a, const P &b) { return a.time != b.time ? a.time > b.time : a.id > b.id; } int main() { int n, m; while (cin >> n >> m, n || m) { vector<int> consume[n], charge[n]; priority_queue<P> Q; REP(i, n) { int a; int cnt = 0; while (cin >> a, a) { if (cnt % 2) charge[i].push_back(a); else consume[i].push_back(a); cnt++; } Q.push(P(consume[i][0], i, 0)); } int pcnt[n]; memset(pcnt, 0, sizeof(pcnt)); int res = 0; int nowtime = 0; for (int now = 0;; ++now) { P p = Q.top(); int time = p.time, id = p.id, pcnt = p.pcnt; if (time >= m) break; if (now >= time) { // cout << now <<" " <<time << " " << id << " " <<pcnt << endl; res += min(now, m) - time; now += charge[id][pcnt]; Q.pop(); int next = (pcnt + 1) % charge[id].size(); // cout << now+consume[id][next] << endl; Q.push(P(now + consume[id][next], id, next)); now--; } } cout << res << endl; } }
[["+", 0, 1, 0, 11, 12, 16, 31, 2, 63, 22], ["+", 0, 11, 12, 16, 31, 2, 3, 4, 0, 24], ["+", 0, 11, 12, 16, 31, 2, 3, 4, 0, 21], ["+", 0, 11, 12, 16, 31, 2, 3, 4, 0, 22], ["+", 0, 11, 12, 16, 31, 2, 3, 4, 0, 25]]
1
422
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; const ld eps = 1e-9; const ld pi = acos(-1.0); ld solve(int N, int M, vector<ld> ps) { vector<vector<vector<ld>>> dp(N + 1, vector<vector<ld>>(M + 1, vector<ld>(N, -1e8))); dp[1][1][0] = 0; for (int now = 1; now < N; ++now) { for (int used_cnt = 0; used_cnt < M; ++used_cnt) { for (int pre_use_id = 0; pre_use_id < now; ++pre_use_id) { dp[now + 1][used_cnt][pre_use_id] = max( dp[now + 1][used_cnt][pre_use_id], dp[now][used_cnt][pre_use_id]); if (used_cnt != M) { ld plus = 0.5 * (sin(2 * pi * (ps[now] - ps[pre_use_id]))); dp[now + 1][used_cnt + 1][now] = max(dp[now + 1][used_cnt + 1][now], dp[now][used_cnt][pre_use_id] + plus); } } } } ld ans = 0; for (int pre_use_id = 0; pre_use_id < N; ++pre_use_id) { ld plus = 0.5 * (sin(2 * pi * (ps[0] - ps[pre_use_id]))); ans = max(ans, dp[N][M][pre_use_id] + plus); } return ans; } int main() { while (true) { int N, M; cin >> N >> M; if (!N) break; vector<ld> ps; for (int i = 0; i < N; ++i) { ld theta; cin >> theta; ps.push_back(theta); } ld ans = 0; for (int i = 0; i < N; ++i) { rotate(ps.begin(), ps.begin() + 1, ps.end()); ans = max(ans, solve(N, M, ps)); } cout << setprecision(6) << fixed << ans << endl; } return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; const ld eps = 1e-9; const ld pi = acos(-1.0); ld solve(int N, int M, vector<ld> ps) { vector<vector<vector<ld>>> dp(N + 1, vector<vector<ld>>(M + 1, vector<ld>(N, -1e8))); dp[1][1][0] = 0; for (int now = 1; now < N; ++now) { for (int used_cnt = 0; used_cnt <= M; ++used_cnt) { for (int pre_use_id = 0; pre_use_id < now; ++pre_use_id) { dp[now + 1][used_cnt][pre_use_id] = max( dp[now + 1][used_cnt][pre_use_id], dp[now][used_cnt][pre_use_id]); if (used_cnt != M) { ld plus = 0.5 * (sin(2 * pi * (ps[now] - ps[pre_use_id]))); dp[now + 1][used_cnt + 1][now] = max(dp[now + 1][used_cnt + 1][now], dp[now][used_cnt][pre_use_id] + plus); } } } } ld ans = 0; for (int pre_use_id = 0; pre_use_id < N; ++pre_use_id) { ld plus = 0.5 * (sin(2 * pi * (ps[0] - ps[pre_use_id]))); ans = max(ans, dp[N][M][pre_use_id] + plus); } return ans; } int main() { while (true) { int N, M; cin >> N >> M; if (!N) break; vector<ld> ps; for (int i = 0; i < N; ++i) { ld theta; cin >> theta; ps.push_back(theta); } ld ans = 0; for (int i = 0; i < N; ++i) { rotate(ps.begin(), ps.begin() + 1, ps.end()); ans = max(ans, solve(N, M, ps)); } cout << setprecision(6) << fixed << ans << endl; } return 0; }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19]]
1
485
#include <algorithm> #include <cmath> #include <cstdio> #include <vector> using namespace std; double dp[40][40][40]; double p[40]; int main() { double pi = acos(-1.0); int n, m; double x; while (scanf("%d%d", &n, &m), n != 0) { fill(**dp, **dp + sizeof(dp) / sizeof(double), 0.0); for (int i = 0; i < n; ++i) { scanf("%lf", &x); p[i] = x * 2.0 * pi; } for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) { dp[1][i][j] = 0.5 * sin(p[j] - p[i]); } for (int i = 2; i < m; ++i) for (int s = 0; s < n; ++s) for (int k = s; k < n; ++k) for (int f = k + 1; f < n; ++f) { x = dp[i - 1][s][k] + 0.5 * sin(p[f] - p[k]); dp[i][s][f] = max(dp[i][s][f], x); } double ans = 0.0; for (int s = 0; s < n; ++s) for (int f = s + m - 1; f < n; ++f) { ans = max(ans, dp[m - 1][s][f] + 0.5 * sin(p[s] - p[f])); } printf("%f\n", ans); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <vector> using namespace std; double dp[40][40][40]; double p[40]; int main() { double pi = acos(-1.0); int n, m; double x; while (scanf("%d%d", &n, &m), n != 0) { fill(**dp, **dp + sizeof(dp) / sizeof(double), -1e300); for (int i = 0; i < n; ++i) { scanf("%lf", &x); p[i] = x * 2.0 * pi; } for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) { dp[1][i][j] = 0.5 * sin(p[j] - p[i]); } for (int i = 2; i < m; ++i) for (int s = 0; s < n; ++s) for (int k = s; k < n; ++k) for (int f = k + 1; f < n; ++f) { x = dp[i - 1][s][k] + 0.5 * sin(p[f] - p[k]); dp[i][s][f] = max(dp[i][s][f], x); } double ans = 0.0; for (int s = 0; s < n; ++s) for (int f = s + m - 1; f < n; ++f) { ans = max(ans, dp[m - 1][s][f] + 0.5 * sin(p[s] - p[f])); } printf("%f\n", ans); } }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
1
392
i, table[151201]; main(j, n) { for (; i < 54; i++) for (j = 0; j < 54; j++) if ((n = i * i * i + j * (j + 1) * (j + 2) / 6) < 151201) table[n] = 1; for (; scanf("%d", &n), n; printf("%d\n", n)) while (!table[n]) n--; return 0; }
i, table[151201]; main(j, n) { for (; i < 54; i++) for (j = 0; j < 97; j++) if ((n = i * i * i + j * (j + 1) * (j + 2) / 6) < 151201) table[n] = 1; for (; scanf("%d", &n), n; printf("%d\n", n)) while (!table[n]) n--; return 0; }
[["-", 0, 9, 0, 7, 8, 7, 15, 16, 12, 13], ["+", 0, 9, 0, 7, 8, 7, 15, 16, 12, 13]]
0
115
#include <algorithm> #include <iostream> using namespace std; int solve(int x) { int ret = 0; for (int i = 1; i * i * i <= x; i++) { int l = 0, r = 100; while (r - l > 1) { int m = (l + r) / 2; int res = m * (m + 1) * (m + 2) / 6; if (res + i * i * i <= x) l = m; else r = m; } ret = max(ret, i * i * i + l * (l + 1) * (l + 2) / 6); } return ret; } int main() { int n; while (cin >> n, n) cout << solve(n) << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; int solve(int x) { int ret = 0; for (int i = 0; i * i * i <= x; i++) { int l = 0, r = 100; while (r - l > 1) { int m = (l + r) / 2; int res = m * (m + 1) * (m + 2) / 6; if (res + i * i * i <= x) l = m; else r = m; } ret = max(ret, i * i * i + l * (l + 1) * (l + 2) / 6); } return ret; } int main() { int n; while (cin >> n, n) cout << solve(n) << endl; return 0; }
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
1
172
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n && n) { int ans = 0; for (int i = 0; i * i * i < n; i++) { int x = n - i * i * i; for (int j = 0; j < x; j++) { if (j * (j + 1) * (j + 2) / 6 > x) break; if (x >= j * (j + 1) * (j + 2) / 6) ans = max(ans, i * i * i + j * (j + 1) * (j + 2) / 6); } } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n && n) { int ans = 0; for (int i = 0; i * i * i <= n; i++) { int x = n - i * i * i; for (int j = 0; j <= x; j++) { if (j * (j + 1) * (j + 2) / 6 > x) break; if (x >= j * (j + 1) * (j + 2) / 6) ans = max(ans, i * i * i + j * (j + 1) * (j + 2) / 6); } } cout << ans << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19]]
1
157
while True: n = eval(input()) if n == 0: break ans = 0 for i in range(96): tri = i*(i+1)*(i+2)/6 if tri > n: break ans = max(ans,int((n-tri)**(1.0/3))**3 + tri) print(ans)
while True: n = eval(input()) if n == 0: break ans = 0 for i in range(96): tri = i*(i+1)*(i+2)/6 if tri > n: break ans = max(ans,int((n-tri)**(1.0/3)+1e-8)**3 + tri) print(ans)
[["+", 31, 657, 31, 652, 3, 4, 0, 657, 17, 72], ["+", 31, 657, 31, 652, 3, 4, 0, 657, 12, 531]]
5
80
// AOJ 1225: e-market // 2017.10.25 bal4u@uu #include <stdio.h> #include <stdlib.h> #include <string.h> #define HASHSIZ 2003 typedef struct { char *s; int id; } HASH; HASH hash[HASHSIZ + 2], *hashend = hash + HASHSIZ; // Hash table int lookup(char *s, int id) { int w = strlen(s); int i = (101 * *s + 103 * *(s + w - 1) + 107 * w) % HASHSIZ; HASH *tp = hash + i; while (tp->s != NULL) { if (!strcmp(tp->s, s)) return tp->id; if (++tp == hashend) tp = hash; } tp->s = s, tp->id = id; return -1; } typedef struct { int id, buy, m, p, fin; } T; T tbl[1002]; int n; typedef struct { int min, max, sum, n; } M; M mono[26]; typedef struct { char *nm; int paid, got; } D; D dealer[1002]; char dealerName[1002][12]; int sz; int cmp(D *a, D *b) { return strcmp(a->nm, b->nm); } int main() { int i, j, id; char b1[10], b2[5]; T *tp; while (scanf("%d", &n) && n > 0) { memset(hash, 0, sizeof(hash)); memset(mono, 0, sizeof(mono)); for (sz = 0, tp = tbl, i = 0; i < n; i++, tp++) { scanf("%s%s%s%d", dealerName[sz], b1, b2, &tp->p); if ((id = lookup(dealerName[sz], sz)) < 0) { dealer[sz].nm = dealerName[sz], dealer[sz].paid = dealer[sz].got = 0; id = sz++; } tp->id = id, tp->buy = (*b1 == 'B'), tp->m = (*b2 - 'A'), tp->fin = 0; } for (i = 1; i < n; i++) { int buy, p, best, m, jj; if (tbl[i].fin) continue; buy = tbl[i].buy, m = tbl[i].m, p = tbl[i].p, best = -1; for (j = 0; j < i; j++) { if (tbl[j].id != tbl[i].id && tbl[j].buy != buy && tbl[j].m == m && !tbl[j].fin) { if (buy && tbl[j].p <= p) { if (best < 0 || best < tbl[j].p) best = tbl[j].p, jj = j; } else if (!buy && p <= tbl[j].p) { if (best < 0 || best > tbl[j].p) best = tbl[j].p, jj = j; } } } if (best >= 0) { tbl[i].fin = tbl[jj].fin = 1; best = (p + tbl[jj].p) / 2; if (mono[m].n == 0) mono[m].min = mono[m].max = best; else { if (best < mono[m].min) mono[m].min = best; else if (best > mono[m].max) mono[m].max = best; } mono[m].sum += best, mono[m].n++; id = tbl[jj].id; if (buy) dealer[id].got += best; else dealer[id].paid += best; id = tbl[i].id; if (buy) dealer[id].paid += best; else dealer[id].got += best; } } for (i = 0; i < 26; i++) if (mono[i].n > 0) { printf("%c %d %d %d\n", i + 'A', mono[i].min, mono[i].sum / mono[i].n, mono[i].max); } puts("--"); qsort(dealer, sz, sizeof(D), cmp); for (i = 0; i < sz; i++) { printf("%s %d %d\n", dealer[i].nm, dealer[i].paid, dealer[i].got); } puts("----------"); } return 0; }
// AOJ 1225: e-market // 2017.10.25 bal4u@uu #include <stdio.h> #include <stdlib.h> #include <string.h> #define HASHSIZ 2003 typedef struct { char *s; int id; } HASH; HASH hash[HASHSIZ + 2], *hashend = hash + HASHSIZ; // Hash table int lookup(char *s, int id) { int w = strlen(s); int i = (101 * *s + 103 * *(s + w - 1) + 107 * w) % HASHSIZ; HASH *tp = hash + i; while (tp->s != NULL) { if (!strcmp(tp->s, s)) return tp->id; if (++tp == hashend) tp = hash; } tp->s = s, tp->id = id; return -1; } typedef struct { int id, buy, m, p, fin; } T; T tbl[1002]; int n; typedef struct { int min, max, sum, n; } M; M mono[26]; typedef struct { char *nm; int paid, got; } D; D dealer[1002]; char dealerName[1002][12]; int sz; int cmp(D *a, D *b) { return strcmp(a->nm, b->nm); } int main() { int i, j, id; char b1[10], b2[5]; T *tp; while (scanf("%d", &n) && n > 0) { memset(hash, 0, sizeof(hash)); memset(mono, 0, sizeof(mono)); for (sz = 0, tp = tbl, i = 0; i < n; i++, tp++) { scanf("%s%s%s%d", dealerName[sz], b1, b2, &tp->p); if ((id = lookup(dealerName[sz], sz)) < 0) { dealer[sz].nm = dealerName[sz], dealer[sz].paid = dealer[sz].got = 0; id = sz++; } tp->id = id, tp->buy = (*b1 == 'B'), tp->m = (*b2 - 'A'), tp->fin = 0; } for (i = 1; i < n; i++) { int buy, p, best, m, jj; if (tbl[i].fin) continue; buy = tbl[i].buy, m = tbl[i].m, p = tbl[i].p, best = -1; for (j = 0; j < i; j++) { if (tbl[j].id != tbl[i].id && tbl[j].buy != buy && tbl[j].m == m && !tbl[j].fin) { if (buy && tbl[j].p <= p) { if (best < 0 || tbl[j].p < best) best = tbl[j].p, jj = j; } else if (!buy && p <= tbl[j].p) { if (best < 0 || best < tbl[j].p) best = tbl[j].p, jj = j; } } } if (best >= 0) { tbl[i].fin = tbl[jj].fin = 1; best = (p + tbl[jj].p) / 2; if (mono[m].n == 0) mono[m].min = mono[m].max = best; else { if (best < mono[m].min) mono[m].min = best; else if (best > mono[m].max) mono[m].max = best; } mono[m].sum += best, mono[m].n++; id = tbl[jj].id; if (buy) dealer[id].got += best; else dealer[id].paid += best; id = tbl[i].id; if (buy) dealer[id].paid += best; else dealer[id].got += best; } } for (i = 0; i < 26; i++) if (mono[i].n > 0) { printf("%c %d %d %d\n", i + 'A', mono[i].min, mono[i].sum / mono[i].n, mono[i].max); } puts("--"); qsort(dealer, sz, sizeof(D), cmp); for (i = 0; i < sz; i++) { printf("%s %d %d\n", dealer[i].nm, dealer[i].paid, dealer[i].got); } puts("----------"); } return 0; }
[["-", 0, 57, 15, 23, 0, 16, 12, 16, 31, 22], ["-", 0, 57, 15, 23, 0, 16, 12, 16, 17, 18], ["+", 0, 57, 15, 23, 0, 16, 12, 16, 17, 18], ["+", 0, 57, 15, 23, 0, 16, 12, 16, 12, 22], ["-", 0, 57, 15, 23, 0, 16, 12, 16, 17, 47]]
0
1,010
#include <algorithm> #include <map> #include <stdio.h> #include <string> using namespace std; char in[110]; int h[1100]; int g[1100]; int type[1100]; int v[1100]; int s[1100]; int sum[1100]; int L[1100]; int R[1100]; int t[1100]; int u[1100]; int c[1100]; int main() { int a; while (scanf("%d", &a), a) { map<string, int> m1; int n1 = 0; map<string, int> m2; int n2 = 0; for (int i = 0; i < a; i++) v[i] = 0; for (int i = 0; i < 1100; i++) s[i] = t[i] = u[i] = sum[i] = R[i] = 0; for (int i = 0; i < 1100; i++) L[i] = 99999999; for (int i = 0; i < a; i++) { scanf("%s", in); string A = in; scanf("%s", in); if (in[0] == 'B') type[i] = 1; else type[i] = 0; scanf("%s", in); string B = in; scanf("%d", c + i); if (m1.count(A)) h[i] = m1[A]; else { m1[A] = n1; h[i] = n1++; } if (m2.count(B)) g[i] = m2[B]; else { m2[B] = n2; g[i] = n2++; } } // for(int i=0;i<a;i++)printf("%d %d %d %d\n",h[i],type[i],g[i],c[i]); for (int i = 0; i < a; i++) { if (v[i]) continue; if (type[i]) { int at = -1; int val = 99999999; for (int j = 0; j < a; j++) { if (v[j] || h[i] == h[j]) continue; if (g[i] == g[j] && type[j] == 0 && c[j] <= c[i] && val > c[j]) { val = c[j]; at = j; } } if (!~at) continue; v[at] = v[i] = 1; int tmp = (val + c[i]) / 2; sum[g[i]]++; s[g[i]] += tmp; L[g[i]] = min(L[g[i]], tmp); R[g[i]] = max(R[g[i]], tmp); t[h[i]] += tmp; u[h[at]] += tmp; } else { int at = -1; int val = 0; for (int j = 0; j < a; j++) { if (v[j] || h[i] == h[j]) continue; if (g[i] == g[j] && type[j] == 1 && c[i] <= c[j] && val < c[j]) { val = c[j]; at = j; } } if (!~at) continue; v[at] = v[i] = 1; int tmp = (val + c[i]) / 2; sum[g[i]]++; L[g[i]] = min(L[g[i]], tmp); R[g[i]] = max(R[g[i]], tmp); s[g[i]] += tmp; t[h[at]] += tmp; u[h[i]] += tmp; } } for (map<string, int>::iterator it = m2.begin(); it != m2.end(); it++) { string name = (*it).first; int num = (*it).second; if (!sum[num]) continue; printf("%s %d %d %d\n", name.c_str(), L[num], s[num] / sum[num], R[num]); } printf("--\n"); for (map<string, int>::iterator it = m1.begin(); it != m1.end(); it++) { string name = (*it).first; int num = (*it).second; printf("%s %d %d\n", name.c_str(), t[num], u[num]); } printf("----------\n"); } }
#include <algorithm> #include <map> #include <stdio.h> #include <string> using namespace std; char in[110]; int h[1100]; int g[1100]; int type[1100]; int v[1100]; int s[1100]; int sum[1100]; int L[1100]; int R[1100]; int t[1100]; int u[1100]; int c[1100]; int main() { int a; while (scanf("%d", &a), a) { map<string, int> m1; int n1 = 0; map<string, int> m2; int n2 = 0; for (int i = 0; i < a; i++) v[i] = 0; for (int i = 0; i < 1100; i++) s[i] = t[i] = u[i] = sum[i] = R[i] = 0; for (int i = 0; i < 1100; i++) L[i] = 99999999; for (int i = 0; i < a; i++) { scanf("%s", in); string A = in; scanf("%s", in); if (in[0] == 'B') type[i] = 1; else type[i] = 0; scanf("%s", in); string B = in; scanf("%d", c + i); if (m1.count(A)) h[i] = m1[A]; else { m1[A] = n1; h[i] = n1++; } if (m2.count(B)) g[i] = m2[B]; else { m2[B] = n2; g[i] = n2++; } } // for(int i=0;i<a;i++)printf("%d %d %d %d\n",h[i],type[i],g[i],c[i]); for (int i = 0; i < a; i++) { if (v[i]) continue; if (type[i]) { int at = -1; int val = 99999999; for (int j = 0; j < i; j++) { if (v[j] || h[i] == h[j]) continue; if (g[i] == g[j] && type[j] == 0 && c[j] <= c[i] && val > c[j]) { val = c[j]; at = j; } } if (!~at) continue; v[at] = v[i] = 1; int tmp = (val + c[i]) / 2; sum[g[i]]++; s[g[i]] += tmp; L[g[i]] = min(L[g[i]], tmp); R[g[i]] = max(R[g[i]], tmp); t[h[i]] += tmp; u[h[at]] += tmp; } else { int at = -1; int val = 0; for (int j = 0; j < i; j++) { if (v[j] || h[i] == h[j]) continue; if (g[i] == g[j] && type[j] == 1 && c[i] <= c[j] && val < c[j]) { val = c[j]; at = j; } } if (!~at) continue; v[at] = v[i] = 1; int tmp = (val + c[i]) / 2; sum[g[i]]++; L[g[i]] = min(L[g[i]], tmp); R[g[i]] = max(R[g[i]], tmp); s[g[i]] += tmp; t[h[at]] += tmp; u[h[i]] += tmp; } } for (map<string, int>::iterator it = m2.begin(); it != m2.end(); it++) { string name = (*it).first; int num = (*it).second; if (!sum[num]) continue; printf("%s %d %d %d\n", name.c_str(), L[num], s[num] / sum[num], R[num]); } printf("--\n"); for (map<string, int>::iterator it = m1.begin(); it != m1.end(); it++) { string name = (*it).first; int num = (*it).second; printf("%s %d %d\n", name.c_str(), t[num], u[num]); } printf("----------\n"); } }
[["-", 0, 57, 64, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 57, 64, 9, 0, 7, 15, 16, 12, 22], ["-", 75, 76, 0, 9, 0, 7, 15, 16, 12, 22], ["+", 75, 76, 0, 9, 0, 7, 15, 16, 12, 22]]
1
1,024
#include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; const int MAXN = 1005; const int INF = 1 << 28; int N; bool used[MAXN]; string name[MAXN]; bool sell[MAXN]; string mono[MAXN]; int price[MAXN]; map<string, int> low, high, sum, num; map<string, int> B, S; int main() { while (cin >> N && N) { low.clear(); high.clear(); sum.clear(); num.clear(); B.clear(); S.clear(); for (int i = 0; i < N; ++i) { string sb; cin >> name[i] >> sb >> mono[i] >> price[i]; sell[i] = (sb == "SELL"); low[mono[i]] = INF; high[mono[i]] = -INF; sum[mono[i]] = 0; num[mono[i]] = 0; B[name[i]] = 0; S[name[i]] = 0; } fill(used, used + MAXN, false); for (int i = 0; i < N; ++i) { if (used[i]) continue; int p = sell[i] ? -1 : 1; int bestP = INF; int bestJ = -1; for (int j = 0; j < N; ++j) { if (used[j]) continue; if (name[i] == name[j]) continue; if (!(sell[i] ^ sell[j])) continue; if (mono[i] != mono[j]) continue; int newP = price[j] * p; if (newP < bestP) { bestP = newP; bestJ = j; } } if (bestJ == -1) continue; bestP *= p; int buyP, sellP; if (sell[i]) { buyP = bestP; sellP = price[i]; } else { buyP = price[i]; sellP = bestP; } if (sellP > buyP) continue; int v = (buyP + sellP) / 2; used[i] = used[bestJ] = true; low[mono[i]] = min(low[mono[i]], v); high[mono[i]] = max(high[mono[i]], v); sum[mono[i]] += v; ++num[mono[i]]; if (sell[i]) { B[name[bestJ]] += v; S[name[i]] += v; } else { B[name[i]] += v; S[name[bestJ]] += v; } } for (map<string, int>::iterator it = low.begin(); it != low.end(); ++it) { if (num[it->first] == 0) continue; cout << it->first; cout << " " << low[it->first]; cout << " " << sum[it->first] / num[it->first]; cout << " " << high[it->first] << endl; } cout << "--" << endl; for (map<string, int>::iterator it = B.begin(); it != B.end(); ++it) { cout << it->first; cout << " " << B[it->first]; cout << " " << S[it->first] << endl; } cout << "----------" << endl; } return 0; }
#include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; const int MAXN = 1005; const int INF = 1 << 28; int N; bool used[MAXN]; string name[MAXN]; bool sell[MAXN]; string mono[MAXN]; int price[MAXN]; map<string, int> low, high, sum, num; map<string, int> B, S; int main() { while (cin >> N && N) { low.clear(); high.clear(); sum.clear(); num.clear(); B.clear(); S.clear(); for (int i = 0; i < N; ++i) { string sb; cin >> name[i] >> sb >> mono[i] >> price[i]; sell[i] = (sb == "SELL"); low[mono[i]] = INF; high[mono[i]] = -INF; sum[mono[i]] = 0; num[mono[i]] = 0; B[name[i]] = 0; S[name[i]] = 0; } fill(used, used + MAXN, false); for (int i = 0; i < N; ++i) { if (used[i]) continue; int p = sell[i] ? -1 : 1; int bestP = INF; int bestJ = -1; for (int j = 0; j < i; ++j) { if (used[j]) continue; if (name[i] == name[j]) continue; if (!(sell[i] ^ sell[j])) continue; if (mono[i] != mono[j]) continue; int newP = price[j] * p; if (newP < bestP) { bestP = newP; bestJ = j; } } if (bestJ == -1) continue; bestP *= p; int buyP, sellP; if (sell[i]) { buyP = bestP; sellP = price[i]; } else { buyP = price[i]; sellP = bestP; } if (sellP > buyP) continue; int v = (buyP + sellP) / 2; used[i] = used[bestJ] = true; low[mono[i]] = min(low[mono[i]], v); high[mono[i]] = max(high[mono[i]], v); sum[mono[i]] += v; ++num[mono[i]]; if (sell[i]) { B[name[bestJ]] += v; S[name[i]] += v; } else { B[name[i]] += v; S[name[bestJ]] += v; } } for (map<string, int>::iterator it = low.begin(); it != low.end(); ++it) { if (num[it->first] == 0) continue; cout << it->first; cout << " " << low[it->first]; cout << " " << sum[it->first] / num[it->first]; cout << " " << high[it->first] << endl; } cout << "--" << endl; for (map<string, int>::iterator it = B.begin(); it != B.end(); ++it) { cout << it->first; cout << " " << B[it->first]; cout << " " << S[it->first] << endl; } cout << "----------" << endl; } return 0; }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22]]
1
778
// AOJ 1226: Fishnet // 2017.10.14 bal4u@uu #include <math.h> #include <stdio.h> typedef struct { double x, y; } PP; PP tbl[35][35]; double a[4][35]; void intersection(PP *p, PP *p1, PP *p2, PP *p3, PP *p4) { double bo, ua; bo = (p4->y - p3->y) * (p2->x - p1->x) - (p4->x - p3->x) * (p2->y - p1->y); ua = (p4->x - p3->x) * (p1->y - p3->y) - (p4->y - p3->y) * (p1->x - p3->x); p->x = p1->x + ua / bo * (p2->x - p1->x); p->y = p1->y + ua / bo * (p2->y - p1->y); } double calc_area(PP *pp) { int i; double s; for (s = 0, i = 0; i < 4; i++) s += (pp[i].x - pp[i + 1].x) * (pp[i].y + pp[i + 1].y); return fabs(s) / 2; } int main() { int n, i, j; double ans, s; PP t[5]; while (scanf("%d", &n) && n > 0) { for (j = 0; j < 4; j++) for (i = 1; i <= n; i++) scanf("%lf", &a[j][i]); n++; tbl[0][0].x = 0, tbl[0][0].y = 0; for (i = 1; i < n; i++) tbl[i][0].x = a[0][i], tbl[i][0].y = 0; tbl[0][n].x = 1, tbl[0][n].y = 0; tbl[0][n].x = 0, tbl[0][n].y = 1; for (i = 1; i < n; i++) tbl[i][n].x = a[1][i], tbl[i][n].y = 1; tbl[n][n].x = 1, tbl[n][n].y = 1; for (j = 1; j < n; j++) tbl[0][j].x = 0, tbl[0][j].y = a[2][j]; for (j = 1; j < n; j++) tbl[n][j].x = 1, tbl[n][j].y = a[3][j]; for (j = 1; j < n; j++) for (i = 1; i < n; i++) intersection(&tbl[i][j], &tbl[i][0], &tbl[i][n], &tbl[0][j], &tbl[n][j]); ans = 0; for (j = 1; j <= n; j++) for (i = 1; i <= n; i++) { t[0] = t[4] = tbl[i - 1][j - 1], t[1] = tbl[i][j - 1]; t[2] = tbl[i][j], t[3] = tbl[i - 1][j]; if ((s = calc_area(t)) >= ans) ans = s; } printf("%.6lf\n", ans); } return 0; }
// AOJ 1226: Fishnet // 2017.10.14 bal4u@uu #include <math.h> #include <stdio.h> typedef struct { double x, y; } PP; PP tbl[35][35]; double a[4][35]; void intersection(PP *p, PP *p1, PP *p2, PP *p3, PP *p4) { double bo, ua; bo = (p4->y - p3->y) * (p2->x - p1->x) - (p4->x - p3->x) * (p2->y - p1->y); ua = (p4->x - p3->x) * (p1->y - p3->y) - (p4->y - p3->y) * (p1->x - p3->x); p->x = p1->x + ua / bo * (p2->x - p1->x); p->y = p1->y + ua / bo * (p2->y - p1->y); } double calc_area(PP *pp) { int i; double s; for (s = 0, i = 0; i < 4; i++) s += (pp[i].x - pp[i + 1].x) * (pp[i].y + pp[i + 1].y); return fabs(s) / 2; } int main() { int n, i, j; double ans, s; PP t[5]; while (scanf("%d", &n) && n > 0) { for (j = 0; j < 4; j++) for (i = 1; i <= n; i++) scanf("%lf", &a[j][i]); n++; tbl[0][0].x = 0, tbl[0][0].y = 0; for (i = 1; i < n; i++) tbl[i][0].x = a[0][i], tbl[i][0].y = 0; tbl[n][0].x = 1, tbl[n][0].y = 0; tbl[0][n].x = 0, tbl[0][n].y = 1; for (i = 1; i < n; i++) tbl[i][n].x = a[1][i], tbl[i][n].y = 1; tbl[n][n].x = 1, tbl[n][n].y = 1; for (j = 1; j < n; j++) tbl[0][j].x = 0, tbl[0][j].y = a[2][j]; for (j = 1; j < n; j++) tbl[n][j].x = 1, tbl[n][j].y = a[3][j]; for (j = 1; j < n; j++) for (i = 1; i < n; i++) intersection(&tbl[i][j], &tbl[i][0], &tbl[i][n], &tbl[0][j], &tbl[n][j]); ans = 0; for (j = 1; j <= n; j++) for (i = 1; i <= n; i++) { t[0] = t[4] = tbl[i - 1][j - 1], t[1] = tbl[i][j - 1]; t[2] = tbl[i][j], t[3] = tbl[i - 1][j]; if ((s = calc_area(t)) >= ans) ans = s; } printf("%.6lf\n", ans); } return 0; }
[["-", 31, 11, 31, 118, 28, 69, 28, 69, 71, 13], ["+", 31, 11, 31, 118, 28, 69, 28, 69, 71, 22], ["-", 0, 34, 31, 11, 31, 118, 28, 69, 71, 22], ["+", 0, 34, 31, 11, 31, 118, 28, 69, 71, 13], ["-", 12, 11, 31, 118, 28, 69, 28, 69, 71, 13], ["+", 12, 11, 31, 118, 28, 69, 28, 69, 71, 22], ["-", 0, 34, 12, 11, 31, 118, 28, 69, 71, 22], ["+", 0, 34, 12, 11, 31, 118, 28, 69, 71, 13]]
0
827
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; class state { public: int n; string str; state() : n(0), str("") {} }; bool word(char n, char c) { return (n == '2' && (c == 'a' || c == 'b' || c == 'c') || n == '3' && (c == 'd' || c == 'e' || c == 'f') || n == '4' && (c == 'g' || c == 'h' || c == 'i') || n == '5' && (c == 'j' || c == 'k' || c == 'l') || n == '6' && (c == 'm' || c == 'n' || c == 'o') || n == '7' && (c == 'p' || c == 'q' || c == 'r' || c == 's') || n == '8' && (c == 't' || c == 'u' || c == 'v') || n == '9' && (c == 'w' || c == 'x' || c == 'y' || c == 'z')); } int main() { int n; while (cin >> n && n) { vector<string> v; string str; for (int i = 0; i < n; i++) { cin >> str; v.push_back(str); } cin >> str; queue<state> Q; state u, s; Q.push(u); while (!Q.empty()) { u = Q.front(); Q.pop(); for (int i = 0; i < v.size(); i++) { int j; for (j = 0; j < v[i].size(); j++) if (j + u.n >= str.size() || !word(str[j + u.n], v[i][j])) break; if (j == v[i].size()) { s = u; s.str += v[i]; s.n += v[i].size(); if (s.n == str.size()) cout << s.str << endl; else { s.str += " "; Q.push(s); } } } } cout << "--" << endl; } return 0; }
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; class state { public: int n; string str; state() : n(0), str("") {} }; bool word(char n, char c) { return (n == '2' && (c == 'a' || c == 'b' || c == 'c') || n == '3' && (c == 'd' || c == 'e' || c == 'f') || n == '4' && (c == 'g' || c == 'h' || c == 'i') || n == '5' && (c == 'j' || c == 'k' || c == 'l') || n == '6' && (c == 'm' || c == 'n' || c == 'o') || n == '7' && (c == 'p' || c == 'q' || c == 'r' || c == 's') || n == '8' && (c == 't' || c == 'u' || c == 'v') || n == '9' && (c == 'w' || c == 'x' || c == 'y' || c == 'z')); } int main() { int n; while (cin >> n && n) { vector<string> v; string str; for (int i = 0; i < n; i++) { cin >> str; v.push_back(str); } cin >> str; queue<state> Q; state u, s; Q.push(u); while (!Q.empty()) { u = Q.front(); Q.pop(); for (int i = 0; i < v.size(); i++) { int j; for (j = 0; j < v[i].size(); j++) if (j + u.n >= str.size() || !word(str[j + u.n], v[i][j])) break; if (j == v[i].size()) { s = u; s.str += v[i]; s.n += v[i].size(); if (s.n == str.size()) cout << s.str << "." << endl; else { s.str += " "; Q.push(s); } } } } cout << "--" << endl; } return 0; }
[["+", 64, 1, 0, 16, 31, 16, 12, 5, 0, 62], ["+", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 64, 9, 0, 57, 64, 1, 0, 16, 17, 151]]
1
547
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <vector> using namespace std; #define REP(i, b, n) for (int i = b; i < n; i++) #define rep(i, n) REP(i, 0, n) #define ALL(C) (C).begin(), (C).end() #define pb push_back #define mp make_pair const int N = 100; const int inf = (1 << 28); class Edge { public: int sh, sm, dh, dm; int d; int cost; }; class state { public: int now, h, m; int cost; bool operator<(const state &a) const { return cost > a.cost; } }; bool isless(int h, int m, int nh, int nm) { if (h < nh) return true; else if (h == nh && m <= nm) return true; return false; } bool isgreater(int h, int m, int nh, int nm) { if (h > nh) return true; else if (h == nh && m >= nm) return true; return false; } void dijkstra(int n, int s, vector<Edge> *edge, int cost[24][60][N], bool isrev) { rep(i, 24) rep(j, 60) rep(k, n) cost[i][j][k] = inf; priority_queue<state> Q; if (isrev) Q.push((state){s, 17, 59, 0}); else Q.push((state){s, 8, 0, 0}); while (!Q.empty()) { state now = Q.top(); Q.pop(); if (cost[now.h][now.m][now.now] != inf) continue; cost[now.h][now.m][now.now] = now.cost; rep(i, edge[now.now].size()) { if (isrev) { if (isgreater(now.h, now.m, edge[now.now][i].sh, edge[now.now][i].sm)) Q.push((state){edge[now.now][i].d, edge[now.now][i].dh, edge[now.now][i].dm, now.cost + edge[now.now][i].cost}); } else { if (isless(now.h, now.m, edge[now.now][i].sh, edge[now.now][i].sm)) Q.push((state){edge[now.now][i].d, edge[now.now][i].dh, edge[now.now][i].dm, now.cost + edge[now.now][i].cost}); } } } if (isrev) { rep(k, 100) { int tmp = inf; for (int i = 17; i >= 6; i--) { for (int j = 59; j >= 0; j--) { cost[i][j][k] = min(cost[i][j][k], tmp); tmp = min(cost[i][j][k], tmp); } } } } else { rep(k, 100) { int tmp = inf; rep(i, 24) { rep(j, 60) { cost[i][j][k] = min(cost[i][j][k], tmp); tmp = min(tmp, cost[i][j][k]); } } } } } // from tokyo,from hakodate, to tokyo,to hakodate int ft[24][60][N], fh[24][60][N], tt[24][60][N], th[24][60][N]; int solve(int n, int hakodate, int tokyo, vector<Edge> *edge, vector<Edge> *revedge) { dijkstra(n, tokyo, edge, ft, false); dijkstra(n, hakodate, edge, fh, false); dijkstra(n, tokyo, revedge, tt, true); dijkstra(n, hakodate, revedge, th, true); int ret = inf; rep(k, n) { int tmp = inf; REP(i, 8, 18) { rep(j, 30) { tmp = min(tmp, ft[i][j][k] + fh[i][j][k] + tt[i][j + 30][k] + th[i][j + 30][k]); } REP(j, 30, 60) { tmp = min(tmp, ft[i][j][k] + fh[i][j][k] + tt[i + 1][(j + 30) % 60][k] + th[i + 1][(j + 30) % 60][k]); } } ret = min(ret, tmp); } return ret; } int getnum(string &a, map<string, int> &M) { int index = M.size(); if (M.find(a) == M.end()) M[a] = index; return M[a]; } main() { int n; vector<Edge> edge[N]; vector<Edge> rev[N]; const string name[2] = {"Hakodate", "Tokyo"}; while (cin >> n && n) { rep(i, N) edge[i].clear(), rev[i].clear(); map<string, int> M; string from, to; int f, t; int sh, sm, dh, dm; int cost; char dummy; rep(i, n) { cin >> from >> sh >> dummy >> dummy >> sm; cin >> to >> dh >> dummy >> dummy >> dm; cin >> cost; f = getnum(from, M); t = getnum(to, M); edge[f].pb((Edge){sh, sm, dh, dm, t, cost}); rev[t].pb((Edge){dh, dm, sh, sm, f, cost}); } int ans = solve(M.size(), M[name[0]], M[name[1]], edge, rev); if (ans == inf) cout << 0 << endl; else cout << ans << endl; } }
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <vector> using namespace std; #define REP(i, b, n) for (int i = b; i < n; i++) #define rep(i, n) REP(i, 0, n) #define ALL(C) (C).begin(), (C).end() #define pb push_back #define mp make_pair const int N = 100; const int inf = (1 << 28); class Edge { public: int sh, sm, dh, dm; int d; int cost; }; class state { public: int now, h, m; int cost; bool operator<(const state &a) const { return cost > a.cost; } }; bool isless(int h, int m, int nh, int nm) { if (h < nh) return true; else if (h == nh && m <= nm) return true; return false; } bool isgreater(int h, int m, int nh, int nm) { if (h > nh) return true; else if (h == nh && m >= nm) return true; return false; } void dijkstra(int n, int s, vector<Edge> *edge, int cost[24][60][N], bool isrev) { rep(i, 24) rep(j, 60) rep(k, n) cost[i][j][k] = inf; priority_queue<state> Q; if (isrev) Q.push((state){s, 17, 59, 0}); else Q.push((state){s, 8, 0, 0}); while (!Q.empty()) { state now = Q.top(); Q.pop(); if (cost[now.h][now.m][now.now] != inf) continue; cost[now.h][now.m][now.now] = now.cost; rep(i, edge[now.now].size()) { if (isrev) { if (isgreater(now.h, now.m, edge[now.now][i].sh, edge[now.now][i].sm)) Q.push((state){edge[now.now][i].d, edge[now.now][i].dh, edge[now.now][i].dm, now.cost + edge[now.now][i].cost}); } else { if (isless(now.h, now.m, edge[now.now][i].sh, edge[now.now][i].sm)) Q.push((state){edge[now.now][i].d, edge[now.now][i].dh, edge[now.now][i].dm, now.cost + edge[now.now][i].cost}); } } } if (isrev) { rep(k, 100) { int tmp = inf; for (int i = 17; i >= 6; i--) { for (int j = 59; j >= 0; j--) { cost[i][j][k] = min(cost[i][j][k], tmp); tmp = min(cost[i][j][k], tmp); } } } } else { rep(k, 100) { int tmp = inf; rep(i, 24) { rep(j, 60) { cost[i][j][k] = min(cost[i][j][k], tmp); tmp = min(tmp, cost[i][j][k]); } } } } } // from tokyo,from hakodate, to tokyo,to hakodate int ft[24][60][N], fh[24][60][N], tt[24][60][N], th[24][60][N]; int solve(int n, int hakodate, int tokyo, vector<Edge> *edge, vector<Edge> *revedge) { dijkstra(n, tokyo, edge, ft, false); dijkstra(n, hakodate, edge, fh, false); dijkstra(n, tokyo, revedge, tt, true); dijkstra(n, hakodate, revedge, th, true); int ret = inf; rep(k, n) { int tmp = inf; REP(i, 8, 18) { rep(j, 30) { tmp = min(tmp, ft[i][j][k] + fh[i][j][k] + tt[i][j + 30][k] + th[i][j + 30][k]); } REP(j, 30, 60) { tmp = min(tmp, ft[i][j][k] + fh[i][j][k] + tt[i + 1][(j + 30) % 60][k] + th[i + 1][(j + 30) % 60][k]); } } ret = min(ret, tmp); } return ret; } int getnum(string &a, map<string, int> &M) { int index = M.size(); if (M.find(a) == M.end()) M[a] = index; return M[a]; } main() { int n; vector<Edge> edge[N]; vector<Edge> rev[N]; const string name[2] = {"Hakodate", "Tokyo"}; while (cin >> n && n) { rep(i, N) edge[i].clear(), rev[i].clear(); map<string, int> M; string from, to; int f, t; int sh, sm, dh, dm; int cost; char dummy; rep(i, n) { cin >> from >> sh >> dummy >> sm; cin >> to >> dh >> dummy >> dm; cin >> cost; f = getnum(from, M); t = getnum(to, M); edge[f].pb((Edge){sh, sm, dh, dm, t, cost}); rev[t].pb((Edge){dh, dm, sh, sm, f, cost}); } int ans = solve(M.size(), M[name[0]], M[name[1]], edge, rev); if (ans == inf) cout << 0 << endl; else cout << ans << endl; } }
[["-", 8, 9, 0, 1, 0, 16, 31, 16, 12, 22], ["-", 0, 14, 8, 9, 0, 1, 0, 16, 17, 152], ["-", 0, 1, 0, 16, 31, 16, 31, 16, 17, 152], ["-", 0, 1, 0, 16, 31, 16, 31, 16, 12, 22]]
1
1,414
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <valarray> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() typedef long long ll; typedef pair<int, int> pii; const int INF = 1 << 29; const double PI = acos(-1); const double EPS = 1e-8; struct P { int dst, time, cost; }; int cost1[2][601][100]; int cost2[2][601][100]; vector<P> g[601][100]; vector<P> h[601][100]; int main() { int n, m; while (cin >> m, m) { REP(i, 601) REP(j, 100) { g[i][j].clear(); h[i][j].clear(); } map<string, int> mp; mp["Tokyo"] = 0; mp["Hakodate"] = 1; n = 2; REP(i, m) { char s[20], t[20]; int sh, sm, th, tm, cost; scanf("%s %d:%d %s %d:%d %d", s, &sh, &sm, t, &th, &tm, &cost); int t1 = sh * 60 + sm - 480, t2 = th * 60 + tm - 480; if (t1 < 0 || t1 > 600 || t2 < 0 || t2 > 600) continue; string u(s), v(t); if (!mp.count(u)) mp[u] = n++; if (!mp.count(v)) mp[v] = n++; int a = mp[u], b = mp[v]; g[t1][a].push_back((P){b, t2, cost}); h[t2][b].push_back((P){a, t1, cost}); } REP(k, 2) REP(i, 601) REP(j, 100) cost1[k][i][j] = cost2[k][i][j] = INF; cost1[0][0][0] = cost1[1][0][1] = 0; REP(k, 2) { REP(i, 600) { REP(j, n) { cost1[k][i + 1][j] = min(cost1[k][i + 1][j], cost1[k][i][j]); FOR(it, g[i][j]) { cost1[k][it->time][it->dst] = min(cost1[k][it->time][it->dst], cost1[k][i][j] + it->cost); } } } } cost2[0][600][0] = cost2[1][600][1] = 0; REP(k, 2) { for (int i = 600; i > 0; --i) { REP(j, n) { cost2[k][i - 1][j] = min(cost2[k][i - 1][j], cost2[k][i][j]); FOR(it, h[i][j]) { cost2[k][it->time][it->dst] = min(cost2[k][it->time][it->dst], cost2[k][i][j] + it->cost); } } } } int ans = INF; REP(i, 601 - 30) { REP(j, n) { ans = min(ans, cost1[0][i][j] + cost1[1][i][j] + cost2[0][i + 30][j] + cost2[1][i + 30][j]); } } cout << (ans == INF ? 0 : ans) << endl; } }
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <valarray> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() typedef long long ll; typedef pair<int, int> pii; const int INF = 1 << 28; const double PI = acos(-1); const double EPS = 1e-8; struct P { int dst, time, cost; }; int cost1[2][601][100]; int cost2[2][601][100]; vector<P> g[601][100]; vector<P> h[601][100]; int main() { int n, m; while (cin >> m, m) { REP(i, 601) REP(j, 100) { g[i][j].clear(); h[i][j].clear(); } map<string, int> mp; mp["Tokyo"] = 0; mp["Hakodate"] = 1; n = 2; REP(i, m) { char s[20], t[20]; int sh, sm, th, tm, cost; scanf("%s %d:%d %s %d:%d %d", s, &sh, &sm, t, &th, &tm, &cost); int t1 = sh * 60 + sm - 480, t2 = th * 60 + tm - 480; if (t1 < 0 || t1 > 600 || t2 < 0 || t2 > 600) continue; string u(s), v(t); if (!mp.count(u)) mp[u] = n++; if (!mp.count(v)) mp[v] = n++; int a = mp[u], b = mp[v]; g[t1][a].push_back((P){b, t2, cost}); h[t2][b].push_back((P){a, t1, cost}); } REP(k, 2) REP(i, 601) REP(j, 100) cost1[k][i][j] = cost2[k][i][j] = INF; cost1[0][0][0] = cost1[1][0][1] = 0; REP(k, 2) { REP(i, 600) { REP(j, n) { cost1[k][i + 1][j] = min(cost1[k][i + 1][j], cost1[k][i][j]); FOR(it, g[i][j]) { cost1[k][it->time][it->dst] = min(cost1[k][it->time][it->dst], cost1[k][i][j] + it->cost); } } } } cost2[0][600][0] = cost2[1][600][1] = 0; REP(k, 2) { for (int i = 600; i > 0; --i) { REP(j, n) { cost2[k][i - 1][j] = min(cost2[k][i - 1][j], cost2[k][i][j]); FOR(it, h[i][j]) { cost2[k][it->time][it->dst] = min(cost2[k][it->time][it->dst], cost2[k][i][j] + it->cost); } } } } int ans = INF; REP(i, 601 - 30) { REP(j, n) { ans = min(ans, cost1[0][i][j] + cost1[1][i][j] + cost2[0][i + 30][j] + cost2[1][i + 30][j]); } } cout << (ans == INF ? 0 : ans) << endl; } }
[["-", 0, 30, 0, 43, 49, 50, 51, 16, 12, 13], ["+", 0, 30, 0, 43, 49, 50, 51, 16, 12, 13]]
1
904
N, i, M; double X[30], Y[30], Z[30], cx, cy, cz, d, m; #define Q(x) (x) * (x) double D(i) { return sqrt(Q(X[i] - cx) + Q(Y[i] - cy) + Q(Z[i] - cz)); } int L() { for (m = i = 0; i < N; i++) if (D(i) > m) m = D(i), M = i; return M; } main(t) { for (; scanf("%d", &N), N; printf("%.f\n", D(L()))) { for (i = 0; i < N; ++i) scanf("%lf%lf%lf", X + i, Y + i, Z + i); cx = cy = cz = 0.5; for (d = 0.5; d > 1e-8; d /= 2) { for (t = 0; t < 100; ++t) { int i = L(); cx += (X[i] - cx) * d; cy += (Y[i] - cy) * d; cz += (Z[i] - cz) * d; } } } exit(0); }
N, i, M; double X[30], Y[30], Z[30], cx, cy, cz, d, m; #define Q(x) (x) * (x) double D(i) { return sqrt(Q(X[i] - cx) + Q(Y[i] - cy) + Q(Z[i] - cz)); } int L() { for (m = i = 0; i < N; i++) if (D(i) > m) m = D(i), M = i; return M; } main(t) { for (; scanf("%d", &N), N; printf("%f\n", D(L()))) { for (i = 0; i < N; ++i) scanf("%lf%lf%lf", X + i, Y + i, Z + i); cx = cy = cz = 0.5; for (d = 0.5; d > 1e-8; d /= 2) { for (t = 0; t < 100; ++t) { int i = L(); cx += (X[i] - cx) * d; cy += (Y[i] - cy) * d; cz += (Z[i] - cz) * d; } } } exit(0); }
[["-", 0, 7, 26, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 7, 26, 2, 3, 4, 0, 5, 0, 6]]
0
283
#include <cmath> #include <cstdio> #include <vector> using namespace std; typedef vector<double> point; typedef const point &rpoint; point add(rpoint p1, rpoint p2) { point p(3); for (int i = 0; i < 3; ++i) p[i] = p1[i] + p2[i]; return p; } point sub(rpoint p1, rpoint p2) { point p(3); for (int i = 0; i < 3; ++i) p[i] = p1[i] - p2[i]; return p; } point mul(double t, rpoint p2) { point p(3); for (int i = 0; i < 3; ++i) p[i] = t * p2[i]; return p; } double abs(rpoint p) { double s = 0.0; for (int i = 0; i < 3; ++i) s += p[i] * p[i]; return sqrt(s); } int main() { int n; point p(3); while (scanf("%d", &n), n != 0) { vector<point> v; v.reserve(n); for (int i = 0; i < n; ++i) { for (int j = 0; j < 3; ++j) { scanf("%lf", &p[j]); } v.push_back(p); } double mv = 0.5; double minr; while (mv > 1e-8) { for (int k = 0; k < 10; ++k) { minr = -1.0; int mini = -1; for (int i = 0; i < n; ++i) { double r = abs(sub(v[i], p)); if (minr < r) { minr = r; mini = i; } } p = add(p, mul(mv, sub(v[mini], p))); } mv *= 0.5; } printf("%.10f\n", minr); } }
#include <cmath> #include <cstdio> #include <vector> using namespace std; typedef vector<double> point; typedef const point &rpoint; point add(rpoint p1, rpoint p2) { point p(3); for (int i = 0; i < 3; ++i) p[i] = p1[i] + p2[i]; return p; } point sub(rpoint p1, rpoint p2) { point p(3); for (int i = 0; i < 3; ++i) p[i] = p1[i] - p2[i]; return p; } point mul(double t, rpoint p2) { point p(3); for (int i = 0; i < 3; ++i) p[i] = t * p2[i]; return p; } double abs(rpoint p) { double s = 0.0; for (int i = 0; i < 3; ++i) s += p[i] * p[i]; return sqrt(s); } int main() { int n; point p(3); while (scanf("%d", &n), n != 0) { vector<point> v; v.reserve(n); for (int i = 0; i < n; ++i) { for (int j = 0; j < 3; ++j) { scanf("%lf", &p[j]); } v.push_back(p); } double mv = 0.5; double minr; while (mv > 1e-8) { for (int k = 0; k < 50; ++k) { minr = -1.0; int mini = -1; for (int i = 0; i < n; ++i) { double r = abs(sub(v[i], p)); if (minr < r) { minr = r; mini = i; } } p = add(p, mul(mv, sub(v[mini], p))); } mv *= 0.5; } printf("%.10f\n", minr); } }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13]]
1
435
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <vector> using namespace std; struct P { double x, y, z; }; double dist(P a, P b) { double x = a.x - b.x; double y = a.y - b.y; double z = a.z - b.z; return x * x + y * y + z * z; } int main() { int N; P p[30]; while (cin >> N && N) { for (int i = 0; i < N; i++) cin >> p[i].x >> p[i].y >> p[i].z; double e = 1.0; P pos; pos.x = 0; pos.y = 0; pos.z = 0; while (e > 1e-8) { double dis = -1; int idx = 0; for (int i = 0; i < N; i++) { if (dis < dist(pos, p[i])) { dis = dist(pos, p[i]); idx = i; } } double x = p[idx].x - pos.x; double y = p[idx].y - pos.y; double z = p[idx].z - pos.z; pos.x += x * e; pos.y += y * e; pos.z += z * e; e *= 0.98; } double dis = -1; int idx = 0; for (int i = 0; i < N; i++) { if (dis < dist(pos, p[i])) { dis = dist(pos, p[i]); idx = i; } } printf("%.8lf\n", sqrt(dis)); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <vector> using namespace std; struct P { double x, y, z; }; double dist(P a, P b) { double x = a.x - b.x; double y = a.y - b.y; double z = a.z - b.z; return x * x + y * y + z * z; } int main() { int N; P p[30]; while (cin >> N && N) { for (int i = 0; i < N; i++) cin >> p[i].x >> p[i].y >> p[i].z; double e = 1.0; P pos; pos.x = 0; pos.y = 0; pos.z = 0; while (e > 1e-8) { double dis = -1; int idx = 0; for (int i = 0; i < N; i++) { if (dis < dist(pos, p[i])) { dis = dist(pos, p[i]); idx = i; } } double x = p[idx].x - pos.x; double y = p[idx].y - pos.y; double z = p[idx].z - pos.z; pos.x += x * e; pos.y += y * e; pos.z += z * e; e *= 0.998; } double dis = -1; int idx = 0; for (int i = 0; i < N; i++) { if (dis < dist(pos, p[i])) { dis = dist(pos, p[i]); idx = i; } } printf("%.8lf\n", sqrt(dis)); } }
[["-", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13]]
1
380
import java.util.ArrayList; import java.util.Scanner; public class Main { Scanner sc = new Scanner(System.in); int[] sosuu = new int[100001]; public void run() { sosuuCalc(); while (sc.hasNext()) { int m = sc.nextInt(); double a = sc.nextInt(); double b = sc.nextInt(); double c = a / b; if (m == 0 && a == 0 && b == 0) break; else calc(m, c); } } public void sosuuCalc() { sosuu[0] = -1; sosuu[1] = -1; for (int i = 2; i < 100001; i++) { if (sosuu[i] == 0) { sosuu[i] = 1; for (int j = i * 2; j < 100001; j = j + i) { sosuu[j] = -1; } } } } public void calc(int m, double c) { int t = 0; for (int pq = m; pq > 0; pq--) { for (int p = 2; p * p <= pq; p++) { if (sosuu[p] == 1 && pq % p == 0) { int q = pq / p; if (sosuu[q] == 1 && c <= ((double)p) / ((double)q)) { System.out.println(p + " " + q); t++; break; } } } if (t != 0) break; } } public static void main(String[] args) { new Main().run(); } }
import java.util.ArrayList; import java.util.Scanner; public class Main { Scanner sc = new Scanner(System.in); int[] sosuu = new int[100001]; public void run() { sosuuCalc(); while (sc.hasNext()) { int m = sc.nextInt(); double a = sc.nextInt(); double b = sc.nextInt(); double c = a / b; if (m == 0 && a == 0 && b == 0) break; else calc(m, c); } } public void sosuuCalc() { sosuu[0] = -1; sosuu[1] = -1; for (int i = 2; i < 100001; i++) { if (sosuu[i] == 0) { sosuu[i] = 1; for (int j = i * 2; j < 100001; j = j + i) { sosuu[j] = -1; } } } } public void calc(int m, double c) { int t = 0; for (int pq = m; pq > 0; pq--) { for (int p = 2; p * p <= pq; p++) { if (sosuu[p] == 1 && pq % p == 0) { int q = pq / p; if (sosuu[q] == 1 && c <= ((double)p) / ((double)q)) { System.out.println(p + " " + q); t++; break; } } } if (t != 0) break; } } public static void main(String[] args) { new Main().run(); } }
[["-", 3, 4, 0, 16, 31, 16, 12, 5, 0, 491], ["+", 3, 4, 0, 16, 31, 16, 12, 5, 0, 491]]
3
357
#include <stdio.h> char isprime[100000]; int pr[100000]; int primenum; void mpl(void) { int i, j; for (i = 0; i < 100000; i++) isprime[i] = 1; isprime[0] = isprime[1] = 0; primenum = 0; for (i = 2; i < 100000; i++) { if (isprime[i]) { pr[primenum++] = i; for (j = i + i; j < 100000; j += i) isprime[j] = 0; } } } int main(void) { int m, a, b; mpl(); while (scanf("%d%d%d", &m, &a, &b) == 3 && (m | a | b) != 0) { int i; int p = 0, q = 0; for (i = 0; i < primenum; i++) { int l = i, r = primenum - 1; while (l <= r) { int j = (l + r) / 2; if ((long long)pr[i] * pr[j] <= m && (long long)a * pr[j] <= (long long)pr[i] * b) { l = j + 1; } else { r = j - 1; } } if ((long long)pr[i] * pr[l - 1] <= m && (long long)a * pr[l - 1] <= (long long)pr[i] * b && pr[i] * pr[l - 1] > p * q) { p = pr[i]; q = pr[l - 1]; } } printf("%d %d\n", p, q); } return 0; }
#include <stdio.h> char isprime[100000]; int pr[100000]; int primenum; void mpl(void) { int i, j; for (i = 0; i < 100000; i++) isprime[i] = 1; isprime[0] = isprime[1] = 0; primenum = 0; for (i = 2; i < 100000; i++) { if (isprime[i]) { pr[primenum++] = i; for (j = i + i; j < 100000; j += i) isprime[j] = 0; } } } int main(void) { int m, a, b; mpl(); while (scanf("%d%d%d", &m, &a, &b) == 3 && (m | a | b) != 0) { int i; int p = 0, q = 0; for (i = 0; i < primenum; i++) { int l = i, r = primenum - 1; while (l <= r) { int j = (l + r) / 2; if ((long long)pr[i] * pr[j] <= m && (long long)a * pr[j] <= (long long)pr[i] * b) { l = j + 1; } else { r = j - 1; } } if (l > i && (long long)pr[i] * pr[l - 1] <= m && (long long)a * pr[l - 1] <= (long long)pr[i] * b && pr[i] * pr[l - 1] > p * q) { p = pr[i]; q = pr[l - 1]; } } printf("%d %d\n", p, q); } return 0; }
[["+", 0, 16, 31, 16, 31, 16, 31, 16, 31, 22], ["+", 0, 16, 31, 16, 31, 16, 31, 16, 17, 47], ["+", 0, 16, 31, 16, 31, 16, 31, 16, 12, 22], ["+", 15, 23, 0, 16, 31, 16, 31, 16, 17, 98]]
0
377
#include <stdio.h> int main() { int a, b, c, M = 100005, mi, mj, i, j, d[100010], r = 0, max; char p[100010] = {1, 1}; for (i = 2; i * i < M; i++) { if (p[i]) continue; for (j = i * i; j < M; j += i) p[j] = 1; } for (i = r = 0; i < M; i++) { if (p[i] == 0) d[r++] = i; } while (scanf("%d %d %d", &a, &b, &c), a) { for (i = max = 0; d[i] < a; i++) { for (j = 0; j <= i && d[i] * d[j] < a; j++) { if (b * d[i] > c * d[j]) continue; if (max < d[i] * d[j]) max = d[mi = i] * d[mj = j]; } } printf("%d %d\n", d[mj], d[mi]); } return 0; }
#include <stdio.h> int main() { int a, b, c, M = 100005, mi, mj, i, j, d[100010], r = 0, max; char p[100010] = {1, 1}; for (i = 2; i * i < M; i++) { if (p[i]) continue; for (j = i * i; j < M; j += i) p[j] = 1; } for (i = r = 0; i < M; i++) { if (p[i] == 0) d[r++] = i; } while (scanf("%d %d %d", &a, &b, &c), a) { for (i = max = 0; d[i] <= a; i++) { for (j = 0; j <= i && d[i] * d[j] <= a; j++) { if (b * d[i] > c * d[j]) continue; if (max < d[i] * d[j]) max = d[mi = i] * d[mj = j]; } } printf("%d %d\n", d[mj], d[mi]); } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 17, 18], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 19]]
0
271
#include <stdio.h> #include <stdlib.h> #define num 100000 #define true 1 #define false 0 void isPrime(); int isCheck(int i, int j, int m, int a, int b); int isbigger(int num1, int num2); int is1(int i, int j); int N; int *prime; int main() { prime = (int *)malloc(num); isPrime(); int m, a, b; int i, j, x, y; while (scanf("%d %d %d", &m, &a, &b) != EOF) { int max = 0; for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { if (isCheck(i, j, m, a, b)) break; if (isbigger(max, prime[i] * prime[j]) && is1(i, j)) { max = prime[i] * prime[j]; x = i; y = j; } } } printf("%d %d\n", prime[x], prime[y]); } free(prime); exit(0); } void isPrime() { int i, index = 0; for (int j = 2; j < num; j++) { for (i = 2; i < j; ++i) { if (j % i == 0) { break; } } if (j == i) { *(prime + index) = j; index++; } } N = index; } int isCheck(int i, int j, int m, int a, int b) { double min = (double)a / b; double real = (double)prime[i] / prime[j]; int pq = prime[i] * prime[j]; // printf("%d %d %d %lf\n",prime[i],prime[j],j,real); if (min <= real && pq <= m) return false; return true; } int is1(int i, int j) { double real = (double)prime[i] / prime[j]; if (real <= 1.0) return true; return false; } int isbigger(int num1, int num2) { if (num1 >= num2) { return false; } return true; }
#include <stdio.h> #include <stdlib.h> #define num 100000 #define true 1 #define false 0 void isPrime(); int isCheck(int i, int j, int m, int a, int b); int isbigger(int num1, int num2); int is1(int i, int j); int N; int *prime; int main() { prime = (int *)malloc(num); isPrime(); int m, a, b; int i, j, x, y; while (scanf("%d %d %d", &m, &a, &b), m) { int max = 0; for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { if (isCheck(i, j, m, a, b)) break; if (isbigger(max, prime[i] * prime[j]) && is1(i, j)) { max = prime[i] * prime[j]; x = i; y = j; } } } printf("%d %d\n", prime[x], prime[y]); } free(prime); exit(0); } void isPrime() { int i, index = 0; for (int j = 2; j < num; j++) { for (i = 2; i < j; ++i) { if (j % i == 0) { break; } } if (j == i) { *(prime + index) = j; index++; } } N = index; } int isCheck(int i, int j, int m, int a, int b) { double min = (double)a / b; double real = (double)prime[i] / prime[j]; int pq = prime[i] * prime[j]; if (min <= real && pq <= m) return false; return true; } int is1(int i, int j) { double real = (double)prime[i] / prime[j]; if (real <= 1.0) return true; return false; } int isbigger(int num1, int num2) { if (num1 >= num2) { return false; } return true; }
[["-", 8, 9, 0, 52, 15, 23, 0, 16, 17, 79], ["-", 8, 9, 0, 52, 15, 23, 0, 16, 12, 22], ["+", 8, 9, 0, 52, 15, 23, 0, 34, 0, 21], ["+", 8, 9, 0, 52, 15, 23, 0, 34, 12, 22]]
0
469
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1232 #include <algorithm> #include <climits> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <numeric> #include <vector> #define ALL(v) (v).begin(), (v).end() #define REP(i, p, n) for (int i = p; i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) #define dump(a) (cerr << #a << "=" << (a) << endl) #define DUMP(list) \ cout << "{ "; \ for (auto nth : list) { \ cout << nth << " "; \ } \ cout << "}" << endl; using namespace std; #define PRIME_MAX 100000 vector<int> init_prime() { bool is_prime[PRIME_MAX]; is_prime[0] = false; is_prime[1] = false; REP(i, 2, PRIME_MAX) is_prime[i] = true; REP(i, 2, PRIME_MAX) { if (is_prime[i]) { for (int j = i + i; j < PRIME_MAX; j += i) is_prime[j] = false; } } vector<int> prime; rep(i, PRIME_MAX) { if (is_prime[i]) { prime.push_back(i); } } return prime; } int main() { vector<int> prime = init_prime(); int M; double A, B; while (cin >> M >> A >> B && M) { int p, q; int max_pq = 0; double a_b = A / B; rep(i, prime.size()) { if (prime[i] > M) { break; } REP(j, i, prime.size()) { if (prime[i] * prime[j] > M) { break; } int pq = prime[i] * prime[j]; double p_q = (double)prime[i] / prime[j]; if (pq > max_pq && a_b <= p_q && p_q <= 1.0) { max_pq = pq; p = prime[i]; q = prime[j]; } } } cout << p << " " << q << endl; } return 0; }
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1232 #include <algorithm> #include <climits> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <numeric> #include <vector> #define ALL(v) (v).begin(), (v).end() #define REP(i, p, n) for (int i = p; i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) #define dump(a) (cerr << #a << "=" << (a) << endl) #define DUMP(list) \ cout << "{ "; \ for (auto nth : list) { \ cout << nth << " "; \ } \ cout << "}" << endl; using namespace std; #define PRIME_MAX 100000 vector<int> init_prime() { bool is_prime[PRIME_MAX]; is_prime[0] = false; is_prime[1] = false; REP(i, 2, PRIME_MAX) is_prime[i] = true; REP(i, 2, PRIME_MAX) { if (is_prime[i]) { for (int j = i + i; j < PRIME_MAX; j += i) is_prime[j] = false; } } vector<int> prime; rep(i, PRIME_MAX) { if (is_prime[i]) { prime.push_back(i); } } return prime; } int main() { vector<int> prime = init_prime(); int M; double A, B; while (cin >> M >> A >> B && M) { int p, q; int max_pq = 0; double a_b = A / B; rep(i, prime.size()) { if (prime[i] > M) { break; } rep(j, prime.size()) { if (prime[i] * prime[j] > M) { break; } int pq = prime[i] * prime[j]; double p_q = (double)prime[i] / prime[j]; if (pq > max_pq && a_b <= p_q && p_q <= 1.0) { max_pq = pq; p = prime[i]; q = prime[j]; } } } cout << p << " " << q << endl; } return 0; }
[["-", 8, 9, 0, 9, 0, 1, 0, 2, 63, 22], ["+", 8, 9, 0, 9, 0, 1, 0, 2, 63, 22], ["-", 0, 9, 0, 1, 0, 2, 3, 4, 0, 21], ["-", 0, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
1
376
import static java.lang.Math.*; import java.awt.geom.*; import java.util.*; public class Main { final Scanner sc = new Scanner(System.in); public static void main(String[] args) { new Main().init(); } void init() { new AOJ1237(); } class AOJ1237 { AOJ1237() { while (true) { target = sc.nextInt(); seq = sc.next(); if (target == 0) break; solve(); } } int target, ans; int[] alist; boolean valid; String seq; void solve() { valid = true; ans = -1; alist = new int[seq.length()]; for (int i = 0; i < alist.length; ++i) alist[i] = -1; dfs(new int[seq.length()], seq, 0, 0); if (ans < 0) System.out.println("rejected"); else if (!valid) System.out.println("error"); else { System.out.print(ans); for (int i = 0; i < alist.length; ++i) if (alist[i] >= 0) System.out.print(" " + alist[i]); System.out.println(); } } void dfs(int[] list, String remain, int sum, int depth) { int cur = Integer.parseInt(remain); if (sum + cur <= target) { if (sum + cur == ans) valid = false; if (sum + cur > ans) { valid = true; ans = sum + cur; for (int i = 0; i < depth; ++i) alist[i] = list[i]; alist[depth] = cur; for (int i = depth + 1; i < list.length; ++i) alist[i] = -1; } } for (int i = 1; i <= remain.length(); ++i) { String l = remain.substring(0, i), r = remain.substring(i); if (r.isEmpty()) continue; cur = Integer.parseInt(l); if (sum + cur > target) continue; list[depth] = cur; dfs(list, r, sum + cur, depth + 1); } } } }
import static java.lang.Math.*; import java.awt.geom.*; import java.util.*; public class Main { final Scanner sc = new Scanner(System.in); public static void main(String[] args) { new Main().init(); } void init() { new AOJ1237(); } class AOJ1237 { AOJ1237() { while (true) { target = sc.nextInt(); seq = sc.next(); if (target == 0) break; solve(); } } int target, ans; int[] alist; boolean valid; String seq; void solve() { valid = true; ans = -1; alist = new int[seq.length()]; for (int i = 0; i < alist.length; ++i) alist[i] = -1; dfs(new int[seq.length()], seq, 0, 0); if (ans < 0) System.out.println("error"); else if (!valid) System.out.println("rejected"); else { System.out.print(ans); for (int i = 0; i < alist.length; ++i) if (alist[i] >= 0) System.out.print(" " + alist[i]); System.out.println(); } } void dfs(int[] list, String remain, int sum, int depth) { int cur = Integer.parseInt(remain); if (sum + cur <= target) { if (sum + cur == ans) valid = false; if (sum + cur > ans) { valid = true; ans = sum + cur; for (int i = 0; i < depth; ++i) alist[i] = list[i]; alist[depth] = cur; for (int i = depth + 1; i < list.length; ++i) alist[i] = -1; } } for (int i = 1; i <= remain.length(); ++i) { String l = remain.substring(0, i), r = remain.substring(i); if (r.isEmpty()) continue; cur = Integer.parseInt(l); if (sum + cur > target) continue; list[depth] = cur; dfs(list, r, sum + cur, depth + 1); } } } }
[["-", 64, 1, 0, 492, 3, 4, 0, 5, 0, 491], ["+", 64, 1, 0, 492, 3, 4, 0, 5, 0, 491]]
3
519
#include <algorithm> #include <cstring> #include <iostream> #include <map> #include <set> #include <sstream> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define INF 1 << 30 #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef pair<int, int> P; int stoa(string s) { int ret = 0, t = 1; for (int i = s.size() - 1; i >= 0; i--) { ret += (s[i] - '0') * t; t *= 10; } return ret; } int n; string s; int vmin = INF, vmax = 0, ans = 0, cnt = 0; vector<int> res; void dfs(int i, int sum, vector<int> v) { if (i == s.size()) { vmin = min(vmin, sum); vmax = max(vmax, sum); if (sum <= n) { if (ans < sum) { ans = sum; cnt = 1; res.clear(); rep(i, v.size()) res.push_back(v[i]); } else if (ans == sum) { cnt++; } else cnt = 0; } return; } REP(j, 1, s.size() + 1) { // cout << "i:" << i << " j:" << j << " sum:" << sum << " " << s.substr(i, // j) << " " << stoa(s.substr(i, j)) << endl; if (i + j <= s.size()) { vector<int> t(v.begin(), v.end()); t.push_back(stoa(s.substr(i, j))); dfs(i + j, sum + stoa(s.substr(i, j)), t); } else return; } } int main() { while (cin >> n >> s) { if (n == 0 && s == "0") break; vmin = INF, vmax = 0, ans = 0, cnt = 0; res.clear(); vector<int> v; dfs(0, 0, v); if (vmin > n) { cout << "error" << endl; } else if (cnt >= 2) { cout << "rejected" << endl; } else { cout << ans; rep(i, res.size()) { cout << " " << res[i]; } cout << endl; } } return 0; }
#include <algorithm> #include <cstring> #include <iostream> #include <map> #include <set> #include <sstream> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define INF 1 << 30 #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef pair<int, int> P; int stoa(string s) { int ret = 0, t = 1; for (int i = s.size() - 1; i >= 0; i--) { ret += (s[i] - '0') * t; t *= 10; } return ret; } int n; string s; int vmin = INF, vmax = 0, ans = 0, cnt = 0; vector<int> res; void dfs(int i, int sum, vector<int> v) { if (i == s.size()) { vmin = min(vmin, sum); vmax = max(vmax, sum); if (sum <= n) { if (ans < sum) { ans = sum; cnt = 1; res.clear(); rep(i, v.size()) res.push_back(v[i]); } else if (ans == sum) { cnt++; } } return; } REP(j, 1, s.size() + 1) { // cout << "i:" << i << " j:" << j << " sum:" << sum << " " << s.substr(i, // j) << " " << stoa(s.substr(i, j)) << endl; if (i + j <= s.size()) { vector<int> t(v.begin(), v.end()); t.push_back(stoa(s.substr(i, j))); dfs(i + j, sum + stoa(s.substr(i, j)), t); } else return; } } int main() { while (cin >> n >> s) { if (n == 0 && s == "0") break; vmin = INF, vmax = 0, ans = 0, cnt = 0; res.clear(); vector<int> v; dfs(0, 0, v); if (vmin > n) { cout << "error" << endl; } else if (cnt >= 2) { cout << "rejected" << endl; } else { cout << ans; rep(i, res.size()) { cout << " " << res[i]; } cout << endl; } } return 0; }
[["-", 0, 57, 75, 76, 0, 57, 75, 76, 0, 95], ["-", 0, 57, 75, 76, 0, 1, 0, 11, 31, 22], ["-", 0, 57, 75, 76, 0, 1, 0, 11, 17, 32], ["-", 0, 57, 75, 76, 0, 1, 0, 11, 12, 13], ["-", 75, 76, 0, 57, 75, 76, 0, 1, 0, 35]]
1
501
#include <bits/stdc++.h> using namespace std; int main() { string str; int target; while (cin >> target >> str, str != "0" || target != 0) { int len = str.size() - 1; int ans = -1; int ansi = 0; bool reject = false; for (int i = 0; i < (1 << len); i++) { int prev = 0; int sum = 0; for (int j = 0; j < len; j++) { if (i & (1 << j)) { sum += stoi(str.substr(prev, (j - prev) + 1), nullptr, 10); prev = j + 1; } } sum += stoi(str.substr(prev, str.size() - prev), nullptr, 10); // cout << sum << endl; if (sum <= target) { if (ans == sum) { reject = true; } else if (ans < sum) { ans = sum; reject = false; ansi = i; } } } if (ans == -1) { cout << "error" << endl; } else if (reject) { cout << "reject" << endl; } else { cout << ans; int prev = 0; for (int j = 0; j < len; j++) { if (ansi & (1 << j)) { cout << " " << stoi(str.substr(prev, (j - prev) + 1), nullptr, 10); prev = j + 1; } } cout << " " << stoi(str.substr(prev, str.size() - prev), nullptr, 10); cout << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { string str; int target; while (cin >> target >> str, str != "0" || target != 0) { int len = str.size() - 1; int ans = -1; int ansi = 0; bool reject = false; for (int i = 0; i < (1 << len); i++) { int prev = 0; int sum = 0; for (int j = 0; j < len; j++) { if (i & (1 << j)) { sum += stoi(str.substr(prev, (j - prev) + 1), nullptr, 10); prev = j + 1; } } sum += stoi(str.substr(prev, str.size() - prev), nullptr, 10); // cout << sum << endl; if (sum <= target) { if (ans == sum) { reject = true; } else if (ans < sum) { ans = sum; reject = false; ansi = i; } } } if (ans == -1) { cout << "error" << endl; } else if (reject) { cout << "rejected" << endl; } else { cout << ans; int prev = 0; for (int j = 0; j < len; j++) { if (ansi & (1 << j)) { cout << " " << stoi(str.substr(prev, (j - prev) + 1), nullptr, 10); prev = j + 1; } } cout << " " << stoi(str.substr(prev, str.size() - prev), nullptr, 10); cout << endl; } } }
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
355
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <set> #include <stack> #include <string> #include <vector> #define REP(i, n) for (int i = 0; i < n; ++i) #define REPR(i, n) for (int i = n; i >= 0; --i) #define REPI(itr, v) \ for (vector<int>::iterator itr = v.begin(); itr != v.end(); ++itr) #define REPIR(itr, v) for (auto itr = v.rbegin(); itr != v.rend(); ++itr) #define FOR(i, a, b) for (int i = a; i < b; ++i) #define SORT(v, n) sort(v, v + n) #define SORTV(v) sort(v.begin(), v.end()) #define llong long long #define INF 999999999 #define SUR 1000000007 #define pb(a) push_back(a) #define pf(a) push_front(a) #define MP make_pair int dx[] = {0, 0, -1, 1}; int dy[] = {1, -1, 0, 0}; using namespace std; int t, max_num, f = 0; vector<int> ans; int get_nextnum(int x) { char s[10]; sprintf(s, "%d", x); return s[0] + '0'; } int calc_digit(int x) { int res = 0; while (x != 0) { x /= 10; res++; } return res; } void solve(vector<int> v, int digit, int n, int num) { if (n == digit) { int sum = 0; v.pb(num); REPI(itr, v) { sum += *itr; } if (t >= sum && sum == max_num) { f = 1; } else if (t >= sum && sum > max_num) { max_num = sum; f = 0; ans.clear(); copy(v.begin(), v.end(), back_inserter(ans)); } return; } solve(v, digit, n + 1, num); if (get_nextnum(num) != 0) { int tmp = 1; REP(i, digit - n) { tmp *= 10; } v.pb(num / tmp); solve(v, digit, n + 1, num % tmp); } } int main() { while (true) { int num; scanf("%d %d", &t, &num); if (t == 0 && num == 0) break; if (t == num) { printf("%d %d\n", t, num); continue; } int digit = calc_digit(num); max_num = -1, f = 0; vector<int> v; solve(v, digit, 1, num); if (max_num == -1) { printf("error\n"); } else if (f == 1) { printf("reject\n"); } else { printf("%d ", max_num); REPI(itr, ans) { printf("%d", *itr); vector<int>::iterator itrt = itr; itrt++; if (itrt != ans.end()) { printf(" "); } } printf("\n"); } } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <set> #include <stack> #include <string> #include <vector> #define REP(i, n) for (int i = 0; i < n; ++i) #define REPR(i, n) for (int i = n; i >= 0; --i) #define REPI(itr, v) \ for (vector<int>::iterator itr = v.begin(); itr != v.end(); ++itr) #define REPIR(itr, v) for (auto itr = v.rbegin(); itr != v.rend(); ++itr) #define FOR(i, a, b) for (int i = a; i < b; ++i) #define SORT(v, n) sort(v, v + n) #define SORTV(v) sort(v.begin(), v.end()) #define llong long long #define INF 999999999 #define SUR 1000000007 #define pb(a) push_back(a) #define pf(a) push_front(a) #define MP make_pair int dx[] = {0, 0, -1, 1}; int dy[] = {1, -1, 0, 0}; using namespace std; int t, max_num, f = 0; vector<int> ans; int get_nextnum(int x) { char s[10]; sprintf(s, "%d", x); return s[0] + '0'; } int calc_digit(int x) { int res = 0; while (x != 0) { x /= 10; res++; } return res; } void solve(vector<int> v, int digit, int n, int num) { if (n == digit) { int sum = 0; v.pb(num); REPI(itr, v) { sum += *itr; } if (t >= sum && sum == max_num) { f = 1; } else if (t >= sum && sum > max_num) { max_num = sum; f = 0; ans.clear(); copy(v.begin(), v.end(), back_inserter(ans)); } return; } solve(v, digit, n + 1, num); if (get_nextnum(num) != 0) { int tmp = 1; REP(i, digit - n) { tmp *= 10; } v.pb(num / tmp); solve(v, digit, n + 1, num % tmp); } } int main() { while (true) { int num; scanf("%d %d", &t, &num); if (t == 0 && num == 0) break; if (t == num) { printf("%d %d\n", t, num); continue; } int digit = calc_digit(num); max_num = -1, f = 0; vector<int> v; solve(v, digit, 1, num); if (max_num == -1) { printf("error\n"); } else if (f == 1) { printf("rejected\n"); } else { printf("%d ", max_num); REPI(itr, ans) { printf("%d", *itr); vector<int>::iterator itrt = itr; itrt++; if (itrt != ans.end()) { printf(" "); } } printf("\n"); } } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
606
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <tuple> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; typedef vector<ll> V; typedef complex<double> Point; #define PI acos(-1.0) #define EPS 1e-10 const ll INF = 1e12; const ll MOD = 1e9 + 7; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, N) for (int i = 0; i < (N); i++) #define ALL(s) (s).begin(), (s).end() #define EQ(a, b) (abs((a) - (b)) < EPS) #define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag())) #define fi first #define se second #define N_SIZE (1LL << 20) #define NIL -1 ll sq(ll num) { return num * num; } ll mod_pow(ll x, ll n) { if (n == 0) return 1; if (n == 1) return x % MOD; ll res = sq(mod_pow(x, n / 2)); res %= MOD; if (n % 2 == 1) { res *= x; res %= MOD; } return res; } ll mod_add(ll a, ll b) { return (a + b) % MOD; } ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; } ll mod_mul(ll a, ll b) { return a * b % MOD; } string t, num; int main() { while (cin >> t >> num && !(t == "0" && num != "0")) { ll n = -1; bool f = 1; vector<int> ans; rep(i, (1 << (num.size() - 1))) { ll sum = 0; vector<int> k; ll nn = num[0] - '0'; rep(j, num.size() - 1) { if (i & (1 << j)) { nn *= 10; } else { sum += nn; k.push_back(nn); nn = 0; } // cout << "!!" << nn << endl; nn += num[j + 1] - '0'; } sum += nn; // cout << "!" << sum << endl; if (nn != 0 || num.back() == '0') k.push_back(nn); if (sum <= stoi(t)) { if (sum > n) { n = sum; f = 1; ans = k; } else if (sum == n) { f = 0; } } } if (!f) cout << "rejected" << endl; else { if (n == -1) cout << "error" << endl; else { cout << n << " "; rep(i, ans.size()) { if (i != 0) cout << " "; cout << ans[i]; } cout << endl; } } } }
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <tuple> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; typedef vector<ll> V; typedef complex<double> Point; #define PI acos(-1.0) #define EPS 1e-10 const ll INF = 1e12; const ll MOD = 1e9 + 7; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, N) for (int i = 0; i < (N); i++) #define ALL(s) (s).begin(), (s).end() #define EQ(a, b) (abs((a) - (b)) < EPS) #define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag())) #define fi first #define se second #define N_SIZE (1LL << 20) #define NIL -1 ll sq(ll num) { return num * num; } ll mod_pow(ll x, ll n) { if (n == 0) return 1; if (n == 1) return x % MOD; ll res = sq(mod_pow(x, n / 2)); res %= MOD; if (n % 2 == 1) { res *= x; res %= MOD; } return res; } ll mod_add(ll a, ll b) { return (a + b) % MOD; } ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; } ll mod_mul(ll a, ll b) { return a * b % MOD; } string t, num; int main() { while (cin >> t >> num && !(t == "0" && num == "0")) { ll n = -1; bool f = 1; vector<int> ans; rep(i, (1 << (num.size() - 1))) { ll sum = 0; vector<int> k; ll nn = num[0] - '0'; rep(j, num.size() - 1) { if (i & (1 << j)) { nn *= 10; } else { sum += nn; k.push_back(nn); nn = 0; } // cout << "!!" << nn << endl; nn += num[j + 1] - '0'; } sum += nn; // cout << "!" << sum << endl; if (nn != 0 || num.back() == '0') k.push_back(nn); if (sum <= stoi(t)) { if (sum > n) { n = sum; f = 1; ans = k; } else if (sum == n) { f = 0; } } } if (!f) cout << "rejected" << endl; else { if (n == -1) cout << "error" << endl; else { cout << n << " "; rep(i, ans.size()) { if (i != 0) cout << " "; cout << ans[i]; } cout << endl; } } } }
[["-", 12, 91, 28, 23, 0, 16, 12, 16, 17, 79], ["+", 12, 91, 28, 23, 0, 16, 12, 16, 17, 60]]
1
602
#include <algorithm> #include <cstdio> #include <queue> #include <vector> using namespace std; int main() { int n, p1, p2; while (scanf("%d%d%d", &n, &p1, &p2), n || p1 || p2) { vector<vector<int>> G(p1 + p2 + 1); for (int i = 0; i < n; ++i) { int x, y; char c; scanf("%d%d %c%*s", &x, &y, &c); G[x].push_back(y << 1 | (c != 'y')); G[y].push_back(x << 1 | (c != 'y')); } queue<int> q; vector<char> vis(p1 + p2 + 1); vector<vector<int>> c[2]; for (int i = 1; i <= p1 + p2; ++i) { if (!vis[i]) { c[0].push_back(vector<int>()); c[1].push_back(vector<int>()); q.push(i << 1); vis[i] = 1; while (!q.empty()) { int u = q.front() >> 1; int a = q.front() & 1; q.pop(); c[a].back().push_back(u); for (size_t j = 0; j < G[u].size(); ++j) { if (!vis[G[u][j] >> 1]) { vis[G[u][j] >> 1] = 1; q.push(G[u][j] ^ a); } } } } } vector<vector<int>> dp(c[0].size() + 1, vector<int>(p1 + 1)); vector<vector<int>> from(c[0].size() + 1, vector<int>(p1 + 1)); dp[0][0] = 1; for (size_t i = 0; i < c[0].size(); ++i) { for (int j = p1 + 1; j >= 0; --j) { for (int k = 0; k <= 1; ++k) { int t = j - (int)c[k][i].size(); if (t >= 0 && dp[i][t]) { dp[i + 1][j] += dp[i][t]; from[i + 1][j] = k; } } if (dp[i + 1][j] > 1) { dp[i + 1][j] = 2; } } } if (dp.back()[p1] != 1) { puts("no"); } else { vector<int> ans; int s = p1; for (int i = c[0].size(); i > 0; --i) { vector<int> &t = c[from[i][s]][i - 1]; ans.insert(ans.end(), t.begin(), t.end()); s -= t.size(); } sort(ans.begin(), ans.end()); for (size_t i = 0; i < ans.size(); ++i) { printf("%d\n", ans[i]); } puts("end"); } } }
#include <algorithm> #include <cstdio> #include <queue> #include <vector> using namespace std; int main() { int n, p1, p2; while (scanf("%d%d%d", &n, &p1, &p2), n || p1 || p2) { vector<vector<int>> G(p1 + p2 + 1); for (int i = 0; i < n; ++i) { int x, y; char c; scanf("%d%d %c%*s", &x, &y, &c); G[x].push_back(y << 1 | (c != 'y')); G[y].push_back(x << 1 | (c != 'y')); } queue<int> q; vector<char> vis(p1 + p2 + 1); vector<vector<int>> c[2]; for (int i = 1; i <= p1 + p2; ++i) { if (!vis[i]) { c[0].push_back(vector<int>()); c[1].push_back(vector<int>()); q.push(i << 1); vis[i] = 1; while (!q.empty()) { int u = q.front() >> 1; int a = q.front() & 1; q.pop(); c[a].back().push_back(u); for (size_t j = 0; j < G[u].size(); ++j) { if (!vis[G[u][j] >> 1]) { vis[G[u][j] >> 1] = 1; q.push(G[u][j] ^ a); } } } } } vector<vector<int>> dp(c[0].size() + 1, vector<int>(p1 + 1)); vector<vector<int>> from(c[0].size() + 1, vector<int>(p1 + 1)); dp[0][0] = 1; for (size_t i = 0; i < c[0].size(); ++i) { for (int j = p1; j >= 0; --j) { for (int k = 0; k <= 1; ++k) { int t = j - (int)c[k][i].size(); if (t >= 0 && dp[i][t]) { dp[i + 1][j] += dp[i][t]; from[i + 1][j] = k; } } if (dp[i + 1][j] > 1) { dp[i + 1][j] = 2; } } } if (dp.back()[p1] != 1) { puts("no"); } else { vector<int> ans; int s = p1; for (int i = c[0].size(); i > 0; --i) { vector<int> &t = c[from[i][s]][i - 1]; ans.insert(ans.end(), t.begin(), t.end()); s -= t.size(); } sort(ans.begin(), ans.end()); for (size_t i = 0; i < ans.size(); ++i) { printf("%d\n", ans[i]); } puts("end"); } } }
[["-", 0, 7, 10, 43, 49, 50, 51, 16, 17, 72], ["-", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
1
767
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; class Edge { public: int to; bool same; Edge(int to0, bool same0) { to = to0; same = same0; } }; vector<vector<Edge>> edges; vector<bool> check; void solve(int curr, vector<vector<int>> &group) { group[0].push_back(curr); check[curr] = true; for (unsigned i = 0; i < edges[curr].size(); ++i) { int next = edges[curr][i].to; if (check[next]) continue; if (!edges[curr][i].same) group[0].swap(group[1]); solve(next, group); if (!edges[curr][i].same) group[0].swap(group[1]); } } int main() { for (;;) { int q, a, b; cin >> q >> a >> b; if (q == 0 && a == 0 && b == 0) return 0; int n = a + b; edges.assign(n + 1, vector<Edge>()); for (int i = 0; i < q; ++i) { int x, y; string s; cin >> x >> y >> s; edges[x].push_back(Edge(y, s == "yes")); edges[y].push_back(Edge(x, s == "yes")); } check.assign(n + 1, false); vector<vector<vector<int>>> group; for (int i = 1; i <= n; ++i) { if (check[i]) continue; group.resize(group.size() + 1, vector<vector<int>>(2)); solve(i, group.back()); } vector<vector<int>> dp(group.size() + 1, vector<int>(a + 1, 0)); dp[0][0] = 1; for (unsigned i = 0; i < group.size(); ++i) { for (int j = 0; j < a; ++j) { int k = j + group[i][0].size(); if (k <= a) dp[i + 1][k] += dp[i][j]; k = j + group[i][1].size(); if (k <= a) dp[i + 1][k] += dp[i][j]; } } if (dp[group.size()][a] != 1) { cout << "no" << endl; continue; } vector<int> ret; for (int i = group.size() - 1; i >= 0; --i) { int k = 0; int a2 = a - group[i][k].size(); if (a2 < 0 || dp[i][a2] != 1) k = 1; for (unsigned j = 0; j < group[i][k].size(); ++j) ret.push_back(group[i][k][j]); a -= group[i][k].size(); } sort(ret.begin(), ret.end()); for (unsigned i = 0; i < ret.size(); ++i) cout << ret[i] << endl; cout << "end" << endl; } }
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; class Edge { public: int to; bool same; Edge(int to0, bool same0) { to = to0; same = same0; } }; vector<vector<Edge>> edges; vector<bool> check; void solve(int curr, vector<vector<int>> &group) { group[0].push_back(curr); check[curr] = true; for (unsigned i = 0; i < edges[curr].size(); ++i) { int next = edges[curr][i].to; if (check[next]) continue; if (!edges[curr][i].same) group[0].swap(group[1]); solve(next, group); if (!edges[curr][i].same) group[0].swap(group[1]); } } int main() { for (;;) { int q, a, b; cin >> q >> a >> b; if (q == 0 && a == 0 && b == 0) return 0; int n = a + b; edges.assign(n + 1, vector<Edge>()); for (int i = 0; i < q; ++i) { int x, y; string s; cin >> x >> y >> s; edges[x].push_back(Edge(y, s == "yes")); edges[y].push_back(Edge(x, s == "yes")); } check.assign(n + 1, false); vector<vector<vector<int>>> group; for (int i = 1; i <= n; ++i) { if (check[i]) continue; group.resize(group.size() + 1, vector<vector<int>>(2)); solve(i, group.back()); } vector<vector<int>> dp(group.size() + 1, vector<int>(a + 1, 0)); dp[0][0] = 1; for (unsigned i = 0; i < group.size(); ++i) { for (int j = 0; j <= a; ++j) { int k = j + group[i][0].size(); if (k <= a) dp[i + 1][k] += dp[i][j]; k = j + group[i][1].size(); if (k <= a) dp[i + 1][k] += dp[i][j]; } } if (dp[group.size()][a] != 1) { cout << "no" << endl; continue; } vector<int> ret; for (int i = group.size() - 1; i >= 0; --i) { int k = 0; int a2 = a - group[i][k].size(); if (a2 < 0 || dp[i][a2] != 1) k = 1; for (unsigned j = 0; j < group[i][k].size(); ++j) ret.push_back(group[i][k][j]); a -= group[i][k].size(); } sort(ret.begin(), ret.end()); for (unsigned i = 0; i < ret.size(); ++i) cout << ret[i] << endl; cout << "end" << endl; } }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19]]
1
807
import java.io.IOException; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.PriorityQueue; import java.util.Scanner; public class Main { public static void main(String[] args) { final Scanner sc = new Scanner(System.in); final int n = sc.nextInt(); for (int i = 0; i < n; i++) { char[] order = new StringBuilder(sc.next()).reverse().toString().toCharArray(); StringBuilder input = new StringBuilder(sc.next()); for (char o : order) { // System.out.println(input); if (o == 'J') { if (input.length() != 1) { input.insert(0, input.charAt(input.length() - 1)) .deleteCharAt(input.length() - 1); } } else if (o == 'C') { if (input.length() != 1) { input.append(input.charAt(0)).deleteCharAt(0); } } else if (o == 'E') { if (input.length() != 1) { CharSequence sb1 = input.subSequence( input.length() - input.length() / 2, input.length()); CharSequence sb2 = input.subSequence(0, input.length() / 2); StringBuilder s = new StringBuilder(); if (input.length() % 2 == 1) { s.append(input.charAt(input.length() / 2 + 1)); } s.insert(0, sb1); s.append(sb2); input = s; } } else if (o == 'A') { input.reverse(); } else if (o == 'P') { StringBuilder tmp = new StringBuilder(); char[] array = input.toString().toCharArray(); for (char c : array) { if ('0' <= c && c <= '9') { tmp.append((char)((c - 1) < '0' ? '9' : (c - 1))); } else { tmp.append(c); } // System.out.println(tmp); } input = tmp; } else if (o == 'M') { StringBuilder tmp = new StringBuilder(); char[] array = input.toString().toCharArray(); for (char c : array) { if ('0' <= c && c <= '9') { tmp.append((char)((c + 1) > '9' ? '0' : (c + 1))); } else { tmp.append(c); } } input = tmp; } } System.out.println(input); } } }
import java.io.IOException; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.PriorityQueue; import java.util.Scanner; public class Main { public static void main(String[] args) { final Scanner sc = new Scanner(System.in); final int n = sc.nextInt(); for (int i = 0; i < n; i++) { char[] order = new StringBuilder(sc.next()).reverse().toString().toCharArray(); StringBuilder input = new StringBuilder(sc.next()); for (char o : order) { // System.out.println(input); if (o == 'J') { if (input.length() != 1) { input.insert(0, input.charAt(input.length() - 1)) .deleteCharAt(input.length() - 1); } } else if (o == 'C') { if (input.length() != 1) { input.append(input.charAt(0)).deleteCharAt(0); } } else if (o == 'E') { if (input.length() != 1) { CharSequence sb1 = input.subSequence( input.length() - input.length() / 2, input.length()); CharSequence sb2 = input.subSequence(0, input.length() / 2); StringBuilder s = new StringBuilder(); if (input.length() % 2 == 1) { s.append(input.charAt(input.length() / 2)); } s.insert(0, sb1); s.append(sb2); input = s; } } else if (o == 'A') { input.reverse(); } else if (o == 'P') { StringBuilder tmp = new StringBuilder(); char[] array = input.toString().toCharArray(); for (char c : array) { if ('0' <= c && c <= '9') { tmp.append((char)((c - 1) < '0' ? '9' : (c - 1))); } else { tmp.append(c); } // System.out.println(tmp); } input = tmp; } else if (o == 'M') { StringBuilder tmp = new StringBuilder(); char[] array = input.toString().toCharArray(); for (char c : array) { if ('0' <= c && c <= '9') { tmp.append((char)((c + 1) > '9' ? '0' : (c + 1))); } else { tmp.append(c); } } input = tmp; } } System.out.println(input); } } }
[["-", 3, 4, 0, 492, 3, 4, 0, 16, 17, 72], ["-", 3, 4, 0, 492, 3, 4, 0, 16, 12, 499]]
3
576
import java.util.Scanner; public class Main { Scanner sc = new Scanner(System.in); public void run() { int n = sc.nextInt(); sc.nextLine(); for (int i = 0; i < n; i++) { String order = sc.nextLine(); String message = sc.nextLine(); calc(order, message); } } public void calc(String order, String message) { if (order.equals("")) System.out.println(message); else { String person = order.substring(order.length() - 1); if (person.equals("J")) { String me = message.substring(message.length() - 1) + message.substring(0, message.length() - 1); message = me; } else if (person.equals("C")) { String me = message.substring(1) + message.substring(0, 1); message = me; } else if (person.equals("E")) { int half = message.length() / 2; String me = message.substring(message.length() - half) + message.substring(half, message.length() - half) + message.substring(0, half); message = me; } else if (person.equals("A")) { String me = reverse(message, ""); message = me; } else if (person.equals("P")) { String me = addNum(message, -1); message = me; } else if (person.equals("M")) { String me = addNum(message, 1); message = me; } calc(order.substring(0, order.length() - 1), message); } } public String reverse(String s, String ans) { if (s.equals("")) return ans; else { ans = ans + s.substring(s.length() - 1); return reverse(s.substring(0, s.length() - 1), ans); } } public String addNum(String message, int n) { for (int i = 0; i < message.length(); i++) { String c = message.substring(i, i + 1); if (c.equals("0")) message = message.substring(0, i) + addN(0, n) + message.substring(i + 1); else if (c.equals("1")) message = message.substring(0, i) + addN(1, n) + message.substring(i + 1); else if (c.equals("2")) message = message.substring(0, i) + addN(2, n) + message.substring(i + 1); else if (c.equals("3")) message = message.substring(0, i) + addN(3, n) + message.substring(i + 1); else if (c.equals("4")) message = message.substring(0, i) + addN(4, n) + message.substring(i + 1); else if (c.equals("5")) message = message.substring(0, i) + addN(5, n) + message.substring(i + 1); else if (c.equals("6")) message = message.substring(0, i) + addN(6, n) + message.substring(i + 1); else if (c.equals("7")) message = message.substring(0, i) + addN(7, n) + message.substring(i + 1); else if (c.equals("8")) message = message.substring(0, i) + addN(8, n) + message.substring(i + 1); else if (c.equals("9")) message = message.substring(0, i) + addN(9, n) + message.substring(i + 1); } return message; } public String addN(int m, int n) { int ans = m + n; if (ans > 10) ans = ans - 10; if (ans == -1) ans = 9; return Integer.toString(ans); } public static void main(String[] args) { new Main().run(); } }
import java.util.Scanner; public class Main { Scanner sc = new Scanner(System.in); public void run() { int n = sc.nextInt(); sc.nextLine(); for (int i = 0; i < n; i++) { String order = sc.nextLine(); String message = sc.nextLine(); calc(order, message); } } public void calc(String order, String message) { if (order.equals("")) System.out.println(message); else { String person = order.substring(order.length() - 1); if (person.equals("J")) { String me = message.substring(message.length() - 1) + message.substring(0, message.length() - 1); message = me; } else if (person.equals("C")) { String me = message.substring(1) + message.substring(0, 1); message = me; } else if (person.equals("E")) { int half = message.length() / 2; String me = message.substring(message.length() - half) + message.substring(half, message.length() - half) + message.substring(0, half); message = me; } else if (person.equals("A")) { String me = reverse(message, ""); message = me; } else if (person.equals("P")) { String me = addNum(message, -1); message = me; } else if (person.equals("M")) { String me = addNum(message, 1); message = me; } calc(order.substring(0, order.length() - 1), message); } } public String reverse(String s, String ans) { if (s.equals("")) return ans; else { ans = ans + s.substring(s.length() - 1); return reverse(s.substring(0, s.length() - 1), ans); } } public String addNum(String message, int n) { for (int i = 0; i < message.length(); i++) { String c = message.substring(i, i + 1); if (c.equals("0")) message = message.substring(0, i) + addN(0, n) + message.substring(i + 1); else if (c.equals("1")) message = message.substring(0, i) + addN(1, n) + message.substring(i + 1); else if (c.equals("2")) message = message.substring(0, i) + addN(2, n) + message.substring(i + 1); else if (c.equals("3")) message = message.substring(0, i) + addN(3, n) + message.substring(i + 1); else if (c.equals("4")) message = message.substring(0, i) + addN(4, n) + message.substring(i + 1); else if (c.equals("5")) message = message.substring(0, i) + addN(5, n) + message.substring(i + 1); else if (c.equals("6")) message = message.substring(0, i) + addN(6, n) + message.substring(i + 1); else if (c.equals("7")) message = message.substring(0, i) + addN(7, n) + message.substring(i + 1); else if (c.equals("8")) message = message.substring(0, i) + addN(8, n) + message.substring(i + 1); else if (c.equals("9")) message = message.substring(0, i) + addN(9, n) + message.substring(i + 1); } return message; } public String addN(int m, int n) { int ans = m + n; if (ans == 10) ans = 0; if (ans == -1) ans = 9; return Integer.toString(ans); } public static void main(String[] args) { new Main().run(); } }
[["-", 8, 196, 0, 57, 15, 15, 0, 16, 17, 47], ["+", 8, 196, 0, 57, 15, 15, 0, 16, 17, 60], ["-", 0, 57, 64, 1, 0, 11, 12, 16, 31, 22], ["-", 0, 57, 64, 1, 0, 11, 12, 16, 17, 33], ["-", 0, 57, 64, 1, 0, 11, 12, 16, 12, 499], ["+", 8, 196, 0, 57, 64, 1, 0, 11, 12, 499]]
3
971
#include <bits/stdc++.h> using namespace std; #define all(a) (a).begin(), (a).end() int main() { int n; cin >> n; while (n--) { string m, s; cin >> m >> s; reverse(all(m)); for (int i = 0; i < m.size(); i++) { if (m[i] == 'C') { string a(s, 0, 1); string b(s, 1, s.size() - 1); s = b + a; } else if (m[i] == 'J') { string a(s, 0, s.size() - 1); string b(s, s.size() - 1, 1); // cout << s << " " << a << " " << b << endl; s = b + a; } else if (m[i] == 'E') { string a(s, 0, s.size() / 2); string b(s, s.size() / 2, s.size() % 2); string c(s, s.size() / 2 + s.size() % 2, s.size() / 2); s = c + b + a; } else if (m[i] == 'A') { reverse(all(s)); } else if (m[i] == 'M') { for (int j = 0; j < s.size(); j++) { if (s[j] >= '0' && s[j] <= '8') { s[j]++; } else if (s[j] == '9') { s[j] = '0'; } } } else if (m[i] == 'P') { for (int j = 0; j < s.size(); j++) { if (s[j] >= '1' && s[j] <= '9') { s[j]--; } else if (m[i] == '0') { s[i] = '9'; } } } } cout << s << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define all(a) (a).begin(), (a).end() int main() { int n; cin >> n; while (n--) { string m, s; cin >> m >> s; reverse(all(m)); for (int i = 0; i < m.size(); i++) { if (m[i] == 'C') { string a(s, 0, 1); string b(s, 1, s.size() - 1); s = b + a; } else if (m[i] == 'J') { string a(s, 0, s.size() - 1); string b(s, s.size() - 1, 1); // cout << s << " " << a << " " << b << endl; s = b + a; } else if (m[i] == 'E') { string a(s, 0, s.size() / 2); string b(s, s.size() / 2, s.size() % 2); string c(s, s.size() / 2 + s.size() % 2, s.size() / 2); s = c + b + a; } else if (m[i] == 'A') { reverse(all(s)); } else if (m[i] == 'M') { for (int j = 0; j < s.size(); j++) { if (s[j] >= '0' && s[j] <= '8') { s[j]++; } else if (s[j] == '9') { s[j] = '0'; } } } else if (m[i] == 'P') { for (int j = 0; j < s.size(); j++) { if (s[j] >= '1' && s[j] <= '9') { s[j]--; } else if (s[j] == '0') { s[j] = '9'; } } } } cout << s << endl; } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 31, 69, 28, 22], ["+", 0, 57, 15, 339, 51, 16, 31, 69, 28, 22], ["-", 15, 339, 51, 16, 31, 69, 341, 342, 0, 22], ["+", 15, 339, 51, 16, 31, 69, 341, 342, 0, 22], ["-", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22], ["+", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22]]
1
460
// 1240 #include <algorithm> #include <iostream> #include <string> using namespace std; char table[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; string now; void A() { reverse(now.begin(), now.end()); } void J() { char tmp = now[0]; for (int i = 0; i < now.size() - 1; i++) now[i] = now[i + 1]; now[now.size() - 1] = tmp; } void E() { string a = "", b = "", c = ""; for (int i = 0; i < now.size(); i++) { if (i < now.size() / 2) a.push_back(now[i]); else if (i == now.size() / 2 && now.size() % 2 == 1) c.push_back(now[i]); else b.push_back(now[i]); } // reverse(a.begin(),a.end()); // reverse(b.begin(),b.end()); now = b + c + a; } void C() { char tmp = now[now.size() - 1]; for (int i = now.size() - 1; i > 0; i--) now[i] = now[i - 1]; now[0] = tmp; } void P() { for (int i = 0; i < now.size(); i++) { if (now[i] - '0' == 0) now[i] = '9'; else if (now[i] - '0' == 1) now[i] = '0'; else if (now[i] - '0' == 2) now[i] = '1'; else if (now[i] - '0' == 3) now[i] = '2'; else if (now[i] - '0' == 4) now[i] = '3'; else if (now[i] - '0' == 5) now[i] = '4'; else if (now[i] - '0' == 6) now[i] = '5'; else if (now[i] - '0' == 7) now[i] = '6'; else if (now[i] - '0' == 8) now[i] = '7'; else if (now[i] - '0' == 9) now[i] = '8'; } } void M() { for (int i = 0; i < now.size(); i++) { if (now[i] - '0' == 0) now[i] = '1'; else if (now[i] - '0' == 1) now[i] = '2'; else if (now[i] - '0' == 2) now[i] = '3'; else if (now[i] - '0' == 3) now[i] = '4'; else if (now[i] - '0' == 4) now[i] = '5'; else if (now[i] - '0' == 5) now[i] = '6'; else if (now[i] - '0' == 6) now[i] = '7'; else if (now[i] - '0' == 7) now[i] = '8'; else if (now[i] - '0' == 8) now[i] = '9'; else if (now[i] - '0' == 8) now[i] = '0'; } } int main() { int n; cin >> n; for (int kki = 0; kki < n; kki++) { string a; cin >> a >> now; for (int i = a.size(); i >= 0; i--) { if (a[i] == 'A') A(); if (a[i] == 'C') J(); if (a[i] == 'P') P(); if (a[i] == 'M') M(); if (a[i] == 'E') E(); if (a[i] == 'J') C(); // cout<<now<<endl; } cout << now << endl; } return 0; }
// 1240 #include <algorithm> #include <iostream> #include <string> using namespace std; char table[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; string now; void A() { reverse(now.begin(), now.end()); } void J() { char tmp = now[0]; for (int i = 0; i < now.size() - 1; i++) now[i] = now[i + 1]; now[now.size() - 1] = tmp; } void E() { string a = "", b = "", c = ""; for (int i = 0; i < now.size(); i++) { if (i < now.size() / 2) a.push_back(now[i]); else if (i == now.size() / 2 && now.size() % 2 == 1) c.push_back(now[i]); else b.push_back(now[i]); } // reverse(a.begin(),a.end()); // reverse(b.begin(),b.end()); now = b + c + a; } void C() { char tmp = now[now.size() - 1]; for (int i = now.size() - 1; i > 0; i--) now[i] = now[i - 1]; now[0] = tmp; } void P() { for (int i = 0; i < now.size(); i++) { if (now[i] - '0' == 0) now[i] = '9'; else if (now[i] - '0' == 1) now[i] = '0'; else if (now[i] - '0' == 2) now[i] = '1'; else if (now[i] - '0' == 3) now[i] = '2'; else if (now[i] - '0' == 4) now[i] = '3'; else if (now[i] - '0' == 5) now[i] = '4'; else if (now[i] - '0' == 6) now[i] = '5'; else if (now[i] - '0' == 7) now[i] = '6'; else if (now[i] - '0' == 8) now[i] = '7'; else if (now[i] - '0' == 9) now[i] = '8'; } } void M() { for (int i = 0; i < now.size(); i++) { if (now[i] - '0' == 0) now[i] = '1'; else if (now[i] - '0' == 1) now[i] = '2'; else if (now[i] - '0' == 2) now[i] = '3'; else if (now[i] - '0' == 3) now[i] = '4'; else if (now[i] - '0' == 4) now[i] = '5'; else if (now[i] - '0' == 5) now[i] = '6'; else if (now[i] - '0' == 6) now[i] = '7'; else if (now[i] - '0' == 7) now[i] = '8'; else if (now[i] - '0' == 8) now[i] = '9'; else if (now[i] - '0' == 9) now[i] = '0'; } } int main() { int n; cin >> n; for (int kki = 0; kki < n; kki++) { string a; cin >> a >> now; for (int i = a.size(); i >= 0; i--) { if (a[i] == 'A') A(); if (a[i] == 'C') J(); if (a[i] == 'P') P(); if (a[i] == 'M') M(); if (a[i] == 'E') E(); if (a[i] == 'J') C(); // cout<<now<<endl; } cout << now << endl; } return 0; }
[["-", 75, 76, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 75, 76, 0, 57, 15, 339, 51, 16, 12, 13]]
1
987
#include <algorithm> #include <cstdio> using std::swap; void J(char s[], int len) { char tmp = s[len - 1]; for (int i = len - 1; i > 0; i--) { s[i] = s[i - 1]; } s[0] = tmp; } void C(char s[], int len) { char tmp = s[0]; for (int i = 0; i < len - 1; i++) { s[i] = s[i + 1]; } s[len - 1] = tmp; } void E(char s[], int len) { int mid = len / 2; for (int i = 0; i < mid; i++) { swap(s[i], s[len - mid + i]); } } void A(char s[], int len) { char tmp = s[len - 1]; for (int i = 0; i < len - 1 - i; i++) { swap(s[i], s[len - 1 - i]); } } void P(char s[], int len) { for (int i = 0; i < len - 1; i++) { if ('0' <= s[i] && s[i] <= '9') { s[i] = '0' + (s[i] - '0' + 9) % 10; } } } void M(char s[], int len) { for (int i = 0; i < len - 1; i++) { if ('0' <= s[i] && s[i] <= '9') { s[i] = '0' + (s[i] - '0' + 1) % 10; } } } int main() { int N; scanf("%d", &N); while (N--) { char s1[50], s2[50]; scanf("%s %s", s1, s2); int len1; for (len1 = 0; s1[len1] != '\0'; len1++) ; int len2; for (len2 = 0; s2[len2] != '\0'; len2++) ; for (int i = len1 - 1; i >= 0; i--) { if (s1[i] == 'J') J(s2, len2); if (s1[i] == 'C') C(s2, len2); if (s1[i] == 'E') E(s2, len2); if (s1[i] == 'A') A(s2, len2); if (s1[i] == 'P') P(s2, len2); if (s1[i] == 'M') M(s2, len2); } printf("%s\n", s2); } return 0; }
#include <algorithm> #include <cstdio> using std::swap; void J(char s[], int len) { char tmp = s[len - 1]; for (int i = len - 1; i > 0; i--) { s[i] = s[i - 1]; } s[0] = tmp; } void C(char s[], int len) { char tmp = s[0]; for (int i = 0; i < len - 1; i++) { s[i] = s[i + 1]; } s[len - 1] = tmp; } void E(char s[], int len) { int mid = len / 2; for (int i = 0; i < mid; i++) { swap(s[i], s[len - mid + i]); } } void A(char s[], int len) { char tmp = s[len - 1]; for (int i = 0; i < len - 1 - i; i++) { swap(s[i], s[len - 1 - i]); } } void P(char s[], int len) { for (int i = 0; i < len; i++) { if ('0' <= s[i] && s[i] <= '9') { s[i] = '0' + (s[i] - '0' + 9) % 10; } } } void M(char s[], int len) { for (int i = 0; i < len; i++) { if ('0' <= s[i] && s[i] <= '9') { s[i] = '0' + (s[i] - '0' + 1) % 10; } } } int main() { int N; scanf("%d", &N); while (N--) { char s1[50], s2[50]; scanf("%s %s", s1, s2); int len1; for (len1 = 0; s1[len1] != '\0'; len1++) ; int len2; for (len2 = 0; s2[len2] != '\0'; len2++) ; for (int i = len1 - 1; i >= 0; i--) { if (s1[i] == 'J') J(s2, len2); if (s1[i] == 'C') C(s2, len2); if (s1[i] == 'E') E(s2, len2); if (s1[i] == 'A') A(s2, len2); if (s1[i] == 'P') P(s2, len2); if (s1[i] == 'M') M(s2, len2); } printf("%s\n", s2); } return 0; }
[["-", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
627
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctype.h> #include <iostream> #include <map> #include <queue> #include <sstream> #include <string> #include <utility> #include <vector> using namespace std; string c_j(string str); string c_c(string str); string c_e(string str); string c_a(string str); string c_p(string str); string c_m(string str); int main() { int n; string order, message; cin >> n; for (int i = 0; i < n; i++) { cin >> order >> message; string ans = message; for (int j = order.length() - 1; j >= 0; j--) { if (order[j] == 'J') ans = c_j(ans); else if (order[j] == 'C') ans = c_c(ans); else if (order[j] == 'E') ans = c_e(ans); else if (order[j] == 'A') ans = c_a(ans); else if (order[j] == 'P') ans = c_p(ans); else if (order[j] == 'M') ans = c_m(ans); } cout << ans << endl; } return 0; } string c_j(string str) { char c; c = str[str.length() - 1]; str.erase(str.end() - 1); str = c + str; return str; } string c_c(string str) { char c; c = str[0]; str.erase(str.begin()); str += c; return str; } string c_e(string str) { string a; int l = str.length(); a = str.substr(0, l / 2); if (l % 2 == 1) { char c = str[l / 2]; str.erase(str.begin(), str.begin() + (l / 2) + 1); str += c; str += a; } else { str.erase(str.begin(), str.begin() + (l / 2)); str += a; } return str; } string c_a(string str) { reverse(str.begin(), str.end()); return str; } string c_p(string str) { for (int i = 0; i < str.length() - 1; i++) { if (isdigit(str[i])) { int n = str[i] - '0'; n--; if (n == -1) n = 9; str[i] = n + '0'; } } return str; } string c_m(string str) { for (int i = 0; i < str.length() - 1; i++) { if (isdigit(str[i])) { int n = str[i] - '0'; n = (n + 1) % 10; str[i] = n + '0'; } } return str; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctype.h> #include <iostream> #include <map> #include <queue> #include <sstream> #include <string> #include <utility> #include <vector> using namespace std; string c_j(string str); string c_c(string str); string c_e(string str); string c_a(string str); string c_p(string str); string c_m(string str); int main() { int n; string order, message; cin >> n; for (int i = 0; i < n; i++) { cin >> order >> message; string ans = message; for (int j = order.length() - 1; j >= 0; j--) { if (order[j] == 'J') ans = c_j(ans); else if (order[j] == 'C') ans = c_c(ans); else if (order[j] == 'E') ans = c_e(ans); else if (order[j] == 'A') ans = c_a(ans); else if (order[j] == 'P') ans = c_p(ans); else if (order[j] == 'M') ans = c_m(ans); } cout << ans << endl; } return 0; } string c_j(string str) { char c; c = str[str.length() - 1]; str.erase(str.end() - 1); str = c + str; return str; } string c_c(string str) { char c; c = str[0]; str.erase(str.begin()); str += c; return str; } string c_e(string str) { string a; int l = str.length(); a = str.substr(0, l / 2); if (l % 2 == 1) { char c = str[l / 2]; str.erase(str.begin(), str.begin() + (l / 2) + 1); str += c; str += a; } else { str.erase(str.begin(), str.begin() + (l / 2)); str += a; } return str; } string c_a(string str) { reverse(str.begin(), str.end()); return str; } string c_p(string str) { for (int i = 0; i < str.length(); i++) { if (isdigit(str[i])) { int n = str[i] - '0'; n--; if (n == -1) n = 9; str[i] = n + '0'; } } return str; } string c_m(string str) { for (int i = 0; i < str.length(); i++) { if (isdigit(str[i])) { int n = str[i] - '0'; n = (n + 1) % 10; str[i] = n + '0'; } } return str; }
[["-", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
645
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <utility> #include <vector> #define INF 1000000000 #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long int ll; string A(string s) { reverse(s.begin(), s.end()); return s; } string J(string s) { string ss = s.substr(s.length() - 1, 1), sss = s.substr(0, s.length() - 1); return ss + sss; } string C(string s) { string ss = s.substr(0, 1), sss = s.substr(1, s.length() - 1); return sss + ss; } string E(string s) { if (s.length() % 2 == 0) { string ss = s.substr(0, s.length() / 2); string sss = s.substr(s.length() / 2, s.length() / 2); return sss + ss; } else { string ss = s.substr(0, s.length() / 2); string tt = s.substr(s.length(), 1); string sss = s.substr(s.length() / 2 + 1, s.length() / 2); return sss + tt + ss; } } string P(string s) { for (int i = 0; i < s.length(); i++) { if (s[i] == '9' or s[i] == '1' or s[i] == '2' or s[i] == '3' or s[i] == '4' or s[i] == '5' or s[i] == '6' or s[i] == '7' or s[i] == '8') { s[i] = (char)((int)s[i] - 1); continue; } if (s[i] == '0') { s[i] = '9'; } } return s; } string M(string s) { for (int i = 0; i < s.length(); i++) { if (s[i] == '0' or s[i] == '1' or s[i] == '2' or s[i] == '3' or s[i] == '4' or s[i] == '5' or s[i] == '6' or s[i] == '7' or s[i] == '8') { s[i] = (char)((int)s[i] + 1); continue; } if (s[i] == '9') { s[i] = '0'; } } return s; } int main() { int n; cin >> n; for (int wq = 0; wq < n; wq++) { string t, s; cin >> t >> s; for (int i = t.length() - 1; i >= 0; i--) { if (t[i] == 'J') s = J(s); if (t[i] == 'C') s = C(s); if (t[i] == 'E') s = E(s); if (t[i] == 'A') s = A(s); if (t[i] == 'P') s = P(s); if (t[i] == 'M') s = M(s); } cout << s << endl; } }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <utility> #include <vector> #define INF 1000000000 #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long int ll; string A(string s) { reverse(s.begin(), s.end()); return s; } string J(string s) { string ss = s.substr(s.length() - 1, 1), sss = s.substr(0, s.length() - 1); return ss + sss; } string C(string s) { string ss = s.substr(0, 1), sss = s.substr(1, s.length() - 1); return sss + ss; } string E(string s) { if (s.length() % 2 == 0) { string ss = s.substr(0, s.length() / 2); string sss = s.substr(s.length() / 2, s.length() / 2); return sss + ss; } else { string ss = s.substr(0, s.length() / 2); string tt = s.substr(s.length() / 2, 1); string sss = s.substr(s.length() / 2 + 1, s.length() / 2); return sss + tt + ss; } } string P(string s) { for (int i = 0; i < s.length(); i++) { if (s[i] == '9' or s[i] == '1' or s[i] == '2' or s[i] == '3' or s[i] == '4' or s[i] == '5' or s[i] == '6' or s[i] == '7' or s[i] == '8') { s[i] = (char)((int)s[i] - 1); continue; } if (s[i] == '0') { s[i] = '9'; } } return s; } string M(string s) { for (int i = 0; i < s.length(); i++) { if (s[i] == '0' or s[i] == '1' or s[i] == '2' or s[i] == '3' or s[i] == '4' or s[i] == '5' or s[i] == '6' or s[i] == '7' or s[i] == '8') { s[i] = (char)((int)s[i] + 1); continue; } if (s[i] == '9') { s[i] = '0'; } } return s; } int main() { int n; cin >> n; for (int wq = 0; wq < n; wq++) { string t, s; cin >> t >> s; for (int i = t.length() - 1; i >= 0; i--) { if (t[i] == 'J') s = J(s); if (t[i] == 'C') s = C(s); if (t[i] == 'E') s = E(s); if (t[i] == 'A') s = A(s); if (t[i] == 'P') s = P(s); if (t[i] == 'M') s = M(s); } cout << s << endl; } }
[["+", 49, 50, 51, 2, 3, 4, 0, 16, 17, 85], ["+", 49, 50, 51, 2, 3, 4, 0, 16, 12, 13]]
1
800
#include <bits/stdc++.h> using namespace std; int main() { string number = "0123456789"; int n; cin >> n; while (n--) { string order, message; cin >> order >> message; for (int i = order.size() - 1; i >= 0; i--) { if (order[i] == 'M') { for (int j = 0; j < message.size(); j++) { if ('0' <= message[j] && message[j] <= '9') { int idx = number.find(message[j]); message[j] = number[(idx + 1) % number.size()]; } } } else if (order[i] == 'P') { for (int j = 0; j < number.size(); j++) { if ('0' <= message[j] && message[j] <= '9') { int idx = number.find(message[j]); message[j] = number[(idx - 1 + number.size()) % number.size()]; } } } else if (order[i] == 'A') { reverse(message.begin(), message.end()); } else if (order[i] == 'E') { for (int i = 0; i < message.size() / 2; i++) { swap(message[i], message[i + (message.size() + 1) / 2]); } } else if (order[i] == 'C') { message = message.substr(1, message.size() - 1) + message[0]; } else if (order[i] == 'J') { message = message[message.size() - 1] + message.substr(0, message.size() - 1); } } cout << message << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string number = "0123456789"; int n; cin >> n; while (n--) { string order, message; cin >> order >> message; for (int i = order.size() - 1; i >= 0; i--) { if (order[i] == 'M') { for (int j = 0; j < message.size(); j++) { if ('0' <= message[j] && message[j] <= '9') { int idx = number.find(message[j]); message[j] = number[(idx + 1) % number.size()]; } } } else if (order[i] == 'P') { for (int j = 0; j < message.size(); j++) { if ('0' <= message[j] && message[j] <= '9') { int idx = number.find(message[j]); message[j] = number[(idx - 1 + number.size()) % number.size()]; } } } else if (order[i] == 'A') { reverse(message.begin(), message.end()); } else if (order[i] == 'E') { for (int i = 0; i < message.size() / 2; i++) { swap(message[i], message[i + (message.size() + 1) / 2]); } } else if (order[i] == 'C') { message = message.substr(1, message.size() - 1) + message[0]; } else if (order[i] == 'J') { message = message[message.size() - 1] + message.substr(0, message.size() - 1); } } cout << message << endl; } return 0; }
[["-", 0, 7, 15, 16, 12, 2, 63, 118, 28, 22], ["+", 0, 7, 15, 16, 12, 2, 63, 118, 28, 22]]
1
426
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; //// < "D:\D_Download\Visual Studio ///2015\Projects\programing_contest_c++\Debug\a.txt" int main() { int N; cin >> N; while (N--) { string st; cin >> st; string aa; cin >> aa; for (int i = 0; i < st.size(); ++i) { char c = st[st.size() - 1 - i]; if (c == 'A') { reverse(aa.begin(), aa.end()); } else if (c == 'C') { rotate(aa.begin(), aa.begin() + 1, aa.end()); } else if (c == 'M') { for (auto &ac : aa) { if (isdigit(ac)) ac = (ac - '0' + 1) % 10 + '0'; } } else if (c == 'P') { for (auto &ac : aa) { if (isdigit(ac)) ac = (ac - '0' + 9) % 10 + '0'; } } else if (c == 'E') { string from = aa.substr(0, aa.size() / 2); string to = aa.substr(aa.size() - aa.size() / 2, aa.size() / 2); if (aa.size() % 2) aa = to + aa[st.size() / 2] + from; else aa = to + from; } else if (c == 'J') { rotate(aa.begin(), aa.begin() + aa.size() - 1, aa.end()); } } cout << aa << endl; } return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; //// < "D:\D_Download\Visual Studio ///2015\Projects\programing_contest_c++\Debug\a.txt" int main() { int N; cin >> N; while (N--) { string st; cin >> st; string aa; cin >> aa; for (int i = 0; i < st.size(); ++i) { char c = st[st.size() - 1 - i]; if (c == 'A') { reverse(aa.begin(), aa.end()); } else if (c == 'C') { rotate(aa.begin(), aa.begin() + 1, aa.end()); } else if (c == 'M') { for (auto &ac : aa) { if (isdigit(ac)) ac = (ac - '0' + 1) % 10 + '0'; } } else if (c == 'P') { for (auto &ac : aa) { if (isdigit(ac)) ac = (ac - '0' + 9) % 10 + '0'; } } else if (c == 'E') { string from = aa.substr(0, aa.size() / 2); string to = aa.substr(aa.size() - aa.size() / 2, aa.size() / 2); if (aa.size() % 2) aa = to + aa[aa.size() / 2] + from; else aa = to + from; } else if (c == 'J') { rotate(aa.begin(), aa.begin() + aa.size() - 1, aa.end()); } } cout << aa << endl; } return 0; }
[["-", 341, 342, 0, 16, 31, 2, 63, 118, 28, 22], ["+", 341, 342, 0, 16, 31, 2, 63, 118, 28, 22]]
1
407
#include <algorithm> #include <iostream> #include <limits.h> #include <math.h> #include <set> #include <stack> #include <stdlib.h> #include <string> #include <vector> #define el endl #define fd fixed #define INF INT_MAX / 2 - 1 #define pb push_back using namespace std; string num = "0123456789"; string mrj(string s) { return s[s.length() - 1] + s.substr(0, s.length() - 1); } string missc(string s) { return s.substr(1, s.length() - 1) + s[0]; } string mre(string s) { if (s.length() % 2 == 1) return s.substr(s.length() / 2 + 1, s.length() / 2) + s[s.length() / 2] + s.substr(0, s.length() / 2); else return s.substr(s.length() / 2, s.length() / 2) + s.substr(0, s.length() / 2); } string mra(string s) { string res = ""; for (int i = s.length() - 1; i >= 0; i--) { res += s[i]; } return res; } string drp(string s) { for (int i = 0; i < s.length(); i++) { int index = (int)num.find(s[i]); if (index > 0) { s[i] = num[(index + 9) % 10]; } } return s; } string mrm(string s) { for (int i = 0; i < s.length(); i++) { int index = (int)num.find(s[i]); if (index > 0) { s[i] = num[(index + 1) % 10]; } } return s; } int main() { int n; string order, message; cin >> n; while (n--) { cin >> order >> message; for (int i = order.length() - 1; i >= 0; i--) { if (order[i] == 'J') message = mrj(message); if (order[i] == 'C') message = missc(message); if (order[i] == 'E') message = mre(message); if (order[i] == 'A') message = mra(message); if (order[i] == 'P') message = drp(message); if (order[i] == 'M') message = mrm(message); } cout << message << el; } }
#include <algorithm> #include <iostream> #include <limits.h> #include <math.h> #include <set> #include <stack> #include <stdlib.h> #include <string> #include <vector> #define el endl #define fd fixed #define INF INT_MAX / 2 - 1 #define pb push_back using namespace std; string num = "0123456789"; string mrj(string s) { return s[s.length() - 1] + s.substr(0, s.length() - 1); } string missc(string s) { return s.substr(1, s.length() - 1) + s[0]; } string mre(string s) { if (s.length() % 2 == 1) return s.substr(s.length() / 2 + 1, s.length() / 2) + s[s.length() / 2] + s.substr(0, s.length() / 2); else return s.substr(s.length() / 2, s.length() / 2) + s.substr(0, s.length() / 2); } string mra(string s) { string res = ""; for (int i = s.length() - 1; i >= 0; i--) { res += s[i]; } return res; } string drp(string s) { for (int i = 0; i < s.length(); i++) { int index = (int)num.find(s[i]); if (index >= 0) { s[i] = num[(index + 9) % 10]; } } return s; } string mrm(string s) { for (int i = 0; i < s.length(); i++) { int index = (int)num.find(s[i]); if (index >= 0) { s[i] = num[(index + 1) % 10]; } } return s; } int main() { int n; string order, message; cin >> n; while (n--) { cin >> order >> message; for (int i = order.length() - 1; i >= 0; i--) { if (order[i] == 'J') message = mrj(message); if (order[i] == 'C') message = missc(message); if (order[i] == 'E') message = mre(message); if (order[i] == 'A') message = mra(message); if (order[i] == 'P') message = drp(message); if (order[i] == 'M') message = mrm(message); } cout << message << el; } }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 47], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 20]]
1
568
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> i_i; typedef pair<long long, int> ll_i; typedef pair<double, int> d_i; typedef pair<long long, long long> ll_ll; typedef pair<double, double> d_d; typedef vector<int> Vint; #define PI 3.141592653589793238462643383279 #define mod 1000000007LL #define rep(i, n) for (i = 0; i < n; ++i) #define rep1(i, n) for (i = 1; i < n; ++i) #define rep2d(i, j, n) \ for (i = 0; i < n; ++i) \ for (j = i + 1; j < n; ++j) #define per(i, n) for (i = n - 1; i > -1; --i) #define int(x) \ int x; \ scanf("%d", &x) #define int2(x, y) \ int x, y; \ scanf("%d%d", &x, &y) #define int3(x, y, z) \ int x, y, z; \ scanf("%d%d%d", &x, &y, &z) #define int4(v, x, y, z) \ int v, x, y, z; \ scanf("%d%d%d%d", &v, &x, &y, &z) #define int5(v, w, x, y, z) \ int v, w, x, y, z; \ scanf("%d%d%d%d%d", &v, &w, &x, &y, &z) #define scn(n, a) rep(i, n) cin >> a[i] #define sc2n(n, a, b) rep(i, n) cin >> a[i] >> b[i] #define pri(x) cout << x << "\n" #define pri2(x, y) cout << x << " " << y << "\n" #define pri3(x, y, z) cout << x << " " << y << " " << z << "\n" #define pb push_back #define mp make_pair #define all(a) (a).begin(), (a).end() #define endl "\n" #define kabe puts("---------------------------") #define kara puts("") #define debug(x) cout << " --- " << x << "\n" #define debug2(x, y) cout << " --- " << x << " " << y << "\n" #define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\n" #define X first #define Y second #define eps 0.0001 #define prid(x) printf("%.15lf\n", x) string s; void f(char c) { if (c == 'C') { s = s.substr(1, s.size() - 1) + s.substr(0, 1); } else if (c == 'J') { s = s.substr(s.size() - 1, 1) + s.substr(0, s.size() - 1); } else if (c == 'E') { int qwe = s.size() / 2; if (s.size() % 2) s = s.substr(qwe + 1, qwe) + s.substr(qwe, 1) + s.substr(0, qwe); else s = s.substr(qwe, qwe) + s.substr(0, qwe); } else if (c == 'A') { reverse(all(s)); } else if (c == 'M') { int i; rep(i, s.size()) if ('0' <= s[i] && s[i] <= '9') { s[i] == '9' ? s[i] == '0' : ++s[i]; } } else if (c == 'P') { int i; rep(i, s.size()) if ('0' <= s[i] && s[i] <= '9') { s[i] == '0' ? s[i] == '9' : --s[i]; } } return; } signed main(void) { int i, j, k; int(n); for (int testcase = 0; testcase < n; testcase++) { string t; cin >> t >> s; per(i, t.size()) f(t[i]); pri(s); s.clear(); t.clear(); //*/ //*/ break; } return 0; }
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> i_i; typedef pair<long long, int> ll_i; typedef pair<double, int> d_i; typedef pair<long long, long long> ll_ll; typedef pair<double, double> d_d; typedef vector<int> Vint; #define PI 3.141592653589793238462643383279 #define mod 1000000007LL #define rep(i, n) for (i = 0; i < n; ++i) #define rep1(i, n) for (i = 1; i < n; ++i) #define rep2d(i, j, n) \ for (i = 0; i < n; ++i) \ for (j = i + 1; j < n; ++j) #define per(i, n) for (i = n - 1; i > -1; --i) #define int(x) \ int x; \ scanf("%d", &x) #define int2(x, y) \ int x, y; \ scanf("%d%d", &x, &y) #define int3(x, y, z) \ int x, y, z; \ scanf("%d%d%d", &x, &y, &z) #define int4(v, x, y, z) \ int v, x, y, z; \ scanf("%d%d%d%d", &v, &x, &y, &z) #define int5(v, w, x, y, z) \ int v, w, x, y, z; \ scanf("%d%d%d%d%d", &v, &w, &x, &y, &z) #define scn(n, a) rep(i, n) cin >> a[i] #define sc2n(n, a, b) rep(i, n) cin >> a[i] >> b[i] #define pri(x) cout << x << "\n" #define pri2(x, y) cout << x << " " << y << "\n" #define pri3(x, y, z) cout << x << " " << y << " " << z << "\n" #define pb push_back #define mp make_pair #define all(a) (a).begin(), (a).end() #define endl "\n" #define kabe puts("---------------------------") #define kara puts("") #define debug(x) cout << " --- " << x << "\n" #define debug2(x, y) cout << " --- " << x << " " << y << "\n" #define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\n" #define X first #define Y second #define eps 0.0001 #define prid(x) printf("%.15lf\n", x) string s; void f(char c) { if (c == 'C') { s = s.substr(1, s.size() - 1) + s.substr(0, 1); } else if (c == 'J') { s = s.substr(s.size() - 1, 1) + s.substr(0, s.size() - 1); } else if (c == 'E') { int qwe = s.size() / 2; if (s.size() % 2) s = s.substr(qwe + 1, qwe) + s.substr(qwe, 1) + s.substr(0, qwe); else s = s.substr(qwe, qwe) + s.substr(0, qwe); } else if (c == 'A') { reverse(all(s)); } else if (c == 'M') { int i; rep(i, s.size()) if ('0' <= s[i] && s[i] <= '9') { s[i] == '9' ? s[i] = '0' : ++s[i]; } } else if (c == 'P') { int i; rep(i, s.size()) if ('0' <= s[i] && s[i] <= '9') { s[i] == '0' ? s[i] = '9' : --s[i]; } } return; } signed main(void) { int i, j, k; int(n); for (int testcase = 0; testcase < n; testcase++) { string t; cin >> t >> s; per(i, t.size()) f(t[i]); pri(s); s.clear(); t.clear(); //*/ //*/ break; } return 0; }
[["-", 64, 9, 0, 1, 0, 41, 64, 16, 17, 60], ["+", 64, 9, 0, 1, 0, 41, 64, 11, 17, 32]]
1
734
#include <deque> #include <iostream> #include <string> using namespace std; int main() { int n; string s, t; char c, d; deque<char> de, sub; cin >> n; for (int i = 0; i < n; i++) { cin >> s >> t; de.clear(); for (int j = 0; j < t.size(); j++) { de.push_back(t[j]); } for (int j = s.size() - 1; j >= 0; j--) { // cout << s[j] <<endl; if (s[j] == 'C') { c = de.front(); de.pop_front(); de.push_back(c); } else if (s[j] == 'J') { c = de.back(); de.pop_back(); de.push_front(c); } else if (s[j] == 'A') { for (int k = 0; k <= de.size() / 2; k++) { swap(de[k], de[de.size() - k - 1]); } } else if (s[j] == 'E') { if (de.size() % 2 == 1) { for (int k = 0; k < t.size() / 2; k++) { c = de.back(); de.pop_back(); sub.push_front(c); } c = de.back(); de.pop_back(); de.push_front(c); // cout << c << endl; while (!sub.empty()) { c = sub.back(); sub.pop_back(); de.push_front(c); } } else { for (int k = 0; k < de.size() / 2; k++) { c = de.front(); de.pop_front(); de.push_back(c); } } } else if (s[j] == 'M') { for (int k = 0; k < de.size(); k++) { if (de[k] >= '0' && de[k] <= '8') { de[k]++; } else if (de[k] == '9') { de[k] = '0'; } } } else if (s[j] == 'P') { for (int k = 0; k < de.size(); k++) { if (de[k] >= '1' && de[k] <= '9') { de[k]--; } else if (de[k] == '0') { de[k] = '9'; } } } } for (int j = 0; j < de.size(); j++) { cout << de[j]; if (j == de.size() - 1) cout << endl; } } return 0; }
#include <deque> #include <iostream> #include <string> using namespace std; int main() { int n; string s, t; char c, d; deque<char> de, sub; cin >> n; for (int i = 0; i < n; i++) { cin >> s >> t; de.clear(); for (int j = 0; j < t.size(); j++) { de.push_back(t[j]); } for (int j = s.size() - 1; j >= 0; j--) { // cout << s[j] <<endl; if (s[j] == 'C') { c = de.front(); de.pop_front(); de.push_back(c); } else if (s[j] == 'J') { c = de.back(); de.pop_back(); de.push_front(c); } else if (s[j] == 'A') { for (int k = 0; k < de.size() / 2; k++) { swap(de[k], de[de.size() - k - 1]); } } else if (s[j] == 'E') { if (de.size() % 2 == 1) { for (int k = 0; k < t.size() / 2; k++) { c = de.back(); de.pop_back(); sub.push_front(c); } c = de.back(); de.pop_back(); de.push_front(c); // cout << c << endl; while (!sub.empty()) { c = sub.back(); sub.pop_back(); de.push_front(c); } } else { for (int k = 0; k < de.size() / 2; k++) { c = de.front(); de.pop_front(); de.push_back(c); } } } else if (s[j] == 'M') { for (int k = 0; k < de.size(); k++) { if (de[k] >= '0' && de[k] <= '8') { de[k]++; } else if (de[k] == '9') { de[k] = '0'; } } } else if (s[j] == 'P') { for (int k = 0; k < de.size(); k++) { if (de[k] >= '1' && de[k] <= '9') { de[k]--; } else if (de[k] == '0') { de[k] = '9'; } } } } for (int j = 0; j < de.size(); j++) { cout << de[j]; if (j == de.size() - 1) cout << endl; } } return 0; }
[["-", 0, 57, 64, 9, 0, 7, 15, 16, 17, 19], ["+", 0, 57, 64, 9, 0, 7, 15, 16, 17, 18]]
1
635
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n--) { string q, s; cin >> q >> s; for (int i = s.size() - 1; 0 <= i; i--) { if (q[i] == 'C') s = s.substr(1, s.size() - 1) + s[0]; if (q[i] == 'J') s = s[s.size() - 1] + s.substr(0, s.size() - 1); if (q[i] == 'E') for (int j = 0; j < s.size() / 2; j++) swap(s[j], s[(s.size() + 1) / 2 + j]); if (q[i] == 'A') reverse(s.begin(), s.end()); if (q[i] == 'P') for (int j = 0; j < s.size(); j++) if (isdigit(s[j])) s[j] = ((s[j] - '0' + 10) - 1) % 10 + '0'; if (q[i] == 'M') for (int j = 0; j < s.size(); j++) if (isdigit(s[j])) s[j] = ((s[j] - '0') + 1) % 10 + '0'; } cout << s << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n--) { string q, s; cin >> q >> s; for (int i = q.size() - 1; 0 <= i; i--) { if (q[i] == 'C') s = s.substr(1, s.size() - 1) + s[0]; if (q[i] == 'J') s = s[s.size() - 1] + s.substr(0, s.size() - 1); if (q[i] == 'E') for (int j = 0; j < s.size() / 2; j++) swap(s[j], s[(s.size() + 1) / 2 + j]); if (q[i] == 'A') reverse(s.begin(), s.end()); if (q[i] == 'P') for (int j = 0; j < s.size(); j++) if (isdigit(s[j])) s[j] = ((s[j] - '0' + 10) - 1) % 10 + '0'; if (q[i] == 'M') for (int j = 0; j < s.size(); j++) if (isdigit(s[j])) s[j] = ((s[j] - '0') + 1) % 10 + '0'; } cout << s << endl; } return 0; }
[["-", 49, 50, 51, 16, 31, 2, 63, 118, 28, 22], ["+", 49, 50, 51, 16, 31, 2, 63, 118, 28, 22]]
1
354
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iomanip> #include <iostream> #include <numeric> #include <sstream> #include <string> #include <typeinfo> #include <utility> #include <vector> #define REP(i, n) for (int i = 0; i < n; i++) typedef long long ll; using namespace std; typedef vector<ll> vl; typedef pair<ll, ll> pll; typedef vector<pll> vpll; typedef vector<string> vs; int main() { int s[100001] = {}; s[0] = 1; s[1] = 1; for (int i = 2; i * i < 100000; i++) { if (s[i] == 0) { for (int j = i * i; j < 100000; j += i) { s[j] = 1; } } } int m, a, b; while (scanf("%d%d%d", &m, &a, &b) && !(m == 0 && a == 0 && b == 0)) { int ans(0); int w; int h; for (int i = m; i >= 2; i--) { if (!s[i]) { for (int j = min(m / i, i); j >= 2; j--) { if (!s[j]) { if (ans < i * j && i * j <= m && j * b >= i * a) { ans = i * j; w = j; h = i; } } } } } printf("%d %d %d\n", ans, w, h); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iomanip> #include <iostream> #include <numeric> #include <sstream> #include <string> #include <typeinfo> #include <utility> #include <vector> #define REP(i, n) for (int i = 0; i < n; i++) typedef long long ll; using namespace std; typedef vector<ll> vl; typedef pair<ll, ll> pll; typedef vector<pll> vpll; typedef vector<string> vs; int main() { int s[100001] = {}; s[0] = 1; s[1] = 1; for (int i = 2; i * i < 100000; i++) { if (s[i] == 0) { for (int j = i * i; j < 100000; j += i) { s[j] = 1; } } } int m, a, b; while (scanf("%d%d%d", &m, &a, &b) && !(m == 0 && a == 0 && b == 0)) { int ans(0); int w; int h; for (int i = m; i >= 2; i--) { if (!s[i]) { for (int j = min(m / i, i); j >= 2; j--) { if (!s[j]) { if (ans < i * j && i * j <= m && j * b >= i * a) { ans = i * j; w = j; h = i; } } } } } printf("%d %d\n", w, h); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 21], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
1
323
#include <iostream> using namespace std; #define PRIMENUM 200100 //その数は素数かどうか判定する変数 int isPrime[PRIMENUM]; void makePrimeNumber() { isPrime[2] = 1; isPrime[3] = 1; for (int i = 4; i < PRIMENUM; i++) { int flag = 0; for (int j = 2; j * j <= i; j++) { if (i % j == 0) { flag = 1; break; } } if (flag == 0) { isPrime[i] = 1; // cout<<i<<endl; } } } //素数列、上の関数を使ってから発動する。 int primeLine[PRIMENUM]; void makePrimeLine() { int pos = 0; for (int i = 0; i < PRIMENUM; i++) { if (isPrime[i] == 1) { primeLine[pos] = i; pos++; } } } int main() { makePrimeNumber(); makePrimeLine(); int m, a, b; while (cin >> m >> a >> b) { if (m == 0 && a == 0 && b == 0) break; int maxP = 0, maxQ = 0; for (int i = 0; primeLine[i] <= m; i++) { int nextNum = m / primeLine[i]; // cout<<"p="<<primeLine[i]; for (int j = nextNum; j >= 0; j--) { int flag = 0; if (primeLine[i] > j) break; if (isPrime[j] == 1) { // cout<<"q="<<j<<endl; if (j * primeLine[i] < m && maxP * maxQ <= j * primeLine[i] && j >= primeLine[i] && a * j <= primeLine[i] * b) { maxP = primeLine[i]; maxQ = j; // cout<<"unko"<<endl; flag = 1; } } if (flag == 1) break; } } // cout<<endl; cout << maxP << " " << maxQ << endl; } return 0; }
#include <iostream> using namespace std; #define PRIMENUM 200100 //その数は素数かどうか判定する変数 int isPrime[PRIMENUM]; void makePrimeNumber() { isPrime[2] = 1; isPrime[3] = 1; for (int i = 4; i < PRIMENUM; i++) { int flag = 0; for (int j = 2; j * j <= i; j++) { if (i % j == 0) { flag = 1; break; } } if (flag == 0) { isPrime[i] = 1; // cout<<i<<endl; } } } //素数列、上の関数を使ってから発動する。 int primeLine[PRIMENUM]; void makePrimeLine() { int pos = 0; for (int i = 0; i < PRIMENUM; i++) { if (isPrime[i] == 1) { primeLine[pos] = i; pos++; } } } int main() { makePrimeNumber(); makePrimeLine(); int m, a, b; while (cin >> m >> a >> b) { if (m == 0 && a == 0 && b == 0) break; int maxP = 0, maxQ = 0; for (int i = 0; primeLine[i] <= m; i++) { int nextNum = m / primeLine[i]; // cout<<"p="<<primeLine[i]; for (int j = nextNum; j >= 0; j--) { int flag = 0; if (primeLine[i] > j) break; if (isPrime[j] == 1) { // cout<<"q="<<j<<endl; if (j * primeLine[i] <= m && maxP * maxQ <= j * primeLine[i] && j >= primeLine[i] && a * j <= primeLine[i] * b) { maxP = primeLine[i]; maxQ = j; // cout<<"unko"<<endl; flag = 1; } } if (flag == 1) break; } } // cout<<endl; cout << maxP << " " << maxQ << endl; } return 0; }
[["-", 51, 16, 31, 16, 31, 16, 31, 16, 17, 18], ["+", 51, 16, 31, 16, 31, 16, 31, 16, 17, 19]]
1
376
#include <algorithm> #include <iostream> #include <iterator> #include <vector> using namespace std; #define DEBUG(x) cerr << #x << " = " << x << endl typedef long long ll; const int MAX_M = 100000; bool prime[MAX_M + 1]; int main() { fill(prime, prime + MAX_M + 1, true); prime[0] = prime[1] = false; for (int i = 2; i * i <= MAX_M; ++i) { if (prime[i]) { for (int j = i * i; j <= MAX_M; j += i) { prime[j] = false; } } } while (true) { int M, A, B; cin >> M >> A >> B; if (M == 0 && A == 0 && B == 0) break; int maxP = 0, maxQ = 0; for (ll i = 2; i <= M; ++i) { if (prime[i]) { for (ll j = 2; j <= min(i, M / i); ++j) { if (prime[j] && maxP * maxQ < i * j && A * i <= B * j) { maxP = i; maxQ = j; } } } } cout << maxP << ' ' << maxQ << endl; } }
#include <algorithm> #include <iostream> #include <iterator> #include <vector> using namespace std; #define DEBUG(x) cerr << #x << " = " << x << endl typedef long long ll; const int MAX_M = 100000; bool prime[MAX_M + 1]; int main() { fill(prime, prime + MAX_M + 1, true); prime[0] = prime[1] = false; for (int i = 2; i * i <= MAX_M; ++i) { if (prime[i]) { for (int j = i * i; j <= MAX_M; j += i) { prime[j] = false; } } } while (true) { int M, A, B; cin >> M >> A >> B; if (M == 0 && A == 0 && B == 0) break; int maxP = 0, maxQ = 0; for (ll i = 2; i <= M; ++i) { if (prime[i]) { for (ll j = 2; j <= min(i, M / i); ++j) { if (prime[j] && maxP * maxQ < i * j && A * i <= B * j) { maxP = j; maxQ = i; } } } } cout << maxP << ' ' << maxQ << endl; } }
[["-", 0, 57, 64, 9, 0, 1, 0, 11, 12, 22], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 12, 22]]
1
260
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <vector> using namespace std; #define INF 999999999 #define REP(i, n) for (int i = 0; i < (int)(n); i++) int pr[100001]; void erato() { pr[0] = 0; pr[1] = 0; pr[2] = 1; for (int i = 3; i <= 100000; i++) { pr[i] = 1; for (int j = 2; j * j <= i; j++) { if (i % j == 0) { pr[i] = 0; break; } } } } int main() { int m, a, b, p, q; erato(); while (1) { cin >> m >> a >> b; if (!m) break; p = 1; q = 1; for (int i = 2; i <= m; i++) { for (int j = i; j * i <= m; j++) { if (!pr[i] || !pr[j]) continue; if (i * j <= m && a * j <= i * b && i * j > p * q) { p = i; q = j; } } } cout << p << " " << q << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <vector> using namespace std; #define INF 999999999 #define REP(i, n) for (int i = 0; i < (int)(n); i++) int pr[100001]; void erato() { pr[0] = 0; pr[1] = 0; pr[2] = 1; for (int i = 3; i <= 100000; i++) { pr[i] = 1; for (int j = 2; j * j <= i; j++) { if (i % j == 0) { pr[i] = 0; break; } } } } int main() { long long int m, a, b, p, q; erato(); while (1) { cin >> m >> a >> b; if (!m) break; p = 1; q = 1; for (long long int i = 2; i <= m; i++) { for (long long int j = i; j * i <= m; j++) { if (!pr[i] || !pr[j]) continue; if (i * j <= m && a * j <= i * b && i * j > p * q) { p = i; q = j; } } } cout << p << " " << q << endl; } return 0; }
[["+", 0, 14, 8, 9, 0, 43, 39, 86, 0, 96], ["+", 8, 9, 0, 7, 10, 43, 39, 86, 0, 96]]
1
271
#include <iostream> #include <vector> using namespace std; #define MAX 100000 int m, a, b; vector<int> t; bool isP[MAX]; int main() { for (int i = 2; i < MAX; i++) isP[i] = true; for (int i = 2; i * i < MAX; i++) { if (!isP[i]) continue; for (int j = i * i; j < MAX; j += i) { isP[j] = false; } } for (int i = 2; i < MAX; i++) if (isP[i]) t.push_back(i); while (1) { cin >> m >> a >> b; if (m == 0 && a == 0 && b == 0) break; int ansi = 0, ansj = 0; for (int i = 0; i < (int)t.size(); i++) { if (t[i] > m) break; for (int j = i; j < (int)t.size(); j++) { if (t[i] * t[j] > m) break; if (a * t[j] <= t[i] * b) { if (ansi * ansj <= t[i] * t[j]) { ansi = t[i]; ansj = t[j]; } } } } cout << ansi << ' ' << ansj << endl; } return 0; }
#include <iostream> #include <vector> using namespace std; #define MAX 100000 int m, a, b; vector<int> t; bool isP[MAX]; int main() { for (int i = 2; i < MAX; i++) isP[i] = true; for (int i = 2; i * i < MAX; i++) { if (!isP[i]) continue; for (int j = i * i; j < MAX; j += i) { isP[j] = false; } } for (int i = 2; i < MAX; i++) if (isP[i]) t.push_back(i); while (1) { cin >> m >> a >> b; if (m == 0 && a == 0 && b == 0) break; int ansi = 0, ansj = 0; for (int i = 0; i < (int)t.size(); i++) { if (t[i] * t[i] > m) break; for (int j = i; j < (int)t.size(); j++) { if (t[i] * t[j] > m) break; if (a * t[j] <= t[i] * b) { if (ansi * ansj <= t[i] * t[j]) { ansi = t[i]; ansj = t[j]; } } } } cout << ansi << ' ' << ansj << endl; } return 0; }
[["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 48], ["+", 15, 339, 51, 16, 31, 16, 12, 69, 28, 22], ["+", 51, 16, 31, 16, 12, 69, 341, 342, 0, 70], ["+", 51, 16, 31, 16, 12, 69, 341, 342, 0, 22], ["+", 51, 16, 31, 16, 12, 69, 341, 342, 0, 73]]
1
316
#include <iostream> using namespace std; int judge(int n) { if (n < 2) return 0; if (n == 2) return 1; if (n % 2 == 0) return 0; for (int i = 3; i < n / i; i += 3) { if (n % i == 0) return 0; } return 1; } int main() { int m, a, b, prime[10000], n = 0, p, q; double ab; for (int i = 0; i < 10000; i++) { if (judge(i)) prime[n++] = i; } while (1) { cin >> m >> a >> b; if (m == 0 && a == 0 && b == 0) break; ab = (double)a / b; p = 1; q = 1; for (int i = 0; i < n; i++) { for (int j = i; prime[i] * prime[j] <= m; j++) { if (prime[i] * prime[j] > p * q && ab <= (double)prime[i] / prime[j]) { p = prime[i]; q = prime[j]; } } } cout << p << " " << q << endl; } return 0; }
#include <iostream> using namespace std; int judge(int n) { if (n < 2) return 0; if (n == 2) return 1; if (n % 2 == 0) return 0; for (int i = 3; i <= n / i; i += 2) { if (n % i == 0) return 0; } return 1; } int main() { int m, a, b, prime[10000], n = 0, p, q; double ab; for (int i = 0; i < 10000; i++) { if (judge(i)) prime[n++] = i; } while (1) { cin >> m >> a >> b; if (m == 0 && a == 0 && b == 0) break; ab = (double)a / b; p = 1; q = 1; for (int i = 0; i < n; i++) { for (int j = i; prime[i] * prime[j] <= m; j++) { if (prime[i] * prime[j] > p * q && ab <= (double)prime[i] / prime[j]) { p = prime[i]; q = prime[j]; } } } cout << p << " " << q << endl; } return 0; }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 0, 14, 8, 9, 0, 7, 26, 11, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 26, 11, 12, 13]]
1
285
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctype.h> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define pi 3.141592653589793 bool Isprime(int n) { if (n == 1) return false; if (n == 2) return true; for (int i = 2; i * i < n; i++) { if (n % i == 0) return false; } return true; } int main() { int prime[100000]; int cnt = 0; for (int i = 1; i < 100000; i++) { if (Isprime(i)) { prime[cnt] = i; cnt++; } // cout << i << " " << isprime[i] << endl; } int m, a, b; while (1) { cin >> m >> a >> b; if (m == 0) break; int answ = 0, ansh = 0; for (int i = 0; i < cnt; i++) { for (int j = i; j < cnt; j++) { if (prime[i] * prime[j] > m || a * prime[j] > b * prime[i]) break; if (prime[i] * prime[j] > answ * ansh) { answ = prime[i]; ansh = prime[j]; } } } cout << answ << " " << ansh << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctype.h> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define pi 3.141592653589793 bool Isprime(int n) { if (n == 1) return false; if (n == 2) return true; for (int i = 2; i * i < n + 1; i++) { if (n % i == 0) return false; } return true; } int main() { int prime[10000]; int cnt = 0; for (int i = 1; i < 10000; i++) { if (Isprime(i)) { prime[cnt] = i; cnt++; } // cout << i << " " << isprime[i] << endl; } int m, a, b; while (1) { cin >> m >> a >> b; if (m == 0) break; int answ = 0, ansh = 0; for (int i = 0; i < cnt; i++) { for (int j = i; j < cnt; j++) { if (prime[i] * prime[j] > m || a * prime[j] > b * prime[i]) break; if (prime[i] * prime[j] > answ * ansh) { answ = prime[i]; ansh = prime[j]; } } } cout << answ << " " << ansh << endl; } return 0; }
[["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 72], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13], ["-", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13], ["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13]]
1
297
#include "bits/stdc++.h" #include <unordered_map> #pragma warning(disable : 4996) using namespace std; const int My_Inf = 2147483647; const long long int My_LInf = 9223372036854775807; int aa[1000000]; bool isprime(long long int a) { if (a == 1 || a == 0) return false; for (long long int i = 2; i * i <= a; ++i) { if ((a % i)) { } else { return false; } } return true; } int main() { for (int i = 0; i < 1000000; ++i) { aa[i] = isprime(i); } while (1) { int m, a, b; cin >> m >> a >> b; if (!m) break; int ans = 0; int p; int q; for (int i = 2; i < sqrt(m) + 2; ++i) { if (aa[i]) continue; for (int j = i; j < m / i + 2; ++j) { if (i * j <= m && aa[j]) { if (double(a) / b <= double(i) / j + 1e-8) { if (ans < i * j) { ans = i * j; p = i; q = j; } } } } } cout << p << " " << q << endl; } return 0; }
#include "bits/stdc++.h" #include <unordered_map> #pragma warning(disable : 4996) using namespace std; const int My_Inf = 2147483647; const long long int My_LInf = 9223372036854775807; int aa[1000000]; bool isprime(long long int a) { if (a == 1 || a == 0) return false; for (long long int i = 2; i * i <= a; ++i) { if ((a % i)) { } else { return false; } } return true; } int main() { for (int i = 0; i < 1000000; ++i) { aa[i] = isprime(i); } while (1) { int m, a, b; cin >> m >> a >> b; if (!m) break; int ans = 0; int p; int q; for (int i = 2; i < sqrt(m) + 2; ++i) { if (!aa[i]) continue; for (int j = i; j < m / i + 2; ++j) { if (i * j <= m && aa[j]) { if (double(a) / b <= double(i) / j + 1e-8) { if (ans < i * j) { ans = i * j; p = i; q = j; } } } } } cout << p << " " << q << endl; } return 0; }
[["+", 8, 9, 0, 57, 15, 339, 51, 91, 17, 111]]
1
289
#include <bits/stdc++.h> using namespace std; #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 ALL(a) (a).begin(), (a).end() #define DEBUG(a) "(" << #a << ": " << (a) << ")" template <typename T> void chmin(T &x, T y) { x = min(x, y); } template <typename T> void chmax(T &x, T y) { x = max(x, y); } typedef long long int lli; typedef long double ld; typedef tuple<int, int> P; const int INF = INT_MAX / 2 - 1; const double EPS = 1e-14; const int dx[4] = {0, 1, 0, -1}; // {-1, 0, 1, -1, 1, -1, 0, 1}; const int dy[4] = {1, 0, -1, 0}; // {-1, -1, -1, 0, 0, 1, 1, 1}; const lli PRIMEMAX = 100000; int FACTOR[PRIMEMAX]; vector<int> Primes; void sieve() { FACTOR[0] = FACTOR[1] = -1; for (lli n = 2; n < PRIMEMAX; ++n) { if (FACTOR[n] != 0) continue; Primes.push_back(n); lli kn = n * n; while (kn < PRIMEMAX) { FACTOR[kn] = n; kn += n; } } } bool is_prime(int n) { if (n < PRIMEMAX) return FACTOR[n] == 0; for (int p : Primes) { if (p * p > n) break; if (n % p == 0) return false; } return true; } void solve(int a, int b, int m) { int len = Primes.size(), ans = -1; int pp = 0, qq = 0; FOR(i, 0, len) { FOR(j, i, len) { int p = Primes[i], q = Primes[j]; if (p * q > m || a * q > b * p) break; if (ans < p * q) { pp = p, qq = q, ans = p * q; } } } cout << pp << " " << qq << endl; } int main() { cout << fixed << setprecision(10); sieve(); int a, b, m; while (cin >> m >> a >> b and b) { solve(a, b, m); } return 0; }
#include <bits/stdc++.h> using namespace std; #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 ALL(a) (a).begin(), (a).end() #define DEBUG(a) "(" << #a << ": " << (a) << ")" template <typename T> void chmin(T &x, T y) { x = min(x, y); } template <typename T> void chmax(T &x, T y) { x = max(x, y); } typedef long long int lli; typedef long double ld; typedef tuple<int, int> P; const int INF = INT_MAX / 2 - 1; const double EPS = 1e-14; const int dx[4] = {0, 1, 0, -1}; // {-1, 0, 1, -1, 1, -1, 0, 1}; const int dy[4] = {1, 0, -1, 0}; // {-1, -1, -1, 0, 0, 1, 1, 1}; const lli PRIMEMAX = 100000; int FACTOR[PRIMEMAX]; vector<int> Primes; void sieve() { FACTOR[0] = FACTOR[1] = -1; for (lli n = 2; n < PRIMEMAX; ++n) { if (FACTOR[n] != 0) continue; Primes.push_back(n); lli kn = n * n; while (kn < PRIMEMAX) { FACTOR[kn] = n; kn += n; } } } bool is_prime(int n) { if (n < PRIMEMAX) return FACTOR[n] == 0; for (int p : Primes) { if (p * p > n) break; if (n % p == 0) return false; } return true; } void solve(int a, int b, int m) { lli len = Primes.size(), ans = -1; lli pp = 0, qq = 0; FOR(i, 0, len) { FOR(j, i, len) { lli p = Primes[i], q = Primes[j]; if (p * q > m || a * q > b * p) break; if (ans < p * q) { pp = p, qq = q, ans = p * q; } } } cout << pp << " " << qq << endl; } int main() { cout << fixed << setprecision(10); sieve(); int a, b, m; while (cin >> m >> a >> b and b) { solve(a, b, m); } return 0; }
[["-", 0, 30, 0, 14, 8, 9, 0, 43, 39, 40], ["+", 0, 30, 0, 14, 8, 9, 0, 43, 39, 78], ["-", 0, 9, 0, 14, 8, 9, 0, 43, 39, 40], ["+", 0, 9, 0, 14, 8, 9, 0, 43, 39, 78]]
1
507